一、httpd的基本描叙和基本理论:

1、httpd:hypertext transport protocol(超文本传输协议),遵循html格式所研发的文本。它属于应用层协议,应用层协议都需要建立在通信子网的传输功能的基础上予以实现数据通信,真正通信的是进程,不是主机,而每个进程在内核中注册个套接字,并监听在这个套接字上。而httpd是工作在C/S架构上的,httpd服务器的httpd进程在启动时被动监听某个套接字,而内核把httpd客户端主动发来的请求转给httpd服务进程,httpd服务进程响应此请求。

2、http协议是一种stateless(无状态)协议,一次HTTP事务(一次请求加响应)结束后,由于服务器端无从得知2次请求,是否来此同一个客户端,故连接即行断开。 要解决这种无状态协议就得使用加速方式:
               并行请求:客户端的浏览器支持多线程并行请求多个;
               持久连接:如果请求的众多资源来自同一个服务器,3次TCP连接后不断开,发生连续的多个的请求响应,最后才进行4次TCP断开。
  说明:客户端与服务器不可能一直保持连接,需要有断开的方式,即超时和限制最大资源请求数。(如限制10秒内只能请求100个资源:如果10秒钟只请求了3个,但超过10秒了,则断开; 如果2秒钟就请求了100个资源,超过了100个,但时间没超,也得断开)

3、CGI: Common Gateway Interface,即通用网关协议。web服务器能够跟某一种应用程序执行环境通信,并且web服务器能够从这种应用程序获取执行结果的协议,这种方式叫CGI。

二、源码安装httpd-2.4.9并做好基本配置:

1、httpd-2.4新特性:
           1) MPM支持在运行时装载(可在主配置文件手动加载loadmodules或卸载模块);
               --enable-mpms-shared=all(启用基于共享的所用MPM模块)
               --with-mpm=event  (设定默认启用的MPM模块)
           2) 支持event;
           3) 异步读写;
           4) 在每模块及每目录上指定日志级别;
           5) 每请求配置:<If>, <Elseif>;
           6) 增强版的表达式分析器;
           7) 毫秒级的keepalive timeout;
           8) 基于FQDN的虚拟主机不再需要NameVirtualHost指令(不用手动启用它);
           9) 支持使用自定义变量。

2、准备环境:

安装开发包组,服务器包组,和解决依赖关系:

[root@www ~]# yum -y groupinstall "development tools" "server platform development"
[root@www ~]# yum -y install pcre-devel

说明:pcre-devel为其开发分支包,pcre能够让apache支持正则表达式,pcre即perl-compatible regular expression library(perl兼容的正则表达式库),支持cgi的机制。

3、编译安装apr-1.5.0:

[root@www ~]# tar xf apr-1.5.0.tar.bz2
[root@www ~]# cd apr-1.5.0
[root@www apr-1.5.0]# ./configure --prefix=/usr/local/apr
[root@www apr-1.5.0]# make && make install


说明:httpd依赖于更高版本的apr和apr-util。apr全称为apache portable runtime,而apr是一个让apache能够跨平台的工具。

4、编译安装apr-util-1.5.3:

[root@www ~]# tar xf apr-util-1.5.3.tar.bz2
[root@www ~]# cd apr-util-1.5.3
[root@www apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@www apr-util-1.5.3]# make && make install

5、、解压http-2.4.9的源码包并进入解压后的httpd源码目录:

1
2
[root@www ~] # tar xf httpd-2.4.9.tar.bz2
[root@www ~] # cd httpd-2.4.9

6、执行配置脚本时,

 --prefix=/usr/local/apache:指定apache的安装路径;

 --sysconfdir=/etc/httpd24:指定apache配置文件的路径;

 --enable-so:支持基于DSO的模式动态加载模块;

 --enable-ssl:支持安全的套接字层协议;

 --enable-cgi:支持通用网关借口模式;

 --enable-rewrite:支持URL重写;

 --with-zlib:支持在网络发送数据包时的通用压缩库的一种API,比如说使用gzip对数据进行压缩后再发送,就得使用zlib来支持各种压缩工具;  

 --with-pcre:支持使用perl的cgi;

 --with-apr=/usr/local/apr:指定apr的位置;

 --with-apr-util=/usr/local/apr-util:指定apr-util的位置;

 --enable-modules=most:启用大多数的常用模块;

 --enable-mpm-shared=all:编译所有的mpm模块;

 --with-mpm=event:默认使用event模块:

1
[root@www httpd-2.4.9] #  ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24  --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib  --with-pcre --with-apr=/usr/local/apr  --with-apr-util=/usr/local/apr-util  --enable-modules=most --enable-mpm-shared=all --with-mpm=event

7、编译并且安装:

[root@www httpd-2.4.9]# make && make install

8、导出http的头文件:

