在CentOS上配置Node.js應用程序的SSL證書,通常需要以下幾個步驟:
獲取SSL證書:
安裝Certbot:
sudo yum install epel-release
sudo yum install certbot python2-certbot-nginx
獲取證書:
example.com
,運行以下命令:sudo certbot --nginx -d example.com -d www.example.com
配置Node.js應用程序:
http
模塊來創建一個簡單的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/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem')
};
// 創建HTTPS服務器
https.createServer(options, app).listen(443, () => {
console.log('HTTPS Server running on port 443');
});
重定向HTTP到HTTPS:
server {
listen 80;
server_name example.com www.example.com;
location / {
return 301 https://$host$request_uri;
}
}
測試配置:
自動續訂證書:
sudo certbot renew --dry-run
通過以上步驟,你可以在CentOS上成功配置Node.js應用程序的SSL證書。確保定期檢查和更新證書,以保持安全性。