在Ubuntu上部署Node.js项目可以通过以下步骤完成:
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
sudo apt-get install -y nodejs
或者安装最新LTS版本的Node.js:
curl -sL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt-get install -y nodejs
node -v
npm -v
mkdir my-node-app
cd my-node-app
npm init -y
npm install express --save
app.js):const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
  res.send('Hello World!');
});
app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}/`);
});
npm start
或者使用forever使应用在后台运行:
npm install -g forever
forever start app.js
sudo apt-get install nginx
编辑/etc/nginx/sites-available/default文件,添加以下内容:
server {
  listen 80;
  server_name example.com;
  location / {
    proxy_pass http://127.0.0.1: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;
  }
}
sudo systemctl restart nginx
sudo npm install -g pm2
pm2 start app.js
pm2 startup
以上步骤是在Ubuntu上部署Node.js项目的基本流程,具体项目可能需要根据项目代码和需求进行相应的调整。