Let's Encrypt 的服務相信很多人都知道了,我個人認為這是最好的免費 SSL 服務。下面內容即使如何在自己的網站上使用 Let's Encrypt 實現 SSL.
前提條件
-
自己擁有一個域名,備案了的域名最好。
-
Ubuntu 16.04 的服務器,你可以到騰訊雲或者阿里雲購買這樣的雲主機。
-
將域名自己解析到你的 Ubuntu 16.04 的雲主機 ip;
注意如果你的域名沒備案,在購買主機的時候,推薦大家購買騰訊雲或者阿里雲境外的主機。
1.安裝 Certbot
Certbot 其實就是維護 Let's Encrypt 的 Package,在 Ubuntu 16.04 上,我們可以這樣安裝:
首先安裝 Nginx:
sudo apt-get install nginx
以上過程,等待安裝完畢就好。
然后添加 package repository
sudo add-apt-repository ppa:certbot/certbot
這個過程中,等待驗證完畢,按下 ENTER
就好。然后更新 apt 源數據:
sudo apt-get update
最后,安裝 Certbot 的 Nginx package:
sudo apt-get install python-certbot-nginx
2.配置 Nginx
安裝完 Nginx 和 Certbot 之后,需要簡單配置 Nginx 以便於 Let's Encrypt 能起作用:
sudo vi /etc/nginx/sites-available/default
使用 vi 編輯器打開 /etc/nginx/sites-available/default
,可以直接刪除里面的所有內容,然后再添加下面的配置:
server { listen 80; listen [::]:80; server_name your-domain.com www.your-domain.com; }
注意這里的
your-domain.com
換成你自己的域名。
保存退出之后,執行以下命令來檢測 Nginx 的配置文件是否有錯:
sudo nginx -t
如果出現類似 syntax ok 這樣的語句,就說明 Nginx 的配置文件沒有問題。之后就是重新加載 Nginx 的配置文件了:
sudo service nginx reload
3.簽發 SSL 證書
前面的兩大步配置完成,就可以使用 Let's Encrypt 簽發 SSL 證書了:
sudo certbot --nginx -d your-domian.com -d www.your-domain.com
注意這里的
your-domain.com
換成你自己的域名。
如果你第一次運行 certbot
命令的話,你需要在彈出的窗口輸入你的郵箱地址還有需要接受 Let's Encrypt 的協議,這樣之后,你大概會看到下面的文字:
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):
在上面這里選擇 1
或者 2
,我推薦大家直接選擇 2
,因為這個會直接將你的 nginx 文件配置好並且是會將 http 跳轉到 https 的。
選擇完畢之后,等待 SSL 生成完畢,就會有類似這樣的輸出:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/your-domain.com/fullchain.pem. Your cert will expire on 2017-12-29. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - 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. - 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/your-domain.com/fullchain.pem
路徑很重要,就是你的 SSL 證書路徑。
其實到這里,訪問
your-domain.com
應該就可以看到 https 的效果了。
4.自動更新證書
因為 Let's Encrypt 簽發的 SSL 證書有效期只有 90 天,所有在過期之前,我們需要自動更新 SSL 證書,而如果你使用最新的 certbot
的話,Let's Encrypt 會幫你添加自動更新的腳本到 /etc/cron.d
里,你只需要去檢測一下這個命令是否生效就OK!
sudo certbot renew --dry-run
如果這個命令你沒看到什么 error 的話,那就是沒什么問題了。
總結
本文主要是介紹了在 Ubuntu 16.04 的雲服務器上,使用 Nginx 作為服務器軟件情況下,如何配置 Let's Encrypt 的 SSL 證書,使得網站得以支持 https
的過程。內容很淺,但是我覺得還是挺有用的。