在 Ubuntu 上部署 JavaScript 项目,通常分为 前端项目(如 Vue / React) 和 Node.js 后端项目 两类。下面给你一份实用、可操作的部署指南。
sudo apt update
sudo apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
验证:
node -v
npm -v
scp -r yourproject user@server_ip:/var/www/yourproject
或 git 拉取:
git clone https://github.com/xxx/xxx.git
cd xxx
npm install
npm start
sudo npm install -g pm2
pm2 start app.js
pm2 startup
pm2 save
npm install
npm run build
生成目录一般是:
distbuild安装 Nginx:
sudo apt install nginx
配置:
sudo vim /etc/nginx/sites-available/yourproject
示例:
server {
listen 80;
server_name yourdomain.com;
root /var/www/yourproject/dist;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
启用:
sudo ln -s /etc/nginx/sites-available/yourproject /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
用户 → Nginx(80) → Node(3000)
Nginx 配置示例:
location /api/ {
proxy_pass http://localhost:3000/;
}
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx
sudo lsof -i:3000www-data 或 pm2 启动sudo ufw allow 80
sudo ufw allow 443
如果你愿意,可以告诉我:
我可以直接给你一份专属部署方案。