Debian Apache 虚拟主机优化指南
一 基础与连接优化
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>
以上做法有助于降低页面加载时间、提升并发处理能力,并兼顾 SEO 与用户体验。二 缓存与静态资源策略
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 week"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
</IfModule>
通过合理的缓存策略与压缩,可显著降低服务器负载并缩短首屏时间。三 PHP 与动态内容优化
memory_limit = 256M
[opcache]
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
四 虚拟主机配置与启用步骤
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
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>
sudo a2ensite example.com.conf
sudo a2enmod rewrite deflate expires ssl http2
sudo systemctl reload apache2
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com
规范的虚拟主机结构、访问控制与自动化证书部署,有助于安全与可维护性。五 监控 调参与容量规划
net.core.somaxconn = 4096
net.ipv4.tcp_tw_reuse = 1