作者:劉剛梳虽,叩丁狼高級講師垒手。原創(chuàng)文章绪撵,轉載請注明出處姆蘸。
Let's Encrypt 是國外一個公共的免費 SSL 項目官扣,該項目是為了普及 HTTPS 而發(fā)起的翅敌,目前已經被 Mozilla、Google惕蹄、Microsoft 和 Apple 等主流瀏覽器支持蚯涮,對 HTTPS 技術的普及有著巨大貢獻。隨著 HTTPS 的普及卖陵,Let's Encrypt 已經成為全球最受歡迎的免費 SSL 證書簽發(fā)機構遭顶。
注意事項
- Let's Encrypt 的基礎證書只提供了數據加密,不驗證身份泪蔫,從而也無法證明網站的所有者棒旗,雖然已經滿足絕大部分應用場景,但若涉及核心功能時還是需要進行身份驗證。
- Let's Encrypt 簽發(fā)的證書一次的有效期為 3 個月铣揉,需要定期更新證書(官方推薦兩個月左右更新一次饶深,本文有提供自動更新方案)。
安裝 EPEL 倉庫
Let's Encrypt 的簽發(fā)證書工具在 EPEL 倉庫中才能找到逛拱,所以如果沒有安裝 EPEL 倉庫的話要先安裝一下敌厘。
# 查詢是否已安裝 epel 倉庫
rpm -qa epel-release
# 若沒有安裝再安裝 epel 倉庫
sudo yum install -y epel-release
安裝簽發(fā)證書工具
用 Certbot 工具申請 Let's Encrypt 證書,要先安裝該工具:
sudo yum install certbot-nginx
如果報以下錯誤:
ImportError:No module named 'requests.packages.urllib3'
執(zhí)行以下命令解決:
# 如果沒有 pip 可使用 `sudo yum install python-pip` 安裝
pip install --upgrade --force-reinstall 'requests==3.6.0' urllib3
網站運行在申請 SSL 證書的服務器上
這邊將申請證書分為兩種情況朽合,正常情況下我們使用第一種就好了俱两,也就是網站運行在申請 SSL 證書的服務器上。另外就是比如在 A 機器上申請證書曹步,但網站運行在 B 機器上宪彩,當然如果你是用 Docker 跑的 Nginx 也得用第二種方式。
第一種方式有以下幾個條件:
i. 你要申請 SSL 證書的域名已經指向到申請證書服務器的 ip
ii. 你的 Web 服務器需要使用 Nginx(在本例子中讲婚,可以在官網查看其它服務器的方式)
iii. Nginx 配置完成尿孔,并且已經綁定了你要申請 SSL 證書的域名
具備以上條件,就可以執(zhí)行以下操作啦:
# 申請 SSL 證書磺樱,certonly 表示只生成證書纳猫,不做額外操作
sudo certbot --nginx certonly
certbot 會自動查找當前 Nginx 上的虛擬主機,并提示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org
Which names would you like to activate HTTPS for?
-------------------------------------------------------------------------------
1: xxx.wolfcode.cn
-------------------------------------------------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
如果出現以下錯誤:
# Saving debug log to /var/log/letsencrypt/letsencrypt.log
# The nginx plugin is not working; there may be problems with your existing configuration.
# The error was: NoInstallationError()
可能是因為找不到 nginx 命令竹捉,解決方案:
# 以下命令是分別將 nginx 安裝目錄的 nginx 命令與配置文件軟連接到指定文件
# 你可以根據自己的具體情況修改目錄
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf /etc/nginx
還有可能提示無法連接到你的服務器芜辕,主要要開放防火墻的 443 端口
看到前面正確提示,出現我的 Nginx 上綁定的一個域名块差,如果你的 Nginx 有綁定多個侵续,那么列表中也會以多個顯示,然后輸入你需要申請 SSL 證書對應域名的序號即可憨闰,驗證成功后 Let's Encrypt 就會立即給你簽發(fā) SSL 證書状蜗,成功后提示:
# Obtaining a new certificate
# Performing the following challenges:
# tls-sni-01 challenge for g.wangchujiang.com
# Waiting for verification...
# Cleaning up challenges
#
# IMPORTANT NOTES:
# - Congratulations! Your certificate and chain have been saved at:
# /etc/letsencrypt/live/xxx.wolfcode.cn/fullchain.pem
# Your key file has been saved at:
# /etc/letsencrypt/live/xxx.wolfcode.cn/privkey.pem
# Your cert will expire on 2018-03-29. 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/xxx.wolfcode.cn/fullchain.pem
/etc/letsencrypt/live/xxx.wolfcode.cn/privkey.pem
# 過期時間:Your cert will expire on 2018-06-19.
# 你的證書將在 2018-06-19 過期
# 可以使用如下命令更新證書有效期
certbot renew --dry-run
配置 Nginx轧坎,讓所有 HTTP 請求轉發(fā)到 HTTPS:
# 將 http 請求轉發(fā)到 https
server {
listen 80;
server_name xxx.wolfcode.cn;
rewrite ^(.*)$ https://$host$1 permanent;
# 禁止在 heaader 中出現服務器版本,防止黑客利用版本漏洞攻擊
server_tokens off;
}
# https 的配置
server {
listen 443 ssl;
server_name xxx.wolfcode.cn;
ssl_certificate /etc/letsencrypt/live/xxx.wolfcode.cn/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxx.wolfcode.cn/privkey.pem;
# 禁止在 heaader 中出現服務器版本泽示,防止黑客利用版本漏洞攻擊
server_tokens off;
# 設置ssl/tls會話緩存的類型和大小缸血。如果設置了這個參數一般是shared,buildin可能會參數內存碎片械筛,默認是none捎泻,和off差不多,停用緩存埋哟。如shared:SSL:10m表示我所有的nginx工作進程共享ssl會話緩存笆豁,官網介紹說1M可以存放約4000個sessions。
ssl_session_cache shared:SSL:1m;
# 客戶端可以重用會話緩存中ssl參數的過期時間,內網系統(tǒng)默認5分鐘太短了闯狱,可以設成30m即30分鐘甚至4h煞赢。
ssl_session_timeout 5m;
# 選擇加密套件,不同的瀏覽器所支持的套件(和順序)可能會不同扩氢。
# 這里指定的是OpenSSL庫能夠識別的寫法耕驰,你可以通過 openssl -v cipher 'RC4:HIGH:!aNULL:!MD5'(后面是你所指定的套件加密算法) 來看所支持算法爷辱。
ssl_ciphers HIGH:!aNULL:!MD5;
# 設置協商加密算法時录豺,優(yōu)先使用我們服務端的加密套件,而不是客戶端瀏覽器的加密套件饭弓。
ssl_prefer_server_ciphers on;
location / {
root /www/html/website;
index dist/index.html;
}
error_page 404 /404.html;
location = /404.html {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
到此就配置完成啦双饥,使用 HTTPS 訪問看看吧。
網站運行在申請 SSL 證書以外的服務器上
為其他主機上的網站或 Docker 中的網站申請 SSL 證書弟断,只需要多一步驗證即可:
# 申請證書
# certonly 只生成證書咏花,不做額外操作
# --manual 交互式操作,也就是追加驗證條件
sudo certbot certonly --manual
提示讓你輸入想要申請證書的域名
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'
to cancel): xxx.wolfcode.cn
接著會提示需要記錄你申請 SSL 證書域名對應服務器的 IP阀趴,問你是否同意:
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for ninghao.net
-------------------------------------------------------------------------------
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.
Are you OK with your IP being logged?
-------------------------------------------------------------------------------
(Y)es/(N)o: Y
然后會給一個字符串昏翰,讓你將字符內容保存在一個文件內,并可以通過它所指定的 URL 進行訪問刘急,cretbot 會對該 URL 進行校驗
-------------------------------------------------------------------------------
Create a file containing just this data:
HNIVXWGNL99iXcy5zI9OwSZmugf0xnv786nw5HchlJ4.n3Pm5l0vxnRwAslvOBBkXT1rGGJ7AwbTnaSCsSiQJFc
And make it available on your web server at this URL:
http://wolfcode.cn/.well-known/acme-challenge/HNIVXWGNL99iXcy5zI9OwSZmugf0xnv786nw5HchlJ4
-------------------------------------------------------------------------------
Press Enter to Continue
比如我這個棚菊,則可以在 website 根目錄創(chuàng)建:
# 創(chuàng)建驗證文件夾
mkdir -p /yourwebsite/.well-known/acme-challenge/
# 進入該文件夾
cd /yourwebsite/.well-known/acme-challenge/
# 創(chuàng)建文件(以下兩部內容請修改為你自己的數據)
touch HNIVXWGNL99iXcy5zI9OwSZmugf0xnv786nw5HchlJ4
# 將提示信息寫入文件
echo "HNIVXWGNL99iXcy5zI9OwSZmugf0xnv786nw5HchlJ4.n3Pm5l0vxnRwAslvOBBkXT1rGGJ7AwbTnaSCsSiQJFc" > HNIVXWGNL99iXcy5zI9OwSZmugf0xnv786nw5HchlJ4
驗證通過之后便會簽發(fā)證書:
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/wolfcode.cn/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/wolfcode.cn/privkey.pem
Your cert will expire on 2018-03-29. 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
到此證書申請便完成了,Nginx 配置可以參考上面的配置
添加定時任務完成自動更新
現在證書申請叔汁、配置都已經完成了统求,并且也可以正常訪問,但還有個小問題就是 Let's Encrypt 簽發(fā)的證書只有 90 天的有效期据块,到期后需要再更新码邻,此時便可以使用到 linux 上的 crontab 工具來定時幫我們完成更新操作
certbot 為我們提供了很方便的更新方式,你只需要執(zhí)行如下命令:
# 更新所有證書
sudo certbot renew --dry-run
使用以上命令便可以將所有證書更新另假,那么此時我們可以添加一個定時任務來完成這一操作:
# 進入定時任務存放目錄
cd /etc/cron.d
# 將腳本寫入文件像屋,該腳本是每 2 個月的 20 號凌晨 2:15 執(zhí)行一次
echo "15 2 20 */2 * certbot renew --dry-run" > certbot-auto-renew-cron
# 將腳本加入到 crontab
crontab certbot-auto-renew-cron
至此,SSL 證書就算徹底完成啦边篮。