在Debian上配置Node.js应用程序的SSL证书,通常需要以下几个步骤:
获取SSL证书:
安装Certbot:
sudo apt update
sudo apt install certbot python3-certbot-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应用运行在3000端口
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 certbot --nginx -d yourdomain.com -d www.yourdomain.com
配置Node.js应用程序:
const express = require('express');
const app = express();
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem', 'utf8'),
cert: fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/fullchain.pem', 'utf8')
};
app.get('/', (req, res) => {
res.send('Hello, SSL!');
});
https.createServer(options, app).listen(3000, () => {
console.log('Server is running on https://yourdomain.com:3000');
});
测试配置:
sudo systemctl restart nginx
自动续订证书:
sudo certbot renew --dry-run
通过以上步骤,你应该能够在Debian上成功配置Node.js应用程序的SSL证书。