做者:HelloGitHub-追夢人物css
文中涉及的示例代碼,已同步更新到 HelloGitHub-Team 倉庫html
HTTP 報文以明文形式傳輸,若是你的網站只支持 HTTP 協議,那麼就有可能遭受到安全攻擊。你能夠使用 Google 瀏覽器打開一個 HTTP 協議網站,會發現 Chrome 在網址的左邊將這個網站標記爲不安全。python
HTTPS 爲 HTTP 報文提供了一個加密傳輸的通道,這樣攻擊者就沒法竊聽或者篡改傳輸的內容。要啓用 HTTPS,必須向一個可信任機構申請一個 HTTPS 證書。專業的證書申請須要收費,不過對於我的博客網站來講,有不少免費的證書申請機構。好比 Let’s Encrypt,它提供了免費的證書申請服務,申請過程十分簡單,只須要運行幾條命令便可,並且證書到期後支持自動續期,可謂一勞永逸。接下來咱們就是用 Let’s Encrypt 提供的工具來申請免費的 HTTPS 證書。nginx
首先安裝 Let’s Encrypt 提供的證書申請工具。登陸 https://certbot.eff.org/ 選擇咱們博客網站使用的服務器軟件和操做系統。教程中以 Nginx 和 CentOS 7 爲例:git
首先安裝必要工具:github
$ 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 運行的服務申請證書更加簡單方便。web
而後運行證書申請命令:django
$ 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。
『講解開源項目系列』——讓對開源項目感興趣的人再也不畏懼、讓開源項目的發起者再也不孤單。跟着咱們的文章,你會發現編程的樂趣、使用和發現參與開源項目如此簡單。歡迎留言聯繫咱們、加入咱們,讓更多人愛上開源、貢獻開源~