1. 程式人生 > >免費申請 HTTPS 證書,開啟全站 HTTPS

免費申請 HTTPS 證書,開啟全站 HTTPS

作者:HelloGitHub-追夢人物

文中涉及的示例程式碼,已同步更新到 HelloGitHub-Team 倉庫

HTTP 報文以明文形式傳輸,如果你的網站只支援 HTTP 協議,那麼就有可能遭受到安全攻擊。你可以使用 Google 瀏覽器開啟一個 HTTP 協議網站,會發現 Chrome 在網址的左邊將這個網站標記為不安全。

HTTPS 為 HTTP 報文提供了一個加密傳輸的通道,這樣攻擊者就無法竊聽或者篡改傳輸的內容。要啟用 HTTPS,必須向一個可信任機構申請一個 HTTPS 證書。專業的證書申請需要收費,不過對於個人部落格網站來說,有很多免費的證書申請機構。比如 Let’s Encrypt,它提供了免費的證書申請服務,申請過程十分簡單,只需要執行幾條命令即可,而且證書到期後支援自動續期,可謂一勞永逸。接下來我們就是用 Let’s Encrypt 提供的工具來申請免費的 HTTPS 證書。

首先安裝 Let’s Encrypt 提供的證書申請工具。登入 https://certbot.eff.org/ 選擇我們部落格網站使用的伺服器軟體和作業系統。教程中以 Nginx 和 CentOS 7 為例:

首先安裝必要工具:

$ sudo yum -y install yum-utils
$ sudo sudo yum install -y certbot python2-certbot-nginx

certbot python2-certbot-nginx 是 Let’s Encrypt 提供的 HTTPS 證書申請的工具,python2-certbot-nginx 是專門針對 Nginx 的外掛,使得 Nginx 執行的服務申請證書更加簡單方便。

然後執行證書申請命令:

$ sudo certbot --nginx

注意

經測試,執行上述命令後有可能報 ImportError: No module named 'requests.packages.urllib3' 的錯誤,這是由於 requests 和 urlib3 版本過低所致(可以參考這個 issue 的討論),解決辦法是重灌它們,執行下面的命令:

$ pip uninstall requests
$ pip uninstall urllib3
$ yum remove python-urllib3
$ yum remove python-requests

然後重新安裝 certbot,由於它依賴上面兩個包,所以重灌時會一併裝上:

$ sudo yum install -y certbot python2-certbot-nginx

重新執行證書申請命令:sudo certbot --nginx

會有一系列互動式的提示,首先會讓你輸入郵箱,用於訂閱。然後輸入 a 同意他們的政策。

接著 certbot 會自動掃描出來域名,根據提示輸入想開啟 HTTPS 的域名標號:

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: django-blog-tutorial-v2-demo.zmrenwu.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1

然後 certbot 會做一個域名校驗,證明你對這個域名有控制權限。驗證通過後,Let's Encrypt 就會把證書頒發給你。

最後會提示你是否把 HTTP 重定向到 HTTPS,當然選擇是,這樣 certbot 會自動幫我們修改 Nginx 的配置,將 HTTP 重定向到 HTTPS,如果使用者使用 HTTP 協議訪問我們的部落格網站,就會重定向到 HTTPS 協議訪問,確保安全性。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/django-blog-tutorial-v2.conf

certbot 申請的證書只有 3 個月有效期,不過沒有關係,certbot 可以無限續期,我們增加一條 crontab 定時任務用來執行 certbot 自動續期任務,這樣一次申請,終生使用。

開啟 /etc/crontab,增加定時任務:

echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew" | sudo tee -a /etc/crontab > /dev/null

這裡配置每天 12 點執行自動續期命令。

由於全站開啟了 HTTPS,因此需要把網站中非 HTTPS 的內容(比如通過 HTTP 協議請求的外部資源)改為 HTTPS,我們的部落格中目前有一處引入外部圖示庫的樣式檔案是以 HTTP 協議引入的,需要改為 HTTPS:

base.html

<link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">

以上,簡單幾步,就開啟了全站 HTTPS。


『講解開源專案系列』——讓對開源專案感興趣的人不再畏懼、讓開源專案的發起者不再孤單。跟著我們的文章,你會發現程式設計的樂趣、使用和發現參與開源專案如此簡單。歡迎留言聯絡我們、加入我們,讓更多人愛上開源、貢獻開源