MEAN項目
所謂的MEAN即是指技術(shù)棧為 MongoDB + Express + Angular + Nginx 的項目
SSL證書選擇
在此篇文章中選用 Let's encrypt
可以使用官方提供的工具 certbot 部署證書
生成SSL證書
安裝 certbot nginx
pacman -S certbot certbot-nginx nginx
由于使用了 Nginx,在此使用 certbot-nginx 進(jìn)行部署
使用 certbot
如果沒有使用Nginx,或者進(jìn)行測試可以參考此步。
certbot certonly
選用 Standalone,填入域名信息,OK生成。
生成的證書在 /etc/letsencrypt/live/your.domain.com下
var sslPath = '/etc/letsencrypt/live/your.domain.com';
var ssl = {
key: fs.readFileSync(sslPath + '/privkey.pem'),
cert: fs.readFileSync(sslPath + '/fullchain.pem'),
ca: fs.readFileSync(sslPath + '/chain.pem')
}
http.createServer(app).listen(process.env.PORT || 8000);
https.createServer(ssl, app).listen(process.env.PORT || 8443);
使用 certbot-nginx
- 配置Nginx
cd /etc/nginx
vi nginx.conf
server {
listen 80;
server_name your.domain.com;
location / {
root html;
index index.html index.htm;
}
}
:wq保存退出
- certbot-nginx 生成證書
certbot --nginx
工具會自動讀取nginx的配置,按照提示進(jìn)行下一步即可,很簡單。
完成后會同時對nginx的配置進(jìn)行添加,工具添加的配置條目都會有相應(yīng)注釋的后綴,可以自行查看。
有興趣可以用 https://ssllabs.com/ssltest/analyze.html?d=your.domain.com 進(jìn)行SSL的測試
重啟 nginx
systemctl reload nginx.service
在瀏覽器中訪問域名能看到綠色小鎖就大功告成了。

SSL 證書定時更新
由于免費(fèi)的 SSL 證書的有效期為 90 天,所以在過期時需要用工具刷新證書,方法如下:
certbot renew
這是最簡單的刷新方法,會自動更新有效期小于 30 天的證書
借助 crontab
如果使用 crontab 進(jìn)行刷新,官方建議間隔為 2 天,同時如果只想記錄錯誤日志,可以使用 -q 或者 --quiet
certbot renew --quiet
鉤子方法
使用鉤子方法,可以幫助在刷新證書時起停服務(wù)。
certbot renew --pre-hook "service nginx stop" --post-hook "service nginx start"
參考鏈接
Let's encrypt Express
Configuring Nginx and SSL with Node.js
Certbot User Guide
Deploying NodeJS using Express with NginX and Let's Encrypt
Express behind proxies