Files
gitea-deploy/install-gitea.sh

235 lines
7.6 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 在 Linux 服务器上装一台私有 Gitea(二进制 + systemd + SQLite)
#
# sudo bash install-gitea.sh --domain git.example.com --port 3000
# sudo bash install-gitea.sh --port 3000 # 先用 IP 访问
#
# 参数:
# --dir <路径> 安装目录,默认 /opt/gitea
# --port <端口> HTTP 端口,默认 3000
# --domain <域名> 对外域名(不填就用 IP)
# --user <名字> 管理员用户名,默认 gitea
# --password <密码> 管理员密码,不填自动生成
# --http-scheme 默认 http;上 HTTPS 后传 https
set -euo pipefail
VERSION="${GITEA_VERSION:-}"
APP_DIR="${GITEA_DIR:-/opt/gitea}"
PORT="${GITEA_PORT:-3000}"
DOMAIN="${GITEA_DOMAIN:-}"
ADMIN_USER="${GITEA_ADMIN:-gitea}"
ADMIN_PASS="${GITEA_PASS:-}"
SCHEME="http"
c_reset=$'\033[0m'; c_cyan=$'\033[36m'; c_green=$'\033[32m'; c_red=$'\033[31m'; c_bold=$'\033[1m'
log() { printf '%s▸%s %s\n' "$c_cyan" "$c_reset" "$*"; }
ok() { printf '%s✓%s %s\n' "$c_green" "$c_reset" "$*"; }
die() { printf '%s✗%s %s\n' "$c_red" "$c_reset" "$*" >&2; exit 1; }
while [ $# -gt 0 ]; do
case "$1" in
--dir) APP_DIR="${2:?}"; shift 2 ;;
--port) PORT="${2:?}"; shift 2 ;;
--domain) DOMAIN="${2:?}"; shift 2 ;;
--user) ADMIN_USER="${2:?}"; shift 2 ;;
--password) ADMIN_PASS="${2:?}"; shift 2 ;;
--https) SCHEME="https"; shift ;;
-h|--help) sed -n '2,16p' "$0"; exit 0 ;;
*) die "未知参数:$1" ;;
esac
done
[ "$(id -u)" -eq 0 ] || die "要用 root 跑"
case "$PORT" in ''|*[!0-9]*) die "端口要是数字" ;; esac
command -v systemctl >/dev/null 2>&1 || die "这台机器没有 systemd(容器?),请用 Docker 方式装"
case "$(uname -m)" in
x86_64|amd64) ARCH=amd64 ;;
aarch64|arm64) ARCH=arm64 ;;
armv7l) ARCH=armv7 ;;
*) die "不认识的 CPU 架构:$(uname -m)" ;;
esac
pm_install() {
if command -v apt-get >/dev/null 2>&1; then
DEBIAN_FRONTEND=noninteractive apt-get update -qq >/dev/null 2>&1 || true
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null
elif command -v dnf >/dev/null 2>&1; then dnf install -y "$@" >/dev/null
elif command -v yum >/dev/null 2>&1; then yum install -y "$@" >/dev/null
elif command -v apk >/dev/null 2>&1; then apk add --no-cache "$@" >/dev/null
else die "请手动安装:$*"
fi
}
need() {
command -v "$1" >/dev/null 2>&1 && return 0
log "安装 $1"
pm_install "${2:-$1}"
command -v "$1" >/dev/null 2>&1 || die "$1 装不上,请手动处理"
}
need curl curl
need git git
need tar tar
if [ -z "$VERSION" ]; then
log "查询 Gitea 最新版本"
VERSION="$(curl -fsSL --max-time 20 https://dl.gitea.com/gitea/version.json | tr -d ' \n' | sed -n 's/.*"latest":{"version":"\([^"]*\)"}.*/\1/p')"
[ -n "$VERSION" ] || die "查不到最新版本,请用 --version 指定,如 GITEA_VERSION=28.4.0"
fi
log "Gitea 版本 $VERSION($ARCH)"
run_as_git() {
if command -v runuser >/dev/null 2>&1; then runuser -u git -- "$@"
elif command -v sudo >/dev/null 2>&1; then sudo -u git "$@"
else su -s /bin/bash git -c "$(printf '%q ' "$@")"
fi
}
if ! id -u git >/dev/null 2>&1; then
log "创建 git 用户"
useradd --system --shell /bin/bash --create-home --home-dir /home/git git 2>/dev/null || adduser -S -h /home/git -s /bin/sh git
fi
mkdir -p "$APP_DIR"/{custom/conf,data,log,repositories,lfs,tmp}
cd /tmp
if [ ! -x "$APP_DIR/gitea" ] || [ "$("$APP_DIR/gitea" --version 2>/dev/null | awk '{print $3}')" != "$VERSION" ]; then
log "下载 gitea-$VERSION($ARCH)"
BASE="https://dl.gitea.com/gitea/${VERSION}/gitea-${VERSION}-linux-${ARCH}"
if curl -fsSL --max-time 1200 -o /tmp/gitea.bin "$BASE"; then
mv /tmp/gitea.bin "$APP_DIR/gitea"
elif curl -fsSL --max-time 1200 -o /tmp/gitea.xz "$BASE.xz"; then
command -v xz >/dev/null 2>&1 || pm_install xz-utils
xz -dc /tmp/gitea.xz > "$APP_DIR/gitea"
else
die "下载失败,检查这台机器能不能访问 dl.gitea.com"
fi
chmod +x "$APP_DIR/gitea"
fi
chown -R git:git "$APP_DIR"
SECRET="$(head -c 32 /dev/urandom | base64 | tr -d '\n=+/' | cut -c1-40)"
JWT="$(head -c 32 /dev/urandom | base64 | tr -d '\n=+/' | cut -c1-40)"
INTERNAL="$(head -c 32 /dev/urandom | base64 | tr -d '\n=+/' | cut -c1-40)"
HOST="${DOMAIN:-$(hostname -I 2>/dev/null | awk '{print $1}')}"
ROOT_URL="${SCHEME}://${HOST}${PORT:+:$PORT}"
cat > "$APP_DIR/custom/conf/app.ini" <<EOF
APP_NAME = Gitea
RUN_USER = git
RUN_MODE = prod
WORK_PATH = ${APP_DIR}
[server]
PROTOCOL = http
DOMAIN = ${HOST}
HTTP_ADDR = 0.0.0.0
HTTP_PORT = ${PORT}
ROOT_URL = ${ROOT_URL}
DISABLE_SSH = true
START_SSH_SERVER = false
OFFLINE_MODE = true
LFS_START_SERVER = true
[database]
DB_TYPE = sqlite3
PATH = ${APP_DIR}/data/gitea.db
[repository]
ROOT = ${APP_DIR}/repositories
[security]
INSTALL_LOCK = true
SECRET_KEY = ${SECRET}
INTERNAL_TOKEN = ${INTERNAL}
PASSWORD_HASH_ALGO = pbkdf2
[oauth2]
JWT_SECRET = ${JWT}
[service]
DISABLE_REGISTRATION = true
REQUIRE_SIGNIN_VIEW = true
REGISTER_EMAIL_CONFIRM = false
[log]
MODE = file
LEVEL = Info
ROOT_PATH = ${APP_DIR}/log
[session]
PROVIDER = file
[picture]
DISABLE_GRAVATAR = true
EOF
chown git:git "$APP_DIR/custom/conf/app.ini"
chmod 600 "$APP_DIR/custom/conf/app.ini"
cat > /etc/systemd/system/gitea.service <<EOF
[Unit]
Description=Gitea (private git)
After=network.target
[Service]
Type=simple
User=git
Group=git
WorkingDirectory=${APP_DIR}
ExecStart=${APP_DIR}/gitea web --config ${APP_DIR}/custom/conf/app.ini
Restart=always
RestartSec=3
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now gitea >/dev/null 2>&1 || true
systemctl restart gitea
log "等 Gitea 起来"
for i in $(seq 1 90); do
if curl -fsS "http://127.0.0.1:${PORT}/api/v1/version" >/dev/null 2>&1; then ok "Gitea 已就绪"; break; fi
[ "$i" -eq 45 ] && systemctl restart gitea >/dev/null 2>&1 || true
[ "$i" -eq 90 ] && die "90 秒没起来,看日志:journalctl -u gitea -n 50 --no-pager"
sleep 1
done
if [ -z "$ADMIN_PASS" ]; then
ADMIN_PASS="$(head -c 18 /dev/urandom | base64 | tr -d '\n=+/' | cut -c1-14)"
fi
if ! run_as_git "$APP_DIR/gitea" admin user list --config "$APP_DIR/custom/conf/app.ini" 2>/dev/null | grep -q "${ADMIN_USER}"; then
log "创建管理员 $ADMIN_USER"
run_as_git "$APP_DIR/gitea" admin user create \
--config "$APP_DIR/custom/conf/app.ini" \
--username "$ADMIN_USER" \
--password "$ADMIN_PASS" \
--email "${ADMIN_USER}@localhost" \
--admin --must-change-password=false >/dev/null
fi
# 顺便建一个访问令牌,脚本后面自动建仓库要用
TOKEN=""
if command -v jq >/dev/null 2>&1; then
TOKEN="$(curl -fsS -u "${ADMIN_USER}:${ADMIN_PASS}" -X POST "http://127.0.0.1:${PORT}/api/v1/users/${ADMIN_USER}/tokens" \
-H 'Content-Type: application/json' \
-d '{"name":"devbox-migrate","scopes":["write:repository","write:user"]}' 2>/dev/null | jq -r '.sha1 // empty' || true)"
fi
echo
printf '%sGitea 装好了%s\n' "$c_bold" "$c_reset"
printf ' 地址 %s\n' "$ROOT_URL"
printf ' 账号 %s / %s\n' "$ADMIN_USER" "$ADMIN_PASS"
printf ' 目录 %s\n' "$APP_DIR"
printf ' 服务 systemctl status gitea · journalctl -u gitea -f\n'
[ -n "$TOKEN" ] && printf ' 令牌 %s\n' "$TOKEN"
echo
printf ' 放行端口:ufw allow %s/tcp(云服务器还要在安全组里放行)\n' "$PORT"
printf ' HTTPS:装好 Nginx 后 certbot --nginx -d %s\n' "${DOMAIN:-你的域名}"
printf ' 注册是关闭的(DISABLE_REGISTRATION=true),要加人就:\n'
printf ' sudo -u git %s/gitea admin user create --username alice --password xxx --email a@b.c --config %s/custom/conf/app.ini\n' "$APP_DIR" "$APP_DIR"