先确认 Node.js 环境
先确认 CentOS 已安装 Node.js;未安装时按原文建议到官网下载安装对应版本。进入项目目录后先装依赖:
npm install创建一个最小可运行的 Node.js 服务
新建 app.js:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello Worldn');
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});端口优先取 process.env.PORT,默认 3000;启动后会输出 http://localhost:${PORT}/。
用 pm2 启动并托管服务
npm install pm2 -g
pm2 start app.js --name my-node-appmy-node-app 可替换为你的项目名。
查看状态与常用维护命令
pm2 status
pm2 stop my-node-app
pm2 restart my-node-app需要长期运行时,记得设置开机自启
pm2 startup按提示完成后续步骤即可。








