狼道精神 發表於 2025-12-30 08:21:56

Vue部署项目到阿里云云服务器的实现步骤

<p><strong>部署 Vue 项目到阿里云云服务器 (CentOS/Ubuntu 为例)</strong></p>
<p><strong>核心步骤:</strong> 在本地构建 Vue 项目生成静态文件,然后将这些文件上传到服务器,并使用 Nginx 作为 Web 服务器提供服务。</p>
<p><strong>前提条件:</strong></p>
<ol><li>一台运行 Linux (如 CentOS 或 Ubuntu) 的阿里云 ECS 实例。</li><li>本地开发环境已安装 Node.js 和 npm/yarn。</li><li>本地已安装 SSH 客户端 (如 OpenSSH)。</li><li>服务器已安装 Nginx。</li><li>拥有服务器的 SSH 登录权限 (用户名和密码或密钥对)。</li></ol>
<p><strong>详细步骤:</strong></p>
<p><strong>1. 本地构建 Vue 项目</strong></p>
<ul><li>打开终端,进入你的 Vue 项目根目录。</li><li>运行构建命令生成静态文件:<div class="jb51code"><pre class="brush:bash;">npm run build
# 或者使用 yarn
yarn build
</pre></div></li><li>构建完成后,项目目录下会生成一个 <code>dist</code> 文件夹。这个文件夹里包含了所有优化压缩后的 HTML、CSS、JavaScript 和静态资源文件。这就是你需要上传到服务器的内容。</li></ul>
<p><strong>2. 服务器环境准备</strong></p>
<ul><li><strong>登录服务器:</strong> 使用 SSH 登录你的阿里云服务器。
<div class="jb51code"><pre class="brush:bash;">ssh username@your_server_ip
# 如果使用密钥,可能需要指定密钥路径
ssh -i /path/to/your-key.pem username@your_server_ip
</pre></div></li><li><strong>安装 Nginx (如果未安装):</strong><ul><li><strong>Ubuntu:</strong><div class="jb51code"><pre class="brush:bash;">sudo apt update
sudo apt install nginx
</pre></div></li><li><strong>CentOS:</strong><div class="jb51code"><pre class="brush:bash;">sudo yum update
sudo yum install nginx
</pre></div></li></ul></li><li><strong>启动并设置 Nginx 开机自启:</strong><div class="jb51code"><pre class="brush:bash;">sudo systemctl start nginx
sudo systemctl enable nginx
</pre></div></li><li><strong>配置防火墙 (如果启用):</strong> 确保防火墙允许 HTTP (端口 80) 和 HTTPS (端口 443) 的流量。阿里云安全组也需要在控制台开放这些端口。<ul><li><strong>Ubuntu (ufw):</strong><div class="jb51code"><pre class="brush:bash;">sudo ufw allow 'Nginx Full' # 允许 HTTP(80) 和 HTTPS(443)
</pre></div></li><li><strong>CentOS (firewalld):</strong><div class="jb51code"><pre class="brush:bash;">sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
</pre></div></li></ul></li></ul>
<p><strong>3. 上传构建文件到服务器</strong></p>
<ul><li>在本地终端 (不是在服务器上),使用 <code>scp</code> 命令将 <code>dist</code> 文件夹上传到服务器的一个目录。选择一个合适的目录,例如 <code>/var/www/your_project_name</code> 或用户家目录下的某个位置。
<div class="jb51code"><pre class="brush:bash;">scp -r /path/to/your/local/vue-project/dist username@your_server_ip:/path/to/target/directory/on/server
# 示例: scp -r ./dist user@123.123.123.123:/var/www/my-vue-app
</pre></div>
<ul><li><code>-r</code>: 递归复制整个目录。</li><li>替换 <code>/path/to/your/local/vue-project/dist</code> 为你的本地 <code>dist</code> 文件夹的实际路径。</li><li>替换 <code>username</code> 为你的服务器用户名。</li><li>替换 <code>your_server_ip</code> 为你的服务器公网 IP 地址。</li><li>替换 <code>/path/to/target/directory/on/server</code> 为你想存放 <code>dist</code> 文件夹内容的服务器目标路径。确保该目录存在,或者先登录服务器创建它 (<code>mkdir -p /path/to/target/directory</code>)。</li></ul></li></ul>
<p><strong>4. 配置 Nginx</strong></p>
<ul><li>登录服务器。</li><li>创建一个新的 Nginx 服务器块 (Server Block) 配置文件。通常放在 <code>/etc/nginx/conf.d/</code> 或 <code>/etc/nginx/sites-available/</code> (Ubuntu) 并在 <code>sites-enabled/</code> 创建符号链接。这里以 <code>/etc/nginx/conf.d/</code> 为例:<div class="jb51code"><pre class="brush:bash;">sudo nano /etc/nginx/conf.d/your_project_name.conf
</pre></div></li><li>在打开的编辑器中,输入类似下面的配置:<div class="jb51code"><pre class="brush:bash;">server {
    listen 80;
    server_name your_domain.com www.your_domain.com; # 替换为你的域名,或者用服务器IP时写 _ (下划线)
    # server_name _; # 如果没有域名,使用 IP 访问,可以用这个

    root /path/to/your/dist/folder/on/server; # 替换为 dist 文件夹在服务器上的实际路径
    index index.html;

    location / {
      try_files $uri $uri/ /index.html; # 对于 Vue Router 的 history 模式非常重要
    }

    # 可选:配置错误页面
    error_page 404 /index.html;
}
</pre></div>
<ul><li><strong>关键点:</strong>
<ul><li><code>root</code>: 必须指向你上传的 <code>dist</code> 文件夹的路径。</li><li><code>try_files $uri $uri/ /index.html;</code>: 这是处理前端路由 (如 Vue Router 的 history 模式) 的关键。它告诉 Nginx 在找不到文件时尝试加载 <code>index.html</code>,由 Vue 应用处理路由。</li><li><code>server_name</code>: 如果你有域名,填域名。如果只用 IP 访问,可以写 <code>server_name _;</code>。</li></ul></li></ul></li><li>保存并退出编辑器 (在 <code>nano</code> 中按 <code>Ctrl + O</code> 保存,<code>Ctrl + X</code> 退出)。</li><li><strong>测试 Nginx 配置:</strong> 运行以下命令检查配置文件语法是否正确:<div class="jb51code"><pre class="brush:bash;">sudo nginx -t
</pre></div>如果输出显示 <code>syntax is ok</code> 和 <code>test is successful</code>,则配置正确。</li><li><strong>重新加载 Nginx:</strong> 让新的配置生效:<div class="jb51code"><pre class="brush:bash;">sudo systemctl reload nginx
</pre></div></li></ul>
<p><strong>5. 访问你的应用</strong></p>
<ul><li>打开浏览器。</li><li>访问你的服务器公网 IP 地址 (例如 <code>http://123.123.123.123</code>) 或者你配置的域名 (例如 <code>http://your_domain.com</code>)。</li><li>如果一切配置正确,你应该能看到部署好的 Vue 应用了。</li></ul>
<p><strong>补充说明:</strong></p>
<ul><li><strong>HTTPS (SSL/TLS):</strong> 强烈建议在生产环境中使用 HTTPS。你需要获取 SSL 证书 (可以从阿里云 SSL 证书服务购买或使用 Let&rsquo;s Encrypt 免费证书),并在 Nginx 配置中添加监听 443 端口的 <code>server</code> 块,配置 <code>ssl_certificate</code> 和 <code>ssl_certificate_key</code> 路径。配置完成后也需要 <code>sudo nginx -t</code> 和 <code>sudo systemctl reload nginx</code>。</li><li><strong>域名绑定:</strong> 如果你使用域名,需要在你的域名注册商处将域名解析 (A 记录) 指向你的阿里云服务器的公网 IP 地址。阿里云域名解析在阿里云 DNS 控制台操作。</li><li><strong>权限问题:</strong> 确保 Nginx 进程 (通常是 <code>www-data</code> 或 <code>nginx</code> 用户) 对你上传的 <code>dist</code> 文件夹及其内容有读取权限。如果遇到 403 Forbidden 错误,检查权限 (<code>ls -l /path/to/dist</code>) 并可能需要修改 (<code>sudo chmod -R 755 /path/to/dist</code>, <code>sudo chown -R nginx:nginx /path/to/dist</code> - 根据你的 Nginx 用户调整)。</li><li><strong>路由刷新问题:</strong> 如果使用 Vue Router 的 history 模式,并且刷新页面时出现 404,<strong>一定是</strong> Nginx 配置中缺少或错误配置了 <code>try_files $uri $uri/ /index.html;</code> 这行。仔细检查。</li></ul>
<p>按照以上步骤操作,你应该能够成功地将你的 Vue 项目部署到阿里云服务器上。</p>
頁: [1]
查看完整版本: Vue部署项目到阿里云云服务器的实现步骤