deploy.sh dynamically generates systemd units

- Remove static runtime/*.service files
- generate_systemd_units() writes paths based on PROJECT_DIR
- frps.service: ExecStart uses actual frps binary path
- frps-manager.service: WorkingDir + gunicorn path from PROJECT_DIR
- frpc@.service template also generated at deploy time
This commit is contained in:
Your Name
2026-08-10 14:29:15 +08:00
parent fabcdf2b10
commit a9e2d295b1
4 changed files with 97 additions and 105 deletions
+97 -30
View File
@@ -1,16 +1,9 @@
#!/usr/bin/env bash
# frps-manager one-shot deploy script.
# Installs frps + frps-manager as systemd services on a Debian/Ubuntu or RHEL box.
# Dynamically generates systemd units with correct paths.
# Run as root (or with sudo).
#
# Usage: sudo bash scripts/deploy.sh [--dry-run] [--no-systemd] [--dev]
# What it does:
# 1. Detects distro + package manager, installs system deps
# 2. Downloads the latest stable frps binary if runtime/bin/frps is missing
# 3. Creates a Python venv, installs requirements
# 4. Initializes the SQLite database
# 5. Generates a default frps.ini (admin user, default tenant, bindPort 7000)
# 6. Installs + starts systemd services (frps + frps-manager on :5390)
set -euo pipefail
@@ -34,6 +27,11 @@ cd "$PROJECT_DIR"
log() { echo "[deploy] $*"; }
die() { echo "[deploy][FATAL] $*" >&2; exit 1; }
FRPS_BIN="$PROJECT_DIR/runtime/bin/frps"
FRPS_INI="$PROJECT_DIR/runtime/conf/frps.ini"
GUNICORN="$PROJECT_DIR/venv/bin/gunicorn"
PORT=5390
# --- distro detection -------------------------------------------------------
if [ -f /etc/os-release ]; then
. /etc/os-release
@@ -55,7 +53,7 @@ esac
log "Detected: $OS_ID $OS_VER (pkg manager: $PKG)"
# --- frps download ----------------------------------------------------------
if [ ! -x runtime/bin/frps ]; then
if [ ! -x "$FRPS_BIN" ]; then
log "frps binary missing — fetching v0.70.1 from GitHub"
if [ "$DRY_RUN" = 1 ]; then
log "DRY: would download frp_0.70.1_linux_amd64.tar.gz"
@@ -75,9 +73,9 @@ if [ ! -x runtime/bin/frps ]; then
|| die "Failed to download frps"
tar xzf "$TMPDIR/frp.tgz" -C "$TMPDIR"
mkdir -p runtime/bin
cp "$TMPDIR"/frp_0.70.1_linux_${FRPS_ARCH}/frps runtime/bin/frps
chmod +x runtime/bin/frps
log "Installed runtime/bin/frps"
cp "$TMPDIR"/frp_0.70.1_linux_${FRPS_ARCH}/frps "$FRPS_BIN"
chmod +x "$FRPS_BIN"
log "Installed $FRPS_BIN"
fi
fi
@@ -111,33 +109,101 @@ if [ "$DRY_RUN" = 0 ]; then
venv/bin/python init_db.py
fi
# --- frps systemd -----------------------------------------------------------
# --- generate systemd units -------------------------------------------------
generate_systemd_units() {
# frps.service — generated with actual binary + config paths
cat > /etc/systemd/system/frps.service <<EOF
[Unit]
Description=frps — fatedier/frp server
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=$FRPS_BIN -c $FRPS_INI
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
# frps-manager.service — generated with actual project paths
cat > /etc/systemd/system/frps-manager.service <<EOF
[Unit]
Description=frps-manager — frps Web Management UI
After=network-online.target frps.service
Wants=frps.service
[Service]
Type=simple
WorkingDirectory=$PROJECT_DIR
Environment=FLASK_SECRET=
Environment=FRPS_DASHBOARD_URL=http://127.0.0.1:7500
Environment=FRPS_DASHBOARD_USER=admin
Environment=FRPS_DASHBOARD_PASS=admin123
Environment=FRPS_BIND_PORT=7000
Environment=FRPS_VHOST_HTTP_PORT=80
Environment=FRPS_VHOST_HTTPS_PORT=443
ExecStart=$GUNICORN wsgi:app --bind 0.0.0.0:$PORT --workers 2 --timeout 30
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
# frpc@.service template — for client hosts
cat > /etc/systemd/system/frpc@.service <<EOF
[Unit]
Description=frpc — fatedier/frp client (%i)
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/bin/frpc -c /etc/frpc/%i.ini
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
}
if [ "$NO_SYSTEMD" = 0 ]; then
log "Installing systemd unit frps.service"
log "Generating systemd units (project_dir=$PROJECT_DIR)"
if [ "$DRY_RUN" = 0 ]; then
$SUDO cp runtime/frps.service /etc/systemd/system/frps.service
generate_systemd_units
$SUDO systemctl daemon-reload
$SUDO systemctl enable frps.service
log "systemd units written to /etc/systemd/system/"
fi
# --- start frps ---
log "Starting frps.service"
if [ "$DRY_RUN" = 0 ]; then
$SUDO systemctl enable frps.service 2>/dev/null || true
$SUDO systemctl restart frps.service || true
sleep 2
if ! systemctl is-active --quiet frps.service; then
log "frps.service not active — check 'journalctl -u frps'"
log "WARNING: frps.service not active — check 'journalctl -u frps'"
else
log "frps.service is active"
fi
fi
log "Installing systemd unit frps-manager.service (port 5390)"
# --- start frps-manager ---
log "Starting frps-manager.service (port $PORT)"
if [ "$DRY_RUN" = 0 ]; then
$SUDO cp runtime/frps-manager.service /etc/systemd/system/frps-manager.service
$SUDO systemctl daemon-reload
$SUDO systemctl enable frps-manager.service
$SUDO systemctl enable frps-manager.service 2>/dev/null || true
$SUDO systemctl restart frps-manager.service || true
sleep 2
if ! systemctl is-active --quiet frps-manager.service; then
log "frps-manager.service not active — check 'journalctl -u frps-manager'"
log "WARNING: frps-manager.service not active — check 'journalctl -u frps-manager'"
else
log "frps-manager.service is active on http://0.0.0.0:5390"
log "frps-manager.service is active on http://0.0.0.0:$PORT"
fi
fi
else
@@ -149,7 +215,7 @@ cat <<EOF
================================================================
frps-manager deployed!
Web UI: http://<this-host>:5390
Web UI: http://<this-host>:$PORT
Default: admin / admin (CHANGE IMMEDIATELY under "改密")
frps bind: 0.0.0.0:7000
@@ -157,15 +223,16 @@ cat <<EOF
frps vhost_http: 0.0.0.0:80 (set FRPS_VHOST_HTTP_PORT in service to override)
frps vhost_https: 0.0.0.0:443
To customize, edit:
/etc/systemd/system/frps.service (frps CLI flags)
/etc/systemd/system/frps-manager.service (Web UI port / env)
Systemd units (auto-generated with correct paths):
/etc/systemd/system/frps.service
/etc/systemd/system/frps-manager.service
/etc/systemd/system/frpc@.service
To upgrade later:
To upgrade:
cd $PROJECT_DIR
sudo systemctl stop frps-manager frps
git pull
venv/bin/pip install -r requirements.txt
sudo systemctl start frps frps-manager
sudo bash scripts/deploy.sh # regenerates units + restarts
================================================================
EOF
EOF