+++++++httpd-2.4基础特性及SSL,访问控制,MPM[DSO],status

╰+攻爆jí腚メ 2022-04-25 05:14 218阅读 0赞

HTTP基本特性

  • 0.9文本,method
  • 1.0非持久, method,mime,弱缓存
  • 1.1持久
  • 2.0mime, method,引入spdy优势,缓存加强
  • 2.42.2新特性:

    1、 MPM非编译为核心: httpd -M查看

    2、 event在2.4生产环境

    3、 支持毫秒级别的保持连接(KeepAlive Off KeepAliveTimeout 0.01?)

    4、 虚拟主机直接配置,不需要(#NameVirtualHost *:80)

    5、async IO(由内核决定什么时候写入磁盘: async)

    6、 每个模块及每个目录各自独立的日志级别

    7、 每请求配置;

    8、 增强版的表达式分析器;正则表达式解析速度更快

    9、 配置文件中自定义变量

  • 2.4引入模块

    1. mod_proxy_fcgi //httpd以cgi协议与php结合
    2. mod_ratelimit //支持速率限制
    3. mod_remoteip //远端ip地址的控制
  • 2.4修改配置机制

    1. 访问控制指令变化:
    2. Order, Deny from, Allow from -->
    3. <RequireAll>
    4. Require [not] ip|host
    5. Require all granted
    6. Require all deny
    7. </RequireAll>

安装httpd-2.4

httpd2.4依赖:apr-1.4+, apr-util-1.4+, [apr-icon]

  • aprapache portable[环境] runtime[运行]不同OS平台提供的库接口不同,只有调用POSIX规范的库才能跨平台编译,apr运行环境能将库的不同的差异给抹除[即使开发调用的库不支持POSIX规范也能跨平台运行],就能实现不同OS平台运行httpd程序。

CentOS 6安装编译安装httpd-2.4

CentOS 6默认的apr, apr-util程序的版本:1.3,需要编译安装httpd-2.4。

不建议在CentOS 6上使用httpd-2.4,对大规模布署不方便,除非己定制RPM包

  • 开发环境

    1. # yum -y groupinstall "Development Tools" "Server Platform Development"
  • 获取源码ASF

    apr-1.6.2.tar.bz2

    apr-util-1.6.0.tar.gz

    httpd-2.4.27.tar.bz2

  • 编译apr-1.4+

    1. # tar xf apr-1.6.2.tar.bz2
    2. # ./configure --prefix=/usr/local/apr
    3. # make && make install
    4. (--prefix=安装路径,方便卸载,避免覆盖已有的程序)
  • 编译apr-util-1.4+

    1. # tar xf apr-util-1.6.0.tar.gz
    2. # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
    3. # make && make install
    4. (--with-<>=/path/to/somewhere: 针对于哪个包来安装)
    5. xml/apr_xml.c:35:19: error: expat.h: No such file or directory
    6. xml/apr_xml.c:66: error: expected specifier-qualifier-list before XML_Parser
    7. # yum -y install expat-devel
    8. **注意如果出现错误,需要在安装程序包后,删除apr-util-1.6.0目录,重新编译
  • 编译httpd-2.4

    • 确认安装目录内的文件位置

      1. # ls /usr/local/httpd24/
      2. bin build cgi-bin error htdocs icons include logs man manual modules
    • 导出PATH环境变量

      位置:[全局]/etc/profile.d/*.sh, [个人]~/.bash_profile

      1. # vim /etc/profile.d/httpd24.sh
      2. export PATH=/usr/local/httpd24/bin:$PATH
      3. # . /etc/profile.d/httpd24.sh
    • 导出库文件

      位置:/etc/ld.so.conf.d/*.conf

      1. # vim /etc/ld.so.conf.d/httpd24.conf
      2. /usr/local/httpd24/lib
      3. # ldconfig [-v]
    • 导出头文件

      位置:/usr/include/NAME

      1. # ln -sv /usr/local/httpd24/include /usr/include/httpd24
    • 导出man手册

      位置:/etc/man.conf

      1. # vim /etc/man.config
      2. MANPATH /usr/man
      3. MANPATH /usr/share/man
      4. MANPATH /usr/local/man
      5. MANPATH /usr/local/share/man
      6. MANPATH /usr/X11R6/man
      7. MANPATH /usr/local/httpd24/man //添加的条目
    • 添加用户apache

      1. * 如果已经存在apache用户,则不用添加...
      2. # groupadd -r apache
      3. # useradd -r -g apache apache
    • 安装依赖

      1. # yum -y install pcre-devel
    • 编译

      1. # tar xf httpd-2.4.27.tar.bz2
      2. # ./configure --prefix=/usr/local/httpd24 --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-pcre --with-zlib --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event
      3. # make && make install
      4. --prefix: 安装路径。 方便卸载,避免覆盖已有的程序
      5. --with-<>: 针对于哪个包来安装
      6. --sysconfdir=<>: 避免覆盖,冲突
      7. --enable-so DSO机制
      8. --enable-ssl SSL
      9. --enable-rewrite URL重写
      10. --with-pcre Perl扩展正则表达式
      11. --with-zlib zlib提供多种压缩库,支持压缩传输
      12. --enable-modules DSO
      13. --enable-mpm-shared MPM DSO
      14. --with-mpm 默认的MPM
    • 测试启动

      1. 查看80是否被占用,Socket会独占端口
      2. # ss -tnl
      3. 启用服务
      4. # apachectl start
      5. # hash
      6. /usr/local/httpd24/bin/apachectl //确保启动是2.4httpd
      7. 测试
      8. # iptables -F
      9. # setenforce 0

      wKioL1nNHLezWhFdAAAbHQ_qh2o818.png

CentOS 7安装httpd-2.4

CentOS 7 默认安装httpd-2.4。

  • rpm安装

    1. # yum -y install httpd
  • CentOS 7 对应配置文件

    • 配置路径:

      1. /etc/httpd/conf/httpd.conf
      2. /etc/httpd/conf.d/*.conf
    • 欢迎页:

      1. /etc/httpd/conf.d/welcome.conf
    • 模块路径:

      1. /etc/httpd/conf.modules.d/*.conf
      2. /usr/lib64/httpd/modules/mod_mpm_event|prefork|worker

httpd-2.4基本应用

  • MPM切换及查看

    • 查看编译进核心的模块:

      1. # httpd -l
    • 查看动态装载的模块及编译进核心的模块:

      1. # httpd -M
      2. mpm_event_module (shared)
    • 切换模块

      1. 备份配置:
      2. # cp -v /etc/httpd24/httpd.conf{,.bak}
      3. 进入配置,注释event.so,起动preforkworker
      4. #LoadModule mpm_event_module modules/mod_mpm_event.so
      5. LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
      6. #LoadModule mpm_worker_module modules/mod_mpm_worker.so
      7. 退出配置后:
      8. # httpd -t
      9. # apachectl restart
    • 查看模块

      1. # httpd -M
      2. mpm_prefork_module (shared)
  • 虚拟主机

    • 准备DocumentRoot及默认主页面

      1. # mkdir -p /vhosts/www
      2. # echo "www.mykernel.cn" > /vhosts/www/index.html
    • 添加配置

      1. 备份配置:
      2. # cp -v /etc/httpd24/extra/httpd-vhosts.conf{,.bak}
      3. 仅在配置添加以下内容
      4. # vim /etc/httpd24/extra/httpd-vhosts.conf
      5. <VirtualHost *:80>
      6. ServerName www.mykernel.cn
      7. DocumentRoot /vhosts/www
      8. ErrorLog logs/www.err
      9. CustomLog logs/www.access combined
      10. <Directory "/vhosts/www">
      11. Options None
      12. AllowOverride None
      13. Require all granted
      14. </Directory>
      15. <Location /server-status>
      16. SetHandler server-status
      17. Require all granted
      18. </Location>
      19. #ExtendedStatus On
      20. </VirtualHost>
    • 测试并访问

      1. # httpd -t
      2. # apachectl restart
      3. windows的解析库中添加以下内容(C:\Windows\System32\drivers\etc
      4. 172.16.100.1 www.mykernel.cn

      wKioL1nNJIiBT6JxAAAVy0IyXnk807.pngwKiom1nNJMmQZ4KkAABKiWx3GKQ782.png

    • 启用虚拟主机

      1. # vim /etc/httpd24/httpd.conf
      2. 注释中心主机
      3. #DocumentRoot "/usr/local/httpd24/htdocs"
      4. # Virtual hosts
      5. Include /etc/httpd24/extra/httpd-vhosts.conf
    • 配置虚拟主机
  • 访问控制

    • 本机127.0.0.1访问

      1. 安装文本协议浏览工具
      2. # yum -y install curl elinks telnet
      3. # curl 172.16.100.1
      4. www.mykernel.cn
      5. # elinks --dump http://172.16.100.1
      6. www.mykernel.cn
      7. # telnet 172.16.100.1 80
      8. Trying 172.16.100.1...
      9. Connected to 172.16.100.1.
      10. Escape character is '^]'.
      11. GET / HTTP/1.1
      12. Host: 172.16.100.1
      13. HTTP/1.1 200 OK
      14. Date: Sun, 10 Sep 2017 00:37:49 GMT
      15. Server: Apache/2.4.27 (Unix)
      16. Last-Modified: Sun, 10 Sep 2017 00:20:44 GMT
      17. ETag: "10-558cac7f1211e"
      18. Accept-Ranges: bytes
      19. Content-Length: 16
      20. Content-Type: text/html
      21. www.mykernel.cn
    • 在配置文件中修改为禁止本机访问

      1. # vim /etc/httpd24/extra/httpd-vhosts.conf
      2. <RequireAll>
      3. Require all granted
      4. Require not ip 172.16.100.1
      5. </RequireAll>
    • 测试

      1. # httpd -t
      2. # apachectl restart
      3. # curl -I 172.16.100.1
      4. HTTP/1.1 403 Forbidden
      5. Date: Sun, 10 Sep 2017 00:45:04 GMT
      6. Server: Apache/2.4.27 (Unix)
      7. Content-Type: text/html; charset=iso-8859-1
      8. # elinks --dump 172.16.100.1
      9. Forbidden
      10. You don't have permission to access / on this server.
      11. # telnet 172.16.100.1 80
      12. Trying 172.16.100.1...
      13. Connected to 172.16.100.1.
      14. Escape character is '^]'.
      15. GET / HTTP/1.1
      16. Host: 172.16.100.1
      17. HTTP/1.1 403 Forbidden
      18. Date: Sun, 10 Sep 2017 00:46:02 GMT
      19. Server: Apache/2.4.27 (Unix)
      20. Content-Length: 209
      21. Content-Type: text/html; charset=iso-8859-1
      22. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
      23. <html><head>
      24. <title>403 Forbidden</title>
      25. </head><body>
      26. <h1>Forbidden</h1>
      27. <p>You don't have permission to access /
      28. on this server.<br />
      29. </p>
      30. </body></html>
  • mod_ssl

    • 自建CA

      1. # dir=/etc/pki/CA
      2. # touch $dir/index.txt
      3. # echo "01" > $dir/serial
      4. # (umask 077;openssl genrsa -out $dir/private/cakey.pem 2048)
      5. # openssl req -new -x509 -key $dir/private/cakey.pem -out $dir/cacert.pem -days 7300
      6. SC, ChengDu, ChengDu, MageEdu Ltd, Ops, ca.mykernel.cn, caadmin@mykernel.cn)
    • 生成请求

      1. # mkdir /etc/httpd24/ssl && cd /etc/httpd24/ssl
      2. # (umask 077;openssl genrsa -out httpd.key 2048)
      3. # openssl req -new -key httpd.key -out httpd.csr -days 7300
      4. SC, ChengDu, ChengDu, MageEdu Ltd, Ops, www.mykernel.cn, admin@mykernel.cn)
    • 本机签署

      1. # openssl ca -in httpd.csr -out $dir/certs/www.mykernel.cn.crt -days 365
    • 获取证书

      1. # cp -a /etc/pki/CA/certs/www.mykernel.cn.crt .
    • 启用mod_ssl

      1. # vim /etc/httpd24/httpd.conf
      2. LoadModule ssl_module modules/mod_ssl.so //2.4默认不启用
      3. LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
      4. # Secure (SSL/TLS) connections
      5. Include /etc/httpd24/extra/httpd-ssl.conf
    • 启用ssl虚拟主机

      1. # cp -v /etc/httpd24/extra/httpd-ssl.conf{,.bak}
      2. # vim /etc/httpd24/extra/httpd-ssl.conf
      3. <VirtualHost *:443>
      4. DocumentRoot "/vhosts/www"
      5. ServerName www.mykernel.cn
      6. SSLCertificateFile "/etc/httpd24/ssl/www.mykernel.cn.crt
      7. SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key
    • 测试语法

      1. # httpd -t
      2. # apachectl restart //需要监听新的端口
    • 测试ssl会话是否能建立

      1. Linux主机:
      2. CA所在主机的/etc/hosts文件中添加如下行:
      3. 172.16.100.1 www.mykernel.cn
      4. 测试
      5. # openssl s_client -connect www.mykernel.cn:443 -CAfile /etc/pki/CA/cacert.pem
      6. Certificate chain
      7. Server certificate
      8. subject
      9. issuer
      10. No client certificate CA names sent
      11. New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
      12. Compression: NONE
      13. Expansion: NONE
      14. SSL-Session: TLSv1.2
      15. Windows主机:
      16. windowsC:\Windows\System32\drivers\etc文件中添加
      17. 172.16.100.1 www.mykernel.cn
      18. CA证书[公钥]导入受信任的颁发机构,在浏览器中的输入:
      19. https://www.mykernel.cn即可访问
  • 服务脚本

    • bash shell编程的资深专家,以rpm,为蓝本,修改修改即可

      1. # cp -v /etc/rc.d/init.d/httpd{,24}
    • 修改

      1. # vim /etc/rc.d/init.d/httpd24
      2. apachectl=/usr/local/httpd24/bin/apachectl
      3. httpd=${HTTPD-/usr/local/httpd24/bin/httpd}
      4. prog=httpd
      5. pidfile=${PIDFILE-/var/run/httpd/httpd24.pid}
      6. lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
      7. # vim /etc/httpd24/httpd.conf
      8. PidFile "/var/run/httpd/httpd24.pid"
    • 测试使用

      1. # chkconfig --add httpd24
      2. # killall httpd
      3. # service httpd24 {start|stop|restart|status}

#

本文转自 lccnx 51CTO博客,原文链接:http://blog.51cto.com/sonlich/1969602,如需转载请自行联系原作者

发表评论

表情:
评论列表 (有 0 条评论,218人围观)

还没有评论,来说两句吧...

相关阅读

    相关 访问控制权限

    一、包的定义及导入 定义:package包名称; 所谓的包实际上就是一个文件夹,一个\.class文件要保存在一个文件夹中,既然包本身就是一个文件夹,所以在java编译之灵