作者:HelloGitHub-追夢人物
HTTP 報文以明文形式傳輸,如果你的網(wǎng)站只支持 HTTP 協(xié)議,那么就有可能遭受到安全攻擊。你可以使用 google 瀏覽器打開一個 HTTP 協(xié)議網(wǎng)站,會發(fā)現(xiàn) Chrome 在網(wǎng)址的左邊將這個網(wǎng)站標(biāo)記為不安全。
HTTPS 為 HTTP 報文提供了一個加密傳輸?shù)耐ǖ溃@樣攻擊者就無法竊聽或者篡改傳輸?shù)膬?nèi)容。要啟用 HTTPS,必須向一個可信任機(jī)構(gòu)申請一個 HTTPS 證書。專業(yè)的證書申請需要收費,不過對于個人博客網(wǎng)站來說,有很多免費的證書申請機(jī)構(gòu)。比如 Let’s Encrypt,它提供了免費的證書申請服務(wù),申請過程十分簡單,只需要運行幾條命令即可,而且證書到期后支持自動續(xù)期,可謂一勞永逸。接下來我們就是用 Let’s Encrypt 提供的工具來申請免費的 HTTPS 證書。
首先安裝 Let’s Encrypt 提供的證書申請工具。登錄 https://certbot.eff.org/ 選擇我們博客網(wǎng)站使用的服務(wù)器軟件和操作系統(tǒng)。教程中以 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 運行的服務(wù)申請證書更加簡單方便。
然后運行證書申請命令:
$ sudo certbot --nginx
注意
經(jīng)測試,運行上述命令后有可能報 ImportError: No module named 'requests.packages.urllib3' 的錯誤,這是由于 requests 和 urlib3 版本過低所致(可以參考這個 issue[2] 的討論),解決辦法是重裝它們,運行下面的命令:
$ pip uninstall requests $ pip uninstall urllib3 $ yum remove python-urllib3 $ yum remove python-requests
然后重新安裝 certbot,由于它依賴上面兩個包,所以重裝時會一并裝上:
$ sudo yum install -y certbot python2-certbot-nginx
重新執(zhí)行證書申請命令:sudo certbot --nginx
會有一系列交互式的提示,首先會讓你輸入郵箱,用于訂閱。然后輸入 a 同意他們的政策。
接著 certbot 會自動掃描出來域名,根據(jù)提示輸入想開啟 HTTPS 的域名標(biāo)號:
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 會做一個域名校驗,證明你對這個域名有控制權(quán)限。驗證通過后,Let's Encrypt 就會把證書頒發(fā)給你。
最后會提示你是否把 HTTP 重定向到 HTTPS,當(dāng)然選擇是,這樣 certbot 會自動幫我們修改 Nginx 的配置,將 HTTP 重定向到 HTTPS,如果用戶使用 HTTP 協(xié)議訪問我們的博客網(wǎng)站,就會重定向到 HTTPS 協(xié)議訪問,確保安全性。
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 個月有效期,不過沒有關(guān)系,certbot 可以無限續(xù)期,我們增加一條 crontab 定時任務(wù)用來執(zhí)行 certbot 自動續(xù)期任務(wù),這樣一次申請,終生使用。
打開 /etc/crontab,增加定時任務(wù):
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 點執(zhí)行自動續(xù)期命令。
由于全站開啟了 HTTPS,因此需要把網(wǎng)站中非 HTTPS 的內(nèi)容(比如通過 HTTP 協(xié)議請求的外部資源)改為 HTTPS,我們的博客中目前有一處引入外部圖標(biāo)庫的樣式文件是以 HTTP 協(xié)議引入的,需要改為 HTTPS:
base.html ? <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
以上,簡單幾步,就開啟了全站 HTTPS。
參考資料
[1]HelloGitHub-Team 倉庫: https://github.com/HelloGitHub-Team/HelloDjango-blog-tutorial
[2]issue: https://github.com/certbot/certbot/issues/5104
『講解開源項目系列』——讓對開源項目感興趣的人不再畏懼、讓開源項目的發(fā)起者不再孤單。跟著我們的文章,你會發(fā)現(xiàn)編程的樂趣、使用和發(fā)現(xiàn)參與開源項目如此簡單。歡迎留言聯(lián)系我們、加入我們,讓更多人愛上開源、貢獻(xiàn)開源~