在CentOS系统下为Node.js应用程序配置SSL证书,可以按照以下步骤进行操作。本文将介绍如何生成自签名证书以及如何使用Let’s Encrypt免费获取受信任的SSL证书。
如果尚未安装Node.js,可以使用以下命令通过NodeSource仓库安装最新版本:
# 导入NodeSource仓库的GPG密钥
curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
# 安装Node.js
sudo yum install -y nodejs
注意:将setup_16.x
中的版本号替换为你需要的Node.js版本,例如setup_14.x
或setup_18.x
。
使用OpenSSL生成自签名证书和私钥:
# 创建一个目录用于存放证书文件
sudo mkdir /etc/ssl/certs/myapp
# 进入目录
cd /etc/ssl/certs/myapp
# 生成私钥(2048位)
sudo openssl genrsa -out myapp.key 2048
# 生成证书签名请求(CSR)
sudo openssl req -new -key myapp.key -out myapp.csr -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/CN=localhost"
# 生成自签名证书
sudo openssl x509 -req -days 365 -in myapp.csr -signkey myapp.key -out myapp.crt
说明:
/C=CN/ST=Beijing/L=Beijing/O=MyCompany/CN=localhost
中的信息根据实际情况修改,尤其是CN
(Common Name)应与你的域名或服务器地址匹配。假设你有一个使用Express框架的Node.js应用,可以按照以下方式配置SSL:
const fs = require('fs');
const https = require('https');
const express = require('express');
const app = express();
// 读取证书文件
const options = {
key: fs.readFileSync('/etc/ssl/certs/myapp/myapp.key', 'utf8'),
cert: fs.readFileSync('/etc/ssl/certs/myapp/myapp.crt', 'utf8')
};
// 定义路由
app.get('/', (req, res) => {
res.send('Hello, SSL!');
});
// 启动HTTPS服务器
https.createServer(options, app).listen(443, () => {
console.log('HTTPS Server running on port 443');
});
说明:
在浏览器中访问https://your_server_ip_or_domain
,你应该会看到安全警告,因为使用的是自签名证书。继续访问以确认应用正常运行。
Let’s Encrypt提供免费的SSL/TLS证书,适用于生产环境。可以使用Certbot工具来自动化证书的申请和续期过程。
首先,确保已安装EPEL仓库:
sudo yum install epel-release
然后,安装Certbot及其Node.js插件:
sudo yum install certbot python3-certbot-nodejs
运行Certbot以获取证书。假设你的Node.js应用运行在localhost
的443端口:
sudo certbot --nodejs --http-01-port 80 -d localhost
说明:
--http-01-port 80
:Certbot将通过HTTP-01挑战验证域名所有权,需要访问80端口。-d localhost
:指定要申请证书的域名。按照提示完成验证过程。Certbot会自动配置Nginx或Apache来处理HTTP-01挑战。如果你使用的是Node.js直接处理HTTP请求,可以考虑使用certbot
的HTTP-01插件或手动配置。
Certbot会将证书和私钥文件存放在/etc/letsencrypt/live/localhost/
目录下。配置Node.js应用使用这些文件:
const fs = require('fs');
const https = require('https');
const express = require('express');
const app = express();
// 读取Certbot生成的证书文件
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/localhost/privkey.pem', 'utf8'),
cert: fs.readFileSync('/etc/letsencrypt/live/localhost/fullchain.pem', 'utf8')
};
// 定义路由
app.get('/', (req, res) => {
res.send('Hello, Let\'s Encrypt SSL!');
});
// 启动HTTPS服务器
https.createServer(options, app).listen(443, () => {
console.log('HTTPS Server running on port 443');
});
Let’s Encrypt证书有效期为90天,Certbot可以自动续期。确保设置定时任务以自动执行续期:
sudo crontab -e
添加以下行以每天检查并续期证书:
0 0,12 * * * certbot renew --post-hook "systemctl reload nginx"
注意:如果使用的是Node.js直接处理HTTP请求,而不是反向代理(如Nginx或Apache),需要确保在续期后重新加载Node.js应用。可以使用systemd
服务来实现这一点。
在浏览器中访问https://localhost
,应该能够正常访问且没有安全警告(因为localhost被视为受信任的)。
在生产环境中,通常建议使用Nginx或Apache作为反向代理来处理SSL终端,而不是让Node.js应用直接处理HTTPS连接。这不仅提高了安全性,还提升了性能和可维护性。
sudo yum install nginx
编辑Nginx配置文件(例如/etc/nginx/nginx.conf
或创建一个新的站点配置文件 /etc/nginx/conf.d/myapp.conf
):
server {
listen 80;
server_name your_server_ip_or_domain;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
proxy_pass http://localhost:3000; # Node.js应用监听的端口
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
sudo systemctl start nginx
sudo systemctl enable nginx
使用Certbot通过Nginx获取证书:
sudo certbot --nginx -d your_server_ip_or_domain
Certbot会自动修改Nginx配置文件以处理SSL,并重新加载Nginx服务。
确保Node.js应用监听在非SSL端口(例如3000):
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello via Nginx and Let\'s Encrypt SSL!');
});
app.listen(3000, () => {
console.log('Node.js app running on port 3000');
});
Certbot会自动处理Nginx配置的SSL证书续期。确保/etc/letsencrypt/renewal/your_server_ip_or_domain.conf
文件中包含重新加载Nginx的指令。
以上介绍了在CentOS系统下为Node.js应用配置SSL证书的几种方法:
根据你的具体需求和环境选择合适的方法进行配置。