设为首页 收藏本站
查看: 837|回复: 0

[经验分享] web服务之Apache实现的https访问

[复制链接]

尚未签到

发表于 2018-11-19 13:14:50 | 显示全部楼层 |阅读模式
  本文旨在实践httpd-2.4基于域名的虚拟主机配置,让指定用户访问站点状态信息,并为站点提供https服务。
  

  知识储备
  HTTPS协议
  HTTPS协议就是“HTTP协议”和“SSL/TLS”协议的结合,HTTP over SSL”或“HTTP over TLS”,对http协议的文本数据进行加密处理后,成为二进制形式传输.
  

  SSL会话简化过程
  (1) 客户端发送可供选择的加密方式,并向服务器请求证书;
  (2) 服务器端发送证书以及选定的加密方式给客户端;
  (3) 客户端取得证书并进行证书验正:
          如果信任给其发证书的CA机构:则
          (a) 验正证书来源的合法性;用CA的公钥解密证书上数字签名;
          (b) 验正证书的内容的合法性:完整性验正
          (c) 检查证书的有效期限;
          (d) 检查证书是否被吊销;
          (e) 证书中拥有者的名字,与访问的目标主机要一致;
  (4) 客户端生成临时会话密钥(对称密钥),并使用服务器端的公钥加密此数据发送给服务器,
  完成密钥交换;
  (5) 服务用此密钥加密用户请求的资源,响应给客户端;
  注意:SSL会话是基于IP地址创建;所以单IP的主机上,仅可以使用一个https虚拟主机;

  

  SSL/TLS协议模型
DSC0000.png

  

  环境准备
  1.操作系统及软件
  2台 Centos 7.2 x86_64
  httpd:httpd-2.4.6-40.el7.centos.x86_64
  2.IP地址
  172.16.52.51/16 web服务

  172.16.52.1/6  CA证书颁发机构

  2.提供2个基于域名的虚拟主机
  域名 www1.linux.com、www2.linux.com
  站点目录:/web/vhosts/www{1,2}

  访问日志:/var/log/httpd/www{1,2}/www{1,2}.access_log
  错误日志:/var/log/httpd/www{1,2}/www{1,2}.error_log
  3.输出www1.linux.com的状态信息,且要求只允许提供账号的用户访问
  4.www1不允许10.0.0.0/24网络中的主机访问
  5.为www2提供https服务。
  注意:关闭防火墙和selinux
  安装httpd并配置虚拟主机

  1.安装httpd
[root@study ~]# yum -y install httpd  2.注释主配置文件的DocumentRoot
[root@study ~]# grep "#DocumentRoot" /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"  3.配置虚拟主机
[root@study conf.d]# cat www1.conf

    ServerName www1.linux.com
    DocumentRoot "/web/vhosts/www1"
    CustomLog "/var/log/httpd/www1/www1.access_log" combined
    ErrorLog "/var/log/httpd/www1/www1.error_log"
   
        Options None
        AllowOverride None
        Require all granted
   
  www2的配置与此并无差别,不在不再赘述
  配置站点主页
[root@study ~]# cat /web/vhosts/www{1,2}/index.html
www1 websit http://www1.linux.com
www2 websit http://www2.linux.com   4.检查并启动服务  
[root@study conf.d]# httpd -t
Syntax OK
[root@study conf.d]# systemctl start httpd.service   5.测试
[root@study conf.d]# curl www1.linux.com
www1 websit http://www1.linux.com[root@study conf.d]# curl www2.linux.com
www2 websit http://www2.linux.com   6.输出www1.linux.com的状态信息,且要求只允许提供账号的用户访问
  6.1 编辑www1的虚拟主机文件:添加一个标签
[root@study conf.d]# cat www1.conf

ServerName www1.linux.com
DocumentRoot "/web/vhosts/www1"
CustomLog "/var/log/httpd/www1/www1.access_log" combined
ErrorLog "/var/log/httpd/www1/www1.error_log"
   
        Options None
        AllowOverride None
        Require all granted
   
   
        SetHandler server-status
        AuthType basic
        AuthName "For Adminstrator"
        AuthUserFile "/etc/httpd/conf/.htpasswd"
        Require user tom
   
  6.2 提供账号和密码存储文件
    [root@study conf]# htpasswd -c -m /etc/httpd/conf/.htpasswd tom
    New password:
    Re-type new password:
    Adding password for user tom   6.3 测试:

  浏览器输入http://www1.linux.com/server-status
DSC0001.png

DSC0002.png

  

   7. www1不允许10.0.0.0/24网络中的主机访问
  配置www1虚拟主机,在标签段内添加段

        Options None
        AllowOverride None
        
            Require all granted
            Require not ip 10.0.0.0/24
        
      

  实现虚拟主机 www2 https访问
  
  在172.16.52.1 CA服务器上:
  1. 配置CA证书颁发机构
      1.1 查看openssl相关文件
