本文首發地址:https://liluyang.mehtml
本文將以本站 http://liluyang.me 升級到 https://liluyang.me 的過程來分享一下 https
的原理,http
和 https
的區別,以及如何將網站從 http
升級到 https
。linux
https
?https
更安全。甚至爲了安全,一個專業可靠的網站, https
是必須的。 Firefox
和 Chrome
都計劃將沒有配置 SSL
加密的 https
網站標記爲不安全(貌似 Firefox50
已經這麼幹了),目前它們也正在聯合其餘相關的基金會與公司推進整個互聯網 https
化,如今你們訪問的一些主要的網站。如 Google
多年前就已經所有啓用 https
,國內的淘寶、搜狗、知乎、百度等等也全面 https
了。甚至 Google
的搜索結果也正在給予 https
的網站更高的排名和優先收錄權。nginx
擁有基礎的 Linux
系統知識,經常使用的 Shell
命令。git
若是你只是要測試 https
,請查看Nginx 容器教程web
若是你要在生產環境使用 https
,請接着往下看:docker
一個服務器,一個域名,而且已經正確的將域名指向了你的服務器,而且擁有服務器的 SSH
權限小程序
Tips:
須要注意的是若是是在國內阿里雲或者騰訊雲購買的域名請備案,不然即便你正確的配置了你的域名,可能也依然沒法訪問你的服務器,由於在國內備案是必須的。瀏覽器
簡單來講,http
是一個傳輸網頁內容的協議,好比你看到的 http
開頭的網站 http://www.163.com,其網頁上的文字、圖片、 CSS
、JS
等文件都是經過 http
協議傳輸到咱們的瀏覽器,而後被咱們看到。安全
而 https
能夠理解爲 HTTP over SSL/TLS
,好端端的 http
爲何須要 over SSL/TLS
呢,由於 http
是明文傳輸的,經過 http
協議傳輸的內容很容易被偷看和篡改,爲了安全(你確定不想被人偷看或者篡改網頁內容吧,好比網站銀行密碼什麼的。)就爲 http
協議再加上了一層 SSL/TLS
安全協議,因此就有了 https
。服務器
HTTP over SSL/TLS
字面意思就是帶 安全套接層
的 http
協議,心裏純潔的同窗也能夠理解爲 帶安全套的 http
,由於帶了安全套,因此固然會比較安全(/(ㄒoㄒ)/~~)。其中 SSL
是 Secure Sockets Layer
的縮寫,是 「安全套接層」 的意思。TLS
是 Transport Layer Security
的縮寫,是 傳輸層安全協議
的意思。SSL
和 TLS
是同一個東西的不一樣階段,理解爲同一個東西也行,都是安全協議就對了。
證書頒發機構(CA, Certificate Authority)即頒發數字證書的機構。是負責發放和管理數字證書的權威機構,並做爲電子商務交易中受信任的第三方,承擔公鑰體系中公鑰的合法性檢驗的責任。
Let's Encrypt
成立於 2015
年的數字證書認證機構,成立的目的就是爲了推行 https
協議在全世界的普及。主要贊助商包括電子前哨基金會、Mozilla基金會、Akamai以及思科。Let's Encrypt
旨在以自動化流程消除手動建立和安裝證書的複雜流程,並推廣使萬維網服務器的加密鏈接無所不在,爲安全網站提供免費的 SSL/TLS
證書。
說人話就是 Let’s Encrypt
就是一個能夠給咱們頒發授信證書的 CA
組織。
Let’s Encrypt
官方推出的授信證書生成客戶端。能夠在 Debian
,CentOS
等主流系統上運行
官方連接以下:https://certbot.eff.org/
要升級到 https
首先要得到 CA
簽發的安全證書文件,咱們選擇的證書籤發機構是 Let’s Encrypt
,目前 Let’s Encrypt
推薦的方法是安裝 Certbot
工具經過校驗服務器和域名的歸屬權方式來獲取證書
若是你的英文能力比較好,那麼請訪問 Certbot官網 根據官方教程來下載,官方教程更加細緻,出了問題也方便查看官方的第一手資料。固然你也能夠根據如下命令來安裝 Certbot
工具,這裏咱們以 debian9
系統爲例來安裝
# 下載 certbot,若是是 centOS 等內核,可使用 yum 命令 apt install certbot
certbot
安裝成功後,咱們有兩種模式來獲取證書
certbot certonly --webroot -w /root/docker_nginx/html -d liluyang.me -d www.liluyang.me
這個命令會爲 liluyang.com
和 www.liluyang.com
這兩個域名生成一個證書
--webroot
模式會在 /root/docker_nginx/html
中建立 .well-known
文件夾,這個文件夾裏面包含了一些驗證文件,certbot
會經過訪問 example.com/.well-known/acme-challenge
來驗證你的域名是否綁定的這個服務器。這個文件夾就是你 Nginx
下 index.html
所在的目錄,請自行根據本身服務器 Nginx
的配置狀況修改這個參數。
若是執行成功,命令行會大體輸出如下參數表示成功:
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/liluyang.me/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/liluyang.me/privkey.pem Your cert will expire on 2019-07-01. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
以上信息中包含了生成後的證書的位置,過時時間等信息,根據提示信息咱們打開 /etc/letsencrypt/live/
文件夾,檢查這個文件夾中是否有你生成的對應網站的證書文件,若是成功,應該以下圖所示:
這個命令在大多數狀況下均可以知足需求,若是失敗了,請看下一種模式。
若是你已經經過第一種模式生成證書成功,請跳過這裏,直接看下一章
可是有些時候咱們的一些服務並無根目錄,例如一些微服務,這時候使用 --webroot
就走不通了。certbot
還有另一種模式 --standalone
, 這種模式不須要指定網站根目錄,他會自動啓用服務器的 443
端口,來驗證域名的歸屬。咱們有其餘服務(例如nginx)佔用了 443
端口,就必須先中止這些服務,在證書生成完畢後,再啓用。
certbot certonly --standalone -d example.com -d www.example.com
證書生成完畢後,咱們能夠在 /etc/letsencrypt/live/
目錄下看到對應域名的文件夾,裏面存放了指向證書的一些快捷方式。
這時候咱們的第一輩子成證書已經完成了,接下來就是配置咱們的 web
服務器,啓用 HTTPS
。
若是生成失敗,而且你本身搜索以後又沒有能力解決,請在評論留言
若是生成失敗,而且你本身搜索以後又沒有能力解決,請在評論留言
若是生成失敗,而且你本身搜索以後又沒有能力解決,請在評論留言
證書生成生成後,咱們還要作最後的一步就是把證書的配置信息添加到 nginx
的 conf
目錄的 certs
文件夾中。我我的的 Nginx
是經過 docker
鏡像的方式運行的,而且我將本地的 /root/docker_nginx/conf
文件夾設置爲了 Nginx
的配置文件夾,因此如今咱們將證書 cert.pem
和 privkey.pem
拷貝到這個目錄。配置目錄以下
在 Nginx
的配置文件 nginx.conf
末尾添加如下配置,注意修改對應的配置參數,下面的配置模板僅供參考。
若是存在 conf.d/default.conf
文件請優先修改此文件,具體緣由請百度
# 443 https 配置 server { listen 443 ssl http2; server_name liluyang.me www.liluyang.me; ssl on; ssl_certificate /etc/nginx/certs/cert.pem; ssl_certificate_key /etc/nginx/certs/privkey.pem; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /usr/share/nginx/html; index index.html index.htm; } }
重啓 Nginx
到此爲止,咱們就已經成功的完成了 https
的配置。 如今打開個人博客 https://liluyang.com 就能夠在瀏覽器端看到標有 安全
的網站標識字樣了,以下圖:
若是是 docker
部署的 nginx
,最後重啓啓動 docker
時要將 443(https)
端口映射出來,不然沒法訪問服務器的 443(https)
端口的。
docker run \ --volume "$PWD/html":/usr/share/nginx/html \ --volume "$PWD/conf":/etc/nginx \ -p 80:80 \ -p 443:443 \ -d \ nginx
將 http
請求轉發到 https
請在配置文件夾中添加如下配置,而後重啓 Nginx
便可
server { listen 80; server_name liluyang.me www.liluyang.me; return 301 https://liluyang.me; }
細心的同窗可能會發現, https
證書的有效時間是很是短的,只有三個月,那麼每隔三個月咱們都要操做一次嗎?其實不是的,咱們能夠用過自動續訂的方式來刷新咱們的證書有效時間。
certbot renew --dry-run
若是測試成功,會生成以下提示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/liluyang.me.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cert not due for renewal, but simulating renewal for dry run Plugins selected: Authenticator webroot, Installer None Renewing an existing certificate Performing the following challenges: http-01 challenge for liluyang.me http-01 challenge for www.liluyang.me Waiting for verification... Cleaning up challenges - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - new certificate deployed without reload, fullchain is /etc/letsencrypt/live/liluyang.me/fullchain.pem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates below have not been saved.) Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/liluyang.me/fullchain.pem (success) ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates above have not been saved.) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal.
certbot renew -v
自動續訂也能夠用 certbot
來實現,自動續訂以後會更新 pem
文件到 /etc/letsencrypt/live/example.com/
。若是你的 nginx
配置沒有直接指向 /etc/letsencrypt/live/example.com/
文件夾,請記得將 pem
文件夾拷貝到你的配置文件夾中
certbot renew --quiet --no-self-upgrade
~
廣告:掃碼關注 知乎日報 小程序客戶端