#!/usr/bin/env bash set -euo pipefail SERVICE_NAME="nginx-master" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" BIN_PATH="/usr/local/bin/nginx-master" WORK_DIR="/home/nginx-master" RUN_USER="nginx-master" RUN_GROUP="nginx-master" DOWNLOAD_URL_AMD64="https://github.com/pch18/nginxMaster/releases/download/latest/app-linux-amd64" DOWNLOAD_URL_ARM64="https://github.com/pch18/nginxMaster/releases/download/latest/app-linux-arm64" echo "==> 检测系统架构" ARCH="$(uname -m)" case "${ARCH}" in x86_64|amd64) DOWNLOAD_URL="${DOWNLOAD_URL_AMD64}" ;; aarch64|arm64) DOWNLOAD_URL="${DOWNLOAD_URL_ARM64}" ;; *) echo "错误:暂不支持的系统架构 ${ARCH}" exit 1 ;; esac echo "==> 检查是否为 root 用户" if [ "$(id -u)" -ne 0 ]; then echo "错误:请使用 sudo 或 root 用户执行该脚本" exit 1 fi echo "==> 检查 ${RUN_USER} 用户是否存在" if ! id -u "${RUN_USER}" >/dev/null 2>&1; then echo "==> 创建系统用户 ${RUN_USER}" useradd --system --create-home --home-dir "${WORK_DIR}" --shell /usr/sbin/nologin "${RUN_USER}" else echo "==> 用户 ${RUN_USER} 已存在" fi echo "==> 下载程序到 ${BIN_PATH}" wget -O "${BIN_PATH}" "${DOWNLOAD_URL}" echo "==> 设置执行权限" chmod 755 "${BIN_PATH}" echo "==> 创建工作目录 ${WORK_DIR}" mkdir -p "${WORK_DIR}" echo "==> 设置目录权限 (${RUN_USER}:${RUN_GROUP})" chown -R "${RUN_USER}:${RUN_GROUP}" "${WORK_DIR}" chmod 755 "${WORK_DIR}" echo "==> 检查 nginx.service 是否存在" if systemctl status nginx.service >/dev/null 2>&1; then echo "==> 停止 nginx.service" systemctl stop nginx.service || true echo "==> 禁用 nginx.service 开机自启" systemctl disable nginx.service || true echo "==> nginx.service 已停止并禁用" else echo "==> 未发现 nginx.service,跳过" fi echo "==> 生成 systemd 服务文件 ${SERVICE_FILE}" SERVICE_CONTENT="[Unit] Description=${SERVICE_NAME} After=network-online.target Wants=network-online.target [Service] Type=simple User=${RUN_USER} Group=${RUN_GROUP} WorkingDirectory=${WORK_DIR} Environment=NGINX_MASTER_BASE=${WORK_DIR} ExecStart=${BIN_PATH} Restart=on-failure RestartSec=5 TimeoutStopSec=30 KillSignal=SIGTERM SyslogIdentifier=${SERVICE_NAME} [Install] WantedBy=multi-user.target " echo "${SERVICE_CONTENT}" > "${SERVICE_FILE}" echo "==> 重新加载 systemd 配置" systemctl daemon-reload echo "==> 设置开机自启" systemctl enable "${SERVICE_NAME}" echo "==> 重启服务" systemctl restart "${SERVICE_NAME}" echo "==> 查看服务状态" systemctl --no-pager --full status "${SERVICE_NAME}" || true echo echo "安装完成!" echo "常用命令:" echo " 查看状态: systemctl status ${SERVICE_NAME}" echo " 重启服务: systemctl restart ${SERVICE_NAME}" echo " 停止服务: systemctl stop ${SERVICE_NAME}" echo " 查看日志: journalctl -u ${SERVICE_NAME} -f"