SSI (Server Side Includes) 是一种简单的服务器端脚本语言,允许在 HTML 文件中包含动态内容。以下是配置 Apache 支持 SHTML/SSI 的完整步骤:
首先确保 Apache 已加载 mod_include
模块:
LoadModule include_module modules/mod_include.so
在 Apache 配置文件中(通常是 httpd.conf
或 apache2.conf
,或在 sites-available/
目录下的站点配置文件中),添加以下配置:
<Directory "/var/www/html">
Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</Directory>
如果只需要特定目录支持 SSI:
<Directory "/var/www/html/ssi-enabled">
Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</Directory>
可以限制允许的 SSI 命令:
<Directory "/var/www/html">
Options +IncludesNOEXEC
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</Directory>
IncludesNOEXEC
允许 SSI 但不允许执行 CGI 脚本。
配置完成后,重启 Apache 使更改生效:
# 对于大多数 Linux 系统
sudo systemctl restart apache2 # Debian/Ubuntu
sudo systemctl restart httpd # RHEL/CentOS
# 或者使用传统方式
sudo service apache2 restart
sudo service httpd restart
创建一个测试文件 test.shtml
:
<!--#echo var="DATE_LOCAL" -->
<!--#include virtual="/footer.html" -->
访问该文件时,应该能看到服务器日期和被包含的文件内容。
可以设置 Apache 自动解析有执行权限的 HTML 文件:
XBitHack on
这样任何设置了用户执行位 (chmod +x filename.html
) 的 HTML 文件都会被解析 SSI 指令。
<Directory "/var/www/html">
Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
SSIErrorMsg "SSI 处理发生错误"
SSIStartTag "<!--#"
SSIEndTag "-->"
</Directory>
如果需要执行 CGI 脚本:
<Directory "/var/www/html">
Options +Includes +ExecCGI
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</Directory>
通过以上配置,Apache 服务器就能正确处理 .shtml 文件并执行 SSI 指令了。