1
[root@www ~] # ln -sv /usr/local/apache/include /usr/include/httpd

9、导出httpd的帮助手册:

1
2
[root@www ~] # vim /etc/man.config
MANPATH /usr/local/apache/man

10、输出http的二进制程序,把apache的二进制程序路径放入PATH:

1
2
[root@www ~] # vim /etc/profile.d/httpd.sh
export PATH= /usr/local/apache/bin :$PATH

11、使配置文件立即生效:

1
[root@www ~] # source /etc/profile.d/httpd.sh

12、显示当前apache服务器的版本等信息:

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
[root@www ~] # httpd -V
Server version: Apache /2 .4.9 (Unix)
Server built:   Mar 23 2014 13:50:01
Server's Module Magic Number: 20120211:31
Server loaded:  APR 1.5.0, APR-UTIL 1.5.3
Compiled using: APR 1.5.0, APR-UTIL 1.5.3
Architecture:   64-bit
Server MPM:     event
threaded:     yes (fixed thread count)
forked:     yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT= "/usr/local/apache"
-D SUEXEC_BIN= "/usr/local/apache/bin/suexec"
-D DEFAULT_PIDLOG= "logs/httpd.pid"
-D DEFAULT_SCOREBOARD= "logs/apache_runtime_status"
-D DEFAULT_ERRORLOG= "logs/error_log"
-D AP_TYPES_CONFIG_FILE= "/etc/httpd24/mime.types"
-D SERVER_CONFIG_FILE= "/etc/httpd24/httpd.conf"

13、在主配置文件中添加httpd的pid文件路径:

1
2
[root@www httpd24] # vim /etc/httpd24/httpd.conf
PidFile "/var/run/httpd24.pid"

14、把原有的httpd脚本文件复制生成httpd24脚本文件,并修改如下路径:

apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd24.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}

1
2
3
4
5
6
7
8
[root@www httpd24] # cd /etc/rc.d/init.d/
[root@www init.d] # cp httpd httpd24
[root@www init.d] # vim httpd24
apachectl= /usr/local/apache/bin/apachectl
httpd=${HTTPD- /usr/local/apache/bin/httpd }
prog=httpd
pidfile=${PIDFILE- /var/run/httpd24 .pid}
lockfile=${LOCKFILE- /var/lock/subsys/httpd24 }

15、而后为此脚本赋予执行权限:

1
[root@www init.d] # chmod +x httpd24

16、把http24加入到服务中去,然后查看httpd24服务是否在2345级别默认开机启动,最后让httpd24在2345级别开机默认启动:

1
2
3
4
root@www ~] # chkconfig --add httpd24
[root@www ~] # chkconfig  --list httpd24
httpd24         0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@www ~] # chkconfig httpd24 on

17、启动httpd24服务:

[root@www ~]# service httpd24 start
Starting httpd:                                            [  OK  ]

三、基于虚拟用户和组的访问控制:

1、虚拟用户账号密码的存放位置:SQL数据库,dbm,ldap(轻量级目录服务访问协议)。

2、虚拟用户账号密码的认证类型(auth):basic: 基本认证,帐号和密码明文发送;digest:摘要认证,hash编程之后发送。

3、认证提供者(authentication provider):帐号和密码的存放位置,可以是文件,也可以是SQL数据库或dbm或ldap或alias。

4、授权机制(authorization):根据什么进行授权,如定义用户或组来访问。

5、基于虚拟用户和组的访问机制来限制用户访问网页文件:

   (1)使用htpasswd命令生成认证库文件.htpasswd:


[root@www httpd24]# htpasswd -c -m /etc/httpd24/.htpasswd tom
New password:
Re-type new password:
Adding password for user tom
[root@www httpd24]# htpasswd  -m /etc/httpd24/.htpasswd jim
New password:
Re-type new password:
Adding password for user jim

   说明:htpasswd:
               -c: 创建文件,创建第一个用户时使用。
               -m: 密码基于MD5编码存储。

        在认证库文件.htpasswd文件中创建2个虚拟用户tom和jim。

   (2)编辑主配置文件:


[root@www httpd24]# pwd
/etc/httpd24
[root@www httpd24]# vim httpd.conf

wKioL1NCl2yADCZoAADU9GEyT-c545.jpg

   说明:<Directory "/usr/local/apache/htdocs/ug">  -- 基于网页根目录下的ug目录来做用户名密码的认证
               Options None  -- 没有任何选项
               AllowOverride AuthConfig   -- 覆盖基于客户端IP的访问,而使用基于认证配置的访问(需客户端提供用户名和密码才能访问)
               AuthType Basic    -- 认证类型为基本认证
               AuthName "Private Area"  -- 认证名称为“Private Area”,是对话框中的的提示信息
               AuthUserFile /etc/httpd24/.htpasswd  -- .htpasswd文件中放着用户的用户名和密码
               Require valid-user    -- .htpasswd文件中的所有合法用户都能访问

     </Directory>

   (3)检查配置文件语法是否正确,然后重启服务:


