在Debian系統中配置Node.js應用程序的SSL證書,通常需要以下幾個步驟:
獲取SSL證書:
安裝Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx
配置Nginx作為反向代理(如果你使用Nginx):
編輯Nginx配置文件,通常位于/etc/nginx/sites-available/default。
添加或修改以下內容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
proxy_pass http://localhost:3000; # 你的Node.js應用監聽的端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
啟用配置:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
測試Nginx配置并重啟:
sudo nginx -t
sudo systemctl restart nginx
獲取SSL證書:
運行Certbot來獲取證書:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示完成證書獲取過程。
自動續訂證書:
sudo certbot renew --dry-run
配置Node.js應用:
確保你的Node.js應用監聽HTTPS端口(通常是443),并使用獲取到的SSL證書。
你可以使用https模塊來創建HTTPS服務器:
const https = require('https');
const fs = require('fs');
const express = require('express');
const app = express();
// 讀取SSL證書文件
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/fullchain.pem')
};
// 創建HTTPS服務器
https.createServer(options, app).listen(443, () => {
console.log('HTTPS Server running on port 443');
});
通過以上步驟,你可以在Debian系統中成功配置Node.js應用程序的SSL證書。