最优的 CentOS + Apache 配置并没有“唯一答案”,而是取决于服务器用途、访问量和硬件资源。下面我给你一套通用高性能、安全、可维护的主流配置方案,适用于 CentOS 7 / 8 / Stream,并解释关键参数。
| 组件 | 推荐 |
|---|---|
| 系统 | CentOS 7 / 8 Stream |
| Web 服务 | Apache 2.4 |
| PHP | PHP-FPM(不要 mod_php) |
| 数据库 | MySQL / MariaDB |
| 架构 | Apache + PHP-FPM + MariaDB |
| 加速 | gzip + 缓存 + HTTPS |
yum install -y httpd
systemctl enable httpd
systemctl start httpd
路径:
/etc/httpd/conf/httpd.conf
ServerTokens Prod
ServerSignature Off
# 监听端口
Listen 80
# 使用事件模型(最优)
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 300
MaxConnectionsPerChild 1000
</IfModule>
✅ 推荐使用 event MPM(CentOS 默认已支持)
查看当前 MPM:
httpd -V | grep MPM
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory "/var/www/html">
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# 日志
ErrorLog /var/log/httpd/error.log
CustomLog /var/log/httpd/access.log combined
# 缓存
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 30 days"
ExpiresByType image/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
</IfModule>
# Gzip 压缩
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
</VirtualHost>
LoadModule php_module
yum install -y php php-fpm
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
systemctl start php-fpm
systemctl enable php-fpm
✅ 优点:
Options -Indexes
ServerTokens Prod
ServerSignature Off
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
✅ 使用 event MPM
✅ 使用 PHP-FPM
✅ 开启 gzip + expires 缓存
✅ 避免 .htaccess(放 VirtualHost)
✅ 日志分离
✅ HTTPS(Let’s Encrypt)
如果你愿意,可以告诉我:
我可以给你一套完全定制化的 Apache 配置文件 ✅