Httpd学习笔记

Httpd默认提供的安装包在CentOS 6上是 2.2版本,在CentOS 7上是 2.4版本。

CentOS 6:

 

程序环境

配置文件:

/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:错误日志

站点文档目录:

/var/www/html

模块文件路径:

/usr/lib64/httpd/modules

 

配置文件的组成:

 ~]# grep "Section" /etc/httpd/conf/httpd.conf

### Section 1: Global Environment            //全局配置,定义工作特性,定义日志(主要是错误日志)

### Section 2: 'Main' server configuration           //主服务器配置

### Section 3: Virtual Hosts        //虚拟主机配置

 

配置格式:directive value

directive: 不区分字符大小写;

value: 为路径时,取决于文件系统;

常用配置

自加:cd /etc/httpd/conf

cp httpd.conf{,.bak}       做备份

vim httpd.conf

1、修改监听的IP和Port

Listen [IP:]PORT

 

省略ip表示监听本机所有IP;Listen可重复出现多次

自加:在修改服务的配置文件后,reload即可,不用重启服务;例如,修改/etc/httpd/conf/httpd.conf,增加一条Listen 192.168.1.103:8080使用8080端口监听该地址;之后输入命令]# service httpd reload   即可;查看用]# ss -tunl  命令;同时在浏览器输入192.168.1.103:8080会出现欢迎页面


2、持久连接

Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成;

如何断开?有两种控制机制,进行断开

数量限制:默认100

时间限制:可配置

副作用:对并发访问量较大的服务器,持久连接功能会使用有些请求得不到响应;

折衷方案:使用较短的持久连接时间;

httpd-2.4 支持毫秒级持久时间;

非持久连接

 

自加:打开/etc/httpd/conf/httpd.conf

KeepAlive On|Off        如果是Off,则为短连接;如果为On则为长连接

MaxKeepAliveRequests #  最大持久连接数量,当KeepAliveOn此项有用

KeepAliveTimeout #  持久连接超时时间,默认15

 

测试:

telnet HOST PORT

GET /URL HTTP/1.1

Host: HOSTNAME or IP

自加:测试过程

~]# telnet 192.168.1.103 80

GET / HTTP/1.1

Host:192.168.1.103  此时按下两次回车,即可显示文本格式的欢迎页面

3、MPM

Multipath Process Module:多道处理模块

prefork, worker, event

 

httpd-2.2不支持同时编译多个模块,所以只能编译时选定一个;rpm安装的包提供三个二进制程序文件,分别用于实现对不同MPM机制的支持;确认方法:

# ps aux  | grep httpd

如果使用的是prefork则执行上面一条命令后,结果是/usr/sbin/httpd

如果使用的是worker则执行上面一条命令后,结果是/usr/sbin/worker

如果使用的是event则执行上面一条命令后,结果是/usr/sbin/event

默认为/usr/sbin/httpd, 其使用prefork

 

查看模块列表的方法

查看静态编译的模块

[root@www1 ~]# httpd -l

Compiled in modules:

  core.c

  prefork.c

  http_core.c

  mod_so.c

查看静态编译及动态装载的模块

# httpd -M

 

更换使用的httpd程序:

 编辑/etc/sysconfig/httpd

HTTPD=

重启服务生效;

自加:~]# vim /etc/sysconfig/httpd

启动HTTPD=/usr/sbin/httpd.worker这一项

~]# service httpd restart

使用]# ps aux | grep httpd会显示 /usr/sbin/httpd.worker

 

prefork的配置:vim /etc/httpd/conf/httpd.conf 可以看到下面内容

<IfModule prefork.c>    如果你装载的模块是prefork.c模块,则包含以下内容

StartServers       8   服务启动时启动多少个服务器进程

MinSpareServers    5   最小空闲进程数

MaxSpareServers   20   最大空闲进程数

ServerLimit      256  

MaxClients       256   定义最多256个客户端请求该服务器资源

MaxRequestsPerChild  4000

</IfModule>                                

 

worker的配置:

<IfModule worker.c>

StartServers         4      服务器启动时启动的进程数

MaxClients         300      服务器端启动的最大线程数

MinSpareThreads     25      最小空闲线程数

MaxSpareThreads     75      最大空闲线程数

ThreadsPerChild     25      每个进程所能启动的线程数

MaxRequestsPerChild  0      每个线程所能响应的最大请求数量,0表示不作限制

</IfModule>

 

PV, UV

PV: Page View

UV: User View

独立IP量;

 

300*86400=40W+

4、DSO

]# vim /etc/httpd/conf/httpd.conf 启用或停用模块,之后重载一下,service httpd reload

配置指令实现模块加载

LoadModule <mod_name> <mod_path>

 

模块路径可使用相对地址

相对于ServerRoot(/etc/httpd)指向的路径而言;

/etc/httpd/modules/

自加:/etc/httpd/modules是一个连接文件

~]# ls -l /etc/httpd/modules

lrwxrwxrwx 1 root root 29 Sep 14 17:28 /etc/httpd/modules -> ../../usr/lib64/httpd/modules

 

 

