在阿里云服务器(ECS)上部署 Vue 或 React 前端项目,本质是将构建后的静态文件(dist/ 或 build/ 目录)通过 Web 服务器(如 Nginx)对外提供服务。以下是完整、安全、生产就绪的部署流程(以 Ubuntu 22.04/CentOS 7+ 为例,推荐使用 Nginx):
✅ 一、前提准备
-
已购买并启动阿里云 ECS 实例
- 推荐配置:2核4G(轻量级项目)、带公网 IP(或配置弹性公网 IP)
- 安全组规则:放行端口
80(HTTP)和443(HTTPS)(控制台 → 安全组 → 添加入方向规则)
-
登录服务器
ssh -i your-key.pem root@<your-server-ip>
✅ 二、安装并配置 Nginx(推荐,轻量高效)
# Ubuntu
sudo apt update && sudo apt install -y nginx
# CentOS/RHEL
sudo yum install -y nginx
# 或 CentOS 8+/Alibaba Cloud Linux 3: sudo dnf install -y nginx
# 启动并设置开机自启
sudo systemctl enable nginx
sudo systemctl start nginx
✅ 验证:浏览器访问 http://<你的服务器IP>,看到 "Welcome to nginx!" 即成功。
✅ 三、上传并部署前端静态文件
方式 1:本地构建 + SCP 上传(推荐 ✅)
# 在本地开发机执行(Vue/React 项目根目录)
# Vue 项目
npm run build # 输出到 dist/
# 或 React 项目
npm run build # 输出到 build/
# 将 dist/ 或 build/ 上传到服务器(替换路径)
scp -r ./dist/ root@<your-server-ip>:/var/www/my-app/
方式 2:服务器上构建(不推荐用于生产,仅测试用)
⚠️ 需安装 Node.js(占用资源多,有安全风险,建议只在 CI/CD 中构建)
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - sudo apt-get install -y nodejs # 然后 git clone + npm install + npm run build(略)
✅ 确保文件权限正确:
sudo chown -R www-data:www-data /var/www/my-app # Ubuntu
# 或 CentOS: sudo chown -R nginx:nginx /var/www/my-app
sudo chmod -R 755 /var/www/my-app
✅ 四、配置 Nginx 服务(关键步骤!)
编辑站点配置:
sudo nano /etc/nginx/sites-available/my-app
# 或 CentOS: sudo nano /etc/nginx/conf.d/my-app.conf
📌 标准配置(支持 Vue Router history 模式 & React Router):
server {
listen 80;
server_name your-domain.com; # 可选:绑定域名(需 DNS 解析到服务器 IP)
root /var/www/my-app;
index index.html;
# 关键:处理前端路由(history 模式)
location / {
try_files $uri $uri/ /index.html;
}
# 静态资源缓存优化
location ~* .(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# API X_X(可选:若前端需调用后端接口,避免跨域)
# location /api/ {
# proxy_pass http://127.0.0.1:3000/; # 后端服务地址
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# }
}
✅ 启用配置:
# Ubuntu(启用软链接)
sudo ln -sf /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/
# CentOS(直接放在 conf.d/ 即可)
# 测试配置语法
sudo nginx -t
# 重载 Nginx
sudo systemctl reload nginx
✅ 五、【强烈推荐】配置 HTTPS(SSL)
使用免费 Let's Encrypt 证书(通过 Certbot):
# Ubuntu
sudo apt install -y certbot python3-certbot-nginx
# 获取证书(需域名已解析到服务器)
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
# 自动续期(Certbot 已配置 systemd timer)
sudo systemctl list-timers | grep certbot
✅ 完成后,Nginx 自动配置 HTTPS 并强制跳转 HTTP→HTTPS。
🔐 提示:若无域名,可先用 HTTP 测试;但生产环境必须 HTTPS(现代浏览器对 HTTP 的限制越来越严格)。
✅ 六、常见问题排查
| 问题 | 解决方案 |
|---|---|
| ❌ 访问白屏 / 404 | 检查 root 路径是否正确;确认 index.html 存在;检查 try_files 规则 |
| ❌ 静态资源(JS/CSS)404 | 检查 dist/ 内部路径(如 assets/ 是否存在);Nginx 日志:sudo tail -f /var/log/nginx/error.log |
| ❌ Vue/React 路由刷新 404 | 确保 location / { try_files $uri $uri/ /index.html; } 已配置 ✅ |
| ❌ 权限拒绝(Permission denied) | sudo chown -R www-data:www-data /var/www/my-app(Ubuntu)或 nginx:nginx(CentOS) |
| ❌ 无法访问(防火墙) | sudo ufw allow 'Nginx Full'(Ubuntu)或 sudo firewall-cmd --permanent --add-service=http(CentOS) |
✅ 七、进阶建议(生产环境)
- 🚀 自动化部署:用 GitHub Actions / GitLab CI 构建后自动
scp到服务器 - 📦 多项目管理:为每个项目创建独立 Nginx 配置 + 独立
root目录 - 🛡️ 安全加固:禁用 Nginx 版本号(
server_tokens off;),限制请求大小等 - 📊 监控日志:配合阿里云 SLS(日志服务)收集 Nginx 访问日志
- 🌐 CDN 提速:在阿里云 CDN 控制台接入,源站填 ECS 公网 IP,大幅提升静态资源加载速度
✅ 附:一键脚本(Ubuntu 示例)
#!/bin/bash
APP_NAME="my-vue-app"
DIST_PATH="./dist"
SERVER_IP="192.168.1.100"
# 1. 构建
npm run build
# 2. 上传
scp -r "$DIST_PATH/" "root@$SERVER_IP:/var/www/$APP_NAME/"
# 3. 远程配置 Nginx
ssh root@$SERVER_IP << 'EOF'
echo "server { listen 80; root /var/www/my-vue-app; index index.html; location / { try_files $uri $uri/ /index.html; } }" > /etc/nginx/sites-available/my-vue-app
ln -sf /etc/nginx/sites-available/my-vue-app /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
echo "✅ Deployed!"
EOF
如有具体场景(如:使用 Docker、需要对接后端 API、使用阿里云 OSS 托管静态资源、或想用 PM2 托管 SSR 应用),欢迎补充,我可以为你定制方案 👇
需要我帮你生成 Nginx 配置模板、CI/CD 脚本 或 Docker 部署方案 吗? 😊
CLOUD云计算