1、Centos7系统下实现httpd-2.2的安装,并分别实现prefork、worker、event等几种工作方式
- prefork
prefork是一个两级进程模型,非线程的模式,其实通过由父进程管理创建子进程,子进程响应的相应的请求的方式来运行的。以prefork模式运行的httpd,在启动之际就预派生fork了一些子进程,然后等待请求。每个子进程只有一个线程,在一个时间点内只能处理一个请求。 优点:成熟、稳定、兼容所有新老模块。进程之间完全独立,无须担心线程安全的问题。 缺点:一个进程相对会占用更多的系统资源,消耗更多的内存。不适合处理高并发请求,因其会把请求放进队列中,一直等到有可用进程才会处理相应的请求。
- worker
worker是一个三级结构、多进程多线程的模式,其在启动时也预先fork了几个子进程,每个子进程能够生产若干个服务线程和若干个监听线程,每个服务线程处理一个请求,监听线程负责接入请求并将其传递给服务线程处理和应答。线程比起进程会更轻量,因为线程通常会共享父进程的内存空间,因此内存的占用会减少些,在高并发的场景下表现比prefork模式好。 优点:占用内存少,高并发性能更优秀。 缺点:当一个线程出现问题的时候会导致同一进程下的线程也会出现问题。在keep-alive长连接的方式下,某个线程会被一直占用,即使中间没有请求,也需要等待到超时才会被释放。
- event
event模式是最新的运行模式,在httpd-2.4中已经是稳定可用的模式。其运行原理与worker类似,区别在于,event模式解决了在keep-alive模式下,线程被长期占用直到超时,从而导致资源浪费的问题。 在event模块中,有一个专门的线程来管理这些keep-alive类型的线程,当接收到真实的请求时,会将请求传递给服务线程,执行完毕后,会将对应的服务线程释放,这样就能实现线程的异步非阻塞。
- httpd-2.2安装
调整服务器环境(关闭防火墙)[[email protected] ~]#systemctl stop firewalld.service [[email protected] ~]#setenforce 0
准备安装环境
[[email protected] ~]# yum groupinstall "Development Tools" "ServerPlatform Development" -y
下载并解压安装包
[[email protected] ~]#wget http://archive.apache.org/dist/httpd/httpd-2.2.34.tar.gz [[email protected] ~]# tar xf httpd-2.2.34.tar.gz [[email protected] ~]# ls httpd-2.2.34 httpd-2.2.34.tar.gz
进入对应的解压缩目录开始编译安装
[[email protected] httpd-2.2.34]#./configure --prefix=/usr/local/httpd-2.2.34 -with-mpm=prefork [[email protected] httpd-2.2.34]# make && make install
软链接到对应的目录中
[[email protected] ~]# ln -sv /usr/local/httpd-2.2.34/ /usr/local/httpd ‘/usr/local/httpd’ -> ‘/usr/local/httpd-2.2.34/’
设定环境变量
[[email protected] ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/httpd/bin [[email protected] ~]# PATH="$PATH:/usr/local/httpd/bin" [[email protected] ~]# vim /etc/profile.d/httpd.sh [[email protected] conf]# cat /etc/profile.d/httpd.sh export PATH="$PATH:/usr/local/httpd/bin" [[email protected] ~]# source /etc/profile.d/httpd.sh
调整权限
[[email protected] ~]# useradd -M -s /sbin/nologin httpd [[email protected] ~]# chown -R httpd.httpd /usr/local/httpd [[email protected] ~]# chown -R httpd.httpd /usr/local/httpd-2.2.34/
编辑unit文件使用systemctl 管理启动httpd
[[email protected] conf]# vim /usr/lib/systemd/system/httpd.service [[email protected] conf]# cat /usr/lib/systemd/system/httpd.service [Unit] Description=The httpd service After=network.target [Service] Type=forking ExecStart=/usr/local/httpd-2.2.34/bin/apachectl start ExecStop=/usr/local/httpd-2.2.34/bin/apachectl stop ExecRestart=/usr/local/httpd-2.2.34/bin/apachectl restart PrivateTmp=true [Install] WantedBy=multi-user.target
启动服务
[[email protected] conf]# systemctl daemon-reload [[email protected] conf]# systemctl start httpd.service [[email protected] conf]# ps aux |grep httpd root 10938 0.0 0.1 49292 1928 ? Ss 15:28 0:00 /usr/local/httpd-2.2.34/bin/httpd -k start daemon 10939 0.0 0.0 49428 1300 ? S 15:28 0:00 /usr/local/httpd-2.2.34/bin/httpd -k start daemon 10940 0.0 0.0 49428 1300 ? S 15:28 0:00 /usr/local/httpd-2.2.34/bin/httpd -k start daemon 10941 0.0 0.0 49428 1300 ? S 15:28 0:00 /usr/local/httpd-2.2.34/bin/httpd -k start daemon 10942 0.0 0.0 49428 1300 ? S 15:28 0:00 /usr/local/httpd-2.2.34/bin/httpd -k start daemon 10943 0.0 0.0 49428 1300 ? S 15:28 0:00 /usr/local/httpd-2.2.34/bin/httpd -k start root 10949 0.0 0.0 112660 964 pts/0 R+ 15:28 0:00 grep --color=auto httpd
在编译安装的时候对应做模式变更即可,当前为prefork模式
./configure -prefix=/usr/local/httpd -with-mpm=worker ./configure -prefix=/usr/local/httpd -with-mpm=event
2、简述request报文请求方法和状态响应码
request报文的请求方法:
状态响应码:
常用的状态响应码:
3、详细描述httpd虚拟主机、站点访问控制、基于用户的访问控制、持久链接等应用配置实例
- 虚拟主机
一个物理服务器可以有多个站点,每个站点可通过一个或者多个虚拟主机来实现; httpd有三种类型的虚拟主机 基于IP:为每个虚拟主机准备至少一个IP地址; 基于PORT:为每个虚拟主机使用至少一个独立的PORT; 基于FQDN(ServerName):为每个虚拟主机使用至少一个FQDN; 注意: 1.一般虚拟主机不要与中心主机混用;要使用虚拟主机,得先禁用"main"主机,禁用方法: 注释中心主机的DocumentRoot指令即可. 2.配置VirtualHost,在httpd2.2中,NameVirtualHost这一项需启用,2.2以上版本不需要.
实例
1)基于IP地址
增加两个ip地址,确保网络能ping通
[[email protected] ~]#ip addr add 192.168.2.103/24 dev ens33
[[email protected] ~]#ip addr add 192.168.2.104/24 dev ens33
创建两个虚拟主机的数据目录并输入数据
[[email protected] html]#mkdir -p /var/www/html/103
[[email protected] html]#mkdir -p /var/www/html/204
[[email protected] html]#echo "hello,ip address is "192.168.2.103"" > /var/www/html/103/index.html
[[email protected] html]#echo "hello,ip address is "192.168.2.104"" > /var/www/html/104/index.html
编辑配置文件,修改虚拟主机和权限
[[email protected] ~]# vim /etc/httpd/conf.d/virtualhost.conf
[[email protected] ~]# cat /etc/httpd/conf.d/virtualhost.conf
<VirtualHost 192.168.2.103:80>
DocumentRoot "/var/www/html/103"
ServerName www.EAPDomain103.com
<Directory "/var/www/html/103">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.2.104:80>
DocumentRoot "/var/www/html/104"
ServerName www.EAPDomain104.com
<Directory "/var/www/html/104">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
测试语法并重启httpd服务,然后测试对应结果
[[email protected] ~]#httpd -t
Syntax OK
[[email protected] ~]#systemctl restart httpd.service
[[email protected] ~]#curl 192.168.2.103
hello,ip address is 192.168.2.103
[[email protected] ~]#curl 192.168.2.104
hello,ip address is 192.168.2.104
2)基于端口号
创建基于端口的目录和索引文件
[[email protected] ~]#mkdir -p /var/www/html/8086
[[email protected] ~]#mkdir -p /var/www/html/8088
[[email protected] ~]#echo "hi,the ip Port is ‘8086‘" >/var/www/html/8086/index.html
[[email protected] ~]#echo "hi,the ip Port is ‘8088‘" >/var/www/html/8088/index.html
修改配置文件端口号及相关配置
[[email protected] ~]# vim /etc/httpd/conf.d/virtualhost.conf
[[email protected] ~]# cat /etc/httpd/conf.d/virtualhost.conf
Listen 8086
Listen 8088
<VirtualHost 192.168.2.100:8086>
DocumentRoot "/var/www/html/8086"
ServerName "www.EAPDomain.com"
<Directory "/var/www/html/8086">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.2.100:8088>
DocumentRoot "/var/www/html/8088"
ServerName "www.EAPDomain.com"
<Directory "/var/www/html/8088">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
测试语法并重启httpd服务,然后测试对应结果
[[email protected] ~]#httpd -t
Syntax OK
[[email protected] ~]#systemctl restart httpd.service
[[email protected] ~]#curl 192.168.2.100:8086
hi,the ip Port is ‘8086‘
[[email protected] ~]#curl 192.168.2.100:8088
hi,the ip Port is ‘8088‘
3)基于主机域名
创建基于主机域名的目录和索引文件
[[email protected] ~]#mkdir -p /var/www/html/EAPDomain
[[email protected] ~]#mkdir -p /var/www/html/EDomain
[[email protected] ~]#echo "domain name is ‘www.EAPDomain.com‘" >/var/www/html/EAPDomain/index.html
[[email protected] ~]#echo "domain name is ‘www.EDomain.com‘" >/var/www/html/EDomain/index.html
修改主配置文件相关配置
[[email protected] ~]# vim /etc/httpd/conf.d/virtualhost.conf
[[email protected] ~]# cat /etc/httpd/conf.d/virtualhost.conf
<VirtualHost 192.168.2.100:80>
DocumentRoot "/var/www/html/EAPDomain"
ServerName www.EAPDomain.com
<Directory "/var/www/html/EAPDomain">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.2.100:80>
DocumentRoot "/var/www/html/EDomain"
ServerName www.EDomain.com
<Directory "/var/www/html/EDomain">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
更改本地hosts文件对应关系或自建DNS做对应修改
[[email protected] named]#vim /etc/hosts
[[email protected] named]#cat /etc/hosts
192.168.2.100 www.EAPDomain.com www.EDomain.com
测试语法并重启httpd服务,然后测试对应结果
[[email protected] ~]#httpd -t
Syntax OK
[[email protected] ~]#systemctl restart httpd.service
[[email protected] named]#curl www.EAPDomain.com
domain name is ‘www.EAPDomain.com‘
[[email protected] named]#curl www.EDomain.com
domain name is ‘www.EDomain.com‘
- 站点访问控制
可基于来源地址或者基于账号两种访问控制机制;定义的方式有两种: 文件系统路径:<Directory ""> ... </Directory> URL路径:<Location ""> ... </Location> <Directory>中“基于源地址”实现访问控制: (1) Options后跟1个或多个以空白字符分隔的“选项”列表; Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户;作为下载站点时才使用;否则,不使用此选项; FollowSymLinks:允许跟踪符号链接文件所指向的源文件; None:都禁止; All:都允许; Includes:允许启用服务器包含; SymLinksifOwnerMatch:比FollowSymLinks在限制上更为严格的机制;表示只有原文件的属主和链接文件的属主相同时,才允许跟踪; ExecCGI:允许执行cgi脚本; MultiViews:允许执行内容协商;非常消耗资源且不×××全; (2) AllowOverride:与访问控制相关的哪些指令可以放在.htaccess文件(每个目录即网站目录下都可以有一个,指令放在此文件中表示当前目录下的配置文件都无效,所在目录下的这个隐藏文件生效)中; All: 所有指令都可放在这个隐藏文件中; None:这个隐藏文件中什么指令都不放;或给个指定列表,文件信息、根认证相关的信息,限制可放进这个隐藏文件中;会使网站资源解析时性能影响非常大; (3) http-2.2中基于IP地址的访问控制是利用Allow和Deny来实现的;order:定义生效次序;写在后面的表示默认法则;
实例
禁止ip:192.168.2.33访问
修改虚拟主机配置文件
[[email protected] ~]#vim /etc/httpd/conf.d/virtualhost.conf
[[email protected] ~]#cat /etc/httpd/conf.d/virtualhost.conf
<VirtualHost 192.168.2.103:80>
Servername www.EAPDomain.io
DocumentRoot "/var/www/html/EAPDomain"
<Directory "/var/www/html/EAPDomain">
Options None
AllowOverride None
Require all granted
</Directory>
Customlog "logs/EAPDomain_access_log" combined
</VirtualHost>
LISTEN 8080
<VirtualHost 192.168.2.104:8080>
Servername www.EDomain.io
DocumentRoot "/var/www/html/EDomain"
<Directory "/var/www/html/EDomain">
Options None
AllowOverride None
<RequireAll>
require all granted
require not ip 192.168.2.33
</RequireAll>
</Directory>
Customlog "logs/EDomain_access_log" combined
</VirtualHost>
重启httpd服务,然后在主机192.168.2.33上检测结果
[[email protected] ~]# systemctl restart httpd.service
[[email protected] ~]# curl http://192.168.2.103
<h1>EAPDomain</h1>
[[email protected] ~]# curl http://192.168.2.104:8080
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Apache HTTP Server Test Page powered by CentOS</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- 基于用户的访问控制
对于网站中的一些资源,需要只对特定的用户开放,可以通过用户的访问控制来实现.有质询和认证两种方式. 质询:服务器用401响应码拒绝客户端请求,并说明要求客户端提供账号和密码 认证:客户端用户填入账号和密码后再次发送请求报文,认证通过时,则服务器发送响应的资源.有basic基本认证和digest摘要认证两种.这里以basic认证做实验. 登陆使用的账号为虚拟账号而非系统的账号,而且httpd是明文传输的,所以使用htpasswd命令来创建账号文件格式如下: htpasswd [options] /PATH/TO/HTTPD_PASSWD_FILE username -c:自动创建此处指定的文件,因此,仅应该在此文件不存在时使用 -m:md5格式加密 -s: sha格式加密 -D:删除指定用户
实例
创建密码账户和允许访问的用户
[[email protected] ~]#htpasswd -c /tmp/test.users tom
[[email protected] ~]#htpasswd -m /tmp/test.users jerry
[[email protected] ~]#htpasswd -m /tmp/test.users obama
把存放密码的文件移动到httpd目录下,且保存为隐藏文件
[[email protected] ~]#mv /tmp/test.users /etc/httpd/conf.d/.htpasswd
创建测试主页面
[[email protected] conf.d]#mkdir -p /var/www/html/testusers
[[email protected] conf.d]#echo "Testusers Area" > /var/www/html/testusers/index.html
创建模块化文件并且做对应配置
[[email protected] conf.d]#vim /etc/httpd/conf.d/testusers.conf
<Directory "/var/www/html/testusers">
Options None
AllowOverride None
AuthType basic
AuthName "Test Area,pls enter your username and password"
AuthUserFile "/etc/httpd/conf.d/.htpasswd"
Require user tom jerry obama
#Require valid-user
</Directory>
编辑配置文件
[[email protected] ~]# vim /etc/httpd/conf.d/text.conf
[[email protected] ~]# cat /etc/httpd/conf.d/text.conf
DocumentRoot "/var/www/html/testusers"
<Directory "/var/www/html/testusers">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
测试语法并重启httpd服务,然后测试
[[email protected] conf.d]#httpd -t
Syntax OK
[[email protected] conf.d]#systemctl restart httpd.service
- 持久连接
Persistent Connection:tcp连接建立后,每个资源获取完成后不全断开连接,而是继续等待其他资源请求的进行;但是总归要断开,如何断开?可以通过对连接数量和时间限制来控制;比如:限制100个连接,超过100个后会断开最先的连接;限制连接60秒后没有进行任何操作则断开; 副作用:对并发访问量较大的服务器,长连接机制会使得后续某些请求无法得到正常响应; 折衷的改进办法:使用较短的持久连接时长,以及较少的请求数量; 持久连接的相关参数: KeepAlive Off|On #关闭或打开持久连接 MaxKeepAliveRequserts 100 #最大持久连接请求数量 KeepAliveTimeout 15 #连接时长,单位秒;httpd-2.4支持毫秒
实例
创建模块化进行对应设置
[[email protected] ~]#vim /etc/httpd/conf.d/keepalive.conf
[[email protected] ~]#cat /etc/httpd/conf.d/keepalive.conf
KeepAlive On
KeepAliveTimeout 35
MaxKeepAliveRequests 100
测试语法并重启httpd服务,然后测试
[[email protected] conf.d]#httpd -t
Syntax OK
[[email protected] conf.d]#systemctl restart httpd.service
原文地址:http://blog.51cto.com/13984342/2327917
时间: 2024-10-04 17:00:46