主程序文件
/usr/sbin/httpd (默认)
/usr/sbin/httpd.event
/usr/sbin/httpd.worker
MPM(Multipath Process Moule多道处理模块)
- prefok 多进程模型,一个进程响应一个请求
- worker多线程模型(多进程生成,一个进程生成多个线程)一个线程响应一个请求
- event事件驱动模型,一个线程响应多个请求
查看使用哪种方式,ps -aux | grep httpd
usr/sbin/http —– prefork
usr/sbin/http.worker —– worker
usr/sbin/http.event —– event
三种方式的切换
日志文件 /var/log/httpd
配置文件
- /etc/httpd/conf/
- /etc/httpd/conf.d/
- 脚本配置文件/etc/sysconfig/httpd
1.修改监听的ip和port
Listen [IP:]PORT
1. 省略ip表示监听所有端口
2. Listen可出现多次
vim /etc/httpd/conf/httpd.config
42 Listen 80
43 Listen 192.168.1.66:8080
service httpd restart
2.连接配置
- 通过KeepAlive on|off 开启或者关闭持久连接
- MaxKeepAliveRequests 100 最大持久连接数(默认100)
- KeepAliveTimeout 15 持久连接超时时间(默认15秒)
持久连接(Persistent Connection,每个资源获取完成后不会断开,继续等待其他请求)
断开持久连接设置
- 连接数量限制
- 时间限制
持久连接缺陷
并发访问量较大的时候,会使有些请求一直处于等待状态,以至于超时得不到响应
折中方案
使用较短的持久连接时间,2.4支持毫秒级持久时间
查看静态编译模块
httpd -l
Compiled in modules:
core.c
mod_so.c
http_core.c
查看静态编译及动态装载的模块(static 静态模块;share动态模块)
httpd -M
centos7防火墙设置
开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
重启防火墙
firewall-cmd –reload
站点目录设置/etc/httpd/conf/httpd.conf
站点根目录DocumentRoot
站点访问控制
Directory
File 对具体文件进行访问控制
FileMatch 使用正则
访问控制机制
Options
- Options Indexes FollowSymLinks (Index允许显示文件列表;FollowSymLinks允许访问链接文件链接的文件内容)
基于来源地址
Order 检查次序
- Order allow,deny 默认deny,只有显式指定allow的才allow
- Order deny,allow 默认allow,只有显式指定deny的才deny
Allow from xxx (允许来源地址)
Deny form xxx (组织来源地址)
来源地址可以是ip或者网络地址,如172.16,172.16.0.0,172.16.0.0/16,172.16.0.0/255.255.0.0
基于用户的访问控制
认证咨询
WWW-Authenticate:响应码401,拒绝客户端请求,并要求客户端提供账号和密码
认证:
Authorization:客户端填入账号和密码后再次发送请求报文,认证通过,服务端发送响应内容
认证类型
basic 明文
- 定义安全域
<Directory "">
options
AllowOverride None
AuthType Basic
AuthName "STRING"
AuthUserFile "/path/to/httpd_user_passwd_file" #用户认证文件
Require user username1 username2.... #允许登录用户
</Directory>
实例
<Directory>
options None
AllowOerride None
AuthType Basic
AuthName 'Adminstrator private'
AuthUserFile '/etc/httpd/conf.d/.htpasswd'
Reqire valid-user #允许htpasswd文件中所有用户登录
</Directory>
- 提供账号和密码存储(文本文件,数据库,ldap,nis)
使用htpasswd命令管理
htpasswd [options] passwdfile username
- -c:自动创建passwdfile
- -m md5加密用户密码
- -s sha1加密用户密码
- -D:删除指定用户
htpasswd -c -m /etc/httpd/conf.d/.htpasswd lee
htpasswd -c -m /etc/httpd/conf.d/.htpasswd yongge
digest 消息摘要(MD5)老版本浏览器不支持
基于组的用户认证
<Directory "">
options
AllowOverride None
AuthType Basic
AuthName "STRING"
AuthUserFile "/path/to/httpd_user_passwd_file" #用户认证文件
AuthGroupFile "/path/to/httpd_group_file" #用户认证文件
Require user username1 username2.... #允许登录用户
</Directory>
要提供用户认证文件和组认证文件
组文件:每行定义一个组
GRP_NAME:user1 user2 user3
vim /etc/httpd/conf.d/.htgroup
#组名:用户1 用户2 ....
webadmins:lee yongge
基于账号实现访问控制
默认主页 DirectoryIndex
日志路径配置(相对于/etc/httpd)
错误日志 logs/error_log
错误级别: debug,info,notice,warn,error,crit,alert,emerg
访问日志
CustomLog logs/access_og combined
日志格式 (由LogFormat定义)
- LogFormat “%h %l%u%t\”%r\”%>s\”%{Referer}i\”%{User-Agent}i\”“combined(组合方式)
- LogFormat “%h %l %u %t \”%r\” %>s %b” common
You need to enable mod_logio.c to use %I and %O
- LogFormat “%h %l %u%t\”%r\”%>s%b\”%{Referer}i\”\”%{User-Agent}i\”%I%O”combinedio
格式字符串含义
- %h: 客户端ip地址
- %l: 远程登录用户名,-表示没有
- %u: 认证时输入的用户名,-表示没有
- $t: 服务器收到请求的时间
- %r: 请求报文的首行信息(method url version)
- %s: 响应状态码
- %b: 响应报文主体部分大小(字节,不包含报文首部)
- %{Referer}i:请求报文中referer首部的值,当前资源访问入口,即从哪个页面跳转而来
- %{User-Agent}i: 请求报文中User-Agent首部信息:即发出请求的应用程序,一般是浏览器
别名配置
Alias /bbs/ /var/forum 访问站点根目录下的bbs自动转向/var下的forum,bbs目录在文档根下可以不存在
设定默认字符集
AddDefaultCharset UTF-8
常用(GBK,GB2312,GB18030)
虚拟主机
基于ip
基于port #需提前Listen这些端口
基于hostname
==注意:虚拟主机不能与中心主机混用,要使用虚拟主机先禁用中心主机,禁用方式:注释DocumenRoot==
每个主机都有专用配置
<VirtualHost "IP:PORT">
ServerName
DocumentRoot ""
</VirtualHost>
ServerName和DocumentRoot是基本配置还可定义
- Serveralias虚拟主机别名
ErrorLog
CustomLog
<Directory></Directory>
实例1:基于ip
<VirtualHost 172.16.100.6:80>
ServerName web1.mageedu.com
DocumentRoot '/vhosts/web1/htdocs'
</VirtualHost>
<VirtualHost 172.16.100.7:80>
ServerName web2.mageedu.com
DocumentRoot '/vhosts/web2/htdocs'
</VirtualHost>