#!/usr/bin/env bash # MiaoPanel installer for Debian/Ubuntu and RHEL family distributions. # Usage: # sudo bash install.sh -b ./miaopanel-linux-amd64 # sudo bash install.sh -u https://example.com/miaopanel-linux-amd64 set -euo pipefail PANEL_DIR=/www/server/panel BIN_PATH=/usr/local/bin/miaopanel UNIT=/etc/systemd/system/miaopanel.service NGINX_CONF=/www/server/nginx/conf/nginx.conf PORT=8888 SRC="" URL="" usage() { echo "用法: bash install.sh [-b 本地二进制] [-u 下载地址] [-p 端口]" exit 1 } while getopts "b:u:p:h" opt; do case "$opt" in b) SRC="$OPTARG" ;; u) URL="$OPTARG" ;; p) PORT="$OPTARG" ;; *) usage ;; esac done log() { printf '\033[32m[miaopanel]\033[0m %s\n' "$*"; } die() { printf '\033[31m[错误]\033[0m %s\n' "$*" >&2; exit 1; } [ "$(id -u)" -eq 0 ] || die "请使用 root 运行" if [ -f /etc/os-release ]; then . /etc/os-release else die "无法识别系统(缺少 /etc/os-release)" fi case "${ID:-}" in ubuntu|debian|linuxmint|raspbian) PKG=apt ;; centos|rhel|rocky|almalinux|fedora|openeuler|anolis) PKG=dnf ;; *) die "暂不支持的系统: ${ID:-unknown}" ;; esac log "系统: ${PRETTY_NAME:-$ID} (${PKG})" # --- binary --------------------------------------------------------------- TMP_BIN=/tmp/miaopanel.$$ if [ -n "$SRC" ]; then [ -f "$SRC" ] || die "找不到文件 $SRC" cp "$SRC" "$TMP_BIN" elif [ -n "$URL" ]; then if command -v curl >/dev/null; then curl -fsSL "$URL" -o "$TMP_BIN"; else wget -qO "$TMP_BIN" "$URL"; fi else # Fall back to a binary shipped next to this script. ARCH=$(uname -m) case "$ARCH" in x86_64|amd64) CANDIDATE="$(dirname "$0")/miaopanel-linux-amd64" ;; aarch64|arm64) CANDIDATE="$(dirname "$0")/miaopanel-linux-arm64" ;; *) die "不支持的架构 $ARCH" ;; esac [ -f "$CANDIDATE" ] || die "请通过 -b 指定二进制文件" cp "$CANDIDATE" "$TMP_BIN" fi install -m 0755 "$TMP_BIN" "$BIN_PATH" rm -f "$TMP_BIN" log "二进制已安装到 $BIN_PATH" # --- users and directories ------------------------------------------------ if ! id www >/dev/null 2>&1; then useradd -r -s /sbin/nologin -d /www www || true fi mkdir -p "$PANEL_DIR"/{data,vhost/nginx/extension,vhost/cert,logs} /www/wwwroot /www/wwwlogs chmod 700 "$PANEL_DIR/data" log "目录结构已创建" # --- nginx is optional ---------------------------------------------------- # The panel no longer installs a web server automatically: open the app store # after the first login and install nginx (and any runtime) on demand. if ! command -v nginx >/dev/null 2>&1 && [ ! -x /www/server/nginx/sbin/nginx ]; then log "未检测到 nginx:请登录面板后在「应用商店」中安装所需版本" fi # Resolve the real nginx.conf when nginx is already present (Debian layout). if [ ! -f "$NGINX_CONF" ] && command -v nginx >/dev/null 2>&1; then NGINX_CONF=$(nginx -V 2>&1 | tr ' ' '\n' | sed -n 's/^--conf-path=//p' | head -n1) NGINX_CONF=${NGINX_CONF:-/etc/nginx/nginx.conf} fi if [ -f "$NGINX_CONF" ] && ! grep -q "vhost/nginx/\*.conf" "$NGINX_CONF"; then cp -a "$NGINX_CONF" "${NGINX_CONF}.miaopanel.bak" # Insert the site include into the http block. nginx.conf always opens the # http block with a line starting with "http {". awk 'BEGIN{done=0} { print if (!done && $0 ~ /^[[:space:]]*http[[:space:]]*\{/) { print " include /www/server/panel/vhost/nginx/*.conf;" done=1 } }' "${NGINX_CONF}.miaopanel.bak" > "$NGINX_CONF" if nginx -t >/dev/null 2>&1; then nginx -s reload 2>/dev/null || systemctl reload nginx 2>/dev/null || true log "已向 nginx.conf 注入站点 include" else mv "${NGINX_CONF}.miaopanel.bak" "$NGINX_CONF" die "nginx 配置校验失败,已回滚。请手动在 http {} 中加入: include /www/server/panel/vhost/nginx/*.conf;" fi fi # --- config and admin ----------------------------------------------------- CONFIG="$PANEL_DIR/config.json" if [ ! -f "$CONFIG" ]; then "$BIN_PATH" init -config "$CONFIG" || die "初始化失败" else log "检测到已有配置,跳过初始化" fi # Ensure the listener matches the requested port. "$BIN_PATH" version >/dev/null 2>&1 || true python3 - "$CONFIG" "$PORT" <<'PY' 2>/dev/null || true import json, sys path, port = sys.argv[1], sys.argv[2] with open(path) as f: cfg = json.load(f) cfg["addr"] = f"0.0.0.0:{port}" with open(path, "w") as f: json.dump(cfg, f, indent=2, ensure_ascii=False) PY # --- systemd -------------------------------------------------------------- cat > "$UNIT" </dev/null 2>&1 && ufw status | grep -q "Status: active"; then ufw allow "$PORT"/tcp >/dev/null && log "ufw 已放行 $PORT/tcp" elif command -v firewall-cmd >/dev/null 2>&1 && firewall-cmd --state >/dev/null 2>&1; then firewall-cmd --permanent --add-port="$PORT"/tcp >/dev/null firewall-cmd --reload >/dev/null log "firewalld 已放行 $PORT/tcp" fi ENTRY=$(python3 -c "import json;print(json.load(open('$CONFIG'))['entry'])" 2>/dev/null || true) IP=$(hostname -I 2>/dev/null | awk '{print $1}') cat <