[root@www httpd24]# httpd -t
Syntax OK
[root@www httpd24]# service httpd24 restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

   (4)验证访问:


wKiom1NCmdXQNQW9AACosxevFlw634.jpg

wKioL1NCma3APMTOAADaS7-nHh4902.jpg

   (5)如果要基于组的访问,只允许某组中的用户访问网页文件,则需要先创建组文件:


[root@www httpd24]# vim /etc/httpd24/.htgroup
caiwu: jim

   说明:只允许财务组中的用户jim访问。

   (6)再次修改主配置文件,只允许组文件.htgroup中的caiwu组的用户访问:


DocumentRoot "/usr/local/apache/htdocs"
<Directory "/usr/local/apache/htdocs/ug">
    Options None
    AllowOverride AuthConfig
    AuthType Basic
    AuthName "Private Area"
    AuthUserFile /etc/httpd24/.htpasswd
    AuthGroupFile /etc/httpd24/.htgroup
    Require group caiwu
</Directory>

四、虚拟主机:

1、虚拟主机的含义:一个物理服务器提供多个站点。

2、虚拟主机的实现方式:基于不同的IP实现不同的虚拟主机,变化的是IP地址;
                      基于不同的端口实现不同的虚拟主机,变化的是端口;
                      基于不同的FQDN实现不同的虚拟主机,变化的是ServerName的值。

3、基于完全合格的域名的访问的实现方式:

   (1)先取消中心主机,注释掉DocumentRoot:


vim /etc/httpd24/httpd.conf
#DocumentRoot "/usr/local/apache/htdocs"

   (2)启用虚拟主机:


[root@www httpd24]# vim /etc/httpd24/httpd.conf
Include /etc/httpd24/extra/httpd-vhosts.conf

   (3)创建虚拟主机的网页根目录:


mkdir /web/docs/www1 -pv

   (4)编辑虚拟主机的配置文件:


[root@www extra]# pwd
/etc/httpd24/extra
[root@www extra]# vim httpd-vhosts.conf

wKiom1NCtJDD6Sy9AAG0d0diBkM384.jpg

   (5)重启httpd24服务后,访问域名:

wKiom1NCtQGApEgNAABu-OCJzOM666.jpg

五、服务器status页面:
   1、status页面说明:通过web预以显示服务器内生的status信息,让用户对于服务器的版本、访问等情况清晰明了。

   2、实例:当基于虚拟用户来访问http://www.yanhai2.com/server-status时,能够显示www.yanhai2.com这个网站的访问等情况:

    (1)编辑虚拟主机的配置文件:


[root@www ~]# vim /etc/httpd24/extra/httpd-vhosts.conf

wKioL1NCvVLjYEipAAHUnXKN54U639.jpg

   3、成功访问网站:www.yanhai2.com和www.yanhai2.com的status页面:


wKioL1NCvjvQx-JsAABu63pT7f8031.jpg

wKiom1NCvmSwcKShAAFg4bZqGA0529.jpg

六、https网站的访问:

   1、为什么要使用https来访问网站?

   在互联网有时涉及购物交易的时候,需https协议认证,来加密发送数据,从而保证交易的安全。

   2、https是基于ssl(安全的套接字层)/tls来实现的,它需要证书颁发机构为网站颁发证书,证书有一个统一的标准,即x509.3,x509.3证书的格式是:
           证书格式的版本号;
           证书序列号;
           证书签名算法;
           证书颁发者;
           有效期;
           持有者的名称;
           持有者的公钥;
           CA的ID;
           持有者的ID;
           其它扩展信息:基本约束,证书策略,密钥的使用限制;
           CA签名。

   3、PKI,即证书颁发机构中的公钥基础设施的组成:

           端实体(申请者);
           注册机构(RC);
           签证机构(CA)-->签证机构(CA);
           证书撤消列表(CRL)发布机构;
           证书存取库。

   4、SSL握手要完成的工作:
           交换协议版本号;
           选择双方都支持的加密方式;
           对两端实现身份验正;
           密钥交换。

   5、客户端验正服务器证书时:
           日期检测:证书是否在有效期内;
           证书颁发者的可信度;
           证书的签名检测;
           持有者的身份检测。

   6、https的实现环境:

          SSL会话基于IP地址进行,因此不支持在基于FQDN的虚拟主机上实现。

   7、https实验实例:

   (1)创建CA的私钥和公钥:


[root@www CA]# pwd
/etc/pki/CA
[root@www CA]# (umask 077; openssl genrsa 2048 > private/cakey.pem)
Generating RSA private key, 2048 bit long modulus
.......+++
.................+++
e is 65537 (0x10001)
[root@www CA]# ls private/
cakey.pem

   (2)CA(证书颁发机构)自签证书:


