本文简书 Apache 配置文件。
常用命令
1 2 3 4 5 6 7 8 9 10
| # 查看哪种 MPM httpd -l || apachectl -l # 查看当前的连接数可以用 ps aux | grep httpd | wc -l #或 pgrep httpd | wc -l
# 计算httpd占用内存的平均数 ps aux|grep -v grep|awk '/httpd/{sum+=$6;n++};END{print sum/n}'
|
配置文件
ServerRoot 主机工作目录
Listen 监听端口
LoadModule 加载动态模块
执行用户,包括子进程执行用户
1 2
| User daemon Group daemon
|
服务器域名
ServerName www.gbin.me
文档目录,网站根目录,也即是域名对应的站点目录
DocumentRoot /home/www
www.gbin.me <===> /home/www
目录权限控制
1 2 3 4 5 6 7
| <Directory "/www"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all Require all granted </Directory>
|
- Options是对该目录的一些选项, Indexes表示在没有index.html等文件的时候显示文件列表
- AllowOverride All 表示允许使用 .htaccess 文件重写 URL,None 禁止用户覆盖(重载)配置文件
apache 的安全模块
- mod_evasive20( 防DDOS攻击)
- mod_limittipconn(针对单站点)配置
- mod_security(防止SQL注入)
设置目录默认首页
1
| DirectoryIndex index.html index.php #优先级从左往右依次降低
|
错误日志
1
| ErrorLog "logs/error_log"
|
访问日志
1
| CustomLog "logs/access_log" common
|
解析 .php 的脚本
1
| AddType application/x-httpd-php .php
|
控制错误页面的输出
1
| ErrorDocument 404 /missing.html
|
包含外部配置文件
1
| Include etc//extra/httpd-vhosts.conf
|
虚拟目录
1
| Alias /var/gbin/ "home/www/gbin" # 虚拟目录(目录别名)
|
/var/gbin/ ==> /home/www/gbin
ServerSignature off
apache目录文件权限设置(www:www 目录755 文件644)
mpm 三种模式
- prework
Apache2.2版本prefork参数:
1 2 3 4 5 6 7 8 9
| <IfModule prefork.c> StartServers 8 #默认启动的工作进程数;启动时开启的进程数 MinSpareServers 5 #最少空闲进程数,保存备用;备用进程的最小数目 MaxSpareServers 20 #最大空闲进程数,保存备用;备用进程的最大数目 ServerLimit 256 #最大活动进程数 MaxClients 256 #并发请求的最大数 MaxRequestsPerChild 4000 #每个子进程在生命周期内所能够服务的最多请求个数; #合理配置此参数可以有效避免apache服务器内存溢出 </IfModule>
|
注意:虽然MaxRequestsPerChild缺省设为0可以使每个子进程处理更多的请求,但如果设成非零
值也有两点重要的好处:
1、可防止意外的内存泄漏。
2、在服务器负载下降的时侯会自动减少子进程数。因此,可根据服务器的负载来调整这个值。
Apache2.4版本prefork参数:
1 2 3 4 5 6 7
| <IfModule mpm_prefork_module> #如果mpm_prefork_module这个模块被启用 StartServers 5 #启动时开启的进程数 MinSpareServers 5 #最少空闲进程数,保存备用;备用进程的最小数目 MaxSpareServers 10 #最大空闲进程数,保存备用;备用进程的最大数目 MaxRequestWorkers 250 #并发请求的最大数 MaxConnectionsPerChild 0 #每个子进程在生命周期内所能够服务的最多请求个数; </IfModule>
|
虚拟主机
压测
1
| ab -n XXX -c YYY http://hostname.port/path/filename
|
并发 XXX 个请求 连接数为 YYY
参考链接