centos

如何在 CentOS 上配置 Node.js SSL 证书

小樊
46
2025-04-10 15:01:14
栏目: 云计算

在 CentOS 上配置 Node.js 应用程序的 SSL 证书,可以按照以下步骤进行操作。本文将介绍如何获取 SSL 证书(使用 Let’s Encrypt 是一种常见且免费的选择),以及如何在 Node.js 应用中配置 HTTPS 服务器。

步骤一:安装必要的软件

  1. 更新系统包

    首先,确保你的 CentOS 系统是最新的:

    sudo yum update -y
    
  2. 安装 Node.js

    CentOS 默认的软件仓库中的 Node.js 版本可能较旧,建议使用 NodeSource 提供的安装脚本安装最新版本的 Node.js。

    例如,安装 Node.js 16.x:

    curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
    sudo yum install -y nodejs
    

    验证安装:

    node -v
    npm -v
    
  3. 安装 Certbot

    Certbot 是一个用于获取和管理 Let’s Encrypt 证书的工具。

    sudo yum install epel-release -y
    sudo yum install certbot python3-certbot-nginx -y
    

    注意:如果你使用的是其他 web 服务器(如 Apache),请安装相应的 Certbot 插件,例如 python3-certbot-apache

步骤二:获取 SSL 证书

假设你使用的是 Nginx 作为反向代理来处理 HTTPS 请求,并将流量转发到 Node.js 应用。

  1. 配置 Nginx 反向代理

    编辑 Nginx 配置文件,通常位于 /etc/nginx/nginx.conf/etc/nginx/conf.d/default.conf

    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_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;
        }
    }
    

    保存并退出编辑器,然后测试 Nginx 配置:

    sudo nginx -t
    

    如果配置正确,重新加载 Nginx:

    sudo systemctl reload nginx
    
  2. 获取证书

    使用 Certbot 获取 SSL 证书:

    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
    

    Certbot 会自动配置 Nginx 并请求证书。按照提示操作,通常需要输入邮箱地址,并选择是否设置自动续期。

  3. 验证证书

    确保证书已正确安装:

    sudo openssl s_client -connect yourdomain.com:443 -servername yourdomain.com
    

    检查输出中是否有 Verify return code: 0 (OK),表示证书有效。

步骤三:配置 Node.js 应用以支持 HTTPS

在你的 Node.js 应用中,添加 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')
};

// 定义路由
app.get('/', (req, res) => {
    res.send('Hello, HTTPS!');
});

// 启动 HTTPS 服务器
https.createServer(options, app).listen(3000, () => {
    console.log('HTTPS Server running on port 3000');
});

注意:确保 Node.js 应用监听的端口(如 3000)与 Nginx 配置中的 proxy_pass 一致。

步骤四:设置自动续期

Certbot 会自动为 Let’s Encrypt 证书设置续期任务。你可以手动测试续期过程:

sudo certbot renew --dry-run

如果没有问题,Certbot 会自动续期证书。确保系统的 cron 任务或 systemd 定时任务中包含 Certbot 的续期脚本。

可选步骤:使用 PM2 管理 Node.js 应用

为了更好地管理 Node.js 应用,可以使用 PM2

  1. 安装 PM2

    sudo npm install pm2 -g
    
  2. 启动 Node.js 应用

    pm2 start app.js --name myapp
    
  3. 设置 PM2 开机自启

    pm2 startup
    
  4. 保存当前进程列表

    pm2 save
    

总结

通过以上步骤,你可以在 CentOS 上成功配置 Node.js 应用的 SSL 证书,并确保其通过 HTTPS 安全地提供服务。使用 Certbot 获取和管理证书简化了整个过程,而 Nginx 作为反向代理则提高了性能和安全性。如果有任何问题,请检查相关服务的日志文件以获取更多信息。

0
看了该问题的人还看了