因为借助大模型一直在emlog上增加东西,但是线上的目录因为权限、安全的原因又不能直接git pull,因此重新梳理部署的安装流程。
1. 整体思路
线上emlog下有3个目录:
releases/<时间戳>:每次发版的只读代码副本
shared/:持久写入目录(uploads/ cache/ runtime/ storage/ logs 等)
current -> releases/<时间戳>:Nginx/Apache 指向这个软链(原子切换)
云端/构建机:git pull、构建产物,然后用 rsync 拷到线上新建的 release 目录,最后切软链
1.1 线上服务准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| sudo adduser --disabled-password --gecos "" ubuntu || true sudo usermod -aG www-data ubuntu
cd /var/www/deployment sudo mkdir -p ./{releases,shared}
sudo mkdir -p shared/{uploads,cache,runtime,storage,logs} sudo mkdir -p shared/content/{backup,cache,uploadfile}
cd /var/www sudo chown -R ubuntu:www-data deployment sudo find deployment -type d -exec chmod 2775 {} \; sudo find deployment -type f -exec chmod 0664 {} \;
cd /var/www for d in uploads cache runtime storage logs content/*; do sudo chown -R www-data:www-data ./deployment/shared/$d sudo find ./deployment/shared/$d -type d -exec chmod 2775 {} \; sudo find ./deployment/shared/$d -type f -exec chmod 0664 {} \; done
cd /var/www sudo mkdir -p ./deployment/shared/mysql_data sudo chown -R mysql:mysql ./deployment/shared/mysql_data sudo find ./deployment/shared/mysql_data -type d -exec sudo chmod 0750 {} \; sudo find ./deployment/shared/mysql_data -type f -exec sudo chmod 0640 {} \;
sudo touch ./deployment/shared/config.php sudo chown ubuntu:www-data ./deployment/shared/config.php sudo chmod 0640 ./deployment/shared/config.php
|
1.2 git clone
下载对应代码
1 2
| cd /var/www git clone https://github.com/apostle9891/emlog_modify.git
|
新建同步代码vim emlog_sync.sh,需要修改WWW_DIR的目录地址。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #!/bin/bash set -euo pipefail umask 002
WWW_DIR="/var/www" REPO_URL="https://github.com/apostle9891/emlog_modify.git" BUILD="$WWW_DIR/emlog_modify" REMOTE="$WWW_DIR/deployment" TS="$(date +%Y%m%d%H%M%S)" RELEASE="$REMOTE/releases/$TS"
if [ ! -d "$BUILD/.git" ]; then mkdir -p "$BUILD" git clone "$REPO_URL" "$BUILD" fi cd "$BUILD" git pull --ff-only
mkdir -p "$RELEASE"
rsync -a --delete \ --exclude '.git' \ --exclude '.github' \ --exclude 'node_modules' \ --exclude 'uploads' --exclude 'cache' --exclude 'runtime' --exclude 'storage' --exclude 'logs' \ --exclude 'config.php' --exclude 'mysql_data' \ "$BUILD"/ "$RELEASE"/
cd "$RELEASE" for d in uploads cache runtime storage logs mysql_data content/cache content/backup content/uploadfile; do rm -rf "$d" ln -s "$REMOTE/shared/$d" "$d" done
rm -f config.php ln -s "$REMOTE/shared/config.php" config.php
chown -R ubuntu:www-data "$RELEASE" find "$RELEASE" -type d -exec chmod 2755 {} \; find "$RELEASE" -type f -exec chmod 0644 {} \;
ln -sfn "$RELEASE" "$REMOTE/current"
echo "Deployed $BUILD -> $RELEASE"
|
设置权限
1
| sudo chmod +x ./emlog_sync.sh
|
1.3 原有emlog迁移
因为已经部署了emlog的代码,我们需要把对应的缓存数据迁移到share中。执行以下脚本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #!/usr/bin/env bash set -euo pipefail
SRC="/var/www/emlog" DST="/var/www/deployment/share" CODE_USER="ubuntu" CODE_GROUP="www-data" WRITE_USER="www-data" WRITE_GROUP="www-data" MYSQL_OWNER="mysql:mysql" MOVE_DIRS=(uploads cache runtime storage logs mysql_data) CONFIG_FILE="config.php"
if [[ ! -d "$SRC" ]]; then echo "ERROR: 源目录不存在:$SRC" >&2 exit 1 fi
mkdir -p "$DST"
TS="$(date +%Y%m%d%H%M%S)" BACKUP="/tmp/emlog_migrate_backup_${TS}.tgz" echo "将迁移以下内容:${MOVE_DIRS[*]} 以及 ${CONFIG_FILE}" read -rp "是否先备份这些内容到 ${BACKUP}? [Y/n] " ans ans=${ans:-Y} if [[ "$ans" =~ ^[Yy]$ ]]; then echo ">>> 备份中..." INCLUDES=() for d in "${MOVE_DIRS[@]}"; do [[ -e "$SRC/$d" ]] && INCLUDES+=("$d") done [[ -f "$SRC/$CONFIG_FILE" ]] && INCLUDES+=("$CONFIG_FILE") if ((${#INCLUDES[@]})); then tar -C "$SRC" -czf "$BACKUP" "${INCLUDES[@]}" echo ">>> 备份完成:$BACKUP" else echo ">>> 源中无需备份的对象,跳过打包。" fi fi
chown -R "$CODE_USER:$CODE_GROUP" "$DST" find "$DST" -type d -exec chmod 2775 {} \; 2>/dev/null || true find "$DST" -type f -exec chmod 0664 {} \; 2>/dev/null || true
move_dir() { local name="$1" local src="$SRC/$name" local dst="$DST/$name"
if [[ -L "$src" ]]; then echo "跳过:$src 已是软链。" return 0 fi
mkdir -p "$dst"
if [[ -d "$src" ]]; then echo ">>> 同步 $src -> $dst" rsync -a --delete "$src"/ "$dst"/ 2>/dev/null || rsync -a "$src"/ "$dst"/ || true echo ">>> 移除源并创建软链 $src -> $dst" rm -rf "$src" ln -s "$dst" "$src" else echo "提示:$src 不存在,创建软链到目标空目录。" ln -s "$dst" "$src" fi
if [[ "$name" == "mysql_data" ]]; then chown -R "$MYSQL_OWNER" "$dst" 2>/dev/null || chown -R "$WRITE_USER:$WRITE_GROUP" "$dst" find "$dst" -type d -exec chmod 0750 {} \; 2>/dev/null || true find "$dst" -type f -exec chmod 0640 {} \; 2>/dev/null || true else chown -R "$WRITE_USER:$WRITE_GROUP" "$dst" find "$dst" -type d -exec chmod 2775 {} \; find "$dst" -type f -exec chmod 0664 {} \; fi }
for d in "${MOVE_DIRS[@]}"; do move_dir "$d" done
SRC_CFG="$SRC/$CONFIG_FILE" DST_CFG="$DST/$CONFIG_FILE"
if [[ -f "$SRC_CFG" && ! -e "$DST_CFG" ]]; then echo ">>> 复制 $SRC_CFG -> $DST_CFG" install -o "$CODE_USER" -g "$CODE_GROUP" -m 0640 "$SRC_CFG" "$DST_CFG" elif [[ -f "$SRC_CFG" && -f "$DST_CFG" ]]; then echo ">>> 目标已存在 $DST_CFG,保留目标,源将改为软链。" elif [[ ! -f "$SRC_CFG" && ! -e "$DST_CFG" ]]; then echo "提示:未发现 $CONFIG_FILE(源与目标都没有),跳过。" fi
if [[ -e "$DST_CFG" ]]; then [[ -e "$SRC_CFG" && ! -L "$SRC_CFG" ]] && rm -f "$SRC_CFG" [[ -L "$SRC_CFG" ]] || ln -s "$DST_CFG" "$SRC_CFG" chown "$CODE_USER:$CODE_GROUP" "$DST_CFG" chmod 0640 "$DST_CFG" fi
find "$SRC" -maxdepth 1 -mindepth 1 -type d ! -lname '*' -exec true {} \; >/dev/null 2>&1 || true chown -R "$CODE_USER:$CODE_GROUP" "$SRC" find "$SRC" -type d -exec chmod 2755 {} \; 2>/dev/null || true find "$SRC" -type f -exec chmod 0644 {} \; 2>/dev/null || true
echo "✅ 迁移完成: - 目录:${MOVE_DIRS[*]} 已迁至 $DST 并在 $SRC 建立软链 - 文件:$CONFIG_FILE 已放置于 $DST 并在 $SRC 建立软链 请确认服务正常运行。"
|
1.4 迁移成功
执行1.3的脚本,迁移完毕后,可以进行同步,记得nginx的根目录/var/www/deployment/current
1
| sudo -u ubuntu ./emlog_sync.sh
|