介紹
Let's Encrypt是一個(gè)于2015年三季度推出的數(shù)字證書(shū)認(rèn)證機(jī)構(gòu)绒净,旨在以自動(dòng)化流程消除手動(dòng)創(chuàng)建和安裝證書(shū)的復(fù)雜流程,并推廣使萬(wàn)維網(wǎng)服務(wù)器的加密連接無(wú)所不在,為安全網(wǎng)站提供免費(fèi)的SSL/TLS證書(shū)。證書(shū)有90天的有效期软舌,適合個(gè)人使用或者臨時(shí)使用,不用再忍受自簽發(fā)證書(shū)不受瀏覽器信賴(lài)的提示牛曹。Certbot是一款易于使用的客戶(hù)端佛点,實(shí)現(xiàn)Web服務(wù)器自動(dòng)提取和部署SSL/TLS證書(shū)。
前提
- Ubuntu 16.04操作系統(tǒng)黎比, sudo非root用戶(hù)權(quán)限
- 已注冊(cè)并能成功解析的域名超营,此處以
example.com
為例子 - 已為域名解析服務(wù)器設(shè)置如下DNS記錄
- 一條
example.com
指向你服務(wù)器公網(wǎng)IP地址的A記錄 - 一條
www.example.com
指向你服務(wù)器公網(wǎng)IP地址的A記錄
- 一條
- 已安裝
Nginx
步驟一:安裝Certbot
首先,增加安裝庫(kù)阅虫。
$ sudo add-apt-repository ppa:certbot/certbot
回車(chē)接受演闭,然后更新軟件列表。
$ sudo apt-get update
最后颓帝,安裝Certbot米碰。
$ sudo apt-get install python-certbot-nginx
Certbot現(xiàn)在可以使用了窝革,不過(guò)你要先配置好Nginx。
步驟二:配置Nginx
Certbot在Nginx配置文件中找到server
代碼塊后可以自動(dòng)配置SSL见间。如果你剛安裝Nginx聊闯,可以按照下面設(shè)置工猜,使用nano
或任何你喜歡的編輯器打開(kāi)配置文件:
$ sudo nano /etc/nginx/sites-available/default
找到server_name
那一行米诉,代替為如下內(nèi)容:
...
server_name example.com www.example.com;
...
保存退出,檢驗(yàn)有沒(méi)有語(yǔ)法錯(cuò)誤:
$ sudo nginx -t
如果語(yǔ)法無(wú)誤篷帅,加載Nginx的新配置:
$ sudo systemctl reload nginx
步驟三:獲得SSL證書(shū)
通過(guò)不同命令選項(xiàng)史侣,Certbot可以用不同的方法來(lái)獲得SSL證書(shū)。Certbot的Nginx插件將自動(dòng)配置Nginx并在有必要的時(shí)候自動(dòng)加載它:
$ sudo certbot --nginx -d example.com -d www.example.com
這個(gè)命令的--nginx
選項(xiàng)指明加載nginx插件魏身,--d
指明我們要認(rèn)證的域名惊橱。
如果你第一次運(yùn)行Certbot,會(huì)提示輸入郵件地址箭昵,接受服務(wù)協(xié)議税朴,之后,Certbot會(huì)與Let's Encrypt服務(wù)器連接家制,然后測(cè)試要認(rèn)證的域名是否在你的控制下正林。
如果以上過(guò)程運(yùn)行順利,Certbot接下來(lái)會(huì)詢(xún)問(wèn)是否把所有HTTP的訪問(wèn)轉(zhuǎn)向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):
選擇后回車(chē)觅廓。配置會(huì)生成并更新,Nginx自動(dòng)加載新配置文件涵但。Certbot會(huì)提示你配置成功以及證書(shū)的存儲(chǔ)位置:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/example.com/fullchain.pem. Your cert will
expire on 2017-10-23. 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
如果現(xiàn)在訪問(wèn)你的網(wǎng)站杈绸,你會(huì)發(fā)現(xiàn)瀏覽器地址欄左邊已經(jīng)有了個(gè)綠色鎖標(biāo)識(shí)符,表示你的網(wǎng)站是安全的矮瘟。
步驟四:自動(dòng)更新證書(shū)
Let's Encrypt's證書(shū)有效期只有90天瞳脓,看到許多網(wǎng)上的文章都在crontab
中配置自動(dòng)更新證書(shū)任務(wù)。這次(2018年6月)在Ubuntu 16.04安裝Certbot(版本:0.22.2)澈侠,發(fā)現(xiàn)安裝后自動(dòng)創(chuàng)建了一個(gè)定時(shí)任務(wù)篡殷,位置在:/etc/cron.d/certbot
:
# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc. Renewal will only occur if expiration
# is within 30 days.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew
部分內(nèi)容參考自這篇文章。