5、定义'Main' server的文档页面路径

DocumentRoot  文档根路径

 

文档路径映射:

DocumentRoot指向的路径为URL路径的起始位置;

DocumentRoot "/var/www/html"

test/index.html --> http://HOST:PORT/test/index.html   其中,/test/index.html中标红的部分,相当于/var/www/html

注意:通过getenforce 查看SELinux是否关闭

 

小实验:

1.mkdir /www/htdocs -pv

htdocs下建立index.html文件

2.~]# vim /etc/httpd/conf/httpd.conf

修改DocumentRoot "/var/www/html"DocumentRoot "/www/htdocs"

3.重载   ~]# service httpd reload

4.在浏览器中输入http://192.168.1.103:8080/  则会显示index.html中的内容

6、站点访问控制

可基于两种类型的路径指明对哪些资源进行访问控制

文件系统路径:

<Directory ""> </Direcotry>

<File ""> </File>

<FileMatch ""> </FileMatch> 正则表达式匹配,尽量不要用

URL路径:

<Location ""> </Location>

...

 

访问控制机制:基于来源地址IP(风险较大);基于账号(账号密码)

 

 

7、Directory中“基于来源地址”实现访问控制

自加:打开/etc/httpd/conf/httpd.conf

(1) Options

所有可用特性:Indexes Includes FollowSymLinksSymLinksifOwnerMatch ExecCGI MultiViews

None, All

Indexes: 索引;

FollowSymlinks:允许跟踪符号链接文件;

 

(2) 基于来源地址的访问控制机制

Order:检查次序

Order allow,deny    这是做白名单,默认是deny,只有allow的才可以访问

Order deny,allow    这是做黑名单

Allow from

Deny from

 

来源地址:

IP

NetAddr:

172.16

172.16.0.0

172.16.0.0/16

172.16.0.0/255.255.0.0

 

8、定义默认主页面

DirecotryIndex index.html index.html.var

9、日志设定

自加:打开/etc/httpd/conf/httpd.conf

错误日志:

ErrorLog logs/error_log

LogLevel warn

 

debug, info, notice, warn, error, crit, alert, emerg

 

访问日志:

CustomLog logs/access_log combined  自加:(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、路径别名

 

DocumentRoot "/www/htocs"

例如:http://www.magedu.com/download/bash-4.4.2-3.el6.x86_64.rpm

--> /www/htdocs/download/bash-4.4.2-3.el6.x86_64.rpm

 

Alias /URL/ "/PATH/TO/SOMEDIR/"        

 

Alias /bbs/ "/forum/htdocs"

http://www.magedu.com/bbs/index.html        

--> /forum/htdocs/bbs/

 

11、设定默认字符集

AddDefaultCharset UTF-8

 

GBK, GB2312, GB18030


12、基于用户的访问控制

 

认证质询:

WWW-Authenticate:响应码为401,拒绝客户端请求,并说明要求客户提供账号和密码;

 

认证:

Authorization:客户端用户填入账号和密码后再次发送请求报文;认证通过,则服务器发送响应的资源;

 

认证类型:

basic:明文(这个用的最多)

digest:消息摘要

 

安全域:需要用户认证后方能访问的路径;

应该通过名称对其进行标识,并用于告知用户认证的原因;

 

用户的账号和密码存储于何处?

虚拟账号:仅用于访问某服务时用到的认证标识;

 

存储:

文本文件

SQL数据库

ldap

Nis


basic认证:

(1) 定义安全域

<Directory "">

Options None

AllowOverride None

AuthType Basic

AuthName "STRING"

AuthUserFile "/PATH/TO/HTTPD_USER_PASSWD_FILE"

Require user username1 username2 ...

</Directory>

 

允许账号文件中的所有用户登录访问:Require valid-user

自加:我有开了一台MageCentos3  地址为192.168.1.105

       vim /etc/httpd/conf/httpd.conf之后加入如下内容:

<Directory "/www/htdocs/admin">

   Options None

   AllowOverride None

   AuthType Basic

   AuthName "Adimistator private"

   AuthUserFile "/etc/httpd/conf.d/.htpasswd"

   Require valid-user

</Directory>

重启服务   ]# service httpd restart

之后再浏览器上输入192.168.1.105/admin/会显示如图的内容

wKiom1YL5vHTCRp5AAEimnj3cn8824.jpg

                                (2) 提供账号和密码存储(文本文件)

                                        使用htpasswd命令进行管理

                                            htpasswd [options] passwordfile username    

                                                -c: 自动创建passwordfile,因此,仅应该在添加第一个用户时使用;

                                                -m: md5加密用户密码;

                                                -s: sha1加密用户密码;    

                                                -D: 删除指定用户

                        自加:添加用户tom 和密码123456

                                                        ]# htpasswd -c -m /etc/httpd/conf.d/.htpasswd tom

                                                        ]# htpasswd -m /etc/httpd/conf.d/.htpasswd jerry

                            重载一下 ]# service httpd reload

                            在浏览器里输入192.168.1.105/admin/然后在上图中输入用户名和密码即可登录

                                (3) 实现基于组进行认证

                                        <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>        

 要提供:用户账号文件和组文件;

 组文件:每一行定义一个组

    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>


