Deploy一键部署网站到服务器方案
🚀 终极极简部署全流程
第一步:让 Mac 与两台 VPS 建立双向信任(每台服务器只需做一次)
在你的 Mac 终端(Terminal)中,分别执行以下两条命令,将你的 Mac 密钥托管给服务器。
Bash
ssh-copy-id root@服务器A的IP
ssh-copy-id root@服务器B的IP
注:期间会提示你输入一次对应服务器的 root 密码。输入成功后,两台服务器就记住了你的 Mac,以后任何地方调用 root 连接它们都不再需要密码。
第二步:部署本地“脚本生成器”小网站
在本地(localhost)的 Web 环境下新建一个 index.php,把下面的完整代码复制进去。以后有新项目,直接用它来生成专属脚本。
PHP
<?php
// 如果有表单提交,直接触发下载
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$ip = $_POST['ip'] ?? '';
$remote_path = $_POST['remote_path'] ?? '';
// 清理路径末尾的斜杠,防止双斜杠问题
$remote_path = rtrim($remote_path, '/');
// 组装采用【玩法A】的 deploy.sh 文本
$script_content = <<<SHELL
#!/bin/bash
# ---------------------------------------------------
# 瞬时部署脚本 - 完美复用 .gitignore 规则
# ---------------------------------------------------
SERVER_USER="root"
SERVER_IP="{$ip}"
SERVER_PATH="{$remote_path}/"
LOCAL_PATH="./"
echo "🚀 正在向 \$SERVER_IP 同步最新代码..."
# 核心武器:rsync 增量同步
# --filter='dir-merge,- .gitignore' 自动读取并应用项目中的 .gitignore 过滤规则
rsync -avz --progress \\
--filter='dir-merge,- .gitignore' \\
--exclude='deploy.sh' \\
\$LOCAL_PATH \$SERVER_USER@\$SERVER_IP:\$SERVER_PATH
echo "✨ 部署成功!服务器已同步至最新版本。"
SHELL;
// 强制浏览器作为脚本文件下载
header('Content-Type: application/x-sh');
header('Content-Disposition: attachment; filename="deploy.sh"');
echo $script_content;
exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN" data-theme="dark">
<head>
<meta charset="UTF-8">
<title>Deploy.sh 智能生成器 (Git同步版)</title>
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.12.10/dist/full.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-base-300 min-h-screen flex items-center justify-center p-4">
<div class="card w-full max-w-md bg-base-100 shadow-xl border border-base-200">
<div class="card-body">
<h2 class="card-title text-2xl font-bold text-primary mb-2">⚡ Deploy.sh 生成器</h2>
<p class="text-sm text-base-content/70 mb-6">完美复用项目 .gitignore 规则,一键生成项目专属的 root 增量部署脚本。</p>
<form method="POST" action="">
<div class="form-control w-full mb-4">
<label class="label"><span class="label-text font-semibold">服务器 IP 地址</span></label>
<input type="text" name="ip" placeholder="例如: 154.x.x.x" class="input input-bordered w-full" required />
</div>
<div class="form-control w-full mb-6">
<label class="label"><span class="label-text font-semibold">服务器网站绝对路径</span></label>
<input type="text" name="remote_path" placeholder="例如: /www/wwwroot/my_site" class="input input-bordered w-full" required />
<label class="label">
<span class="label-text-alt text-warning">※ 请确保该目录在服务器上已存在</span>
</label>
</div>
<div class="card-actions justify-end">
<button type="submit" class="btn btn-primary w-full">生成并下载 deploy.sh</button>
</div>
</form>
</div>
</div>
</body>
</html>
第三步:日常开发与新站点部署(日常三步走)
每次当你新建了网站,或者想给现有网站配置一键部署时,只需按照以下流程操作:
1. 检查或编写 .gitignore
确保你项目根目录下的 .gitignore 文件里已经写好了你不渴望上传的内容。例如:
Plaintext
.git/
.DS_Store
node_modules/
*.log
2. 生成并激活脚本
-
打开你本地的生成器网页,输入这台网站所在的 服务器IP 和 网站绝对路径,点击生成并下载。
-
将得到的
deploy.sh文件直接拖进该项目的根目录下。 -
打开 Mac 终端,切到当前项目目录下,执行一次赋权命令:
Bashchmod +x deploy.sh
3. 随写随传
以后在这个项目里写完代码、或者做完 Git 备份,直接在终端里顺手敲一行:
Bash
./deploy.sh
rsync 就会像雷达一样,自动扫描 .gitignore 避开所有垃圾文件,并在不到 1 秒的时间内,把改动的代码精准“闪传”到你指定的服务器对应的文件夹中。
这就是专属 Vibe Coder 的自动化部署完全体,极简、爽快!