zoukankan      html  css  js  c++  java
  • 记录启用HTTPS的全过程

      因为 https 采用 ssl 加密,所以部署 https 时需要申请证书,证书的作用就是对浏览器和Web服务器双方的身份验证。

    步骤1:申请证书

      我们采用Let's Encrypt签发的免费证书,虽然 Let’s Encrypt 只有90天的有效期,但是可以用Certbot自动部署工具,进入Certbot 主页,可以看到下图所示。

    Let's Encrypt 签发的证书有效期只有90天,甚至希望缩短到60天。有效期越短,泄密后可供监控的窗口就越短。为了支撑这么短的有效期,就必须自动化验证和签发。

    因为自动化了,长远而言,维护反而比手动申请再安装要简单。

    Certbot首页选择自己的web服务器类型和操作系统,我们使用的是 nginx+centos6.5,选择后就会自动跳转到install页面,

    注意:centos6.5默认的python版本是2.6.6安装Certbot需要python2.7版本,未来Certbot会对python3.x支持(Python 3.x support will hopefully be added in the future)

    遇到了升级python的问题,可以参考另一篇博客:如何升级python

    1.安装certbot

      wget https://dl.eff.org/certbot-auto

      chmod a+x certbot-auto

      certbot-auto

    执行certbot-auto命令会安装一些packages,创建虚拟环境,在创建虚拟环境的过程中,需要输入域名(提醒:用逗号或/或空格分割域名)、输入邮箱(用于急切的通知和找回丢失的key)、同意agreement、选择ssl.conf;看下面的图片

         

    2.申请证书

      申请证书有两种验证方式,一种是standalone,这种验证方式虽然也可以部署后,但是以后更新证书的时候需要重启 web 服务器;

      第二种是webroot,就是在网站根目录下生成一个文件,通过访问该文件来验证,不需要重启 web 服务器。

      第一种命令:./path/to/certbot-auto certonly --standalone -d example.com -d www.example.com

      第二种命令:./path/to/certbot-auto certonly --webroot -w /var/www/example -d example.com -d www.example.com -w /var/www/baidu -d baidu.com -d www.baidu.com (/var/www/example是网站的目录

    我用了第二种方法。执行第二种命令的过程中会有提醒,图片如下

      

    3.测试自动生成证书是否成功

      ./path/to/certbot-auto renew --dry-run

    4.添加定时任务自动更新证书

      Since Let's Encrypt certificates last for 90 days,所以我们还是写个定时任务吧

      crontab -u root -e (-u root 表示哪个用户的定时任务)

      添加:* * * */3 * ./path/to/certbot-auto renew --quiet (表示每3个月执行一次)

    步骤2:部署https

      修改nginx的配置文件(比如在/usr/local/nginx/conf/nginx.conf),添加如下代码,然后重启nginx

    # HTTPS server
    # 部署HTTPS
    server {
      listen 443 ssl;
      server_name www.example.com example.com;
      root /var/www/example;

      ssl on;

      ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
      ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

      ssl_session_cache shared:SSL:1m;
      ssl_session_timeout 5m;
      ssl_protocols TLSv1.2;(version1.2 is the only secure protocol version. 请参考SSL Labs: Increased Penalty When TLS 1.2 Is Not Supported

      ssl_ciphers HIGH:!aNULL:!MD5;
      ssl_prefer_server_ciphers on;

    }

      另外还可以加入如下代码实现80端口重定向到443,代码片段如下

    server {
      listen 80;
      server_name www.example.com example.com;
      #实现80端口重定向到443
      rewrite ^(.*) https://$server_name$1 permanent;(或return 301 https://www.example.com$request_uri

    }


    另外:安装成功的nginx如何添加未编译模块?

    在重启nginx后发生了错误,错误如下:

    nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:117  //说明缺少http_ssl_module模块
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

    解决方法如下:

    步骤1:查看nginx编译安装时的命令,安装了哪些模块和ngnix版本

      /usr/local/nginx/sbin/nginx -V

      会显示如下信息:

    nginx version: nginx/1.7.7
    built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
    configure arguments: --prefix=/usr/local/nginx --user=www --group=www

    步骤2:重新编译 nginx-1.7.7

      wget http://nginx.org/download/nginx-1.7.7.tar.gz

      tar zxvf nginx-1.7.7.tar.gz

      cd nginx-1.7.7

      //configure参数要和步骤1的configure arguments一模一样

      ./configure --prefix=/usr/local/nginx --with-http_ssl_module  --user=www --group=www

      make (没有make install)

    步骤3:备份nginx的二进制文件

      cp /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.bak

    步骤4:覆盖nginx的二进制文件

      cp objs/nginx   /usr/local/nginx/sbin/

      会提醒如下信息:

      cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y
      cp: 无法创建普通文件"/usr/local/nginx/sbin/nginx": 文本文件忙 (nginx二进制文件繁忙,可以停止nginx,再试一次就可以了

    步骤5:启动nginx

      service nginx start (或/etc/init.d/nginx start)

  • 相关阅读:
    【刷题】BZOJ 4059 [Cerc2012]Non-boring sequences
    【刷题】BZOJ 3745 [Coci2015]Norma
    【刷题】BZOJ 2734 [HNOI2012]集合选数
    【刷题】BZOJ 2287 【POJ Challenge】消失之物
    【刷题】BZOJ 2151 种树
    【刷题】BZOJ 2134 单选错位
    【刷题】BZOJ 1924 [Sdoi2010]所驼门王的宝藏
    【刷题】BZOJ 1823 [JSOI2010]满汉全席
    【刷题】BZOJ 1124 [POI2008]枪战Maf
    编程之美 ---> 1.2中国象棋将帅问题
  • 原文地址:https://www.cnblogs.com/zhangxiaoliu/p/6183558.html
Copyright © 2011-2022 走看看