Akemi

Apache配置文件详解与性能调优

2025/02/28

Apache的默认主配置文件位于/etc/httpd/conf/httpd.conf

也可以通过子配置文件/etc/httpd/conf.d/**.conf进行多网站的管理

还有一个**.htaccess目录级配置文件。但不建议使用,会增加安全隐患和维护的复杂度**

全局配置

1
2
3
4
5
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
DocumentRoot "/var/www/html"
IncludeOptional conf.d/*.conf

ServerRoot “/etc/httpd”

  • 作用:定义 Apache 服务器的根目录,所有配置文件和日志的默认路径均基于此目录。
  • 示例:ErrorLog “logs/error_log” 的实际路径是 /etc/httpd/logs/error_log。

Listen 80

  • 作用:指定 Apache 监听 80 端口的 HTTP 请求。
  • 扩展:若需同时监听 IPv4 和 IPv6,可改为 Listen 0.0.0.0:80 或 Listen [::]:80。

Include conf.modules.d/*.conf

  • 作用:加载 /etc/httpd/conf.modules.d/ 目录下所有以 .conf 结尾的模块配置文件。
  • 意义:模块化设计,方便按需启用/禁用功能(如 PHP、SSL 等)。

DocumentRoot “/var/www/html”

  • 根文件位置

IncludeOptional

  • 包含的配置文件位置

权限与管理员信息

1
2
3
4
User apache
Group apache
ServerAdmin root@localhost
ServerName www.wangsheng-test.com

User 和 Group

  • 作用:Apache 子进程以 apache 用户和组的身份运行,限制权限以提高安全性。
  • 注意:确保 /var/www 及子目录的权限允许 apache 用户读取(如 chown -R apache:apache /var/www)。

ServerAdmin

  • 作用:定义管理员邮箱,错误页面中可能显示此信息。
  • 建议:改为实际邮箱(如 admin@example.com)。

ServerName

  • 作用:指定服务器的域名(FQDN),避免 AH00558 警告。
  • 关键:需与 DNS 解析或 /etc/hosts 中的记录匹配。

目录权限控制

/根目录

1
2
3
4
5
6
7
8
<Directory />
AllowOverride none
Require all denied
</Directory>

禁止访问文件系统根目录(/),防止越权访问
AllowOverride none:忽略此目录下的 .htaccess 文件
Require all denied:拒绝所有访问请求

/var/www父目录

1
2
3
4
5
6
7
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>

允许访问 /var/www 目录(网站根目录的父目录)
Require all granted:允许所有请求

根目录 /var/www/html

1
2
3
4
5
6
7
8
9
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

Options Indexes:当目录中没有 index.html 时,显示文件列表
FollowSymLinks:允许跟踪符号链接
AllowOverride None:禁用 .htaccess 文件覆盖配置

默认索引与别名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
当访问目录时,默认显示 index.html 文件(若存在)

Alias "/downloads" "/var/www/files"
<Directory "/var/www/files">
Options Indexes FollowSymLinks
IndexOptions NameWidth=* FancyIndexing
Require ip 192.168.10.0/24
</Directory>
Indexes:允许目录文件列表
FancyIndexing:显示文件大小、修改时间等
Require限制只允许这个网段内的ip进行访问

安全防护

1
2
3
4
<Files ".ht*">
Require all denied
</Files>
保护 .htaccess 和 .htpasswd 等敏感文件,防止被直接下载

日志配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ErrorLog "logs/error_log"
LogLevel warn
错误日志位置
超过warn等级就会被记录

<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
日志字符串格式化的格式

脚本支持

1
2
3
4
5
6
7
8
9
10
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>

允许通过 http://IP/cgi-bin/ 执行 CGI 脚本

MIME 类型与字符集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
定义文件扩展名与 MIME 类型的映射关系

AddDefaultCharset UTF-8
默认字符集UTF-8

<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>

性能优化参数总览

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
EnableSendfile on
使用零拷贝技术传输文件(适合大文件下载)

EnableMMAP on
内存映射文件读取(提升小文件读取效率)

Timeout 3600
定义请求超时时间为 3600 秒

KeepAlive On
允许复用 TCP 连接处理多个请求

MaxKeepAliveRequests 100
单个连接最多处理100个请求

KeepAliveTimeout 3600
空闲连接保持1小时

<IfModule mod_deflate.c>
DeflateCompressionLevel 6
AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
</IfModule>
启用Gzip压缩,适合移动端,与传输大体积文本

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType text/css "access plus 1 week"
</IfModule>
通过HTTP头告知浏览器缓存静态资源,减少重复请求

ServerTokens Prod
ServerSignature Off
隐藏 Apache 版本和操作系统信息,减少被针对性攻击的风险

<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
允许通过FastCGI协议连接PHP-FPM

<Location "/">
<LimitExcept GET POST HEAD>
Require all denied
</LimitExcept>
</Location>
限制请求方法——只允许 GET/POST/HEAD 方法

SendBufferSize 8192
ReceiveBufferSize 8192
调整TCP缓冲区,优化网络传输性能

MPM性能优化——perfork模式

  • 需要兼容旧模块(如非线程安全的 mod_php)。
  • 处理少量稳定并发请求(如企业内部系统)。
1
2
3
4
5
6
7
8
9
10
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 250
MaxConnectionsPerChild 1000
</IfModule>

MaxRequestWorkers最大并发进程数
MaxConnectionsPerChild单个进程处理请求数后重启

MPM性能优化——event模式(推荐)

  • 高并发长连接(如 WebSocket、Comet)。
  • 静态资源服务器或反向代理。
1
2
3
4
5
6
7
8
9
10
11
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>

ThreadsPerChild每个进程的线程数
MaxConnectionsPerChild 0 禁止进程重启

优化参数场景推荐

  • 静态资源服务:启用 mod_deflate、mod_expires、使用 event模式、EnableSendfile。
  • 动态内容(如 PHP):使用 prefork模式、PHP-FPM、禁用 .htaccess。
  • 反向代理/负载均衡:启用mod_cache、调整KeepAliveTimeout。

重载配置

apache和nginx一样也可以使用命令行来热重载

1
2
3
4
apachectl configtest # 测试配置
Syntax OK

apachectl graceful # 热加载
CATALOG
  1. 1. 全局配置
    1. 1.1. ServerRoot “/etc/httpd”
    2. 1.2. Listen 80
    3. 1.3. Include conf.modules.d/*.conf
    4. 1.4. DocumentRoot “/var/www/html”
    5. 1.5. IncludeOptional
  2. 2. 权限与管理员信息
    1. 2.1. User 和 Group
    2. 2.2. ServerAdmin
    3. 2.3. ServerName
  3. 3. 目录权限控制
    1. 3.1. /根目录
    2. 3.2. /var/www父目录
    3. 3.3. 根目录 /var/www/html
  4. 4. 默认索引与别名
  5. 5. 安全防护
  6. 6. 日志配置
  7. 7. 脚本支持
  8. 8. MIME 类型与字符集
  9. 9. 性能优化参数总览
    1. 9.1. MPM性能优化——perfork模式
    2. 9.2. MPM性能优化——event模式(推荐)
    3. 9.3. 优化参数场景推荐
  10. 10. 重载配置