在 Nginx 上使用 Let’s Encrypt 加密(HTTPS)你的網站[簡明教程]
來自: http://www.appinn.com/use-letsencrypt-with-nginx/
前幾天有個消息,只在特定的圈子里傳播了一下: The PKI platform of StartSSL is now hosted by Qihoo 360 ,大意是老牌的免費證書提供商 StartSSL 將服務器放到了 Qihoo 360 的服務器上,于是擔心證書已不再安全。從而轉向免費證書新星 Let’s Encrypt 。
青小蛙曾經用過好幾年的 StartSSL 服務,為此還付費使用過,一年 59.9 美金,你可以隨意簽發證書。不過近兩年證書普遍降價,再加上免費的 Let’s Encrypt 和即將到來的 HTTP/2,互聯網的全面 HTTPS 化將在不久的將來實現…屆時不會再有劫持,不會再有干擾…總之看起來很美。
Let’s Encrypt 是一個將于2015年末推出的數字證書認證機構,將通過旨在消除當前手動創建和安裝證書的復雜過程的自動化流程,為安全網站提供免費的SSL/TLS證書。
Let’s Encrypt 是由互聯網安全研究小組(ISRG,一個公益組織)提供的服務。主要贊助商包括電子前哨基金會,Mozilla基金會,Akamai以及思科。2015年4月9日,ISRG與Linux基金會宣布合作。
那么,現在使用 Let’s Encrypt 則是一個非常經濟實惠又安全的選擇,于是青小蛙參考 How To Secure Nginx with Let’s Encrypt on Ubuntu 14.04 這篇文章,有了這篇教程。
必備條件:
實現結果:
用戶通過 TLS/SSL 加密訪問你的網站,而后臺實現自動續簽 Let’s Encrypt 證書。
開始
教程基于 Ubuntu 14.04 系統,不夠其他 Linux 版本都是類似的,用你熟悉的系統即可。
第 1 步:安裝 Let’s Encrypt 客戶端
裝前準備,更新系統和安裝 git & bc:
apt-get updateapt-get -y install git bc
克隆 Let’s Encrypt 到 /opt/letsencrypt:
git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt
第 2 步:獲取證書
首先關閉 Nginx:
service nginx stop
并且檢查一下 80 端口沒有被占用:
netstat -na | grep ‘:80.*LISTEN’
運行 Let’s Encrypt:
cd /opt/letsencrypt
./letsencrypt-auto certonly –standalone
</div>
注意:Let’s Encrypt 需要超級用戶權限,如果你沒有使用 sudo 命令,可能會讓你輸入密碼。
之后會出現圖形界面輸入郵箱、條款、域名等信息:
支持多域名,只需要在第三張截圖里用空格或者英文逗號分隔就好了。
目前 Let’s Encrypt 沒有 EV 證書與 泛域名證書的計劃。
IMPORTANT NOTES:
- If you lose your account credentials, you can recover through e-mails sent to domain@appinn.com
- Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will expire on 2016-05-15. To obtain a new version of the certificate in the future, simply run Let’s Encrypt again.
- Your account credentials have been saved in your Let’s Encrypt 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 Let’s Encrypt so making regular backups of this folder is ideal.
- If like Let’s Encrypt, please consider supporting our work by:
Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donateDonating to EFF: https://eff.org/donate-le
看到這一段,就正常證書已經簽發成功,位于 /etc/letsencrypt,注意備份。
如果使用國內 VPS,此處可能會由于 DNS 問題出錯,可以嘗試更換 VPS 的 DNS 為第三方,比如 8.8.8.8。
每一個域名都會自動生成四個文件,位于 /etc/letsencrypt/archive/domain 目錄下:
cert.pem: 域名證書
chain.pem: The Let’s Encrypt 證書
fullchain.pem: 上面兩者合體
privkey.pem: 證書密鑰
</div>
第 3 步:配置 Nginx
有了域名證書,就開始配置 Nginx 了,這部分比較略。
打開對應網站的配置文件,一般在 /etc/nginx/sites-available/default 或者 /usr/local/nginx/conf/ 中,試你自己的情況。
server {
listen 443 ssl;
server_name appinn.com ;
…
ssl on;
ssl_certificate /etc/letsencrypt/live/ appinn.com /fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/
appinn.com
/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ‘EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH’;
…
}
</div>
注意修改紅色的部分為你的域名
如果你想開啟全站 https,需要將 http 轉向到 https,再添加一個 server 就好了:
server {
listen 80;
server_name appinn .com;
return 301 https://$host$request_uri;
}
</div>
注意以上配置文件需要根據實際情況修改。
保存,重啟 Nginx
service nginx restart
此時,打開你的域名比如 https:// 就能看到綠色的地址欄了。
第 4 步:自動續簽證書
Let’s Encrypt 證書只有 90 天的有效期,這和之前按年使用的商業證書有些區別,所以我們還需要設置自動續簽,好讓證書一直有效。
安裝 Webroot 插件
這是一個可以不用停止 Web 服務就能讓 Let’s Encrypt 驗證域名的插件,再次打開 Nginx 配置文件,在 ssl 下面添加:
location ~ /.well-known {
allow all;
}
</div>
保存。
使用命令行續簽證書
cd /opt/letsencrypt
./letsencrypt-auto certonly -a webroot –agree-tos –renew-by-default –webroot-path= /usr/share/nginx/html -d example.com -d www.example.com
</div>
注意修改 webroot-path 參數,這是你的網站路徑。
service nginx reload
重新加載 Nginx 配置文件。
創建 Let’s Encrypt 續簽配置文件
cp /opt/letsencrypt/examples/cli.ini /usr/local/etc/le-renew-webroot.ini
我們將直接編輯示例配置文件:
vi /usr/local/etc/le-renew-webroot.ini
修改以下幾行:
rsa-key-size = 4096
email = you@example.com
domains = example.com, www.example.com
webroot-path = /usr/share/nginx/html
</div>
保存。
現在就可以使用配置文件來續簽證書了:
cd /opt/letsencrypt
./letsencrypt-auto certonly -a webroot –renew-by-default –config /usr/local/etc/le-renew-webroot.ini
</div>
創建自動續簽腳本
下載腳本并設置權限:
curl -L -o /usr/local/sbin/le-renew-webroot https://gist.githubusercontent.com/thisismitch/e1b603165523df66d5cc/raw/fbffbf358e96110d5566f13677d9bd5f4f65794c/le-renew-webroot
chmod +x /usr/local/sbin/le-renew-webroot
</div>
注意:確保上一步創建的 續簽配置文件 /usr/local/etc/le-renew-webroot.ini 存在,否則腳本將無法運行。
運行腳本:
le-renew-webroot
正常情況下,你將得到當前證書的剩余時間:
Checking expiration date for example.com…The certificate is up to date, no need for renewal (82 days left).
創建定時任務:
crontab -e
添加下面一行,讓每周一早上 2 點 30 分運行一次,并記錄到日志文件中。
30 2 * * 1 /usr/local/sbin/le-renew-webroot >> /var/log/le-renewal.log
結束
至此,在 Nginx 上使用 Let’s Encrypt 的配置與續簽全部完成,今后就完全不需要打理這一部分了,HTTPS 將默默工作。目前 善用佳軟 已經在使用 Let’s Encrypt,可以圍觀一下: https://xbeta.info
小眾軟件還有一枚有效期內的泛域名證書,目前還沒有使用 Let’s Encrypt,是的,你可以用 https://www.appinn.com 來訪問小眾軟件,不過由于歷史以及 CDN 遺留問題,還沒有全站切換過去,等待 HTTP/2 吧。
不過發現頻道( https://faxian.appinn.com )已經 HTTPS 了哦。有疑問歡迎留言反饋。
</div>