温馨提示×

linux js怎样部署项目

小樊
47
2026-10-27 22:02:38
栏目: 编程语言

在 Linux 上部署 JavaScript(前端 / Node.js)项目,常见做法如下,按项目类型分类说明:


一、纯前端项目(Vue / React / 静态 HTML+JS)

1. 构建项目

npm install
npm run build

构建后会生成:

  • Vue:dist/
  • React:build/

2. 上传到服务器

scp -r dist user@ip:/var/www/project

3. 使用 Nginx 部署(推荐)

安装 Nginx:

sudo apt update
sudo apt install nginx

配置:

sudo vim /etc/nginx/sites-available/project

示例配置:

server {
    listen 80;
    server_name yourdomain.com;

    root /var/www/project/dist;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

启用:

sudo ln -s /etc/nginx/sites-available/project /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

二、Node.js 项目(Express / Koa / Nest 等)

1. 安装 Node.js

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt install nodejs

2. 上传并安装依赖

npm install --production

3. 启动项目

node app.js
# 或
npm start

4. 使用 PM2 守护进程(推荐)

npm install -g pm2
pm2 start app.js
pm2 save
pm2 startup

三、Node.js + Nginx 反向代理

Nginx 配置示例:

server {
    listen 80;
    server_name api.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
    }
}

四、使用 Docker 部署(进阶)

FROM node:20
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "app.js"]
docker build -t myjs .
docker run -d -p 3000:3000 myjs

五、常见问题

  • 端口被占用 → lsof -i:3000
  • 权限问题 → sudo chown -R user:user /var/www
  • HTTPS → 使用 certbot

如果你能告诉我:

  • 是 前端还是 Node 后端
  • 用的是 Vue / React / Express / Nest
  • 是否有 域名 / 服务器系统

我可以给你一套精准部署方案。

0 踩