1. httpd简介

httpd是Apache的超文本传输​​协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。
通常,httpd的不应该被直接调用,而应该在类Unix的系统中由的apachectl调用,在Windows中作为服务运行。

2. httpd版本

本文主要介绍的httpd的两大版本,httpd-2.2和httpd-2.4。

CentOS6系列的版本默认提供的是的httpd-2.2版本的rpm包
CentOS7系列的版本默认提供的是的httpd-2.4版本的rpm包

2.1 httpd的特性
httpd的有很多特性,下面就分别来说说的httpd-2.2版本和的httpd-2.4版本各自的特性。

httpd-2.2的特性

  • 事先创建进程
  • 按需维持适当的进程
  • 模块化设计,核心比较小,各种功能通过模块添加(包括PHP),支持运行时配置,支持单独编译模块
  • 支持多种方式的虚拟主机配置,如基于ip的虚拟主机,基于端口的虚拟主机,基于域名的虚拟主机等
  • 支持https协议(通过mod_ssl模块实现)
  • 支持用户认证
  • 支持基于IP或域名的ACL访问控制机制
  • 支持每目录的访问控制(用户访问默认主页时不需要提供用户名和密码,但是用户访问某特定目录时 需要提供用户名和密码)
  • 支持URL重写
  • 支持MPM(Multi Path Modules,多处理模块)。用于定义httpd的工作模型(单进程、单进程多线程、多进程、多进程单线程、多进程多线程)

httpd-2.4的新特性:

  • MPM支持运行DSO机制(Dynamic Share Object,模块的动态装/卸载机制),以模块形式按需加载
  • 支持event MPM,eventMPM模块生产环境可用
  • 支持异步读写
  • 支持每个模块及每个目录分别使用各自的日志级别
  • 每个请求相关的专业配置,使用来配置
  • 增强版的表达式分析器
  • 支持毫秒级的keepalive timeout
  • 基于FQDN的虚拟主机不再需要NameVirtualHost指令
  • 支持用户自定义变量
  • 支持新的指令(AllowOverrideList)
  • 降低对内存的消耗
  • 工作模型                                        工作方式
    prefork         多进程模型,预先生成进程,请求一个用一个进程响应,一个主进程负责生成Ñ个子进程,进程子也。称为工作进程,每个子进程处理一个用户请求,即使没有用户请求,也会预先生成多个空闲进程,随时等待请求到达,最大不会超过1024个
    worker          基于线程工作,一个请求用一个线程响应(启动多个进程,每个进程生成多个线程)
    event          基于事件的驱动,一个进程处理多个请求

2.2 httpd-2.4新增的模块
的httpd-2.4在之前的版本基础上新增了几大模块,下面就几个常用的来介绍一下。

模块                                             功能
mod_proxy_fcgi      反向代理时支持的Apache服务器后端协议的模块
mod_ratelimit           提供速率限制功能的模块
mod_remoteip        基于ip的访问控制机制被改变,不再支持使用Order,Deny,Allow来做基于IP的访问控制

3. httpd基础

3.1 httpd自带的工具程序

工具                      功能
htpasswd            basic认证基于文件实现时,用到的帐号密码生成工具
apachectl           httpd自带的服务控制脚本,支持start,stop,restart
apxs            由httpd-devel包提供的,扩展httpd使用第三方模块的工具
rotatelogs          日志滚动工具
suexec          访问某些有特殊权限配置的资源时,临时切换至指定用户运行的工具
ab              apache benchmark,httpd的压力测试工具

3.2 rpm包安装的httpd程序环境

