在 CentOS 上配置 Node.js 一般有几种方式,下面给你一套最常用、最稳妥的步骤(以 CentOS 7/8 为例)。
sudo yum update -y
sudo yum install -y curl gcc-c++ make
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
其他版本可改:
setup_16.x/setup_20.x
sudo 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
nvm use 18
npm config set registry https://registry.npmmirror.com
npm config get registry
mkdir test && cd test
npm init -y
app.js
const http = require('http');
http.createServer((req, res) => {
res.end('Hello CentOS Node.js');
}).listen(3000);
运行:
node app.js
浏览器访问:
http://服务器IP:3000
sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --reload
如果你是要:
告诉我你的具体用途,我可以给你更精准的配置方案。