#!/usr/bin/env bash # deploy.cc — one-click install: Nextcloud # Official source of the compose file: https://github.com/nextcloud/docker#base-version---apache # # Usage (recommended: download, check sha256, read, then run): # curl -fsSL https://deploy.cc/i/nextcloud.sh -o nextcloud.sh && sudo bash nextcloud.sh # curl -fsSL https://deploy.cc/i/nextcloud.sh | sudo bash -s -- --domain example.com # # Options: # --domain NAME public domain (used in app URLs; default: http://127.0.0.1:8086) # --dir PATH install directory (default: /opt/nextcloud) # --mirror NAME forwarded to get.docker.com when Docker is missing (Aliyun or AzureChinaCloud) # --no-start write files only, do not pull or start containers # -h, --help show this help # # What it does: installs Docker via the official get.docker.com script if it is # missing, writes docker-compose.yml and .env (random secrets) to the install # directory, then runs `docker compose up -d`. It never overwrites an existing # install, and it refuses to start if a needed host port is already in use. # No telemetry. The whole script is wrapped in main() so a truncated download # (e.g. curl | bash over a flaky link) cannot run half a script. set -euo pipefail main() { APP_SLUG="nextcloud" APP_NAME="Nextcloud" APP_PORT="8086" APP_PROXY="1" APP_SCHEME="http" APP_HOST_PORTS=(8086/tcp) APP_DIR="/opt/${APP_SLUG}" DOMAIN="" MIRROR="" NO_START=0 say() { printf '\033[1;32m[deploy.cc]\033[0m %s\n' "$*"; } warn() { printf '\033[1;33m[deploy.cc]\033[0m %s\n' "$*" >&2; } die() { printf '\033[1;31m[deploy.cc]\033[0m %s\n' "$*" >&2; exit 1; } need_arg() { [[ $# -ge 2 && -n "$2" && "$2" != --* ]] || die "$1 needs a value"; } while [[ $# -gt 0 ]]; do case "$1" in --domain) need_arg "$@"; DOMAIN="$2"; shift 2 ;; --dir) need_arg "$@"; APP_DIR="$2"; shift 2 ;; --mirror) need_arg "$@"; MIRROR="$2"; shift 2 ;; --no-start) NO_START=1; shift ;; -h|--help) echo "Usage: sudo bash $APP_SLUG.sh [--domain NAME] [--dir PATH] [--mirror Aliyun|AzureChinaCloud] [--no-start]"; echo "Docs: https://deploy.cc/zh/apps/$APP_SLUG"; exit 0 ;; *) die "unknown option: $1" ;; esac done [[ "$(uname -s)" == "Linux" ]] || die "Linux only." [[ "${EUID:-$(id -u)}" -eq 0 ]] || die "Please run as root (sudo -i)." if [[ -n "$DOMAIN" && ! "$DOMAIN" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]]; then die "invalid --domain: $DOMAIN" fi [[ "$APP_DIR" == /* && "$APP_DIR" != *..* ]] || die "--dir must be an absolute path without '..'" [[ -z "$MIRROR" || "$MIRROR" == "Aliyun" || "$MIRROR" == "AzureChinaCloud" ]] || die "--mirror must be Aliyun or AzureChinaCloud" case "$(uname -m)" in x86_64|amd64|aarch64|arm64) ;; *) warn "CPU architecture $(uname -m): the images were only checked for amd64 and arm64; they may not run here." ;; esac trap 'warn "Stopped at line $LINENO (see the error above). Re-check, then run again with a fresh --dir or fix $APP_DIR by hand."' ERR rand_hex() { head -c "$1" /dev/urandom | od -An -tx1 | tr -d ' \n'; } # 1. Docker if ! command -v docker >/dev/null 2>&1; then say "Docker not found. Installing with the official script https://get.docker.com ..." command -v curl >/dev/null 2>&1 || die "curl is required." tmp="$(mktemp)" curl -fsSL https://get.docker.com -o "$tmp" if [[ -n "$MIRROR" ]]; then sh "$tmp" --mirror "$MIRROR"; else sh "$tmp"; fi rm -f "$tmp" systemctl enable --now docker >/dev/null 2>&1 || true fi docker compose version >/dev/null 2>&1 || die "'docker compose' plugin not found. See https://docs.docker.com/compose/install/" docker info >/dev/null 2>&1 || die "Docker is installed but the daemon is not running. Try: systemctl start docker" # 2. Files if [[ -e "$APP_DIR/docker-compose.yml" ]]; then die "$APP_DIR/docker-compose.yml already exists. Not overwriting. Use --dir for another location, or update with: cd $APP_DIR && docker compose pull && docker compose up -d" fi mkdir -p "$APP_DIR" cd "$APP_DIR" if [[ -n "$DOMAIN" ]]; then URL="https://${DOMAIN}"; HOST="$DOMAIN"; HOSTPORT="$DOMAIN" else URL="http://127.0.0.1:${APP_PORT}"; HOST="localhost"; HOSTPORT="127.0.0.1:${APP_PORT},localhost:${APP_PORT}" fi cat > docker-compose.yml <<'DEPLOYCC_COMPOSE_EOF' # Nextcloud — docker-compose.yml (deploy.cc) # 官方来源 / Official source: https://github.com/nextcloud/docker#base-version---apache # 核对日期 / Checked: 2026-11-13 # 改动: 数据库密码改为 .env 随机值 # 改动: 宿主端口 8080 改为 8086 并只监听 127.0.0.1 # 改动: 命名卷改为绑定挂载 # 改动: 镜像写明 :apache 标签 services: db: image: mariadb:lts restart: always command: --transaction-isolation=READ-COMMITTED volumes: - ./db:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD:?set DB_ROOT_PASSWORD in .env} - MYSQL_PASSWORD=${DB_PASSWORD:?set DB_PASSWORD in .env} - MYSQL_DATABASE=nextcloud - MYSQL_USER=nextcloud redis: image: redis:alpine restart: always app: image: nextcloud:apache restart: always ports: - "127.0.0.1:8086:80" depends_on: - redis - db volumes: - ./nextcloud:/var/www/html environment: - MYSQL_PASSWORD=${DB_PASSWORD:?set DB_PASSWORD in .env} - MYSQL_DATABASE=nextcloud - MYSQL_USER=nextcloud - MYSQL_HOST=db - REDIS_HOST=redis DEPLOYCC_COMPOSE_EOF umask 077 cat > .env.tmp <<'DEPLOYCC_ENV_EOF' # Nextcloud — deploy.cc .env 模板 / template # __GEN_*__ 由一键脚本替换为随机值;手动部署请自己改掉。 TZ=Asia/Shanghai # 数据库密码(自动生成) / DB password (generated) DB_PASSWORD=__GEN_PW__ # 数据库 root 密码(自动生成) / DB root password (generated) DB_ROOT_PASSWORD=__GEN_PW__ DEPLOYCC_ENV_EOF umask 022 : > .env while IFS= read -r line || [[ -n "$line" ]]; do while [[ "$line" == *__GEN_PW__* ]]; do line="${line/__GEN_PW__/$(rand_hex 16)}"; done while [[ "$line" == *__GEN_HEX32__* ]]; do line="${line/__GEN_HEX32__/$(rand_hex 32)}"; done while [[ "$line" == *__GEN_APPKEY__* ]]; do line="${line/__GEN_APPKEY__/base64:$(head -c 32 /dev/urandom | base64 | tr -d '\n')}"; done line="${line//__URL__/$URL}" line="${line//__HOSTPORT__/$HOSTPORT}" line="${line//__DOMAIN__/$HOST}" printf '%s\n' "$line" >> .env done < .env.tmp rm -f .env.tmp chmod 600 .env APP_DIRS=() for d in "${APP_DIRS[@]+"${APP_DIRS[@]}"}"; do mkdir -p "$APP_DIR/$d"; done chown_dirs() { : } chown_dirs prep() { : } prep say "Wrote $APP_DIR/docker-compose.yml and $APP_DIR/.env (secrets, mode 600)." docker compose config -q || die "docker compose config failed" port_busy() { local p="${1%/*}" proto="${1#*/}" command -v ss >/dev/null 2>&1 || return 1 if [[ "$proto" == "udp" ]]; then ss -Hlun "sport = :$p" 2>/dev/null | grep -q .; else ss -Hltn "sport = :$p" 2>/dev/null | grep -q .; fi } busy=() for hp in "${APP_HOST_PORTS[@]+"${APP_HOST_PORTS[@]}"}"; do if port_busy "$hp"; then busy+=("$hp"); fi done if [[ "$NO_START" -eq 1 ]]; then say "--no-start given. Start later with: cd $APP_DIR && docker compose up -d" exit 0 fi if [[ ${#busy[@]} -gt 0 ]]; then die "Host port(s) already in use: ${busy[*]}. Free them or edit the ports in $APP_DIR/docker-compose.yml, then: cd $APP_DIR && docker compose up -d" fi say "Pulling images and starting $APP_NAME ..." docker compose pull docker compose up -d docker compose ps echo say "$APP_NAME is starting." if [[ "$APP_PROXY" == "1" ]]; then say "Local address: ${APP_SCHEME}://127.0.0.1:${APP_PORT} (bound to 127.0.0.1 only)" say "Reverse proxy config: https://deploy.cc/zh/apps/${APP_SLUG}#proxy" say "Test from your computer: ssh -L ${APP_PORT}:127.0.0.1:${APP_PORT} root@SERVER_IP" fi say "Update: cd $APP_DIR && docker compose pull && docker compose up -d" say "Logs: cd $APP_DIR && docker compose logs -f" say "Remove: cd $APP_DIR && docker compose down (data stays in $APP_DIR)" } main "$@"