温馨提示×

centos nodejs网络配置指南

小樊
44
2025-08-20 16:37:43
栏目: 编程语言

以下是在CentOS上配置Node.js网络的指南:

安装Node.js

使用NodeSource存储库安装,如安装14.x版本:

curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash - sudo yum install -y nodejs 

验证安装:

node -v npm -v 

配置网络接口

可通过编辑配置文件或使用命令行工具。以编辑ifcfg-ens33文件为例:

sudo nano /etc/sysconfig/network-scripts/ifcfg-ens33 

设置静态IP等相关参数:

DEVICE=ens33 BOOTPROTO=static IPADDR=192.168.1.100 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 DNS1=8.8.8.8 ONBOOT=yes 

保存后重启网络服务:

sudo systemctl restart network 

或使用nmcli命令配置(CentOS 7/8均支持):

sudo nmcli connection modify "ens33" ipv4.method manual ipv4.addresses "192.168.1.100/24" ipv4.gateway "192.168.1.1" ipv4.dns "8.8.8.8" sudo nmcli connection up "ens33" 

配置防火墙

使用firewalld允许HTTP和HTTPS流量:

sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload 

配置反向代理(可选)

以Nginx为例,安装后编辑配置文件:

sudo nano /etc/nginx/conf.d/default.conf 

添加如下内容:

server { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } 

重启Nginx:

sudo systemctl restart nginx 

0