文件/目录                                                     对应的功能
/var/log/httpd/access.log                        访问日志
/var/log/httpd/error_log                           错误日志
/var/www/html/                                        站点文档目录
/usr/lib64/httpd/modules/                        模块文件路径
/etc/httpd/conf/httpd.conf                      主配置文件
/etc/httpd/conf.modules.d/*.conf        模块配置文件
/etc/httpd/conf.d/*.conf                           辅助配置文件

mpm:以DSO机制提供,配置文件为/etc/httpd/conf.modules.d/00-mpm.conf

3.3 web相关的命令
3.3.1 curl命令

curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP,FTPS,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE及LDAP等协议。
curl支持以下功能:

  • https认证
  • http的POST/PUT等方法
  • ftp上传
  • kerberos认证
  • http上传
  • 代理服务器
  • cookies
  • 用户名/密码认证
  • 下载文件断点续传
  • socks5代理服务器
  • 通过http代理服务器上传文件到ftp服务器
  • //语法:curl [options] [URL ...]
    //常用的options:
    -A/--user-agent     //设置用户代理发送给服务器
    -basic              //使用Http基本认证
    --tcp-nodelay       //使用TCP_NODELAY选项
    -e/--referer       //来源网址
    --cacert      //CA证书(SSL)
    --compressed        //要求返回时压缩的格式
    -H/--header   //自定义请求首部信息传递给服务器
    -I/--head           //只显示响应报文首部信息
    --limit-rate      //设置传输速度
    -u/--user      //设置服务器的用户和密码
    -0/--http1      //使用http 1.0版本,默认使用1.1版本。这个选项是数字0而不是字母o
    -o/--output     //把输出写到文件中        (一般只用这个命令)
    -#/--progress-bar       //进度条显示当前的传送状态

3.3.2 httpd命令

//语法:httpd [options]
//常用的options:
    -l      //查看静态编译的模块,列出核心中编译了哪些模块。 \
            //它不会列出使用LoadModule指令动态加载的模块
    -M      //输出一个已经启用的模块列表,包括静态编译在服务 \
            //器中的模块和作为DSO动态加载的模块
    -v      //显示httpd的版本,然后退出
    -V      //显示httpd和apr/apr-util的版本和编译参数,然后退出
    -X      //以调试模式运行httpd。仅启动一个工作进程,并且 \
            //服务器不与控制台脱离
    -t      //检查配置文件是否有语法错误        (一般只用这个命令)

4.编译安装http-2.4

//这里的http压缩包可以用curl命令或者wget命令去apache网站下载

[root@wlw ~]# yum groupinstall "Development Tools"

[root@wlw ~]# groupadd -r apache
[root@wlw ~]# useradd -r -g apache apache
[root@wlw ~]# yum -y install openssl-devel pcre-devel expat-devel libtool

[root@wlw ~]# cd /usr/src/
[root@wlw src]# rz

[root@wlw src]# ls
apr-1.6.5         apr-util-1.6.1         debug         httpd-2.4.34.tar.bz2  kernels
apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.34  httpd-2.4.38.tar.bz2
[root@wlw src]# tar xf apr-1.6.5.tar.gz 
[root@wlw src]# tar xf apr-util-1.6.1.tar.gz 
[root@wlw src]# ls
apr-1.6.5         apr-util-1.6.1         debug         httpd-2.4.34.tar.bz2  kernels
apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.34  httpd-2.4.38.tar.bz2
[root@wlw src]# cd apr-1.6.5
[root@wlw apr-1.6.5]# vim configure
 cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"     //此行前面加注释或者删除此行

[root@wlw apr-1.6.5]# ./configure --prefix=/usr/local/apr

[root@wlw apr-util-1.6.1]# cd /usr/src/apr-util-1.6.1
[root@wlw apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr
[root@wlw apr-util-1.6.1]# make && make install 
[root@wlw apr-util-1.6.1]# cd
[root@wlw ~]# ls
[root@wlw ~]# rz
[root@wlw ~]# ls
[root@wlw ~]# tar xf httpd-2.4.38.tar.bz2 
[root@wlw ~]# cd httpd-2.4.38
[root@wlw httpd-2.4.38]# ./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-mpms-shared=all --with-mpm=preforkutil-1.6.1]# make && make install 
//这里编译时间有点长,慢慢等
[root@wlw ~]# source /etc/profile.d/httpd.sh 
[root@wlw ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh 
[root@wlw ~]# source /etc/profile.d/httpd.sh 
[root@wlw ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@wlw ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[root@wlw ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf 
[root@wlw ~]# apachectl start
[root@wlw ~]# ss -antl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN     0      50                           *:139                                      *:*                  
LISTEN     0      64                           *:41771                                    *:*                  
LISTEN     0      128                          *:111                                      *:*                  
LISTEN     0      128                          *:80                                       *:*                  
LISTEN     0      128                          *:20048                                    *:*                  
LISTEN     0      128                          *:22                                       *:*                  
LISTEN     0      100                  127.0.0.1:25                                       *:*                  
LISTEN     0      50                           *:445                                      *:*                  
LISTEN     0      64                           *:2049                                     *:*                  
LISTEN     0      128                          *:56458                                    *:*                  
LISTEN     0      50                          :::139                                     :::*                  
LISTEN     0      128                         :::111                                     :::*                  
LISTEN     0      128                         :::20048                                   :::*                  
LISTEN     0      128                         :::41940                                   :::*                  
LISTEN     0      128                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
LISTEN     0      50                          :::445                                     :::*                  
LISTEN     0      64                          :::2049                                    :::*                  
LISTEN     0      64                          :::43753                                   :::*                  
[root@wlw ~]# cd /usr/local/apache/htdocs/
[root@wlw htdocs]# ls
1.jpg  index.html
[root@wlw htdocs]# 

5. httpd常用配置

切换使用MPM(编辑/etc/httpd/conf.modules.d/00-mpm.conf文件):

  1. LoadModule mpm_NAME_module modules/mod_mpm_NAME.so
  2. NAME有三种,分别是:
  3. prefork
  4. event
  5. worker

访问控制法则:

法则                              功能
Require all granted             允许所有主机访问
Require all deny                拒绝所有主机访问
Require ip IPADDR               授权指定来源地址的主机访问
Require not ip IPADDR           拒绝指定来源地址的主机访问
Require host HOSTNAME       授权指定来源主机名的主机访问
Require not host HOSTNAME   拒绝指定来源主机名的主机访问

IPADDR的类型
IP:192.168.1.1
Network/mask:192.168.1.0/255.255.255.0
Network/Length:192.168.1.0/24
Net:192.168

HOSTNAME的类型
FQDN:特定主机的全名
DOMAIN:指定域内的所有主机

注意:httpd-2.4版本默认是拒绝所有主机访问的,所以安装以后必须做显示授权访问
示例:


    
        Require not ip 192.168.160.89
        Require all granted
    

虚拟主机:
虚拟主机有三类:

相同IP不同端口
不同IP相同端口
相同IP相同端口不同域名

ssl:
启用模块:编辑/etc/httpd/conf.modules.d/00-base.conf文件,添加下面这行,如果已经有了但是注释了,则取消注释即可
LoadModule ssl_module modules/mod_ssl.so

虚拟主机示例:

先启用模块
相同ip不同端口

[root@wlw htdocs]# vim /etc/httpd24/httpd.conf 


    ServerName wlw.example.com
    DocumentRoot "/usr/local/apache/htdocs/www"
    ErrorLog "logs/11_error_log"
    CustomLog "logs/22_access_log" common



    ServerName wan.example.com
    DocumentRoot "/usr/local/apache/htdocs/lll"
    ErrorLog "logs/22_error_log"
    CustomLog "logs/22_access_log" common

再末行:/Listen
在Listen 80下面一行添加
添加个 Listen 81

[root@wlw htdocs]# apachectl -t
Syntax OK
[root@wlw htdocs]# apachectl restart
[root@wlw htdocs]# 

不同ip相同端口

[root@wlw htdocs]# vim /etc/httpd24/httpd.conf 


    ServerName wlw.example.com
    DocumentRoot "/usr/local/apache/htdocs/www"
    ErrorLog "logs/11_error_log"
    CustomLog "logs/22_access_log" common



    ServerName wan.example.com
    DocumentRoot "/usr/local/apache/htdocs/lll"
    ErrorLog "logs/22_error_log"
    CustomLog "logs/22_access_log" common

再添加个ip
[root@wlw htdocs]# ip addr add 192.168.66.6/24 dev eth0
[root@wlw htdocs]# ip a
[root@wlw htdocs]# apachectl -t
Syntax OK
[root@wlw htdocs]# apachectl restart

配置相同IP相同端口不同域名

[root@wlw ~]# apachectl restart
[root@wlw ~]# vim /etc/httpd24/httpd.conf 
//添加配置文件

    ServerName wlw.example.com
    DocumentRoot "/usr/local/apache/www"
    ErrorLog "logs/11_error_log"
    CustomLog "logs/22_access_log" common



    ServerName wan.example.com
    DocumentRoot "/usr/local/apache/lll"
    ErrorLog "logs/22_error_log"
    CustomLog "logs/22_access_log" common

[root@wlw ~]# cd /usr/local/apache/htdocs/
[root@wlw htdocs]# mkdir www lll
mkdir: 无法创建目录"www": 文件已存在
mkdir: 无法创建目录"lll": 文件已存在
[root@wlw htdocs]# chown -R apache.apache www
[root@wlw htdocs]# chown -R apache.apache lll
[root@wlw htdocs]# ll
总用量 0
drwxr-xr-x. 2 apache apache 24 3月  30 13:06 lll
drwxr-xr-x. 2 apache apache 24 3月  30 13:06 www
[root@wlw htdocs]# ls
lll  www
[root@wlw htdocs]# echo "qwe" > www/index.html 
[root@wlw htdocs]# echo "qwe" > lll/index.html 
[root@wlw htdocs]# vim /etc/httpd24/httpd.conf 
[root@wlw htdocs]# 

进入 C:\windows\systemd32\drivers\hosts这个文件里面
添加ip和域名
192.168.66.128  wlw.example.com
192.168.66.128  wan.example.com

https配置

// openssl实现私有CA:
CA的配置文件:/etc/pki/tls/openssl.cnf

a) CA生成一对密钥
[root@wlw ~]# cd /etc/pki/CA
[root@wlw CA]# ls
certs  crl  newcerts  private
[root@wlw CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
.................................................................................+++
..................................................................+++
e is 65537 (0x10001)
[root@wlw CA]# openssl rsa -in private/cakey.pem -pubout
//过程略

b) CA生成自签署证书
[root@wlw CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
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) []:HuBei //省级
Locality Name (eg, city) [Default City]:WuHan //市级
Organization Name (eg, company) [Default Company Ltd]:wlw.example.com    //域名
Organizational Unit Name (eg, section) []:wlw.example.com    //域名
Common Name (eg, your name or your server's hostname) []:wlw.example.com  //域名
Email Address []:[email protected]   //邮箱
[root@wlw CA]# openssl x509 -text -in cacert.pem 
//过程略
[root@wlw CA]# mkdir certs newcerts crl
mkdir: 无法创建目录"certs": 文件已存在
mkdir: 无法创建目录"newcerts": 文件已存在
mkdir: 无法创建目录"crl": 文件已存在
[root@wlw CA]# touch index.html && echo 01 > serial
[root@wlw CA]# ls
cacert.pem  certs  crl  index.html  newcerts  private  serial
[root@wlw CA]# cat serial 
01

c) 服务端生成密钥
[root@wan ~]# cd /etc/httpd24/ && mkdir ssl && cd ssl
[root@wan ssl]# (umask 077;openssl genrsa -out httpd.key 2048)
Generating RSA private key, 2048 bit long modulus
.........................................+++
.............................................................................+++
e is 65537 (0x10001)
[root@wan ssl]# 

d) 服务端生成证书签署请求
[root@wan ssl]# openssl req -new -key httpd.key -days 365 -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) []:HuBei //省级
Locality Name (eg, city) [Default City]:WuHan //市级
Organization Name (eg, company) [Default Company Ltd]:wlw.example.com   //域名
Organizational Unit Name (eg, section) []:wlw.example.com  //域名
Common Name (eg, your name or your server's hostname) []:wlw.example.com //域名
Email Address []:[email protected]   //邮箱

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:       //直接回车不输入密码
An optional company name []:

e) 服务端把证书签署请求文件发送给CA
[root@wan ssl]# scp httpd.csr [email protected]:/root
httpd.csr                                                                     100% 1066   264.6KB/s   00:00    
[root@wan ssl]# 

//切到客户端了
[root@wlw ~]# ls
  httpd.csr       
[root@wlw ~]# 

f) CA签署服务端提交上来的证书
[root@wlw ~]# openssl ca -in /root/httpd.csr -out httpd.crt -days 365
[root@wlw ~]# ls
 httpd.csr    httpd.crt           

 g) CA把签署好的证书httpd.crt发给服务端
 [root@wlw ~]# scp httpd.crt [email protected]:/root
httpd.crt                                                                     100% 4661     3.2MB/s   00:00    

//切到服务端了
[root@wan ~]# ls
123  3.sh    99.sh   abc        index.html  tcs2.sh    Tetris.sh  txz.sh
1if  999.sh  9x9.sh  httpd.crt  s           tetris.sh  txc.sh     ww
[root@wan ~]# mv httpd.crt /etc/httpd24/ssl/
[root@wan ~]# cd /etc/h
host.conf    hostname     hosts        hosts.allow  hosts.deny   httpd24/     
[root@wan ~]# cd /etc/httpd24/ssl/
[root@wan ssl]# ls
httpd.crt  httpd.csr  httpd.key
[root@wan ssl]# 

// ssl配置:

// 配置虚拟主机
/DocumentRoot   //搜索
修改为以下内容:
DocumentRoot "/usr/local/apache/htdocs/wlw"
ServerName wlw.example.com:443
ServerAdmin [email protected]
ErrorLog "/usr/local/apache/logs/wlw.exapmle.error_log"
TransferLog "/usr/local/apache/logs/wlw.example.access_log"
再把
SSLCertificateFile "/etc/httpd24/server.crt"  改为 SSLCertificateFile "/etc/httpd24/ssl/httpd.crt"
SSLCertificateKeyFile "/etc/httpd24/server.key"  改为   SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key"

//配置完成之后检查一下配置文件是否有语法错误:
[root@wan]# apachectl -t
Syntax OK

[root@wan]# vim httpd.conf
Include /etc/httpd24/extra/httpd-ssl.conf    //将此行的注释取消
LoadModule ssl_module modules/mod_ssl.so   //将此行的注释取消

[root@wan]# vim extra/httpd-ssl.conf 
#SSLSessionCache        "shmcb:/usr/local/apache/logs/ssl_scache(512000)"  //此行添加注释

//修改完成之后检查一下配置文件是否有语法错误:
[root@wan]# apachectl -t
Syntax OK

//重启服务
[root@wan]# apachectl restart

//查看https端口(443)起来没有
[root@wan]# ss -antl
State       Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
LISTEN      0      50                      *:139                                 *:*                  
LISTEN      0      128                     *:111                                 *:*                  
LISTEN      0      128                     *:22                                  *:*                  
LISTEN      0      100             127.0.0.1:25                                  *:*                  
LISTEN      0      50                      *:445                                 *:*                  
LISTEN      0      128                     *:44643                               *:*                  
LISTEN      0      50                     :::139                                :::*                  
LISTEN      0      128                    :::111                                :::*                  
LISTEN      0      128                    :::80                                 :::*                  
LISTEN      0      128                    :::22                                 :::*                  
LISTEN      0      100                   ::1:25                                 :::*                  
LISTEN      0      128                    :::443                                :::*          //443端口起来了

修改客户端的hosts文件
C:\Windows\System32\drivers\etc\hosts
192.168.66.128 wlw.example.com

//记得把文件拖出来修改,之后再拖进去
搞完就可以直接用域名登录网页了