在CentOS下部署Node.js项目可以通过以下步骤实现:
使用EPEL安装:
sudo yum update
sudo yum install epel-release
sudo yum install nodejs npm
node -v
npm -v
使用NVM安装(推荐):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm ls-remote
nvm install 16.20.0
nvm alias default 16.20.0
node -v
/etc/profile
文件,添加以下内容:export NODE_HOME=/usr/local/node-v16.20.0-linux-x64
export PATH=$PATH:$NODE_HOME/bin
source /etc/profile
echo $NODE_HOME
echo $PATH
npm install
npm install -g pm2
pm2 start app.js
pm2 startup
pm2 status
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable 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 http_host;
proxy_cache_bypass http_upgrade;
}
}
sudo systemctl restart nginx
通过以上步骤,你可以在CentOS上成功部署Node.js项目。如果有任何问题,请参考相关文档或寻求社区帮助。