Apache 虚拟主机(VirtualHost)允许在同一台服务器上托管多个网站。
启用必要模块
sudo a2enmod rewrite sudo a2enmod ssl sudo a2enmod headers sudo a2enmod expires
创建虚拟主机配置
sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
<Directory /var/www/example>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>
sudo a2ensite example.com.conf sudo systemctl reload apache2
MPM 模式选择
# 查看当前 MPM sudo apachectl -V | grep -i mpm # 切换 MPM(以 event 为例) sudo a2dismod mpm_prefork mpm_worker sudo a2enmod mpm_event sudo systemctl restart apache2
性能优化
# 启用压缩 sudo a2enmod deflate # 配置缓存 sudo a2enmod expires sudo a2enmod cache