[root@www CA]# pwd
/etc/pki/CA
[root@www CA]# openssl req -new -x509 -key private/cakey.pem -days 3655 -out cacert.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:henan
Locality Name (eg, city) [Default City]:xingyang
Organization Name (eg, company) [Default Company Ltd]:yanhai.com
Organizational Unit Name (eg, section) []:linux op
Common Name (eg, your name or your server's hostname) []:ca.yanhai.com
Email Address []:caadmin@yanhai.com
[root@www CA]# ls
cacert.pem  certs  crl  newcerts  private

   (3)http生成一对密钥:


[root@www ssl]# pwd
/etc/httpd24/ssl
[root@www ssl]# (umask 077; openssl genrsa 1024 > httpd.key)
Generating RSA private key, 1024 bit long modulus
...................................++++++
.................................++++++
e is 65537 (0x10001)
[root@www ssl]# ls
httpd.key

   (4)http生成证书签署请求:


[root@www ssl]# openssl req -new -key httpd.key -out httpd.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:henan
Locality Name (eg, city) [Default City]:xingyang
Organization Name (eg, company) [Default Company Ltd]:yanhai.com
Organizational Unit Name (eg, section) []:linux op
Common Name (eg, your name or your server's hostname) []:www.yanhai.com
Email Address []:webadmin@yanhai.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@www ssl]# ls
httpd.csr  httpd.key

   (5)在CA中创建索引等文件:


[root@www ssl]# cd /etc/pki/CA/
[root@www CA]# touch index.txt serial crlnumber
[root@www CA]# echo 01 >serial

   (6)CA给http中的www.yanhai.com域名签署证书:


[root@www CA]# cd /etc/httpd24/ssl/
[root@www ssl]# openssl ca -in httpd.csr -out httpd.crt
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Apr  3 18:54:18 2014 GMT
            Not After : Apr  3 18:54:18 2015 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = henan
            organizationName          = yanhai.com
            organizationalUnitName    = linux op
            commonName                = www.yanhai.com
            emailAddress              = webadmin@yanhai.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                3C:A6:7C:2C:A9:45:F6:B3:04:81:9B:E1:15:DD:20:80:A5:04:8A:2B
            X509v3 Authority Key Identifier:
                keyid:68:8A:24:12:85:E2:C1:9B:58:4B:BC:C4:B7:7D:AB:9E:52:AC:DE:A2
Certificate is to be certified until Apr  3 18:54:18 2015 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@www ssl]# ls
httpd.crt  httpd.csr  httpd.key

   (7)在主配置文件中启用ssl模块:


[root@www ~]# vim /etc/httpd24/httpd.conf
LoadModule ssl_module modules/mod_ssl.so

   (8)编辑ssl配置文件:


[root@www extra]# pwd
/etc/httpd24/extra
[root@www extra]# vim httpd-ssl.conf


<VirtualHost _default_:443>
#   General setup for the virtual host
DocumentRoot "/web/docs/www1"
ServerName www.yanhai.com:443
ErrorLog "/usr/local/apache/logs/yanhai.com.error_log"
TransferLog "/usr/local/apache/logs/yanhai.com.access_log"
<Directory "/web/docs/www1">
        Options none
        allowoverride none
        Require all granted
</Directory>

七、使用mod_deflate模块压缩网页页面优化传输速度:

   1、mod_deflate模块压缩网页的适用环境:网页文件较大时,则需要压缩。

   2、mod_deflate模块压缩网页的作用:节约带宽,使用户体验更好;调用GZIP进行压缩,压缩网页页面优化传输速度。

   3、使用mod_deflate模块压缩网页页面的实例:

       (1)编辑主配置文件,启用mod_deflate模块:

   

[root@www httpd24]# pwd
/etc/httpd24
[root@www httpd24]# vim httpd.conf
LoadModule deflate_module modules/mod_deflate.so

       (2)新建deflate配置文件:

   

[root@www extra]# pwd
/etc/httpd24/extra
[root@www extra]# vim deflate.conf

   wKiom1ND_vPBYaGiAAJt7Kc7c2k481.jpg

   说明:SetOutputFilter DEFLATE:启动deflate过滤器来实现输出内容并完成内容压缩 ;

         AddOutputFilterByType DEFLATE text/plain:通过类型来输出过滤器,对于/text/plain等网页文档类型做压缩 ;

         DeflateCompressionLevel 9:使用的压缩比 ;

         BrowserMatch ^Mozilla/4 gzip-only-text/html:对于什么样的浏览器可以不使用压缩。

     (3)重启服务后访问网页,可以改网页使用的gzip压缩方式:

   wKiom1NEATizDu4AAAIsNUsdCVA078.jpg