在CentOS上配置Node.js以使用特定域名并确保其安全性,可以遵循以下步骤:
sudo yum update
sudo yum install epel-release
sudo yum install -y nodejs
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
安装完成后,使用以下命令检查安装是否成功:
node -v
npm -v
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
编辑Nginx配置文件,通常位于 /etc/nginx/conf.d/
目录下,创建一个新文件(如 myapp.conf
),并添加以下内容:
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host host;
proxy_cache_bypass http_upgrade;
}
}
保存文件后,测试Nginx配置是否正确:
sudo nginx -t
重新加载Nginx以应用更改:
sudo systemctl restart nginx
编辑 /etc/ssh/sshd_config
文件,将默认端口从22改为其他非标准端口,例如2222:
Port 2222
然后重启SSH服务:
sudo systemctl restart sshd
使用 firewalld
配置防火墙,只允许特定端口(如SSH和Node.js应用的端口)通过:
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --reload
如果需要更高级别的安全加固,可以启用SELinux:
sudo yum install policycoreutils-python-utils
sudo setenforce 1
保持Node.js和npm的最新状态,以防止安全漏洞:
sudo npm install -g npm
sudo npm update -g
以上步骤涵盖了在CentOS上安装Node.js、配置Nginx反向代理以及进行基本的安全设置。根据具体需求,可能还需要进行更多的安全加固措施,如配置SSL证书、限制访问权限等。