仰望星辰工作室

miaopanel

miaopanel/ install/install.sh 6.2 KB · 182 行 原始文件
1#!/usr/bin/env bash
2# MiaoPanel installer for Debian/Ubuntu and RHEL family distributions.
3# Usage:
4# sudo bash install.sh -b ./miaopanel-linux-amd64
5# sudo bash install.sh -u https://example.com/miaopanel-linux-amd64
6set -euo pipefail
7
8PANEL_DIR=/www/server/panel
9BIN_PATH=/usr/local/bin/miaopanel
10UNIT=/etc/systemd/system/miaopanel.service
11NGINX_CONF=/www/server/nginx/conf/nginx.conf
12PORT=8888
13SRC=""
14URL=""
15
16usage() {
17 echo "用法: bash install.sh [-b 本地二进制] [-u 下载地址] [-p 端口]"
18 exit 1
19}
20
21while getopts "b:u:p:h" opt; do
22 case "$opt" in
23 b) SRC="$OPTARG" ;;
24 u) URL="$OPTARG" ;;
25 p) PORT="$OPTARG" ;;
26 *) usage ;;
27 esac
28done
29
30log() { printf '\033[32m[miaopanel]\033[0m %s\n' "$*"; }
31die() { printf '\033[31m[错误]\033[0m %s\n' "$*" >&2; exit 1; }
32
33[ "$(id -u)" -eq 0 ] || die "请使用 root 运行"
34
35if [ -f /etc/os-release ]; then
36 . /etc/os-release
37else
38 die "无法识别系统(缺少 /etc/os-release)"
39fi
40case "${ID:-}" in
41 ubuntu|debian|linuxmint|raspbian) PKG=apt ;;
42 centos|rhel|rocky|almalinux|fedora|openeuler|anolis) PKG=dnf ;;
43 *) die "暂不支持的系统: ${ID:-unknown}" ;;
44esac
45log "系统: ${PRETTY_NAME:-$ID} (${PKG})"
46
47# --- binary ---------------------------------------------------------------
48TMP_BIN=/tmp/miaopanel.$$
49if [ -n "$SRC" ]; then
50 [ -f "$SRC" ] || die "找不到文件 $SRC"
51 cp "$SRC" "$TMP_BIN"
52elif [ -n "$URL" ]; then
53 if command -v curl >/dev/null; then curl -fsSL "$URL" -o "$TMP_BIN"; else wget -qO "$TMP_BIN" "$URL"; fi
54else
55 # Fall back to a binary shipped next to this script.
56 ARCH=$(uname -m)
57 case "$ARCH" in
58 x86_64|amd64) CANDIDATE="$(dirname "$0")/miaopanel-linux-amd64" ;;
59 aarch64|arm64) CANDIDATE="$(dirname "$0")/miaopanel-linux-arm64" ;;
60 *) die "不支持的架构 $ARCH" ;;
61 esac
62 [ -f "$CANDIDATE" ] || die "请通过 -b 指定二进制文件"
63 cp "$CANDIDATE" "$TMP_BIN"
64fi
65install -m 0755 "$TMP_BIN" "$BIN_PATH"
66rm -f "$TMP_BIN"
67log "二进制已安装到 $BIN_PATH"
68
69# --- users and directories ------------------------------------------------
70if ! id www >/dev/null 2>&1; then
71 useradd -r -s /sbin/nologin -d /www www || true
72fi
73mkdir -p "$PANEL_DIR"/{data,vhost/nginx/extension,vhost/cert,logs} /www/wwwroot /www/wwwlogs
74chmod 700 "$PANEL_DIR/data"
75log "目录结构已创建"
76
77# --- nginx is optional ----------------------------------------------------
78# The panel no longer installs a web server automatically: open the app store
79# after the first login and install nginx (and any runtime) on demand.
80if ! command -v nginx >/dev/null 2>&1 && [ ! -x /www/server/nginx/sbin/nginx ]; then
81 log "未检测到 nginx:请登录面板后在「应用商店」中安装所需版本"
82fi
83
84# Resolve the real nginx.conf when nginx is already present (Debian layout).
85if [ ! -f "$NGINX_CONF" ] && command -v nginx >/dev/null 2>&1; then
86 NGINX_CONF=$(nginx -V 2>&1 | tr ' ' '\n' | sed -n 's/^--conf-path=//p' | head -n1)
87 NGINX_CONF=${NGINX_CONF:-/etc/nginx/nginx.conf}
88fi
89
90if [ -f "$NGINX_CONF" ] && ! grep -q "vhost/nginx/\*.conf" "$NGINX_CONF"; then
91 cp -a "$NGINX_CONF" "${NGINX_CONF}.miaopanel.bak"
92 # Insert the site include into the http block. nginx.conf always opens the
93 # http block with a line starting with "http {".
94 awk 'BEGIN{done=0} {
95 print
96 if (!done && $0 ~ /^[[:space:]]*http[[:space:]]*\{/) {
97 print " include /www/server/panel/vhost/nginx/*.conf;"
98 done=1
99 }
100 }' "${NGINX_CONF}.miaopanel.bak" > "$NGINX_CONF"
101 if nginx -t >/dev/null 2>&1; then
102 nginx -s reload 2>/dev/null || systemctl reload nginx 2>/dev/null || true
103 log "已向 nginx.conf 注入站点 include"
104 else
105 mv "${NGINX_CONF}.miaopanel.bak" "$NGINX_CONF"
106 die "nginx 配置校验失败,已回滚。请手动在 http {} 中加入: include /www/server/panel/vhost/nginx/*.conf;"
107 fi
108fi
109
110# --- config and admin -----------------------------------------------------
111CONFIG="$PANEL_DIR/config.json"
112if [ ! -f "$CONFIG" ]; then
113 "$BIN_PATH" init -config "$CONFIG" || die "初始化失败"
114else
115 log "检测到已有配置,跳过初始化"
116fi
117
118# Ensure the listener matches the requested port.
119"$BIN_PATH" version >/dev/null 2>&1 || true
120python3 - "$CONFIG" "$PORT" <<'PY' 2>/dev/null || true
121import json, sys
122path, port = sys.argv[1], sys.argv[2]
123with open(path) as f:
124 cfg = json.load(f)
125cfg["addr"] = f"0.0.0.0:{port}"
126with open(path, "w") as f:
127 json.dump(cfg, f, indent=2, ensure_ascii=False)
128PY
129
130# --- systemd --------------------------------------------------------------
131cat > "$UNIT" <<EOF
132[Unit]
133Description=MiaoPanel control panel
134After=network-online.target
135Wants=network-online.target
136
137[Service]
138Type=simple
139ExecStart=$BIN_PATH serve -config $CONFIG
140Restart=always
141RestartSec=3
142LimitNOFILE=65535
143PrivateTmp=true
144ProtectHome=true
145ProtectKernelTunables=true
146ProtectControlGroups=true
147RestrictSUIDSGID=true
148ReadWritePaths=/www
149
150[Install]
151WantedBy=multi-user.target
152EOF
153systemctl daemon-reload
154systemctl enable --now miaopanel
155log "面板服务已启动"
156
157# --- firewall -------------------------------------------------------------
158if command -v ufw >/dev/null 2>&1 && ufw status | grep -q "Status: active"; then
159 ufw allow "$PORT"/tcp >/dev/null && log "ufw 已放行 $PORT/tcp"
160elif command -v firewall-cmd >/dev/null 2>&1 && firewall-cmd --state >/dev/null 2>&1; then
161 firewall-cmd --permanent --add-port="$PORT"/tcp >/dev/null
162 firewall-cmd --reload >/dev/null
163 log "firewalld 已放行 $PORT/tcp"
164fi
165
166ENTRY=$(python3 -c "import json;print(json.load(open('$CONFIG'))['entry'])" 2>/dev/null || true)
167IP=$(hostname -I 2>/dev/null | awk '{print $1}')
168cat <<EOF
169
170============================================================
171 MiaoPanel 安装完成
172 访问地址: http://${IP:-服务器IP}:${PORT}/${ENTRY}/
173 HTTPS : https://${IP:-服务器IP}:${PORT}/${ENTRY}/
174 (自签证书,浏览器首次访问提示不安全时选择「继续访问」即可)
175 用户名 : admin
176 初始密码: 见 $PANEL_DIR/data/initial_password.txt
177 配置文件: $CONFIG
178 服务管理: systemctl {status|restart|stop} miaopanel
179 下一步 : 登录后进入「应用商店」按需安装 nginx / PHP / Node / Go 等
180 说明 : HTTP 与 HTTPS 共用 ${PORT} 端口;不需要 HTTPS 可在 $CONFIG 里把 "https" 设为 false
181============================================================
182EOF