温馨提示×

centos nodejs配置啥步骤

小樊
35
2026-10-11 17:11:04
栏目: 编程语言

在 CentOS 上配置 Node.js 一般有几种方式,下面给你一套最常用、最稳妥的步骤(以 CentOS 7/8 为例)。


一、使用 NodeSource 安装(推荐)

1. 更新系统

sudo yum update -y

2. 安装依赖

sudo yum install -y curl gcc-c++ make

3. 添加 NodeSource 源(以 Node 18 为例)

curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -

其他版本可改:
setup_16.x / setup_20.x

4. 安装 Node.js

sudo yum install -y nodejs

5. 验证

node -v
npm -v

二、使用 NVM 安装(适合多版本切换)

1. 安装 NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

2. 重新加载环境

source ~/.bashrc

3. 安装 Node

nvm install 18
nvm use 18

三、配置 npm(可选但推荐)

1. 设置国内镜像(速度快)

npm config set registry https://registry.npmmirror.com

2. 查看配置

npm config get registry

四、运行一个简单的 Node 服务测试

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

五、防火墙放行(CentOS 7+)

sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --reload

如果你是要:

  • 部署 Vue / React
  • 用 PM2
  • 配 Nginx 反向代理
  • 上 Docker

告诉我你的具体用途,我可以给你更精准的配置方案。

0 踩