在 CentOS 上部署 Node.js 服务,一般流程如下(以 CentOS 7/8 为例,适用于生产环境):
curl -sL https://rpm.nodesource.com/setup_18.x | bash -
yum install -y nodejs
验证:
node -v
npm -v
yum install -y nodejs npm
# 示例
mkdir -p /www/nodeapp
cd /www/nodeapp
将项目文件上传(可用 scp / rz / git):
npm install
测试启动:
node app.js
# 或
npm start
npm install -g pm2
pm2 start app.js --name nodeapp
pm2 list
pm2 logs
pm2 restart nodeapp
pm2 stop nodeapp
pm2 delete nodeapp
pm2 startup
pm2 save
yum install -y nginx
systemctl start nginx
systemctl enable nginx
vi /etc/nginx/conf.d/node.conf
示例:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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:
nginx -t
systemctl restart nginx
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=3000/tcp
firewall-cmd --reload
✅ 使用 PM2 管理进程
✅ 使用 Nginx 做反向代理
✅ 使用 HTTPS(Let’s Encrypt)
✅ 关闭直接 3000 端口对外访问
✅ 日志集中管理
如果你愿意,我可以:
你当前用的是 CentOS 几?Node 版本是多少?