提交工单

Apache虚拟主机配置:一台服务器托管多个网站、HTTPS与PHP-FPM

虽然 Nginx 越来越流行,但仍然有大量网站运行在 Apache 上,特别是依赖 .htaccess 的老程序。Apache 的虚拟主机(VirtualHost)功能可以让一台服务器根据访问的域名,提供不同的网站。Apache 的安装见 Ubuntu 安装 Apache。

本文以 Ubuntu / Debian 为例。RHEL 系发行版的配置目录和管理方式不同(配置在 /etc/httpd/conf.d/ 中),思路相同。

目录结构

Debian 系的 Apache 配置组织方式:

路径 作用
/etc/apache2/apache2.conf 主配置
/etc/apache2/sites-available/ 所有网站的配置文件
/etc/apache2/sites-enabled/ 已启用的网站(指向 sites-available 的链接)
/etc/apache2/mods-available/、mods-enabled/ 模块
/etc/apache2/conf-available/、conf-enabled/ 通用配置片段

管理命令:

a2ensite example.conf     # 启用网站
a2dissite example.conf    # 禁用网站
a2enmod rewrite           # 启用模块
a2dismod 模块名            # 禁用模块
apachectl configtest      # 检查配置语法
systemctl reload apache2  # 重新加载配置

第一步:准备网站目录

mkdir -p /var/www/example.com/public
mkdir -p /var/www/blog.example.net/public
chown -R www-data:www-data /var/www/example.com /var/www/blog.example.net

(目录权限的更严格做法见下文和 最小权限原则。)

第二步:创建虚拟主机配置

/etc/apache2/sites-available/example.com.conf:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public

    <Directory /var/www/example.com/public>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
指令 作用
ServerName 主域名
ServerAlias 其他也指向这个网站的域名
DocumentRoot 网站根目录
Options -Indexes 禁止列出目录内容
AllowOverride All 允许 .htaccess 覆盖配置
Require all granted 允许访问

对第二个网站创建类似的文件。

第三步:启用网站

a2ensite example.com.conf
a2ensite blog.example.net.conf
a2dissite 000-default.conf     # 可选:禁用默认网站
a2enmod rewrite                # 大多数程序的伪静态需要
apachectl configtest
systemctl reload apache2

apachectl configtest 显示 Syntax OK 后再重载。

默认网站的作用

当请求的域名不匹配任何 ServerName 或 ServerAlias 时(例如直接用 IP 访问),Apache 会使用第一个加载的虚拟主机。保留一个「默认站点」,让它返回空白页或 403,可以避免用户通过 IP 或错误的域名看到你的某个网站。

第四步:配置 HTTPS

使用 Certbot 的 Apache 插件:

apt install certbot python3-certbot-apache
certbot --apache -d example.com -d www.example.com
certbot --apache -d blog.example.net

Certbot 会自动:

  • 申请证书
  • 创建 *-le-ssl.conf 的 HTTPS 虚拟主机配置
  • 可选地设置 HTTP 到 HTTPS 的跳转
  • 配置自动续期

确认自动续期:

certbot renew --dry-run

见 Certbot 续期失败。

第五步:使用 PHP-FPM

Apache 运行 PHP 有两种常见方式:

方式 说明
mod_php PHP 作为 Apache 模块运行,只能与 prefork MPM 配合,内存占用大
PHP-FPM PHP 作为独立的进程池运行,Apache 可以使用 event MPM,性能更好,并且每个网站可以使用不同的 PHP 版本和用户

推荐使用 PHP-FPM:

apt install php8.2-fpm           # 版本以实际可用为准
a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm
a2dismod php8.2 mpm_prefork      # 如果之前启用了 mod_php
a2enmod mpm_event
apachectl configtest
systemctl restart apache2

为某个网站指定 PHP-FPM(例如使用不同的版本或进程池),在虚拟主机中添加:

<FilesMatch \.php$>
    SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost"
</FilesMatch>

为每个网站配置独立的 PHP-FPM 进程池和运行用户,可以隔离不同网站,一个网站被攻破不会影响其他网站,见 PHP-FPM 多进程池、PHP 多版本共存。并发模型的差异见 Web 服务器的并发模型。

安全设置

隐藏版本信息

# /etc/apache2/conf-available/security.conf
ServerTokens Prod
ServerSignature Off

禁止访问敏感文件

<FilesMatch "^\.(env|git|htpasswd)">
    Require all denied
</FilesMatch>
<DirectoryMatch "/\.git">
    Require all denied
</DirectoryMatch>

上传目录禁止执行 PHP

<Directory /var/www/example.com/public/uploads>
    <FilesMatch "\.(php|phtml|phar)$">
        Require all denied
    </FilesMatch>
</Directory>

见 文件上传漏洞。

AllowOverride 的取舍

AllowOverride All 允许每个目录的 .htaccess 修改配置,方便程序自动写入伪静态规则,但:

  • Apache 处理每个请求时,都要在每一级目录中查找 .htaccess,有一定的性能开销
  • 如果攻击者能写入 .htaccess,就能修改服务器行为

对于自己可以控制配置的网站,把规则直接写进虚拟主机配置,并设置 AllowOverride None,性能和安全性更好。.htaccess 的写法见 Apache .htaccess 伪静态规则。

常见问题排查

现象 可能的原因
访问域名显示了另一个网站 ServerName / ServerAlias 不匹配,落到了默认站点
403 Forbidden 目录权限不足、缺少 Require all granted、目录中没有首页且禁止了目录列表
伪静态不生效,404 没有启用 rewrite 模块,或 AllowOverride 为 None
PHP 文件被下载 PHP 处理没有配置好
重载失败 配置语法错误,用 apachectl configtest 查看

查看当前加载的虚拟主机:

apachectl -S

它会列出每个端口上的虚拟主机,以及哪个是默认的。

查看错误日志:

tail -f /var/log/apache2/example.com_error.log

考虑迁移到 Nginx

如果网站访问量较大、服务器内存有限,可以考虑:

  • 在 Apache 前面加一层 Nginx 处理静态文件和连接
  • 或者完全迁移到 Nginx

见 Nginx 和 Apache 怎么选、从 Apache 迁移到 Nginx。

小结

  • Debian 系中网站配置放在 sites-available,用 a2ensite 启用,apachectl configtest 检查
  • 每个网站一个 VirtualHost,用 ServerName / ServerAlias 匹配域名,保留一个默认站点
  • 用 Certbot 的 Apache 插件申请 HTTPS;用 PHP-FPM 代替 mod_php,并为每个网站独立进程池
  • 隐藏版本信息、禁止敏感文件、上传目录禁止执行 PHP,按需关闭 AllowOverride

常见问题

修改了配置不生效?

确认网站已经用 a2ensite 启用,并执行了 systemctl reload apache2。用 apachectl -S 确认虚拟主机已加载。

一个网站能同时用 Apache 和 Nginx 吗?

可以让 Nginx 在前面监听 80/443,处理静态文件,再把 PHP 请求反向代理给监听在其他端口上的 Apache。这种组合可以兼顾 .htaccess 的兼容性和 Nginx 处理连接的效率。

RHEL 系的配置在哪里?

通常在 /etc/httpd/conf/httpd.conf 和 /etc/httpd/conf.d/*.conf 中,没有 a2ensite 等命令,直接在 conf.d 中添加 .conf 文件即可,服务名为 httpd。