[root@CA ~]# cd /etc/pki/CA
[root@CA CA]# ll
total 16
drwxr-xr-x. 2 root root 4096 Jul 24 03:09 certs
drwxr-xr-x. 2 root root 4096 Jul 24 03:09 crl
drwxr-xr-x. 2 root root 4096 Jul 24 03:09 newcerts
drwx------. 2 root root 4096 Jul 24 03:09 private     
      1.2 使用openssl生成CA私钥
[root@CA CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
....................................................................................................................................................+++
.....................................................................................+++
e is 65537 (0x10001)  

      1.3 使用openssl给CA服务器生成自签名证书   
[root@CA CA]# openssl req -new -x509 -key private/cakey.pem -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) []:Beijing  
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:ca.test.com
Email Address []:ca@test.com  

      1.4 创建CA相关目录和文件,指定序列号起始数字
[root@CA CA]# touch index.txt  #新建索引文件
[root@CA CA]# touch serial     #建立序列号文件
[root@CA CA]# echo 01 > serial #写入起始序列号  web服务器创建申请证书
   2. 创建申请证书
      2.1 在web服务器配置目录创建ssl目录
    [root@study ~]# mkdir /etc/httpd/ssl   2.2 生成httpd 服务私钥
[root@study ~]# (umask 077;openssl genrsa -out /etc/httpd/ssl/httpd.key 1024)
Generating RSA private key, 1024 bit long modulus
..................++++++
..............................++++++
e is 65537 (0x10001)   
[root@study ~]# ll /etc/httpd/ssl/httpd.key
-rw------- 1 root root 887 Jul 14 15:29 /etc/httpd/ssl/httpd.key     
      2.3 生成证书签署请求文件
[root@study ~]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/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) []:Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:www1.linux.com
Email Address []:ops@admin.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:      2.4 把httpd申请证书发送到CA颁发机构上

[root@study ssl]# scp httpd.csr 172.16.52.1:/tmp
root@172.16.52.1's password:
httpd.csr                                  100%  696     0.7KB/s   00:00     

  2.5 在CA端为给客户端签名并颁发证书
[root@CA tmp]# openssl ca -in httpd.csr -out httpd.crt -days 3650
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: Dec 15 18:13:23 2015 GMT
            Not After : Dec 12 18:13:23 2025 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Beijing
            organizationName          = magedu
            organizationalUnitName    = ops
            commonName                = www1.linux.com
            emailAddress              = ops@admin.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                D0:C4:3B:E1:C4:59:25:D4:0E:DF:AF:83:9C:48:D6:A8:D9:CC:27:27
            X509v3 Authority Key Identifier:
                keyid:67:CC:F6:A8:E6:0B:73:CE:6C:A1:6D:B8:A6:99:1F:CA:7A:A3:D3:AB
Certificate is to be certified until Dec 12 18:13:23 2025 GMT (3650 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@CA tmp]# ll httpd*
    -rw-r--r-- 1 root root 3830 Dec 16 02:15 httpd.crt
    -rw-r--r-- 1 root root  696 Dec 16 02:12 httpd.csr  

   2.6 将生成的证书复制到web服务器上
    [root@ca tmp]# scp httpd.crt root@172.16.52.51:/etc/httpd/ssl      2.7 web服务器查看收到的证书
[root@study ssl]# ll
total 12
-rw-r--r-- 1 root root 3830 Dec 16  2015 httpd.crt
-rw-r--r-- 1 root root  696 Jul 14 16:01 httpd.csr
-rw------- 1 root root  887 Jul 14 16:00 httpd.key  

  3. web服务器配置ssl模块
      3.1装载mod_ssl
[root@study ssl]# yum -y install mod_ssl  

   3.2 修改ssl配置文件
  配置/etc/httpd/conf.d/ssl.conf
  DocumentRoot
[root@study ssl]# sed -n "/^DocumentRoot/p" /etc/httpd/conf.d/ssl.conf
    DocumentRoot "/web/vhosts/www2"  ServerName
[root@study ssl]# sed -n "/^ServerName/p" /etc/httpd/conf.d/ssl.conf
ServerName www2.linux.com:443  
[root@study ssl]# sed -n "186,190p" /etc/httpd/conf.d/ssl.conf

Options None
AllowOverride None
Require all granted
  

  SSLCertificateFile
  SSLCertificateKeyFile
[root@study conf.d]# sed -n '101p;109p' /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key  

     3.3 重启httpd服务
[root@study conf.d]# systemctl restart httpd.service
[root@study conf.d]# ss -tnlp|grep 443
LISTEN     0      128         :::443                     :::*                   users:(("httpd",pid=2475,fd=6),("httpd",pid=2474,fd=6),("httpd",pid=2473,fd=6),("httpd",pid=2472,fd=6),("httpd",pid=2471,fd=6),("httpd",pid=2469,fd=6))   3.5 浏览器访问

DSC0003.png

  

  

  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-637039-1-1.html 上篇帖子: apache2-linux 下篇帖子: nginx与apache的区别及优缺点比较
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表