自加:在增加一个用户,密码123456

]# htpasswd -m /etc/httpd/conf.d/.htpasswd obama

定义一个组文件]# vim /etc/httpd/conf.d/.htgroup 内容 webadmin:  jerry obamba

vim /etc/httpd/conf/httpd.conf之后加入如下内容:  

<Directory "/www/htdocs/admin">

Options None

AllowOverride None

AuthType Basic

AuthName "Adimistator private"

AuthUserFile "/etc/httpd/conf.d/.htpasswd"

 AuthGroupFile "/etc/httpd/conf.d/.htgroup"

Require group webadmin

</Directory>

问题:为什么obama登录不了

13、虚拟主机

 

有三种实现方案:

基于ip:为每个虚拟主机准备至少一个ip地址;

基于port:为每个虚拟主机准备至少一个专用port;实践中很少使用;

基于hostname:为每个虚拟主机准备至少一个专用hostname;

可混合使用上述三种方式中任意方式;

 

注意:一般虚拟主机莫与中心主机混用,所以,要使用虚拟主机,先禁用中心主机;

禁用中心主机:注释DocumentRoot

            每个虚拟主机都有专用配置:

<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>

 

自加:在MageCentos3上做的实验,地址192.168.1.105

  1.注释中心主机  vim /etc/httpd/conf/httpd.conf

      #DocumentRoot "/www/htdocs"

     然后 在文件最后加上如下内容

<VirtualHost 192.168.1.105:80>

     Servername web1.magedu.com

     DocumentRoot "/vhosts/web1/htdocs"

</VirtualHost>

<VirtualHost 192.168.1.106:80>

     Servername web2.magedu.com

     DocumentRoot "/vhosts/web2/htdocs"

</VirtualHost>

    2. 使用]# service httpd configtest来测试配置文件是否有语法错误,用http -t也可以

    3.创建虚拟主机文件   ]# mkdir -p /vhosts/{web1,web2,web3,web4}/htdocs

]# vim /vhosts/web1/htdocs/index.html    里面的内容随便写

]# vim /vhosts/web2/htdocs/index.html

    4.添加一个IP地址

]# ip addr add 192.168.1.106/24 dev eth0

    5.在浏览器中输入192.168.1.105显示如图

wKiom1YL6Wbw3-OAAAAsLVhfmro361.jpg

                    在浏览器中输入192.168.1.106

wKioL1YL6aPCnMo6AAArJkchvCA767.jpg


示例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>

自加:在MageCentos3上做的实验,地址192.168.1.105  接着示例1的实验

 1.vim /etc/httpd/conf/httpd.conf 在后面加入如下内容

<VirtualHost 192.168.1.106:8080>

     Servername web3.magedu.com

     DocumentRoot "/vhosts/web3/htdocs"

</VirtualHost>

      同时增加一条 Listen 8080

  2.web3提供页面   ]# vim /vhosts/web3/htdocs/index.html

 3.]# service httpd restart

 4.在浏览器输入192.168.1.106:8080 则出现如图

wKiom1YL6cWTd3cCAAA4AWRWsPg043.jpg


示例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>

自加:1.要想使用基于hostname的认证方式,则要启用/etc/httpd/conf/httpd.conf中的 NameVirtualHost *:80    (在http2.2中要这样改)

并改为NameVirtualHost 192.168.1.105:80

          2.  将前两个示例中的配置修改为

<VirtualHost 192.168.1.105:80>

     Servername web1.magedu.com

     DocumentRoot "/vhosts/web1/htdocs"

</VirtualHost>

<VirtualHost 192.168.1.105:80>

     Servername web2.magedu.com

     DocumentRoot "/vhosts/web2/htdocs"

</VirtualHost>

<VirtualHost 192.168.1.105:80>

     Servername web3.magedu.com

     DocumentRoot "/vhosts/web3/htdocs"

</VirtualHost>

    检查语法   ]# httpd -t

    重载服务   ]# service httpd reload 

3.Centos7上 地址 192.168.1.104上]# vim /etc/hosts

写入如下内容192.168.1.105 web1.magedu.com web2.magedu.com web3.magedu.com

             wKioL1YL6hbC15OsAADhDkX7id0996.jpg

加入专用日志文件:

]# vim /etc/httpd/conf/httpd.conf   红框中为加入内容

wKioL1YL6k-hGieYAAI_KegFMc4096.jpg

之后检查语法]# httpd -t  然后重载 ]# service httpd reload

Centos7上执行如下命令

wKioL1YL6p6wC0M4AADpoowGkpw454.jpg

查看产生的日志文件

wKiom1YL6r2ilf-aAAChWhJEpao281.jpg

14、内置的status页面

<Location /server-status>

SetHandler server-status

Order deny,allow

Deny from all

Allow from 172.16

</Location>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章