httpd的特性
高度模块化:核心模块+功能模块;
DSO (Dynamic share object ):动态装卸载;
多路处理模块机制:MPM( Multipath processing Modules);
Prefork: 多进程模型,每个进程响应一个请求,一个主进程,多个子进程;主进程负责生成子进程(工作进程)以处理用户的请求,也负责回收子进程,创建套接字进程,接受请求并派发给某子进程进行处理;子进程只负责处理请求;
Worker: 多进程多线程模型,每个进程生成多个线程,每个线程响应一个请求;一个主进程,多个子进程;主进程负责生成子进程(工作进程)以处理用户的请求,也负责回收子进程,创建套接字进程,接受请求并派发给某子进程进行处理;每个子进程只负责生成多个线程;每个线程负责响应用户的请求;
Event: 事件驱动模型,一个进程响应多个请求;一个主进程,多个子进程;主进程负责生成子进程(工作进程)以处理用户的请求,也负责回收子进程,创建套接字进程,接受请求并派发给某子进程进行处理;每个子进程基于事件驱动机制直接响应多个请求;
Httpd功能特性
CGI: Common Gateway Interface
虚拟主机:基于IP,基于端口,基于FQDN
反向代理
负载均衡
路径别名
丰富的用户认证机制Basic:明文
Digest:摘要支持扩展的第三方模块
httpd的安装
httpd的版本:
1.3:停止维护
2.0:
2.2:CentOS 6默认版本, event仍为测试使用模型
2.4:CentOS 7 默认版本, event可生产环境使用
有3中安装方式:rpm包安装、yum源安装、源码编译安装。
在CentOS 6中安装
使用的是yum源安装,安装完成后:
由于在CentOS 6上的2.2版本由于不支持MPM动态切换,所以主程序文件有:/usr/sbin/httpd、/usr/sbin/httpd.event、/usr/sbin/httpd.worker
配置文件:/etc/httpd/conf/httpd.conf(主配置文件)、/etc/httpd/conf.d/*.conf、/etc/sysconfig/httpd(脚本配置文件);
日志文件目录:/var/log/httpd/access.log、/var/log/httpd/error.log
站点文档:/var/www/html
服务控制和启动:service {start|stop|reload} httpd 、chkconfig httpd on|off
在CentOS 7中安装
yum源安装的是2.4版本,支持MPM动态切换,安装完成后主程序文件只有一个:/usr/sbin/httpd
配置文件:/etc/httpd/conf/httpd.conf、/etc/httpd/conf.d/*.conf
服务文件:/usr/lib/systemd/system/httpd.service
日志文件:/var/log/httpd/access.log、/var/log/httpd/error.log
站点文档:/var/www/html
服务控制和启动:systemctl enable | disable httpd.servic 和 systemctl {start | stop | restart | status } httpd.service
httpd的配置(CentOS 6)
在主配置文件/etc/httpd/conf/httpd.conf中,只要有3部分:
###Section 1:Global Environment
###Section 2:’Main’ Server configuration (默认)
###Section 3:Virtual Hosts
[root:rc1.d]# cat /etc/httpd/conf/httpd.conf
#
# This is the main Apache server configuration file. It contains the
...
# server as "/etc/httpd/logs/foo.log".
#
### Section 1: Global Environment ####################
#
# The directives in this section affect the overall operation of Apache,
# such as the number of concurrent requests it can handle or where it
# can find its configuration files.
...
Group apache
### Section 2: 'Main' server configuration ###############
#
# The directives in this section set up the values used by the 'main'
...
### Section 3: Virtual Hosts ############
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
...
配置格式
指令行:指令 Directive:value
Directive :不区分字符大小写,但是字符首位应该大写
Value :作为路径时候,是否区分字符大小写,取决于文件系统
注意:修改监听的socket,只有重启服务进程方可生效。
修改监听的IP和端口(port)
[root:rc1.d]# vim /etc/httpd/conf/httpd.conf
...
128 # Listen: Allows you to bind Apache to specific IP addresses and/or
129 # ports, in addition to the default. See also the <VirtualHost>
130 # directive.
131 #
132 # Change this to Listen on specific IP addresses as shown below to
133 # prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
134 #
135 #Listen 12.34.56.78:80
136 Listen 80
...
格式为:Listen IP:PORT,如上面的配置中,没有指定IP,就是默认的0.0.0.0:80,就是httpd服务器上面的所有的ip的80端口。也可以指定不同的IP和不同的PORT,Listen语句可以出现多次:
135 #Listen 12.34.56.78:80
136 Listen 80
137 Listen 172.16.253.184:8080
修改完配置文件后,保存。并且重启服务。
持久连接
...
72 #
73 # KeepAlive: Whether or not to allow persistent connections (more than
74 # one request per connection). Set to "Off" to deactivate.
75 #
76 KeepAlive Off
77
78 #
79 # MaxKeepAliveRequests: The maximum number of requests to allow
80 # during a persistent connection. Set to 0 to allow an unlimited amount.
81 # We recommend you leave this number high, for maximum performance.
82 #
83 MaxKeepAliveRequests 100
84
85 #
86 # KeepAliveTimeout: Number of seconds to wait for the next request from the
87 # same client on the same connection.
88 #
89 KeepAliveTimeout 15
...
如上,KeepAlive Off | On来控制开始或者关闭KeepAlive功能;KeepAliveTimeout 15持久连接的持续时间为:15秒;MaxKeepAliveRequests 100最大的持久连接为100。在访问并发数量较大的服务器上,通过通过调节持久连接的持续时间和最大连接数来达到最好的访问效果。
MPM
httpd2.2不支持同时编译多个MPM模块,只能编译选定要使用的模块。在CentOS 6提供的rpm有3个应用程序:httpd(prefork)、httpd.worker、httpd.event。默认的使用的是prefork模块。要想查看已转载的模块,使用:
[root:rc1.d]# httpd -l
Compiled in modules:
core.c
prefork.c ###使用是prefork模块
http_core.c
mod_so.c
查看动态装载模块:
[root:rc1.d]# 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)
...
version_module (shared)
dnssd_module (shared)
Syntax OK
要想更换使用的模块,打开脚本配置文件/etc/sysconfig/httpd:
1 # Configuration file for the httpd service.
2
3 #
4 # The default processing model (MPM) is the process-based
5 # 'prefork' model. A thread-based model, 'worker', is also
6 # available, but does not work with some modules (such as PHP).
7 # The service must be stopped before changing this variable.
8 #
9 #HTTPD=/usr/sbin/httpd.worker
10
11 #
12 # To pass additional options (for instance, -D definitions) to the
13 # httpd binary at startup, set OPTIONS here.
14 #
15 #OPTIONS=
16
上图的第九行,修改为HTTPD=/usr/sbin/httpd.worker或者HTTPD=/usr/sbin/httpd.event,然后重启服务即可。
注意:对Linux而言,线程是跟进程一样的量级,多线程模型所能带来的性能的提升是有效的。 Worker和prefork的性能并没有太大的改变。
使用prefork模块的配置:
# prefork MPM
96 # StartServers: number of server processes to start
97 # MinSpareServers: minimum number of server processes which are kept spare
98 # MaxSpareServers: maximum number of server processes which are kept spare
99 # ServerLimit: maximum value for MaxClients for the lifetime of the server
100 # MaxClients: maximum number of server processes allowed to start
101 # MaxRequestsPerChild: maximum number of requests a server process serves
102 <IfModule prefork.c> ##如是转载的是prefork.c模块
103 StartServers 8 ##服务器进程启动时候启动多少空闲进程
104 MinSpareServers 5 ##最少空闲进程数,如果少于5,主控进程再启动几个达到5
105 MaxSpareServers 20 ##最大空闲进程数,如果大于20主控进程对子进程进行回收
106 ServerLimit 256 ##所允许启动的最大在线进程数量
107 MaxClients 256 ##最大允许启动进程数量响应用户请求(最大并发响应数)
108 MaxRequestsPerChild 4000 ##4000 每个进程最多可以处理多少个请求,超过4000销毁
109 </IfModule>
使用worker模块的配置:
111 # worker MPM
112 # StartServers: initial number of server processes to start
113 # MaxClients: maximum number of simultaneous client connections
114 # MinSpareThreads: minimum number of worker threads which are kept spare
115 # MaxSpareThreads: maximum number of worker threads which are kept spare
116 # ThreadsPerChild: constant number of worker threads in each server process
117 # MaxRequestsPerChild: maximum number of requests a server process serves
118 <IfModule worker.c>
119 StartServers 4 ##服务器进程启动时候启动多少空闲进程
120 MaxClients 300 ##最大并发响应数
121 MinSpareThreads 25 ##最小空闲线程数
122 MaxSpareThreads 75 ##最大空闲线程数
123 ThreadsPerChild 25 ##每个进程启动25个线程
124 MaxRequestsPerChild 0 ##每个进程可以处理无限个请求
125 </IfModule>
注意:Httpd在Worer 模式下先启动4个子进程,然后再销毁一个,通过watch可以观察:
Watch -n.5 ‘ ps aux | grep httpd’
DSO(Dynamic Shared Object)
动态装载模块的实现:LoadModules < mod_name >< mod_path >,路径使用的相对路径,相对于ServerRoot(默认为/etc/httpd)。
在/etc/httpd/conf/http.conf中:
139 # Dynamic Shared Object (DSO) Support
140 #
141 # To be able to use the functionality of a module which was built as a DSO you
142 # have to place corresponding `LoadModule' lines at this location so the
143 # directives contained in it are actually available _before_ they are used.
144 # Statically compiled modules (those listed by `httpd -l') do not need
145 # to be loaded here.
146 #
147 # Example:
148 # LoadModule foo_module modules/mod_foo.so
149 #
150 LoadModule auth_basic_module modules/mod_auth_basic.so
151 LoadModule auth_digest_module modules/mod_auth_digest.so
152 LoadModule authn_file_module modules/mod_authn_file.so
153 LoadModule authn_alias_module modules/mod_authn_alias.so
154 LoadModule authn_anon_module modules/mod_authn_anon.so
155 LoadModule authn_dbm_module modules/mod_authn_dbm.so
156 LoadModule authn_default_module modules/mod_authn_default.so
157 LoadModule authz_host_module modules/mod_authz_host.so
...
main server的文档页面路径
在/etc/httpd/conf/httpd.conf文件中,定义页面路径语句为:
# DocumentRoot: The directory out of which you will serve your
289 # documents. By default, all requests are taken from this directory, but
290 # symbolic links and aliases may be used to point to other locations.
291 #
292 DocumentRoot "/var/www/html" ##默认的路径,安装完成后就存在的路径
在使用yum源安装完成后,启动httpd服务,大概浏览器,输入本机ip,有一个欢迎页面,该也页面路径如下:/etc/httpd/conf.d/welcome.conf,删除掉或者改名后,就没有欢迎页面了。
DocumentRoot “/var/www/html”后面的路径可以更改为你自己想要的路径,更改完成后,重启服务。
[root:modules]# vim /etc/httpd/conf/httpd.conf
...
290 # symbolic links and aliases may be used to point to other locations.
291 #
292 DocumentRoot "/www/html" ###更改路径为/www/html
293
...
[root:~]# mkdir -p /www/html ##创建目录
[root:~]# echo "hello china" > /www/html/index.html ##创建文件并输入数据
[root:~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
通过links软件查看:
[root:~]# links 127.0.0.1
http://127.0.0.1/
hello china
本主机上有两张网卡,也可以通过指定监听某一个ip上:
[root:~]# ip a
...
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:bb:1c:30 brd ff:ff:ff:ff:ff:ff
inet 172.16.253.184/16 brd 172.16.255.255 scope global eth1
...
[root:~]# vim /etc/httpd/conf/httpd.conf
...
135 #Listen 12.34.56.78:80
136 #Listen 80
137 Listen 172.16.253.184:8080
138 #
...
[root:~]# !serv
service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
通过上面的修改,只监听172.16.253.184:8080,其他的ip不监听。因此只有它才能访问。
[root:~]# links 172.16.253.184:8080
http://172.16.253.184:8080/
hello china
###而用其他的本机ip则访问不了。
站点访问控制
对于站点访问的控制,可基于两种机制,指明对那些资源进行何种访问:
1、文件系统路径;
2、URL路径;
文件系统路径:
<Directory "">
...
</Directory>
或者
<File "">
...
</File>
或者
<FileMatch "PATTERN">
...
</FileMatch>
URL路径:
<Location "">
...
</Location>
或者
</LocationMatch "">
...
</LocationMatch>
下面就是一个基于文件系统路径的站点访问控制的实例:
316 #
317 <Directory "/var/www/html">
318
319 #
320 # Possible values for the Options directive are "None", "All",
321 # or any combination of:
322 # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
323 #
324 # Note that "MultiViews" must be named *explicitly* --- "Options All"
325 # doesn't give it to you.
326 #
327 # The Options directive is both complicated and important. Please see
328 # http://httpd.apache.org/docs/2.2/mod/core.html#options
329 # for more information.
330 #
331 Options Indexes FollowSymLinks
332
333 #
334 # AllowOverride controls what directives may be placed in .htaccess files.
335 # It can be "All", "None", or any combination of the keywords:
336 # Options FileInfo AuthConfig Limit
337 #
338 AllowOverride None
339
340 #
341 # Controls who can get stuff from this server.
342 #
343 Order allow,deny
344 Allow from all
345
346 </Directory>
Options定义此目录内资源访问的特性,控制被定义的目录下所指定的各种资源的被访问模式(方式),后跟一个或多个以空白字符分割的选项列表:
Indexes:指明的URL路径不存在与定义的主页面资源相符的资源文件时候,返回索引列表给用户,目录浏览功能,一般关掉:
[root:~]# cd /var/www/html/
[root:html]# ls
[root:html]# echo “hello world! ” > index1.html
[root:html]# echo “hello mage! ” > index2.html
[root:html]# ls
index1.html index2.html
[root:html]# mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak
[root:html]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
[root:html]# links 127.0.0.1[ICO] Name Last modified Size Description
════════════════════════════════════════════════════
[TXT] index1.html 11-Dec-2016 15:29 14
[TXT] index2.html 11-Dec-2016 15:29 13
════════════════════════════════════════════════════
Apache/2.2.15 (CentOS) Server at 127.0.0.1 Port 80
Indexes将根路径下的所有页面都列出来了,则是一件较危险的事情,所以一般关掉。关掉后:
330 #
331 Options FollowSymLinks
332
显示为:
[root:html]# links 127.0.0.1
403 Forbidden
ForbiddenYou don’t have permission to access / on this server.
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Apache/2.2.15 (CentOS) Server at 127.0.0.1 Port 80
FollowSymLinks 允许跟踪符号链接文件所指向的源文件跟踪符号链接,如果目录下存在软连接,则可以直接访问软连接指向的文件;
[root:html]# cp -s /etc/issue index.html
[root:html]# ls
index1.html index2.html index.html
[root:html]# ll
total 8
-rw-r--r-- 1 root root 14 Dec 11 15:29 index1.html
-rw-r--r-- 1 root root 13 Dec 11 15:29 index2.html
lrwxrwxrwx 1 root root 10 Dec 11 15:42 index.html -> /etc/issue
[root:html]# !ser
service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
[root:html]# links 127.0.0.1
http://127.0.0.1/
CentOS release 6.7 (Final) Kernel \r on an \m Mage Education Learning Services http://www.magedu.com
后面显示的内容为issue文件的内容。
Includes: 允许启用服务端包含
SymLinksifOwnerMatch: 源文件和链接的目标文件的属主属组相同的时候才允许跟踪访问
ExecCGI: 允许执行CGI脚本
MultiViews: 允许执行内容协商
None:全部都不
ALL: 所有都
337 #
338 AllowOverride None
339
AllowOverride:与访问控制相关的哪些执行可以放在.htacesss文件中,每个目录下都可以有一个 ,但是使用.htacess会使得网站对目录下的资源解析变的非常慢,不推荐使用。 ALL所有都可以放进去, None什么都不可以放。
342 #
343 Order allow,deny
344 Allow from all
345
Order 定义生效法则,写在后面表示默认法则;
Allow from all,Deny from 来源地址IP或者网络地址:
网络地址格式:
172.16
172.16.0.0
172.16.0.0/16
172.16.0.0/255.255.0.0
举例:
Order allow,deny
Allow from 172.16 ##仅允许172.16网段的地址访问
Order allow,deny
Deny from 172.16.12.1
Allow from 172.16 ##仅允许172.16网段除172.16.12.1的地址访
定义站点主页面
# The index.html.var file (a type-map) is used to deliver content-
399 # negotiated documents. The MultiViews Option can be used for the
400 # same purpose, but it is much slower.
401 #
402 DirectoryIndex index.html index.html.var 从左到右优先
当请求的资源不存在的时候 http服务器会有两种处理结果:
1 返回404或者403
2 将当前网页目录下内容全部列出来
定义路径别名
定义路径别名的方式为:Alias /URL/ “/PATH/TO/SOMEDIR”
在/etc/httpd/conf/httpd.conf中定义了DocumentRoot /var/www/html,访问的URL:http://127.0.0.1/roger/index.html即使访问的是/var/www/html/下面的roger/index.html。
[root:html]# links 127.0.0.1/roger/index.html
http://127.0.0.1/roger/index.html
hello world!
现在在/etc/httpd/conf/httpd.conf中加入一行:
293 Alias /roger/ "/www/html/"
并编辑文件:
[root:html]# cat /www/html/index.html
hello china
重启服务并查看:
[root:html]# links 127.0.0.1/roger/index.html
http://127.0.0.1/roger/index.html
hello china
通过上述的实验表明,别名为:
当你定义别名为 Alias /roger/ “/www/html/” 时,当你访问http://127.0.0.1/roger/index.html时,不再是访问的是定义的DocumentRoot “/var/www/html”下面的roger/index.html ,而是访问的是/www/html/index.html 。
设定默认字符集
757 # directive:
758 #
759 AddDefaultCharset UTF-8
760
常用的中文字符集有:UTF-8 、GBK、GB2312、GB18030。
日志
错误日志:
478 # ErrorLog: The location of the error log file.
479 # If you do not specify an ErrorLog directive within a <VirtualHost>
480 # container, error messages relating to that virtual host will be
481 # logged here. If you *do* define an error logfile for a <VirtualHost>
482 # container, that host's errors will be logged there and not here.
483 #
484 ErrorLog logs/error_log
485
486 #
487 # LogLevel: Control the number of messages logged to the error_log.
488 # Possible values include: debug, info, notice, warn, error, crit,
489 # alert, emerg.
490 #
491 LogLevel warn
492
493 #
494 # The following directives define some format nicknames for use with
495 # a CustomLog directive (see below).
496 #
497 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
498 LogFormat "%h %l %u %t \"%r\" %>s %b" common
499 LogFormat "%{Referer}i -> %U" referer
500 LogFormat "%{User-agent}i" agent
ErrorLog logs/error_log 定义日志,相对于/etc/httpd目录,即/etc/httpd/logs/error_log 。
[root:html]# cd /etc/httpd/
[root:httpd]# ll
...
lrwxrwxrwx 1 root root 19 Dec 9 10:50 logs -> ../../var/log/httpd
...
490 #
491 LogLevel warn
492
定义的错误日志级别为:warn
错误日志有如下的级别:
debug, 所有信息,只要产生就全部记录下来
info, 所有的信息数据
notice, 引起注意
warn, 警告
error, 发生错误
crit, 级别很严重
alert, 红色警戒
emerg. 引起恐慌
访问日志
525 #
526 CustomLog logs/access_log combined
527
格式 日志路径 日志格式( combined | common | referer|agent )
日志格式:
496 #
497 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
498 LogFormat "%h %l %u %t \"%r\" %>s %b" common
499 LogFormat "%{Referer}i -> %U" referer
500 LogFormat "%{User-agent}i" agent
%h:客户端ip;
%l:远程用户名,通常为“-”;
%u:远程用户,为非登录访问是为“-”;
%t:服务器收到请求时的时间;
%r:请求报文的首行,记录了此次请求的方法、URL以及协议版本;
%s:状态信息;
%b:响应报文大小,不包括响应报文的http首部;
%{Referer}i:请求报文中的Referer的值,即从哪个页面的超链接跳转到本页面的;
%{User-agent}i:请求报文中首部的User-agent值,即发出请求的应用程序;
实例:
[root:httpd]# cat /etc/httpd/logs/access_log
172.16.253.113 - - [09/Dec/2016:10:55:54 +0800] "GET /?release=6&arch=x86_64&repo=os&infra=stock HTTP/1.1" 403 4961 "-" "urlgrabber/3.9.1 yum/3.2.29"
...
127.0.0.1 - - [11/Dec/2016:16:21:25 +0800] "GET /roger/index.html HTTP/1.1" 200 14 "-" "ELinks/0.12pre5 (textmode; Linux; 169x34-2)"
127.0.0.1 - - [11/Dec/2016:16:29:15 +0800] "GET /roger/index.html HTTP/1.1" 200 12 "-" "ELinks/0.12pre5 (textmode; Linux; 169x34-2)"
基于用户的访问控制
htppd协议自带的认证方式为:
basic :明文认证
表单认证:digest消息摘要认证
认证质询
www-authenticate:响应码为401, 拒绝客户端请求,并说明要求客户端提供账号密码;
Authorization:客户端用户填入账号密码后再次发送请求报文,认证通过时,则服务器发送响应的资源;
安全域:需要用户认证后方能访问的路径。 应该通过名称对其进行标示,以便于告知用户认证的原因;
用户账号密码存放位置虚拟账号: 仅用于访问某服务时候用到的认证标识
存储:文件文件、 SQL数据库、 LDAP 轻量级目录访问协议
Basic认证的步骤:
1、定义安全域:
<Directory “”>
Options None
AllowOverride None
AuthType Basic
AuthName “String”
AuthUserFile “/PATH/TO/HTTPD_USER_PASSWD_FILE”
# AuthgroupFile “/PATH/TO/HTTPD_GROUP_FILE” #基于组来认证
Require user username1 username2...
# Require group grpname1 grpname2 ...
</Directory >
2、创建账号密码
htpasswd -c -m /etc/httpd/conf/.htpasswd roger
-c 第一次创建密码文件的时候使用
-m MD5认证
htpasswd选项
-n Don't update file; display results on stdout.
-m Force MD5 encryption of the password.
-d Force CRYPT encryption of the password (default).
CRYPT加密
-p Do not encrypt the password (plaintext).
-s Force SHA encryption of the password.
-b Use the password from the command line rather than
prompting for it.
-D Delete the specified user. 删除一个用户
实例:
在/etc/httpd/conf/httpd.conf中新建一个安全域:
296 <Directory "/www/html/">
297 Options None
298 AllowOverride None
299 AuthType Basic
300 AuthName "Please enter roger's passwd."
301 AuthUserFile "/etc/httpd/conf/.htpasswd"
302 Require user roger
303 #Require user vaild-user //允许所有用户登录
304 </Directory>
创建用户:
[root:httpd]# htpasswd -c -m /etc/httpd/conf/.htpasswd roger
New password:
Re-type new password:
Adding password for user tom
[root:conf]# links 127.0.0.1/roger/index.html
输入账号密码刷新页面:
如果要使用用户文件中的所用用户登录,这编辑如下:
296 <Directory "/www/html/">
297 Options None
298 AllowOverride None
299 AuthType Basic
300 AuthName "Please enter roger's passwd."
301 AuthUserFile "/etc/httpd/conf/.htpasswd"
302 Require valid-user ###这就话定义了用户文件中的所有用户可以登录
303 #AuthGroupFile "/etc/httpd/conf/.htgroup"
304 #Require group mygrp mygrp1
305 </Directory>
基于用户组的认证:
编辑文件如下:
295
296 <Directory "/www/html/">
297 Options None
298 AllowOverride None
299 AuthType Basic
300 AuthName "Please enter roger's passwd."
301 AuthUserFile "/etc/httpd/conf/.htpasswd" ##
302 AuthGroupFile "/etc/httpd/conf/.htgroup" ##这三行都必须要有
303 Require group mygrp mygrp1 ##指定组
304 </Directory>
编辑.htgroup文件:
1 mygrp:wang wang1 wang2 wang3 wang4
2 mygrp1:wang5 wang6 wang7
将.htgroup中的用户使用命令 htpasswd -m /etc/httpd/conf/.htpasswd添加到用户文件中。
然后重启服务。就可以使用组里面的用户访问页面了。
虚拟主机
在设置虚拟主机之前,需要将DocumentRoot语句注释掉,以禁用main主机。
虚拟主机的实现方式
基于IP 为每个虚拟主机至少准备一个IP地址
基于port 为每个虚拟主机至少一个独立的port
基于FQDN 每个虚拟主机至少一个FQDN
配置语法:
<VirtualHost IP:PORT>
ServerAlias 虚拟主机的别名,可多次使用
Errorlog 专用的错误日志
CustomLog 专用的自定义日志
<Directory “”>… </Directory> 对资源做访问控制
ServerName FQDN
DocumentRoot “” //站点路径
</VirtualHost>
基于IP 为每个虚拟主机至少准备一个IP地址
禁用Main主机:
292 #DocumentRoot "/var/www/html"
可以在/etc/httpd/conf/httpd.conf中直接编辑,也可以在conf.d中创建一个文件。
[root:conf.d]# vim vhost.conf
1 <VirtualHost 172.16.100.101:80>
2 DocumentRoot /www/html
3 ServerName www.roger.com
4 ErrorLog logs/www.roger.com-error_log
5 CustomLog logs/www.roger.com-access_log common
6 </VirtualHost>
7
8
9 <VirtualHost 172.16.253.184:80>
10 DocumentRoot /www/html1
11 ServerName www.roger1.com
12 ErrorLog logs/www.roger1.com-error_log
13 CustomLog logs/www.roger1.com-access_log common
14 </VirtualHost>
[root:www]# cd /www/
[root:www]# mkdir html1
[root:www]# echo "172.16.100.101" > html/index.html
[root:www]# echo "172.16.253.184" > html1/index.html
配置完成后,重启服务,即可在浏览器上面浏览。
基于port 为每个虚拟主机至少一个独立的port
编辑vhost.conf文件:
1 Listen 8000 ##添加需要监听的其他端口
2 <VirtualHost 172.16.100.101:80>
3 DocumentRoot /www/html
4 ServerName www.roger.com
5 ErrorLog logs/www.roger.com-error_log
6 CustomLog logs/www.roger.com-access_log common
7 </VirtualHost>
8
9
10 <VirtualHost 172.16.100.101:8000>
11 DocumentRoot /www/html1
12 ServerName www.roger1.com
13 ErrorLog logs/www.roger1.com-error_log
14 CustomLog logs/www.roger1.com-access_log common
15 </VirtualHost>
编辑完成重启服务,即可查看:
[root:www]# links 172.16.100.101:8000
http://172.16.100.101:8000/
172.16.253.184
[root:www]# links 172.16.100.101
http://172.16.100.101/
172.16.100.101
基于FQDN 每个虚拟主机至少一个FQDN
编辑vhost.conf如下:
1 NameVirtualHost 172.16.100.101 ###
2 NameVirtualHost 172.16.253.184 ###这两行在2.2版本中必须加,在2.4版本中就不用加入
3 <VirtualHost 172.16.100.101:80>
4 DocumentRoot /www/html
5 ServerName www.roger.com
6 ErrorLog logs/www.roger.com-error_log
7 CustomLog logs/www.roger.com-access_log common
8 </VirtualHost>
9
10
11 <VirtualHost 172.16.253.184:80>
12 DocumentRoot /www/html1
13 ServerName www.roger1.com
14 ErrorLog logs/www.roger1.com-error_log
15 CustomLog logs/www.roger1.com-access_log common
16 </VirtualHost>
然后添加hosts记录:
1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
4 172.16.100.101 www.roger.com
5 172.16.253.184 www.roger1.com
重启服务后:
[root:conf.d]# links www.roger.com
http://www.roger.com/
172.16.100.101
[root:conf.d]# links www.roger1.com
http://www.roger1.com/
172.16.253.184
httpd状态页面
要启用状态页面。必须加载状态页面模块
178 LoadModule status_module modules/mod_status.so
开启状态页面:
933 #
934 <Location /server-status>
935 SetHandler server-status
936 Order deny,allow
937 Deny from all
938 Allow from 172.16.0.0/16
939 </Location>
设置允许访问状态页面的ip段或其他的主机等。
设置完重启服务:
[root:conf.d]# links www.roger.com/server-status
[root:conf.d]# hostname roger ##设置主机名
Apache
Status (1/2)
Apache Server Status for www.roger.com
Server Version: Apache/2.2.15 (Unix) DAV/2
Server Built: Nov 18 2016 23:48:55
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Current Time: Sunday, 11-Dec-2016 20:08:43 CST
Restart Time: Sunday, 11-Dec-2016 20:06:32 CST
Parent Server Generation: 0
Server uptime: 2 minutes 11 seconds
1 requests currently being processed, 7 idle workers
W_______........................................................
................................................................
................................................................
................................................................
Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
"C" Closing connection, "L" Logging, "G" Gracefully finishing,
"I" Idle cleanup of worker, "." Open slot with no current process
PID Key:
7280 in state: W , 7281 in state: _ , 7282 in state: _
OK [------]
上图解释:
W_______........................................................
................................................................
................................................................
................................................................
W正处在工作状态,发送响应报文进程
–等待连接请求
.代表还可以开启的进程,该主机最多256个进程,由下面的设置决定:
102 <IfModule prefork.c>
103 StartServers 8
104 MinSpareServers 5
105 MaxSpareServers 20
106 ServerLimit 256
107 MaxClients 256
108 MaxRequestsPerChild 4000
109 </IfModule>
S 正在创建
R 正在读取请求
K 正在保持连接中
D 正在执行DNS查询
C 正在关闭连接
L 正在记录日志
G 正在优雅关闭
I 空闲清理
注意:状态页面通常只对个别对象开放,一般都关闭(注释掉)
使用压缩模块mod_daflate优化速度
httpd2.2
在主配置文件httpd.conf中加入以下设置:
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/xml
DeflateCompressionLevel 5
BrowserMatch ^Mozilla/2 no-gzip
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
然后重启httpd服务,使用谷歌或者火狐浏览器访问网页,大概调试窗口:
响应的编码是gzip格式。
httpd2.4
在httpd2.4上 面要使用压缩页面的方式优化速度,首先要启用响应的模块:
103 LoadModule deflate_module modules/mod_deflate.so
启用模块后,先重启服务,再在http.conf中添加一段设置:
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/xml
DeflateCompressionLevel 5
BrowserMatch ^Mozilla/2 no-gzip
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
重启服务,在访问网页,查看调试窗口的Response HEADERS的Content-Encoding是否为压缩格式。
原创文章,作者:王更生,如若转载,请注明出处:http://www.178linux.com/64407