在Linux上部署Node.js项目通常涉及以下几个步骤:
准备环境:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
build-essential:sudo apt-get install build-essential
上传项目:
安装依赖:
npm install
配置环境变量(如果需要):
~/.bashrc或~/.bash_profile文件中添加:export NODE_ENV=production
export DATABASE_URL=mongodb://user:password@host:port/dbname
source ~/.bashrc或source ~/.bash_profile使变量生效。启动应用:
node app.js
npm install pm2 -g
pm2 start app.js --name "my-app"
配置反向代理(可选):
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;
}
}
sites-available目录,并创建一个符号链接到sites-enabled目录,然后重启Nginx:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
安全性和监控:
以上步骤提供了一个基本的部署流程,具体的步骤可能会根据你的项目需求和服务器配置有所不同。