Debian LAMP中Apache配置技巧
安装与启动Apache
使用sudo apt update && sudo apt install apache2安装Apache,安装后服务会自动启动。通过systemctl status apache2确认服务状态,systemctl enable apache2设置开机自启。
配置虚拟主机
虚拟主机配置文件位于/etc/apache2/sites-available/目录下(如yourdomain.conf),内容模板如下:
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain/html
<Directory /var/www/yourdomain/html>
Options Indexes FollowSymLinks
AllowOverride All # 允许.htaccess覆盖配置
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined
</VirtualHost>
启用虚拟主机用sudo a2ensite yourdomain.conf,禁用默认站点用sudo a2dissite 000-default.conf,最后重启Apache使配置生效。
启用必要模块
常用模块通过a2enmod命令启用:
rewrite:支持URL重写(用于SEO或隐藏路径);ssl:启用HTTPS支持;expires:控制缓存头;deflate:启用Gzip压缩。调整MPM(多处理模块)
Debian默认使用prefork模块(适合兼容性场景),但event或worker模块更适合高并发。切换模块:
sudo a2dismod prefork # 禁用prefork
sudo a2enmod event # 启用event
sudo systemctl restart apache2
event模块配置示例(/etc/apache2/mods-enabled/mpm_event.conf):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150 # 根据服务器内存调整(如4GB内存约150)
MaxConnectionsPerChild 1000 # 每个子进程处理1000个请求后重启
</IfModule>
注意:MaxRequestWorkers需根据服务器内存计算(如每个Apache进程约占用10-20MB内存,1GB内存可设置50-100)。
启用压缩与缓存
apache2.conf中添加:<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>
mod_cache和mod_expires缓存静态内容(如CSS、JS、图片),减少服务器请求。配置示例:<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
创建缓存目录并设置权限:sudo mkdir -p /var/cache/apache2/mod_cache_disk && sudo chown -R www-data:www-data /var/cache/apache2。优化KeepAlive设置
KeepAlive允许客户端复用TCP连接,减少连接建立开销。在apache2.conf中调整:
KeepAlive On
MaxKeepAliveRequests 100 # 单个连接最大请求数
KeepAliveTimeout 5 # 连接保持时间(秒)
过长的KeepAliveTimeout会导致资源占用,建议设置为5-10秒。
禁用不必要的模块
禁用未使用的模块(如authn_file、autoindex)以减少内存消耗。通过ls /etc/apache2/mods-enabled/查看已启用模块,禁用用sudo a2dismod 模块名(如sudo a2dismod authn_file),重启Apache。
配置防火墙
使用ufw限制访问,仅允许HTTP(80)和HTTPS(443)流量:
sudo ufw allow 'Apache Full'
sudo ufw enable
检查防火墙状态:sudo ufw status。
启用SSL/TLS
使用Let’s Encrypt获取免费SSL证书,配置HTTPS:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
按提示完成配置,证书会自动续期。优化SSL性能:启用OCSP Stapling(减少证书验证延迟):
<IfModule mod_ssl.c>
SSLUseStapling On
SSLStaplingCache "shmcb:/var/run/stapling-cache(150000)"
</IfModule>
设置会话缓存:
SSLSessionCache shmcb:/var/run/ssl_scache(512000)
SSLSessionCacheTimeout 300
```。
配置日志与监控
logrotate自动分割Apache日志(默认已配置),避免日志文件过大。mod_status监控服务器状态(需启用status模块):LoadModule status_module modules/mod_status.so
<Location "/server-status">
SetHandler server-status
Require ip 127.0.0.1 # 仅允许本地访问
</Location>
访问http://服务器IP/server-status查看实时状态(需替换为实际IP)。URL重写(SEO优化)
在虚拟主机配置的<Directory>中设置AllowOverride All,允许.htaccess文件覆盖配置。创建.htaccess文件并添加重写规则(如将/old-page重定向到/new-page):
RewriteEngine On
RewriteRule ^old-page$ /new-page [R=301,L]
重启Apache使规则生效。
禁用目录列表
防止未经授权的目录浏览,在<Directory>中添加:
Options -Indexes
这样当目录中没有index.html时,Apache不会显示文件列表。