記錄啟用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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM