在Linux中部署Node.js项目是一个相对直接的过程,但需要确保所有步骤都正确执行。以下是一个详细的部署指南:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nodejs npm
在CentOS上,你可以使用:sudo yum install nodejs npm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
cd /path/to/your/project
npm install
npm run build
sudo npm install pm2 -g
pm2 start app.js
其中app.js
是你的Node.js应用的入口文件。pm2 status
使用Nginx或Apache作为反向代理,将请求转发到Node应用。
server {
listen 80;
server_name 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;
}
}
将以上配置保存为一个.conf
文件,并将其放置在Nginx的配置目录中(通常为/etc/nginx/conf.d/
),然后重启Nginx服务:
sudo systemctl restart nginx
chmod
和chown
命令修改权限和所有者。netstat
或lsof
命令查看端口占用情况。通过以上步骤,你可以在Linux环境下成功部署一个Node.js项目,并确保其稳定运行。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: linux搭建虚拟机要注意啥