在Ubuntu下部署JS项目(以Node.js项目为例)的核心步骤如下:
sudo apt update && sudo apt upgrade -ysudo apt install -y build-essential git curlcurl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -sudo apt install -y nodejsnode -v 和 npm -vscp -r /本地项目路径 ubuntu@服务器IP:/目标路径git clone https://仓库地址.gitnpm install.env文件,定义如PORT=3000等变量node app.js(需在package.json中配置"start": "node app.js")sudo npm install -g pm2pm2 start app.js --name "项目名"pm2 startup systemd 并按提示执行命令sudo apt install nginx/etc/nginx/sites-available/your-domain.conf):server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:3000; # 转发到Node.js端口
proxy_set_header Host $host;
}
}
sudo ln -s /etc/nginx/sites-available/your-domain.conf /etc/nginx/sites-enabled/sudo nginx -t && sudo systemctl restart nginxsudo ufw allow 'Nginx Full'sudo apt install certbot python3-certbot-nginxsudo certbot --nginx -d your-domain.compm2 listpm2 restart 项目名pm2 logs说明:若项目为前端静态文件,可直接通过Nginx托管(无需Node.js运行时),步骤简化为上传文件+配置Nginx静态资源路径。
参考来源: