1.httpd的特性:
(1)高度模块化:core + modules
(2)DSO: Dynamic Shared Object 动态共享对象
(3)MPM:Multipath Processing Modules 多路径处理模块
简单介绍三种模块:
prefork:多进程模型,每个进程响应一个请求;
一个主进程:负责生成n个子进程,子进程也称为工作进程,每个子进程处理一个用户请求;即便没有用户请求,也会预先生成多个空闲进程,随时等待请求到达;最大不会超过1024个;
worker:多线程模型,每个线程响应一个请求;
一个主进程:生成多个子进程,每个子进程负责生个多个线程,每个线程响应一个请求;
m进程,n线程:m*n
event:事件驱动模型,每个线程响应n个请求;
一个主进程:生成m个子进程,每个进程直接n个请求;
m*n
2.yum安装httpd
1
|
yum
install
httpd -y
|
2.1整体环境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@node1 ~]
# rpm -ql httpd
/etc/httpd
/etc/httpd/conf
/etc/httpd/conf
.d
/etc/httpd/conf
.d
/README
/etc/httpd/conf
.d
/welcome
.conf
/etc/httpd/conf/httpd
.conf
/etc/httpd/conf/magic
/etc/httpd/logs
/etc/httpd/modules
/etc/httpd/run
/etc/logrotate
.d
/httpd
/etc/rc
.d
/init
.d
/htcacheclean
/etc/rc
.d
/init
.d
/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd
/usr/lib64/httpd
/usr/lib64/httpd/modules
...
|
配置文件:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
服务脚本:
/etc/rc.d/init.d/httpd
配置文件:/etc/sysconfig/httpd
主程序文件:
/usr/sbin/httpd
/usr/sbin/httpd.event
/usr/sbin/httpd.worker
日志文件目录:
/var/log/httpd
access_log: 访问日志
error_log:错误日志
站点文档目录:(DocumentRoot)
/var/www/html
模块文件路径:
/usr/lib64/httpd/modules
2.2配置文件结构
1
2
3
4
|
# grep "Section" /etc/httpd/conf/httpd.conf
### Section 1: Global Environment
### Section 2: 'Main' server configuration
### Section 3: Virtual Hosts
|
全局配置:对主服务器或虚拟主机都有效,决定Apache服务器的全局参数
主服务器配置:主服务配置,相当于是Apache中的默认Web站点
虚拟服务器:虚拟主机,虚拟主机不能与Main Server主服务器共存
2.3测试配置和查看的命令
1
2
3
4
5
6
7
8
9
|
httpd -t
#测试配置文件有没有语法错误
httpd -l
#当前服务器所使用的模型及开启模块
httpd -D DUMP_MODULES
#当前服务器支持的模块
httpd -M
#列出加载的静态模块与动态模块
service httpd reload
#重新加载配置文件
service httpd restart
#重启httpd服务
注意:修改配置文件后要重新加载配置文件,修改服务器监听端口后要重启服务
|
2.4常用配置
1.修改监听的IP和Port
Listen [IP:]PORT #省略ip表示监听本机所有IP; Listen可重复出现多次;
2.持久连接
Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成;
断开 ---数量限制与时间限制
副作用:对并发访问量较大的服务器,持久连接功能会使用有些请求得不到响应;
1
2
3
|
KeepAlive On|Off
#是否支持长连接
MaxKeepAliveRequests
#超时时间内允许请求的次数
KeepAliveTimeout
#长连接超时时间
|
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
|
查看模块列表:
(1)查看静态编译的模块
# httpd -l
[root@node1 ~]
# httpd -l
Compiled
in
modules:
core.c
prefork.c
http_core.c
mod_so.c
(2)查看静态编译及动态装载的模块
# httpd -M
[root@node1 ~]
# httpd -M
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
auth_basic_module (shared)
auth_digest_module (shared)
authn_file_module (shared)
authn_alias_module (shared)
authn_anon_module (shared)
...
|
3.1安装完默认使用prefork模型
1
2
3
4
5
6
7
8
|
<IfModule prefork.c>
StartServers 8
#服务启动后默认开启的进程数
MinSpareServers 5
#最少空闲进程数
MaxSpareServers 20
#最多空闲进程数
ServerLimit 256
#每个进程允许开启最多的子进程数
MaxClients 256
#每个进程最多用户链接数
MaxRequestsPerChild 4000
#长连接时每个用户最多请求数
<
/IfModule
>
|
3.2worker模型
要支持worker模型需要配置/etc/sysconfig/httpd,开启以下行:
HTTPD=/usr/sbin/httpd.worker
1
2
3
4
5
6
7
8
|
<IfModule worker.c>
StartServers 4
#服务启动默认开启的进程数
MaxClients 300
#最多同时客户连接数
MinSpareThreads 25
#最少空闲进程数
MaxSpareThreads 75
#最好空闲进程数
ThreadsPerChild 25
#每个进程开启的线程数
MaxRequestsPerChild 0
#每个进程启动的最大线程数,如达到限制数时进程将结束,如置为0则子线程永不结束
<
/IfModule
>
|
4.Dynamic Shared Object (DSO)配置加载动态模块
1
2
3
4
5
6
7
|
# 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
LoadModule authn_file_module modules
/mod_authn_file
.so
LoadModule authn_alias_module modules
/mod_authn_alias
.so
LoadModule authn_anon_module modules
/mod_authn_anon
.so
|
LoadModule <mod_name> <mod_path>
模块路径可使用相对地址,相对于ServerRoot
5.配置网站根目录
1
|
DocumentRoot
"/var/www/html"
#指定网站的主目录
|
文档路径映射:DocumentRoot指向的路径为URL路径的起始位置;
#容器内定义站点的访问权限
#容器内限定用户的访问方法
6.站点访问控制
1
2
3
4
5
6
7
8
9
10
11
12
|
可基于两种类型的路径指明对哪些资源进行访问控制
文件系统路径:
<Directory
""
> <
/Direcotry
>
<File
""
> <
/File
>
<FileMatch
""
> <
/FileMatch
>
URL路径:
<Location
""
> <
/Location
>
...
访问控制机制:
基于来源地址;
基于账号;
|
7.Directory中“基于来源地址”实现访问控制
(1) Options
1
2
3
4
5
|
Indexes
#是否允许索引页面文件,建议关闭 FollowSymLinks #是否跟随软连接文件
SymLinksifOwnerMatch
#跟随符号链接,只允许访问运行apache的用户有属主权限的文件
ExecCGI:
#是否允许执行CGI脚本;
All
None
|
(2) 基于来源地址的访问控制机制
1
2
3
4
5
6
7
8
|
系统默认允许所有人访问
Order
#定义allow和deny那个为默认法则;写在后面的为默认法则:写在前面的指令没有显示定义的即受后面的指令控制:
Order allow,deny
Allow from all
#所有人可以访问
配置允许172.16.0.0
/16
访问,但不允许172.16.3.1访问
Order allow,deny
Deny from 172.16.3.1
#禁用一个IP访问
Allow from 172.16.0.0
/16
#允许一个网段访问
|
8.定义默认主页面
1
|
DirecotryIndex index.html index.html.var
|
9.日志设定
错误日志:
1
2
|
ErrorLog logs
/error_log
LogLevel warn debug, info, notice, warn, error, crit, alert, emerg
|
访问日志:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
CustomLog logs
/access_log
combined
LogFormat
"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
combined
%h:客户端IP地址;
%l: Remote
logname
(from identd,
if
supplied). -表示为空;
%u: Remote user, (from auth; may be bogus
if
return
status (%s) is 401);
%t:Time the request was received (standard english
format
),服务器收到请求的时间;
%r:First line of request,请求报文的道行信息(method url version);
%>s: 响应状态码;
%b: 响应报文的大小,单位是字节,不包括响应报文首部;
%{Referer}i:请求报文当中
"referer"
首部的值;当前资源的访问入口,即从哪个页面中的超链接跳转而来;
%{User-Agent}i:请求报文当中
"User-Agent"
首部的值;即发出请求用到的应用程序;
|
详情:http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats
10.路径别名
1
2
3
4
5
6
7
8
|
(1)DocumentRoot
"/www/htocs"
http:
//www
.baidu.com
/download/bash-4
.4.2-3.el6.x86_64.rpm
-->
/www/htdocs/download/bash-4
.4.2-3.el6.x86_64.rpm
(2)Alias
/URL/
"/PATH/TO/SOMEDIR/"
Alias
/bbs/
"/forum/htdocs"
http:
//www
.baidu.com
/bbs/index
.html
-->
/forum/htdocs/bbs/
|
11.设定默认字符集
1
2
|
AddDefaultCharset UTF-8
GBK, GB2312, GB18030
|
12.基于用户的访问控制
认证质询:
WWW-Authenticate:响应码为401,拒绝客户端请求,并说明要求客户提供账号和密码;
认证:
Authorization:客户端用户填入账号和密码后再次发送请求报文;认证通过,则服务器发送响应的资源;
认证类型:
basic:明文
digest:消息摘要
安全域:需要用户认证后方能访问的路径;
应该通过名称对其进行标识,并用于告知用户认证的原因;
用户的账号和密码存储于何处?
虚拟账号:仅用于访问某服务时用到的认证标识;
存储:
文本文件
SQL数据库
ldap
nis
basic认证:
(1) 定义安全域
1
2
3
4
5
6
7
8
|
<Directory
""
>
Options None
AllowOverride None
AuthType Basic
AuthName
"STRING"
AuthUserFile
"/PATH/TO/HTTPD_USER_PASSWD_FILE"
Require user username1 username2 ...
<
/Directory
>
|
允许账号文件中的所有用户登录访问:
1
|
Require valid-user
|
(2) 提供账号和密码存储(文本文件)
使用htpasswd命令进行管理
1
2
3
4
5
|
htpasswd [options] passwordfile username
-c: 自动创建passwordfile,因此,仅应该在添加第一个用户时使用;
-m: md5加密用户密码;
-s: sha1加密用户密码;
-D: 删除指定用户
|
(3) 实现基于组进行认证
1
2
3
4
5
6
7
8
9
|
<Directory
""
>
Options None
AllowOverride None
AuthType Basic
AuthName
"STRING"
AuthUserFile
"/PATH/TO/HTTPD_USER_PASSWD_FILE"
AuthGroupFile
"/PATH/TO/HTTPD_GROUP_FILE"
Require group GROUP1 GROUP2 ...
<
/Directory
>
|
要提供:用户账号文件和组文件;
1
2
3
4
5
6
7
8
9
10
11
12
|
组文件:每一行定义一个组
GRP_NAME:user1 user2 user3 ...
示例:
<Directory
"/www/htdocs/admin"
>
Options None
AllowOverride None
AuthType Basic
AuthName
"Administator private"
AuthUserFile
"/etc/httpd/conf.d/.htpasswd"
AuthGroupFile
"/etc/httpd/conf.d/.htgroup"
Require group webadmin
<
/Directory
>
|
13.虚拟主机
有三种实现方案:
基于ip:为每个虚拟主机准备至少一个ip地址;
基于port:为每个虚拟主机准备至少一个专用port;实践中很少使用;
基于hostname:为每个虚拟主机准备至少一个专用hostname;
可混合使用上述三种方式中任意方式;
注意:一般虚拟主机莫与中心主机混用,所以,要使用虚拟主机,先禁用中心主机;
禁用中心主机:注释DocumentRoot
每个虚拟主机都有专用配置:
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
|
<VirtualHost
"IP:PORT"
>
SeverName
DocumentRoot
""
<
/VirtualHost
>
ServerAlias: 虚拟主机的别名;
ErrorLog
CustomLog
<Directory
""
>
<
/Directory
>
示例1:基于ip
<VirtualHost 172.16.100.6:80>
ServerName web1.magedu.com
DocumentRoot
"/vhosts/web1/htdocs"
<
/VirtualHost
>
<VirtualHost 172.16.100.7:80>
ServerName web2.magedu.com
DocumentRoot
"/vhosts/web2/htdocs"
<
/VirtualHost
>
示例2:基于port
<VirtualHost 172.16.100.7:80>
ServerName web2.magedu.com
DocumentRoot
"/vhosts/web2/htdocs"
<
/VirtualHost
>
<VirtualHost 172.16.100.7:8080>
ServerName web3.magedu.com
DocumentRoot
"/vhosts/web3/htdocs"
<
/VirtualHost
>
示例3:基于
hostname
<VirtualHost 172.16.100.6:80>
ServerName web1.magedu.com
DocumentRoot
"/vhosts/web1/htdocs"
<
/VirtualHost
>
<VirtualHost 172.16.100.6:80>
ServerName web2.magedu.com
DocumentRoot
"/vhosts/web2/htdocs"
<
/VirtualHost
>
<VirtualHost 172.16.100.6:80>
ServerName web3.magedu.com
DocumentRoot
"/vhosts/web3/htdocs"
<
/VirtualHost
>
|
14.内置的status页面
1
2
3
4
5
6
|
<Location
/server-status
>
SetHandler server-status
Order deny,allow
Deny from all
Allow from 172.16
<
/Location
>
|
实现:基于账号实现访问控制
15.配置支持https
(1)安装httpd支持ssl模块
yum install mod_ssl -y
(2)自建CA
#cd /etc/pki/CA
# (umask 077; openssl genrsa -out private/cakey.pem 2048)
#openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
(3)生成私钥
cd /etc/httpd/conf/
mkdir ssl
cd ssl
(umask 077; openssl genrsa -out httpd.key 1024)
(4)生成证书申请
1openssl req -new -key httpd.key -out httpd.csr
(5)ca签署证书
1openssl ca -in httpd.csr -out httpd.crt -days 365
(6)修改httpd的ssl配置文件
vi /etc/httpd/conf.d/ssl.conf
DocumentRoot "/www/web1"
ServerName www.lyd.com
SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key
(7)将ca证书导入到客户端可信任证书服务器,并验证
本文转自 a8757906 51CTO博客,原文链接:http://blog.51cto.com/nxyboy/1931589