1. 启用必要模块
SSI 需要 mod_include,而执行命令(#exec)通常需要 mod_cgi。
bash
sudo a2enmod include
sudo a2enmod cgi
2. 修改站点配置文件
编辑你的虚拟主机文件(如 /etc/apache2/sites-available/000-default.conf),在 <Directory> 段落中添加关键指令。
核心配置如下:
apache
<Directory /var/www/html>
# 1. 必须包含 Includes 和 ExecCGI
Options +Indexes +FollowSymLinks +Includes +ExecCGI
# 2. 允许读取 .shtml 扩展名
AddType text/html .shtml
# 3. 设置输出过滤器,让 Apache 解析文件中的 SSI 标签
AddOutputFilter INCLUDES .shtml
# (可选) 设置默认首页包含 index.shtml
DirectoryIndex index.shtml index.html
AllowOverride All
Require all granted
</Directory>
3. 设置全局安全策略 (可选)
有时 include.conf 中的全局设置会限制 exec。检查 /etc/apache2/mods-available/include.conf:
apache
<IfModule mod_include.c>
# 确保没有使用 IncludesNoExec
# XBitHack 允许通过文件的"可执行"权限位来控制 SSI 行为
XBitHack full
</IfModule>
4. 重启服务并测试
每次修改配置后必须重启:
bash
sudo systemctl restart apache2