Alibaba Cloud Linux 3.2104 LTS 部署 Vue 项目的完整指南
结论: 在 Alibaba Cloud Linux 3.2104 LTS 上部署 Vue 项目需要完成环境配置、项目构建和 Web 服务器部署三个核心步骤,推荐使用 Nginx 作为生产环境服务器。
1. 环境准备
-
更新系统
确保系统是最新状态:sudo yum update -y -
安装 Node.js 和 npm
Vue 项目需要 Node.js 环境,推荐使用nvm(Node Version Manager)管理版本:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash source ~/.bashrc nvm install 16 # 推荐 LTS 版本(如 16.x 或 18.x) node -v # 验证安装 npm -v -
安装 Git(可选)
如果项目代码托管在 Git 仓库:sudo yum install git -y
2. 部署 Vue 项目
-
克隆或上传项目代码
如果是 Git 托管项目:git clone <your-repo-url> cd your-vue-project如果是本地代码,可通过
scp或 SFTP 上传。 -
安装依赖并构建
npm install npm run build # 生成静态文件到 `dist/` 目录关键点:
dist/目录是最终部署的静态资源,无需再运行npm run serve(仅开发模式使用)。
3. 配置 Web 服务器(Nginx)
-
安装 Nginx
sudo yum install nginx -y sudo systemctl start nginx sudo systemctl enable nginx -
配置 Nginx 托管 Vue 项目
编辑默认配置文件:sudo vim /etc/nginx/conf.d/default.conf替换为以下内容(假设项目构建在
/home/user/your-vue-project/dist):server { listen 80; server_name your-domain.com; # 替换为域名或服务器IP root /home/user/your-vue-project/dist; index index.html; location / { try_files $uri $uri/ /index.html; # 支持 Vue Router 的 history 模式 } } -
重启 Nginx
sudo nginx -t # 测试配置 sudo systemctl restart nginx
4. 防火墙与安全组配置
- 开放 80 端口
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reload - 如果是云服务器,还需在阿里云控制台的安全组规则中放行 80/TCP 端口。
5. 验证部署
- 浏览器访问
http://<服务器IP或域名>,应显示 Vue 项目页面。 - 如果出现 404,检查
dist/路径是否正确或 Nginx 配置是否有误。
常见问题解决
- 路由 404 问题:确保 Nginx 配置中包含
try_files $uri $uri/ /index.html。 - 权限问题:确保 Nginx 用户(通常为
nginx)有权限读取dist/目录:sudo chown -R nginx:nginx /home/user/your-vue-project/dist
总结
核心要点:
- Vue 项目部署的关键是生成静态文件(
dist/)并通过 Nginx 等 Web 服务器托管。 - Alibaba Cloud Linux 3 作为 CentOS 替代品,兼容性良好,只需注意 Node.js 版本管理即可。
按照上述步骤操作后,你的 Vue 项目即可在 Alibaba Cloud Linux 3.2104 LTS 上稳定运行。如需 HTTPS,可额外配置 Certbot 申请 SSL 证书。
秒懂云