在 CentOS 上“配置 Node.js”通常是为了在服务器环境中运行 JavaScript 程序,尤其是后端服务、构建工具或前端工程化任务。下面从“为什么要配”和“常见配置方式”两方面说明。
npm installnpm run buildcurl -sL https://rpm.nodesource.com/setup_18.x | bash -
yum install -y nodejs
验证:
node -v
npm -v
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 18
优点:
适合特殊版本或深度定制,但耗时长、易出错。
npm install -g pm2
pm2 start app.js
pm2 save
pm2 startup
location / {
proxy_pass http://127.0.0.1:3000;
}
firewall-cmd --add-port=3000/tcp --permanent
firewall-cmd --reload
CentOS 配置 Node.js,本质是为了在稳定服务器上运行 JS 服务或构建前端项目。
如果你是想问 “为什么 CentOS 默认没有 Node.js” 或 “和 Ubuntu 比有什么坑”,也可以继续问我。