目录:
一、httpd服务基本介绍
二、httpd-2.2常用小选项配置
三、httpd-2.2 VirtualHost配置
四、httpd-2.2的基于https的安全访问
--------------------------------------
一、httpd服务基本介绍:
httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立的运行的后台进程,它会建立一个出来请求的子进程或线程的池。通常httpd不被直接调用,而是由apachectl调用。http是一个应用程的协议,它可以分为http和https,它们分别使用tcp协议端口的80和443端口。
请求响应方式:
客户端需要访问某资源时会向服务器发送http请求报文,服务器根据客户端请求信息做出http响应报文,所以一次http事务就是http请求然后http会给予请求响应。
web资源:
资源的标识:URL:用于标识web资源所在的位置。
格式:协议://主机地址或者主机名[:端口][/目录资源]
静态资源:不需要服务器做任何操作处理,例如.JGP .PNG格式的文件等
动态资源:服务器需要执行一些程序做出处理后返回给客户端请求所需要的信息,例如.php .js
一次完整的htpt请求处理过程:
(1)建立或处理连接:接收请求或者是拒绝请求
(2)接收请求:接收客户端主机请求报文中对某个资源的一次请求过程
(3)处理请求:对请求报文进行解析,获取客户端请求的资源及请求方法相关信息
(4)访问资源:获取请求报文中请求的资源
(5)构建响应报文
(6)发送响应报文
(7)记录日志
并发访问响应模型:
单进程I/O模型:启动一个进程处理用户请求,一次只能处理一个请求对公请求被串行响应
多进程I/O模型:并行启动多个进程,每个进程响应一个请求
复用进程I/O模型:一个进程响应多个请求
多线程模式:一个进程生成多个线程,一个线程处理一个请求
事件驱动模式:一个进程直接响应多个请求
复用多进程I/O模型:启动多个进程,每个进程生成多个线程,响应请求的数量就是线程乘以进程
httpd工作模式:
prefork:多进程模式:一个主进程多个子进程,一个进程只响应一个请求。 一个主进程负责生成子进程及回收子 进程,接收请求,派发请求给子进程处理,生成的多个子进程中每个子进程负责处理一个请求。
worker:多进程多线程模式:一个线程响应一个用户请求。一个主进程负责生成子进程及回收及进程,创建套接字,派发请求给子进程处理。多个子进程负责生成多个线程。每个线程负责响应用户请求。
event:事件驱动模式:多进程模式:一个进程响应多个用户请求。一个主进程负责生成子进程及回收及进程,创建套接字,派发请求给子进程处理。子进程负责基于事件驱动机制直接来响应用户的请求。
二、httpd常用选项配置
1、监听端口
1
2
3
4
Listen 80 ##监听本机的80端口
Listen 8080 ##监听本机的8080端口
Listen 192.168.1.100:80 ##监听特定IP的80端口
Listen 192.168.1.101:8080 ##监听指定IP的特定端口
2、保持连接
1
2
3
KeepAlive Off|ON ##是否允许持续性连接,即建立一次tcp连接完成多个文件传输
MaxKeepAliveRequests 100 ##最大保存连接请求数量
KeepAliveTimeout 15 ##保存持续性连接超时时间
3、MPM多路处理模块
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
[iyunv@bogon ~]# ps aux | grep httpd ##查看httpd使用的MPM模块
root 5012 0.0 0.3 185928 3972 ? Ss 23:44 0:00 /usr/sbin/httpd
apache 5022 0.0 0.2 186060 2492 ? S 23:44 0:00 /usr/sbin/httpd
apache 5023 0.0 0.2 186060 2508 ? S 23:44 0:00 /usr/sbin/httpd
apache 5024 0.0 0.2 186060 2492 ? S 23:44 0:00 /usr/sbin/httpd
apache 5025 0.0 0.2 186060 2492 ? S 23:44 0:00 /usr/sbin/httpd
apache 5026 0.0 0.2 186060 2492 ? S 23:44 0:00 /usr/sbin/httpd
apache 5027 0.0 0.2 186060 2492 ? S 23:44 0:00 /usr/sbin/httpd
apache 5028 0.0 0.2 186060 2492 ? S 23:44 0:00 /usr/sbin/httpd
apache 5029 0.0 0.2 186060 2492 ? S 23:44 0:00 /usr/sbin/httpd
root 5064 0.0 0.0 103304 892 pts/2 S+ 23:55 0:00 grep httpd
[iyunv@bogon ~]# httpd -l #####我们从中可以看出使用的是默认preforkmok
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
如果需要更改我们默认使用的模块方法:
[iyunv@bogon ~]# vim /etc/sysconfig/httpd
HTTPD=/usr/sbin/httpd.worker
-------------------------- ----------------------------------
<IfModule prefork.c>###prefork模块配置
StartServers 8 #启动httpd时预先创建几个子进程数量
MinSpareServers 5 #最小空闲进程数量
MaxSpareServers 20 #最大空闲进程数量
ServerLimit 256 #服务器同一时间内能够响应进程的数量
MaxClients 256 #最多可以允许客户端并发请求连接数量
MaxRequestsPerChild 4000 #每个子进程能够响应的最大请求数量
</IfModule>
<IfModule worker.c>##worker模块配置
StartServers 4 #启动httpd是预先创建几个字进程数量
MaxClients 300 #最多允许客户端并发请求连接数量
MinSpareThreads 25 #最小空闲进程数量
MaxSpareThreads 75 #最大空闲进程数量
ThreadsPerChild 25 #进程启动时候生成的线程数量
MaxRequestsPerChild 0 #每个子进程能够响应的最大请求数量
</IfModule>
4、DSO(动态加载或卸载模块)
1
2
3
4
5
# Example:
# LoadModule foo_module modules/mod_foo.so
#
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
5、基于ip来源访问控制
1
2
3
4
5
6
7
8
9
<Directory "/var/www/html">
Options Indexes FollowSymLinks #定义资源展示方式
AllowOverride None
Order allow,deny
Allow from all
deny from 192.168.1.100 ###禁止192.168.1.100访问网站
</Directory>
6、定义站点主页面
1
DirectoryIndex index.html index.html.var (有多个文件自左而又查找)
7、定义路径别名
1
2
3
4
5
6
7
8
[iyunv@bogon ~]# mkdir -p /etc/url/index.html ###建立别名链接文件
[iyunv@bogon ~]# vim /etc/httpd/conf/httpd.conf
Alias /bbs/ "/etc/url/" ##编辑主配置文件设置路径别名连接
测试:
[iyunv@bogon ~]# curl 192.168.1.100/bbs/index.html
url alias
8、基于用户访问认证控制
第一种:基于用户账号进行认证
1)主配置文件中添加认证区域内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@bogon ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
deny from 192.168.1.100
添加内容##
AuthType Basic ###定义认证的类型:明文(basic)或密文(digest)
AuthName "admin use zone" ###提示信息
AuthUserFile "/etc/httpd/conf/.htpasswd" ###认证用户文件存放路径
Require valid-user ####允许用户文件中的所有用户登录
</Directory>
2)提供用户账号和密码文件:
1
2
[iyunv@bogon ~]# htpasswd -c -m /etc/httpd/conf/.htpasswd tom
[iyunv@bogon ~]# htpasswd -m /etc/httpd/conf/.htpasswd jerry
3)检查配置文件重新加载
1
2
3
4
[iyunv@bogon ~]# httpd -t
Syntax OK
[iyunv@bogon ~]# service httpd reload
Reloading httpd:
第二种:基于组账号进行认证
1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@bogon ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
deny from 192.168.1.100
添加内容##
AuthType Basic ###定义认证的类型:明文(basic)或密文(digest)
AuthName "admin use zone" ###提示信息
AuthUserFile "/etc/httpd/conf/.htpasswd" ##定义用户账号文件
AuthGroupFile "/etc/httpd/conf/.hgpasswd" ###定义组账号文件
Require group groupuser ###定义组账号文件中允许访问的用户
</Directory>
2)建立用户
1
2
[iyunv@bogon ~]# htpasswd -c -m /etc/httpd/conf/.htpasswd user1
[iyunv@bogon ~]# htpasswd -m /etc/httpd/conf/.htpasswd user2
3)建立组账号文件
1
2
[iyunv@bogon ~]# vim /etc/httpd/conf/.hgpasswd
groupuser: user1 user2
4)检查配置文件重新加载
1
2
3
4
[iyunv@bogon ~]# httpd -t
Syntax OK
[iyunv@bogon ~]# service httpd reload
Reloading httpd:
9、status页面
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
1)确保模块加载:LoadModule status_module modules/mod_status.so
2)开启228行 ExtendedStatus On
3)编辑status区域
<Location /var/www/html/server-status>
SetHandler server-status
Order allow,deny
Allow from 192.168
</Location>
4)检查配置文件正确否,加载服务
5)访问
Apache Server Status for 192.168.1.100
Server Version: Apache/2.2.15 (Unix) DAV/2
Server Built: Jul 24 2015 11:52:28
Current Time: Thursday, 12-May-2016 01:24:37 CST
Restart Time: Thursday, 12-May-2016 01:22:20 CST
Parent Server Generation: 7
Server uptime: 2 minutes 16 seconds
Total accesses: 4 - Total Traffic: 0 kB
CPU Usage: u0 s0 cu0 cs0
.0294 requests/sec - 0 B/second - 0 B/request
1 requests currently being processed, 7 idle workers
____W___........................................................
................................................................
................................................................
................................................................
Scoreboard Key:
10、运行httpd服务的属主和属组
1
2
User apache
Group apache
11、使用mod_deflate模块压缩页面优化传输速度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
SetOutputFilter DEFLATE
# mod_deflate configuration
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
# Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 9
# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
三、httpd-2.2 VirtualHost配置
1、基于IP的virtualhost:(事先准备好两个IP)
1)编辑配置文件设置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
<VirtualHost 192.168.1.100:80>
DocumentRoot "/web/vhosts/www1"
ServerName www1.a1.com
ErrorLog "/var/log/httpd/www1.err"
CustomLog "/var/log/httpd/www1.access" common
</VirtualHost>
<VirtualHost 192.168.1.101:80>
DocumentRoot "/web/vhosts/www2"
ServerName www2.a2.com
ErrorLog "/var/log/httpd/www2.err"
CustomLog "/var/log/httpd/www2.access" common
</VirtualHost>
2)修改下
1
DocumentRoot "/web/vhosts/"
3)建立网页文件
1
2
[iyunv@bogon ~]# mkidr -p /web/vhosts/www1/index.html
[iyunv@bogon ~]# mkidr -p /web/vhosts/www2/index.html
4)检查配置文件是否正确,然后重启或加载,ok后测试结果
1
2
3
[iyunv@bogon ~]# httpd -t
Syntax OK
[iyunv@bogon ~]# service httpd reload
5)查看我们的日志文件
1
2
3
4
5
6
7
[iyunv@bogon ~]# cat /var/log/httpd/www1.access
192.168.1.103 - - [12/May/2016:02:14:04 +0800] "GET / HTTP/1.1" 200 25
[iyunv@bogon ~]#
[iyunv@bogon ~]#
[iyunv@bogon ~]# cat /var/log/httpd/www
www1.access www1.err www2.access www2.err
[iyunv@bogon ~]# cat /var/log/httpd/www1.err
2、基于端口的virtualhost
1)编辑配置文件设置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
<VirtualHost 192.168.1.100:80>
DocumentRoot "/web/vhosts/www1"
ServerName www1.a1.com
ErrorLog "/var/log/httpd/www1.err"
CustomLog "/var/log/httpd/www1.access" common
</VirtualHost>
<VirtualHost 192.168.1.100:8080>
DocumentRoot "/web/vhosts/www2"
ServerName www2.a2.com
ErrorLog "/var/log/httpd/www2.err"
CustomLog "/var/log/httpd/www2.access" common
</VirtualHost>
2)设置监听地址
1
2
Listen 80
Listen 8080
3)检查然后重新加载服务,测试
3、基于FQDN的virtualhost
1)开启虚拟主机域名
1
NameVirtualHost 192.168.1.100:80
2)编辑配置文件设置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
<VirtualHost 192.168.1.100:80>
DocumentRoot "/web/vhosts/www1"
ServerName www1.a1.com
ErrorLog "/var/log/httpd/www1.err"
CustomLog "/var/log/httpd/www1.access" common
</VirtualHost>
<VirtualHost 192.168.1.100:80>
DocumentRoot "/web/vhosts/www2"
ServerName www2.a2.com
ErrorLog "/var/log/httpd/www2.err"
CustomLog "/var/log/httpd/www2.access" common
</VirtualHost>
3)解析域名,此处我就用hosts文件来解析了
1
2
3
4
5
6
[iyunv@bogon ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.0.1 server.magelinux.com server
192.168.1.100 www1.a1.com
192.168.1.100 www2.a2.com
4)检查加载服务,测试即可
四、httpd-2.2的基于https的安全访问
###CA服务器上操作:
1)生成密钥对密钥对
1
[iyunv@bogon CA]# (umak 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
2)生成自签证书
1
2
3
4
5
6
7
8
9
[iyunv@bogon CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3655
Country Name (2 letter code) [XX]:CN ##国家
State or Province Name (full name) []:beijing ##省
Locality Name (eg, city) [Default City]:beijin ##市
Organization Name (eg, company) [Default Company Ltd]:magedu.com #公司名称
Organizational Unit Name (eg, section) []:yunwei ##部门
Common Name (eg, your name or your server's hostname) []:bogon ##ca域名
Email Address []:admin@163.com #邮箱
3)为CA提供所需目录及文件
1
2
[iyunv@bogon CA]# touch {serial,index.txt}
[iyunv@bogon CA]# echo 01 > serial
###httpd服务器上操作:
1)生成密钥
1
2
3
4
5
[iyunv@bogon ~]# mkdir /etc/httpd/ssl
[iyunv@bogon ~]# cd /etc/httpd/ssl/
[iyunv@bogon ssl]# (umask 077;openssl genrsa -out /etc/httpd/)
conf/ conf.d/ logs/ modules/ run/ ssl/
[iyunv@bogon ssl]# (umask 077;openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)
2)生成证书签署请求
1
2
3
4
5
6
7
8
9
[iyunv@bogon ssl]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr -days 365
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijin
Organization Name (eg, company) [Default Company Ltd]:magedu.com
Organizational Unit Name (eg, section) []:yunwei
Common Name (eg, your name or your server's hostname) []:bogon
Email Address []:admin@163.com
3)在CA上签署证书,并将证书方式给请求者
1
2
[iyunv@bogon tmp]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
[iyunv@bogon certs]# scp httpd.crt root@192.168.1.100 :/etc/httpd/ssl
4)httpd要支持SSL需要安装mod_ssl模块
1
[iyunv@bogon ~]# yum -y install mod_ssl
5)配置/etc/httpd/conf.d/ssl.conf
1
2
3
4
5
6
<VirtualHost 192.168.1.100:443> ##此行IP地址需要按照你自己需求更改
DocumentRoot "/web/vhosts/www1"
ServerName
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key ##证书私钥
6)检查配置文件,重新加载,测试即可
1
2
3
4
[iyunv@bogon ssl]# httpd -t
Syntax OK
[iyunv@bogon ssl]# service httpd reload
Reloading httpd:
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com