美文网首页Linux初学者学习笔记我爱编程
20170929 http服务和apache(三)

20170929 http服务和apache(三)

作者: 哈喽别样 | 来源:发表于2017-10-09 22:57 被阅读32次
    • mod_flate模块
    • https实现
    • http重定向https
    • HSTS
    • httpd相关程序
    • httpd-2.4
    • 编译安装httpd-2.4

    一、mod_deflate模块

    • 功能:压缩页面优化传输速度

    • 开启压缩功能:

    vim /etc/httpd/conf.d/deflate.conf
    LoadModule deflate_module modules/mod_deflate.so 
    SetOutputFilter DEFLATE
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/css
    DeflateCompressionLevel 9
    
    service httpd reload
    curl -I 192.168.136.229/large.txt
    curl --compressed -I 192.168.136.229/large.txt
    

    二、https实现:

    • 前提:本实验涉及到的主机有:
      httpd服务器:IP 192.136.136.229
      CA服务器:IP 192.136.136.230
      DNS服务器:IP 192.136.136.130
      客户端:IP 192.136.136.129

    (一)为httpd服务器申请数字证书

    通过创建私有CA签发证书
    (a) 创建私有CA

    cd /etc/pki/CA/
    (umask 066;openssl genrsa -out private/cakey.pem 2048)                       //创建私钥
    openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650     //创建自签名证书
    echo 00 > serial
    touch index.txt
    

    (b) 在服务器创建证书签署请求

    mkdir /etc/httpd/conf.d/ssl
    cd /etc/httpd/conf.d/ssl
    (umask 066;openssl genrsa -out httpd.key 2048)      //创建私钥
    openssl req -new -key httpd.key -out httpd.csr      //创建证书申请
    scp httpd.csr 192.168.136.230:/etc/pki/CA           //向CA传送证书申请
    

    (c) CA签证

    openssl ca -in httpd.csr -out certs/httpd.crt -days 365     //签发证书
    scp certs/httpd.crt cacert.pem 192.168.136.229:/etc/httpd/conf.d/ssl/
    //向httpd服务器传送证书和CA的自签名证书
    

    (二)配置httpd支持使用ssl

    yum -y install mod_ssl
    vim /etc/httpd/conf.d/ssl.conf
    SSLCertificateFile /etc/httpd/conf.d/ssl/httpd.crt     //httpd服务器证书
    SSLCertificateKeyFile /etc/httpd/conf.d/ssl/httpd.key     //httpd私钥
    SSLCACertificateFile /etc/httpd/conf.d/ssl/cacert.pem     //CA自签名证书
    
    httpd -t
    service httpd reload
    

    (三)配置DNS服务器

    yum install bind
    //1. 编辑通用配置文件
    vim /etc/named.conf
    options {
            listen-on port 53 { localhost; };      //修改的行
            allow-query     { any; };      //修改的行
    };
    //2. 编辑独立分区解析文件
    vim /etc/named.rfc1912.zones
    zone "hellopeiyang.com" IN {
            type master;
            file "hellopeiyang.com.zone";
    };
    
    named-checkconf
    //3. 编辑解析库文件
    vim /var/named/hellopeiyang.com.zone
    $TTL 1D
    @       IN SOA  dns1 admin.hellopeiyang.com. (
                                            101     ; serial
                                            1D      ; refresh
                                            1H      ; retry
                                            1W      ; expire
                                            3H )    ; minimum
            NS      dns1
    dns1    A       192.168.136.130
    websrv  A       192.168.136.229
    www     CNAME   websrv
    
    named-checkzone hellopeiyang.com /var/named/hellopeiyang.com.zone
    dig www.hellopeiyang.com @127.0.0.1
    

    (四)服务器准备文件

    vim /etc/httpd/conf/httpd.conf
    DocumentRoot "/app"
    
    vim /app/index.html
    <h1>Welcome to hellopeiyang.com</h1>
    
    httpd -t
    service httpd reload
    scp /etc/httpd/conf.d/ssl/cacert.pem 192.168.136.129:/root     //向客户端传送CA自签名证书
    

    (五)客户端测试

    vim /etc/sysconfig/network-scripts/ifcfg-eth1
    DNS1=192.168.136.130                                       //增加一行DNS服务器IP
    service network restart
    cat /etc/resolv.conf
    curl https://www.hellopeiyang.com                          //直接登录失败
    curl -k  https://www.hellopeiyang.com                      //-k选项忽略证书能够看到网页内容正确
    curl --cacert cacert.pem  https://www.hellopeiyang.com     //成功连接
    
    • 注意:SSL是基于IP地址实现,单IP的主机仅可以使用一个https虚拟主机

    三、http重定向https

    • 配置格式
      Redirect [status] URL-path URL

    • status状态:

      • Permanent:永久重定向,301
      • Temp:临时重定向,302,默认设置,一般http重定向https选择temp模式
    • 实验:接上个实验,实现访问http://www.hellopeiyang.com重定向为https

    vim /etc/httpd/conf.d/redirect.conf
    Redirect temp / https://www.hellopeiyang.com/
    httpd -t
    service httpd reload
    

    四、HSTS

    • HSTS: HTTP Strict Transport Security
      服务器端配置支持HSTS后,会在给浏览器返回的HTTP首部中携带HSTS字段。浏览器获取到该信息后,会将所有HTTP访问请求在内部做307跳转到HTTPS,而无需任何网络过程

    • HSTS preload list
      Chrome浏览器中的HSTS预载入列表,在该列表中的网站,使用Chrome浏览器访问时,会自动转换成HTTPS。Firefox、Safari、Edge浏览器也会采用这个列表

    • 实验:开启HSTS功能

    vim /etc/httpd/conf.d/hsts.conf
    Header always set Strict-Transport-Security "max-age=15768000"
    RewriteEngine on
    RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
    
    httpd -t
    service httpd reload
    

    五、httpd相关程序

    (一)httpd自带工具程序

    • htpasswd:basic认证基于文件实现时,用到的账号密码文件生成工具
    • apachectl:httpd自带的服务控制脚本,支持start和stop
    • apxs:httpd-devel包提供,扩展httpd使用第三方模块工具
    • rotatelogs:日志滚动工具
      access.log -->
      access.log, access.1.log -->
      access.log, acccess.1.log, access.2.log
    • suexec:访问某些有特殊权限配置的资源时,临时切换至指定用户身份运行

    (二)httpd的压力测试工具

    • ab(httpd-tools包), webbench, http_load, seige
    • Jmeter:开源
    • Loadrunner:商业,有相关认证
    • tcpcopy:网易,复制生产环境中的真实请求,并将之保存
    • ab [OPTIONS] URL
      -n:总请求数
      -c:模拟的并行数
      -k:以持久连接模式测试
      ulimit -n # 调整能打开的文件数

    六、httpd-2.4

    (一)httpd-2.4的变化

    • 新特性
      (1) MPM支持运行为DSO机制;以模块形式按需加载
      (2) event MPM生产环境可用
      (3) 异步读写机制
      (4) 支持每模块及每目录的单独日志级别定义
      (5) 每请求相关的专用配置
      (6) 增强版的表达式分析式
      (7) 毫秒级持久连接时长定义
      (8) 基于FQDN的虚拟主机不需要NameVirutalHost指令
      (9) 新指令,AllowOverrideList
      (10) 支持用户自定义变量
      (11) 更低的内存消耗

    • 修改了一些配置机制
      不再支持使用Order, Deny, Allow来做基于IP的访问控制

    • 新模块
      (1) mod_proxy_fcgi
      (2) mod_remoteip
      (3) mod_ratelimit

    (二)httpd-2.4的程序环境

    • 配置文件:
      /etc/httpd/conf/httpd.conf
      /etc/httpd/conf.d/*.conf

    • 模块相关的配置文件:/etc/httpd/conf.modules.d/*.conf

    • SystemdUnit文件:/usr/lib/systemd/system/httpd.service

    • 主程序文件:
      /usr/sbin/httpd
      httpd-2.4支持MPM的动态切换

    • 日志文件:
      /var/log/httpd
      access_log:访问日志
      error_log:错误日志

    • 站点文档:
      /var/www/html

    • 模块文件路径:
      /usr/lib64/httpd/modules

    • 服务控制:
      systemctl enable|disable httpd.service
      systemctl {start|stop|restart|status} httpd.service

    (三)httpd-2.4的配置

    (1)切换使用的MPM

    Centos 7环境:
    编辑/etc/httpd/conf.modules.d/00-mpm.conf
    启用要启用的MPM相关的LoadModule指令即可

    (2)主目录

    DocumentRoot /path

    (3)基于IP的访问控制
    • 无明确授权的目录,默认拒绝

      • 允许所有主机访问:Require all granted
      • 拒绝所有主机访问:Require all denied
    • 控制特定的IP访问:
      Require ip IPADDR:授权特定的IP访问
      Require not ip IPADDR:拒绝特定的IP访问

    • 控制特定的主机访问:
      Require host HOSTNAME:授权特定主机访问
      Require not host HOSTNAME:拒绝特定主机访问
      HOSTNAME:
      FQDN:特定主机
      domin.tld:指定域名下的所有主机

    • 不能有失败,至少有一个成功匹配
      <RequireAll>
      Require all granted
      Require not ip IPADDR 拒绝特定IP
      </RequireAll>

    • 多个语句有一个成功,即成功
      <RequireAny>
      Require all denied
      Require ip IPADDR 允许特定IP
      </RequireAny>

    • 实验:将httpd服务的主目录修改为/app,并且只允许192.136.136.130的主机访问

    //1. 修改主目录
    vim /etc/httpd/conf/httpd.conf
    DocumentRoot "/app"
    <Directory "/app">
            Require all granted
    </Directory>
    
    httpd -t
    systemctl reload httpd
    echo "/app/index.html" > /app/index.html
    
    //2. 修改访问权限
    vim /etc/httpd/conf/httpd.conf
    <Directory "/app">
            <RequireAny>
                    Require all denied
                    Require ip 192.168.136.130
            </RequireAny>
    </Directory>
    
    httpd -t
    systemctl reload httpd
    
    (4)虚拟主机
    • 基于FQDN的虚拟主机也不再需要NameVirutalHost指令

    • 任意目录下的页面只有显式授权才能被访问

    • 实验:httpd 2.4环境下实现基于FQDN的主机

    //1. 建立网页文件
    mkdir /app/website{1..3}
    echo "/app/website1/index.html" >  /app/website1/index.html
    echo "/app/website2/index.html" >  /app/website2/index.html
    echo "/app/website3/index.html" >  /app/website3/index.html
    
    //2. 编辑独立配置文件
    vim /etc/httpd/conf.d/virtualhost.conf
    <Virtualhost *:80>
            ServerName www.hello.com
            DocumentRoot "/app/website1"
            <Directory "/app/website1">     //显式授权
                    Require all granted
            </Directory>
    </Virtualhost>
    
    <Virtualhost *:80>
            ServerName www.hi.cn
            DocumentRoot "/app/website2"
            <Directory "/app/website2">     //显式授权
                    Require all granted
            </Directory>
    </Virtualhost>
    
    <Virtualhost *:80>
            ServerName www.bye.net
            DocumentRoot "/app/website3"
            <Directory "/app/website3">     //显式授权
                    Require all granted
            </Directory>
    </Virtualhost>
    
    httpd -t
    systemctl reload httpd
    
    //3. 配置DNS服务器或者编辑hosts文件
    
    //4. 测试
    curl www.hello.com
    curl www.hi.cn
    curl www.bye.net
    
    (5)sendfile机制
    • 传统网络传输过程:
      硬盘>> kernel buffer >> user buffer >> kernel socket buffer >> 协议栈
      过程中经历四次上下文切换,四次拷贝,拷贝的内容基本相同

    • 使用sendfile传输过程:
      硬盘>> kernel buffer (快速拷贝到kernel socket buffer) >> 协议栈
      过程中没有上下文切换,只有一次拷贝,提高了性能

    • 开启sendfile功能

    vim /etc/httpd/conf/httpd.conf
    EnableSendfile on
    
    (6)反向代理
    • 语法:
    ProxyPass "/" "http://www.example.com/"
    ProxyPassReverse "/" "http://www.example.com/"
    
    • 实验:
      接上个实验的环境,将发至www.hello.com (ip: 192.168.136.230)的请求转发至192.168.136.129
    //1. 编辑独立配置文件
    vim /etc/httpd/conf.d/virtualhost.conf
    <Virtualhost *:80>
            ServerName www.hello.com
            DocumentRoot "/app/website1"
            <Directory "/app/website1">
                    Require all granted
            </Directory>
            ProxyPass "/" "http://192.168.136.129/"            //修改的部分
            ProxyPassReverse "/" "http://192.168.136.129/"     //修改的部分
    </Virtualhost>
    
    httpd -t
    systemctl reload httpd
    
    //2. 建立网页文件(ip: 192.168.136.129)
    echo "welcome to hellopeiyang's home" > /var/www/html/index.html
    
    //3. 测试
    curl www.hello.com
    curl 192.168.136.230
    curl 192.168.136.129
    

    七、编译安装httpd-2.4

    (一)APR

    • APR(Apache Portable Run-time libraries,Apache可移植运行库)主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库

    • APR负责解决不同操作系统平台的细节,根据系统平台使用相应的系统调用

    • Apache httpd依赖于APR,httpd-2.4需要APR 1.4以上版本

    (二)CentOS 7环境下源码编译安装httpd-2.4

    (1)安装前准备:
    • 安装开发包组:
      yum groupinstall "development tools"

    • 安装必要的软件开发包:
      yum install apr-devel apr-util-devel pcre-devel openssl-devel

    • 下载安装包,并解压缩
      tar xvf httpd-2.4.27.tar.bz2 -C /usr/local/src

    (2)编译安装过程
    • 进入解压缩后的源码目录
      cd /usr/local/src/httpd-2.4.27/

    • 检查编译环境,生成makefile文件
      ./configure --prefix=/app/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

    • 编译并安装
      make -j 4 && make install

    (3)安装后配置
    • 建立系统账号apache
      useradd -r -d /app/httpd24/htdocs/ -s /sbin/nologin apache

    • 修改运行httpd程序的用户和组

    vim /app/httpd24/conf/httpd.conf
    User apache
    Group apache
    
    • 增加环境变量值
    vim /etc/profile.d/httpd24.sh
    PATH=/app/httpd24/bin:$PATH
    
    . /etc/profile.d/httpd24.sh
    
    • 设置httpd服务开机自启动
    vim /etc/rc.d/rc.local
    /app/httpd24/bin/apachectl start
    
    chmod +x /etc/rc.d/rc.local
    
    • 启动httpd服务
      apachectl start

    (三)CentOS 6环境下源码编译安装httpd-2.4(方法一)

    (1)安装前准备:
    • 安装开发包组和相关开发软件包
    yum groupinstall "development tools"
    yum install pcre-devel openssl-devel expat-devel
    
    • 下载1.4版本以上的apr和apr-util以及httpd2.4的软件包,并解压缩
    tar xvf apr-1.6.2.tar.gz -C /usr/local/src/
    tar xvf apr-util-1.6.0.tar.gz -C /usr/local/src/
    tar xvf httpd-2.4.27.tar.bz2 -C /usr/local/src/
    
    (2)编译安装apr
    • 进入解压缩的源码目录
      cd /usr/local/src/apr-1.6.2/

    • 检查编译环境,并生成makefile文件
      ./configure --prefix=/app/apr

    • 编译并安装
      make && make install
    (3)编译安装apr-util
    • 进入解压缩的源码目录
      cd /usr/local/src/apr-util-1.6.0/

    • 检查编译环境,并生成makefile文件
      ./configure --prefix=/app/apr-util --with-apr=/app/apr

    • 编译并安装
      make && make install
    (4)编译安装httpd-2.4
    • 添加系统账号apache
      useradd -r -d /app/website -s /sbin/nologin apache

    • 进入解压缩的源码目录
      cd /usr/local/src/httpd-2.4.27/

    • 检查编译环境,并生成makefile文件
      ./configure --prefix=/app/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/app/apr/ --with-apr-util=/app/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

    • 编译并安装
      make -j 4 && make install
    (5)安装后配置
    • 修改运行httpd程序的用户和组,修改文件存放根目录
    vim /app/httpd24/conf/httpd.conf
    User apache                     //修改的行
    Group apache                    //修改的行
    DocumentRoot "/app/website"     //修改的行
    <Directory "/app/website">      //修改的行
    
    mkdir /app/website
    
    • 增加环境变量值
    vim /etc/profile.d/httpd24.sh
    PATH=/app/httpd24/bin:$PATH
    
    . /etc/profile.d/httpd24.sh
    
    • 编辑服务脚本,并设置为开机启动
    scp /etc/init.d/httpd 192.168.136.129:/etc/init.d/httpd24     //参考httpd-2.2的服务脚本进行修改
    vim /etc/init.d/httpd24
    apachectl=/app/httpd24/bin/apachectl               //修改的行
    httpd=${HTTPD-/app/httpd24/bin/httpd}              //修改的行
    pidfile=${PIDFILE-/app/httpd24/logs/httpd.pid}     //修改的行
    lockfile=${LOCKFILE-/var/lock/subsys/httpd24}      //修改的行
    
    chkconfig --add httpd24
    chkconfig httpd24 on
    service httpd24 start
    echo "/app/website/index.html" > /app/website/index.html
    
    (6)测试

    curl 192.168.136.129

    (四)CentOS 6环境下源码编译安装httpd-2.4(方法二)

    • 方法二只在编译安装过程与方法一不同,其他安装前准备工作和安装后的配置都与方法一相同

    • 方法二编译过程:一次性编译httpd及其依赖的apr, apr-util

    //将apr, apr-util的源码目录复制到httpd源码的srclib子目录下,注意需要重命名
    cd /usr/local/src/
    cp -r apr-1.6.2/ httpd-2.4.27/srclib/apr
    cp -r apr-util-1.6.0/ httpd-2.4.27/srclib/apr-util
    
    //执行configure脚本时不需再指定apr和apr-util的安装路径,代替以--with-included-apr
    cd httpd-2.4.27/
    ./configure --prefix=/app/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
    make -j 4 && make install
    

    相关文章

      网友评论

        本文标题:20170929 http服务和apache(三)

        本文链接:https://www.haomeiwen.com/subject/ulgiextx.html