feat: Phase 1 - PocketBase 迁移文件、种子数据、部署脚本
- apps/pb_migrations/1710000001_init_collections.js: 9个集合完整定义 - apps/pb_migrations/1710000002_seed_data.js: 默认管理员、分类、标签、页面、示例文章 - scripts/install.sh: Linux 一键部署脚本 (systemd, Nginx, SSL, 防火墙, Fail2Ban) - scripts/install.ps1: Windows 一键部署脚本 (NSSM服务, 计划任务, Certbot) - scripts/start.sh: 服务启动脚本 - scripts/backup.sh: 数据备份脚本 - scripts/update.sh: 代码更新部署脚本 - docs/DEPLOY.md: 详细部署教程文档
This commit is contained in:
@@ -0,0 +1,592 @@
|
||||
<#>
|
||||
.SYNOPSIS
|
||||
云升数码 - Windows 一键部署脚本
|
||||
|
||||
.DESCRIPTION
|
||||
在 Windows Server / Windows 10/11 上部署云升数码展示页面
|
||||
包含 PocketBase 后端、Nginx 反向代理、SSL 证书配置
|
||||
|
||||
.NOTES
|
||||
需要管理员权限运行
|
||||
支持 Windows 10/11, Windows Server 2019/2022
|
||||
#>
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$Domain = "",
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$Email = "",
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[switch]$SkipNginx = $false,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[switch]$SkipSSL = $false
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$ProgressPreference = "SilentlyContinue"
|
||||
|
||||
# 颜色输出函数
|
||||
function Write-Log {
|
||||
param([string]$Message, [string]$Level = "INFO")
|
||||
$colors = @{
|
||||
INFO = "Cyan"
|
||||
SUCCESS = "Green"
|
||||
WARN = "Yellow"
|
||||
ERROR = "Red"
|
||||
}
|
||||
$color = $colors[$Level]
|
||||
Write-Host "[$(Get-Date -Format 'HH:mm:ss')] [$Level] $Message" -ForegroundColor $color
|
||||
}
|
||||
|
||||
# 检查管理员权限
|
||||
function Check-Admin {
|
||||
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
|
||||
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||||
Write-Log "请以管理员身份运行 PowerShell" "ERROR"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
# 检测架构
|
||||
function Get-Architecture {
|
||||
$arch = [Environment]::Is64BitOperatingSystem
|
||||
if ($arch) { return "amd64" } else { return "386" }
|
||||
}
|
||||
|
||||
# 下载文件
|
||||
function Download-File {
|
||||
param([string]$Url, [string]$OutputPath)
|
||||
Write-Log "下载: $Url"
|
||||
try {
|
||||
Invoke-WebRequest -Uri $Url -OutFile $OutputPath -UseBasicParsing
|
||||
Write-Log "下载完成: $OutputPath" "SUCCESS"
|
||||
} catch {
|
||||
Write-Log "下载失败: $_" "ERROR"
|
||||
throw
|
||||
}
|
||||
}
|
||||
|
||||
# 安装 PocketBase
|
||||
function Install-PocketBase {
|
||||
$version = "0.22.0"
|
||||
$arch = Get-Architecture
|
||||
$url = "https://github.com/pocketbase/pocketbase/releases/download/v${version}/pocketbase_${version}_windows_${arch}.zip"
|
||||
$installDir = "C:\pocketbase"
|
||||
$dataDir = "$installDir\pb_data"
|
||||
$migrationsDir = "$installDir\pb_migrations"
|
||||
$publicDir = "$installDir\pb_public"
|
||||
|
||||
Write-Log "安装 PocketBase v$version ($arch)..."
|
||||
|
||||
# 创建目录
|
||||
New-Item -ItemType Directory -Force -Path $installDir, $dataDir, $migrationsDir, $publicDir | Out-Null
|
||||
|
||||
# 下载
|
||||
$zipPath = "$env:TEMP\pocketbase.zip"
|
||||
Download-File $url $zipPath
|
||||
|
||||
# 解压
|
||||
Write-Log "解压..."
|
||||
Expand-Archive -Path $zipPath -DestinationPath $installDir -Force
|
||||
Remove-Item $zipPath -Force
|
||||
|
||||
# 复制迁移文件
|
||||
$projectMigrations = "$PSScriptRoot\..\apps\pb_migrations"
|
||||
if (Test-Path $projectMigrations) {
|
||||
Copy-Item -Path "$projectMigrations\*" -Destination $migrationsDir -Recurse -Force
|
||||
Write-Log "迁移文件已复制" "SUCCESS"
|
||||
}
|
||||
|
||||
# 生成加密密钥
|
||||
$encryptionKey = [Convert]::ToBase64String((1..32 | ForEach-Object { Get-Random -Maximum 256 }))
|
||||
Write-Log "加密密钥: $encryptionKey" "WARN"
|
||||
Write-Log "请妥善保存加密密钥!" "WARN"
|
||||
|
||||
# 创建启动脚本
|
||||
$startScript = @"
|
||||
@echo off
|
||||
cd /d "$installDir"
|
||||
set PB_ENCRYPTION_KEY=$encryptionKey
|
||||
pocketbase.exe serve --http=127.0.0.1:8090 --dir="$dataDir" --publicDir="$publicDir" --migrationsDir="$migrationsDir"
|
||||
"@
|
||||
Set-Content -Path "$installDir\start.bat" -Value $startScript -Encoding UTF8
|
||||
|
||||
# 创建系统服务
|
||||
Write-Log "创建 Windows 服务..."
|
||||
$serviceName = "PocketBase"
|
||||
$serviceDisplayName = "云升数码 PocketBase 服务"
|
||||
$serviceDescription = "PocketBase 后端服务 for 云升数码"
|
||||
|
||||
# 使用 NSSM (Non-Sucking Service Manager) 创建服务
|
||||
# 这里使用 sc.exe 创建简单服务
|
||||
$servicePath = "C:\Windows\System32\cmd.exe"
|
||||
$serviceArgs = "/c `"$installDir\start.bat`""
|
||||
|
||||
# 检查是否已安装 NSSM
|
||||
$nssmPath = "C:\pocketbase\nssm.exe"
|
||||
if (-not (Test-Path $nssmPath)) {
|
||||
Write-Log "下载 NSSM..."
|
||||
$nssmUrl = "https://github.com/nssm/nssm/releases/download/v2.24/nssm-2.24.zip"
|
||||
$nssmZip = "$env:TEMP\nssm.zip"
|
||||
Download-File $nssmUrl $nssmZip
|
||||
Expand-Archive -Path $nssmZip -DestinationPath "$env:TEMP\nssm" -Force
|
||||
Copy-Item "$env:TEMP\nssm\win64\nssm.exe" -Destination $nssmPath -Force
|
||||
Remove-Item $nssmZip -Force
|
||||
Remove-Item "$env:TEMP\nssm" -Recurse -Force
|
||||
}
|
||||
|
||||
# 安装服务
|
||||
& $nssmPath install $serviceName $servicePath $serviceArgs
|
||||
& $nssmPath set $serviceName DisplayName $serviceDisplayName
|
||||
& $nssmPath set $serviceName Description $serviceDescription
|
||||
& $nssmPath set $serviceName AppDirectory $installDir
|
||||
& $nssmPath set $serviceName AppEnvironmentExtra "PB_ENCRYPTION_KEY=$encryptionKey"
|
||||
& $nssmPath set $serviceName Start SERVICE_AUTO_START
|
||||
& $nssmPath set $serviceName AppStdout "$installDir\logs\stdout.log"
|
||||
& $nssmPath set $serviceName AppStderr "$installDir\logs\stderr.log"
|
||||
& $nssmPath set $serviceName AppRotateFiles 1
|
||||
& $nssmPath set $serviceName AppRotateBytes 10485760
|
||||
|
||||
New-Item -ItemType Directory -Force -Path "$installDir\logs" | Out-Null
|
||||
|
||||
Write-Log "PocketBase 服务创建完成" "SUCCESS"
|
||||
|
||||
return @{
|
||||
InstallDir = $installDir
|
||||
DataDir = $dataDir
|
||||
PublicDir = $publicDir
|
||||
EncryptionKey = $encryptionKey
|
||||
}
|
||||
}
|
||||
|
||||
# 安装 Nginx
|
||||
function Install-Nginx {
|
||||
param([string]$Domain)
|
||||
|
||||
if ($SkipNginx) {
|
||||
Write-Log "跳过 Nginx 安装" "WARN"
|
||||
return
|
||||
}
|
||||
|
||||
Write-Log "安装 Nginx..."
|
||||
|
||||
$nginxVersion = "1.25.3"
|
||||
$url = "https://nginx.org/download/nginx-$nginxVersion.zip"
|
||||
$installDir = "C:\nginx"
|
||||
$zipPath = "$env:TEMP\nginx.zip"
|
||||
|
||||
Download-File $url $zipPath
|
||||
Expand-Archive -Path $zipPath -DestinationPath $installDir -Force
|
||||
Remove-Item $zipPath -Force
|
||||
|
||||
# 找到解压后的实际目录
|
||||
$extractedDir = Get-ChildItem "$installDir" -Directory | Select-Object -First 1
|
||||
if ($extractedDir.FullName -ne $installDir) {
|
||||
Move-Item -Path "$extractedDir\*" -Destination $installDir -Force
|
||||
Remove-Item $extractedDir.FullName -Recurse -Force
|
||||
}
|
||||
|
||||
# 配置 Nginx
|
||||
$nginxConf = @"
|
||||
# 云升数码 - Nginx 配置
|
||||
worker_processes auto;
|
||||
error_log logs/error.log warn;
|
||||
pid logs/nginx.pid;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
use iocp;
|
||||
}
|
||||
|
||||
http {
|
||||
include mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
|
||||
'\$status \$body_bytes_sent "\$http_referer" '
|
||||
'"\$http_user_agent" "\$http_x_forwarded_for"';
|
||||
|
||||
access_log logs/access.log main;
|
||||
|
||||
sendfile on;
|
||||
tcp_nopush on;
|
||||
tcp_nodelay on;
|
||||
keepalive_timeout 65;
|
||||
types_hash_max_size 2048;
|
||||
client_max_body_size 100M;
|
||||
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_min_length 1024;
|
||||
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
|
||||
|
||||
# HTTP 重定向到 HTTPS
|
||||
server {
|
||||
listen 80;
|
||||
server_name $Domain www.$Domain;
|
||||
|
||||
location /.well-known/acme-challenge/ {
|
||||
root C:\nginx\acme;
|
||||
try_files \$uri =404;
|
||||
}
|
||||
|
||||
location / {
|
||||
return 301 https://\$host\$request_uri;
|
||||
}
|
||||
}
|
||||
|
||||
# HTTPS 主站点
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
server_name $Domain www.$Domain;
|
||||
|
||||
# SSL 证书路径 (Certbot 会自动配置)
|
||||
# ssl_certificate C:/Certbot/live/$Domain/fullchain.pem;
|
||||
# ssl_certificate_key C:/Certbot/live/$Domain/privkey.pem;
|
||||
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512;
|
||||
ssl_prefer_server_ciphers off;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_timeout 10m;
|
||||
|
||||
# 安全头
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||
|
||||
root $env:USERPROFILE\yunsheng-digital\apps\web\dist;
|
||||
index index.html;
|
||||
|
||||
# 静态文件缓存
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|webp|avif|mp4|webm|ogg|mp3|wav|pdf)$ {
|
||||
expires 1y;
|
||||
add_header Cache-Control "public, immutable";
|
||||
access_log off;
|
||||
try_files \$uri =404;
|
||||
}
|
||||
|
||||
# SPA 路由
|
||||
location / {
|
||||
try_files \$uri \$uri/ /index.html;
|
||||
}
|
||||
|
||||
# API 代理
|
||||
location /api/ {
|
||||
proxy_pass http://127.0.0.1:8090;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 60s;
|
||||
proxy_read_timeout 60s;
|
||||
client_max_body_size 100M;
|
||||
}
|
||||
|
||||
# WebSocket
|
||||
location /api/realtime {
|
||||
proxy_pass http://127.0.0.1:8090;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade \$http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host \$host;
|
||||
proxy_read_timeout 86400;
|
||||
}
|
||||
|
||||
# Admin UI
|
||||
location /_/ {
|
||||
proxy_pass http://127.0.0.1:8090;
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
}
|
||||
|
||||
# 禁止隐藏文件
|
||||
location ~ /\. {
|
||||
deny all;
|
||||
}
|
||||
}
|
||||
}
|
||||
"@
|
||||
|
||||
Set-Content -Path "$installDir\conf\nginx.conf" -Value $nginxConf -Encoding UTF8
|
||||
|
||||
# 创建 ACME 目录
|
||||
New-Item -ItemType Directory -Force -Path "C:\nginx\acme" | Out-Null
|
||||
|
||||
# 测试配置
|
||||
& "$installDir\nginx.exe" -t
|
||||
Write-Log "Nginx 配置测试通过" "SUCCESS"
|
||||
|
||||
# 创建服务
|
||||
$serviceName = "nginx"
|
||||
$nssmPath = "C:\pocketbase\nssm.exe"
|
||||
& $nssmPath install $serviceName "$installDir\nginx.exe" ""
|
||||
& $nssmPath set $serviceName DisplayName "Nginx Web Server"
|
||||
& $nssmPath set $serviceName Description "Nginx 反向代理 for 云升数码"
|
||||
& $nssmPath set $serviceName AppDirectory $installDir
|
||||
& $nssmPath set $serviceName Start SERVICE_AUTO_START
|
||||
|
||||
Write-Log "Nginx 安装完成" "SUCCESS"
|
||||
}
|
||||
|
||||
# 安装 Certbot (Windows)
|
||||
function Install-Certbot {
|
||||
param([string]$Domain, [string]$Email)
|
||||
|
||||
if ($SkipSSL) {
|
||||
Write-Log "跳过 SSL 证书申请" "WARN"
|
||||
return
|
||||
}
|
||||
|
||||
if (-not $Domain -or -not $Email) {
|
||||
Write-Log "域名或邮箱为空,跳过 SSL 证书申请" "WARN"
|
||||
return
|
||||
}
|
||||
|
||||
Write-Log "安装 Certbot..."
|
||||
|
||||
# 使用 Chocolatey 安装
|
||||
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
|
||||
Write-Log "安装 Chocolatey..."
|
||||
Set-ExecutionPolicy Bypass -Scope Process -Force
|
||||
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
|
||||
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
|
||||
}
|
||||
|
||||
choco install certbot -y --no-progress
|
||||
|
||||
# 申请证书
|
||||
Write-Log "申请 SSL 证书..."
|
||||
certbot certonly --standalone -d $Domain -d "www.$Domain" `
|
||||
--non-interactive --agree-tos --email $Email `
|
||||
--preferred-challenges http
|
||||
|
||||
Write-Log "SSL 证书申请完成" "SUCCESS"
|
||||
}
|
||||
|
||||
# 创建维护脚本
|
||||
function Create-MaintenanceScripts {
|
||||
$scriptsDir = "C:\pocketbase\scripts"
|
||||
New-Item -ItemType Directory -Force -Path $scriptsDir | Out-Null
|
||||
|
||||
# 备份脚本
|
||||
$backupScript = @"
|
||||
@echo off
|
||||
set BACKUP_DIR=C:\backup\yunsheng-digital
|
||||
set DATE=%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
|
||||
set DATE=%DATE: =0%
|
||||
mkdir "%BACKUP_DIR%" 2>nul
|
||||
|
||||
echo [INFO] 开始备份...
|
||||
|
||||
REM 备份 PocketBase 数据
|
||||
C:\pocketbase\pocketbase.exe dump --dir=C:\pocketbase\pb_data --output="%BACKUP_DIR%\pocketbase_%DATE%.zip"
|
||||
|
||||
REM 备份 Nginx 配置
|
||||
copy C:\nginx\conf\nginx.conf "%BACKUP_DIR%\nginx_%DATE%.conf"
|
||||
|
||||
REM 清理 30 天前的备份
|
||||
forfiles /p "%BACKUP_DIR%" /s /m *.* /d -30 /c "cmd /c del @path"
|
||||
|
||||
echo [SUCCESS] 备份完成
|
||||
"@
|
||||
Set-Content -Path "$scriptsDir\backup.bat" -Value $backupScript -Encoding UTF8
|
||||
|
||||
# 更新脚本
|
||||
$updateScript = @"
|
||||
@echo off
|
||||
cd /d "%USERPROFILE%\yunsheng-digital"
|
||||
echo [INFO] 拉取最新代码...
|
||||
git pull origin master
|
||||
|
||||
echo [INFO] 安装依赖...
|
||||
cd apps\web
|
||||
pnpm install --frozen-lockfile
|
||||
|
||||
echo [INFO] 构建前端...
|
||||
pnpm build
|
||||
|
||||
echo [INFO] 部署构建产物...
|
||||
xcopy /E /Y /I dist\* C:\pocketbase\pb_public\
|
||||
|
||||
echo [INFO] 重启服务...
|
||||
net stop nginx
|
||||
net stop PocketBase
|
||||
timeout /t 3
|
||||
net start PocketBase
|
||||
timeout /t 3
|
||||
net start nginx
|
||||
|
||||
echo [SUCCESS] 更新完成
|
||||
"@
|
||||
Set-Content -Path "$scriptsDir\update.bat" -Value $updateScript -Encoding UTF8
|
||||
|
||||
# 状态脚本
|
||||
$statusScript = @"
|
||||
@echo off
|
||||
echo === 云升数码 服务状态 ===
|
||||
echo.
|
||||
echo --- PocketBase ---
|
||||
sc query PocketBase
|
||||
echo.
|
||||
echo --- Nginx ---
|
||||
sc query nginx
|
||||
echo.
|
||||
echo --- 磁盘使用 ---
|
||||
wmic logicaldisk get size,freespace,caption
|
||||
echo.
|
||||
echo --- 网络连接 ---
|
||||
netstat -an | findstr "80 443 8090"
|
||||
"@
|
||||
Set-Content -Path "$scriptsDir\status.bat" -Value $statusScript -Encoding UTF8
|
||||
|
||||
Write-Log "维护脚本创建完成" "SUCCESS"
|
||||
}
|
||||
|
||||
# 配置防火墙
|
||||
function Configure-Firewall {
|
||||
Write-Log "配置 Windows 防火墙..."
|
||||
|
||||
# 允许 HTTP/HTTPS
|
||||
New-NetFirewallRule -DisplayName "云升数码 HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow -ErrorAction SilentlyContinue
|
||||
New-NetFirewallRule -DisplayName "云升数码 HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow -ErrorAction SilentlyContinue
|
||||
New-NetFirewallRule -DisplayName "云升数码 PocketBase" -Direction Inbound -Protocol TCP -LocalPort 8090 -Action Allow -Profile Private -ErrorAction SilentlyContinue
|
||||
|
||||
Write-Log "防火墙配置完成" "SUCCESS"
|
||||
}
|
||||
|
||||
# 创建计划任务
|
||||
function Create-ScheduledTasks {
|
||||
Write-Log "创建计划任务..."
|
||||
|
||||
# 每天备份
|
||||
$action = New-ScheduledTaskAction -Execute "C:\pocketbase\scripts\backup.bat"
|
||||
$trigger = New-ScheduledTaskTrigger -Daily -At 3am
|
||||
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd
|
||||
Register-ScheduledTask -TaskName "云升数码-每日备份" -Action $action -Trigger $trigger -Settings $settings -Force -ErrorAction SilentlyContinue
|
||||
|
||||
# 每周清理日志
|
||||
$action2 = New-ScheduledTaskAction -Execute "wevtutil.exe" -Argument "cl System /bu:C:\backup\system_logs\system_%date%.evtx"
|
||||
$trigger2 = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4am
|
||||
Register-ScheduledTask -TaskName "云升数码-周日志清理" -Action $action2 -Trigger $trigger2 -Settings $settings -Force -ErrorAction SilentlyContinue
|
||||
|
||||
Write-Log "计划任务创建完成" "SUCCESS"
|
||||
}
|
||||
|
||||
# 主函数
|
||||
function Main {
|
||||
Write-Host "==========================================" -ForegroundColor Cyan
|
||||
Write-Host " 云升数码 - Windows 一键部署脚本 v1.0" -ForegroundColor Cyan
|
||||
Write-Host "==========================================" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
Check-Admin
|
||||
|
||||
# 获取参数
|
||||
if (-not $Domain) {
|
||||
$Domain = Read-Host "请输入域名 (例: yunsheng.digital)"
|
||||
}
|
||||
|
||||
if (-not $Email) {
|
||||
$Email = Read-Host "请输入邮箱 (用于 SSL 证书)"
|
||||
}
|
||||
|
||||
Write-Log "开始部署..."
|
||||
Write-Log "域名: $Domain"
|
||||
Write-Log "邮箱: $Email"
|
||||
|
||||
# 克隆/更新代码
|
||||
$projectDir = "$env:USERPROFILE\yunsheng-digital"
|
||||
if (Test-Path $projectDir) {
|
||||
Write-Log "更新现有代码..."
|
||||
cd $projectDir
|
||||
git pull origin master
|
||||
} else {
|
||||
Write-Log "克隆仓库..."
|
||||
git clone https://git.grxiao.cn/yuns/plerr-open.git $projectDir
|
||||
}
|
||||
|
||||
# 安装 Node.js (如果需要)
|
||||
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
|
||||
Write-Log "安装 Node.js..."
|
||||
choco install nodejs-lts -y --no-progress
|
||||
refreshenv
|
||||
}
|
||||
|
||||
# 安装 pnpm
|
||||
if (-not (Get-Command pnpm -ErrorAction SilentlyContinue)) {
|
||||
Write-Log "安装 pnpm..."
|
||||
npm install -g pnpm
|
||||
}
|
||||
|
||||
# 构建前端
|
||||
Write-Log "构建前端..."
|
||||
cd "$projectDir\apps\web"
|
||||
pnpm install --frozen-lockfile
|
||||
pnpm build
|
||||
|
||||
# 复制构建产物到 PocketBase public 目录
|
||||
$pbPublic = "C:\pocketbase\pb_public"
|
||||
New-Item -ItemType Directory -Force -Path $pbPublic | Out-Null
|
||||
Copy-Item -Path "dist\*" -Destination $pbPublic -Recurse -Force
|
||||
Write-Log "前端构建产物已部署" "SUCCESS"
|
||||
|
||||
# 安装后端服务
|
||||
$pbInfo = Install-PocketBase
|
||||
|
||||
# 安装 Nginx
|
||||
Install-Nginx -Domain $Domain
|
||||
|
||||
# 配置防火墙
|
||||
Configure-Firewall
|
||||
|
||||
# 创建维护脚本
|
||||
Create-MaintenanceScripts
|
||||
|
||||
# 创建计划任务
|
||||
Create-ScheduledTasks
|
||||
|
||||
# SSL 证书
|
||||
Install-Certbot -Domain $Domain -Email $Email
|
||||
|
||||
# 启动服务
|
||||
Write-Log "启动服务..."
|
||||
net start PocketBase
|
||||
Start-Sleep -Seconds 3
|
||||
net start nginx
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "==========================================" -ForegroundColor Green
|
||||
Write-Host " 部署完成!" -ForegroundColor Green
|
||||
Write-Host "==========================================" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "📋 重要信息:" -ForegroundColor Cyan
|
||||
Write-Host " - 网站地址: https://$Domain"
|
||||
Write-Host " - 后台管理: https://$Domain/_/"
|
||||
Write-Host " - 管理员账号: admin@yunsheng.digital"
|
||||
Write-Host " - 管理员密码: YunSheng@2024!Admin"
|
||||
Write-Host " - PocketBase 目录: C:\pocketbase"
|
||||
Write-Host " - 前端构建目录: C:\pocketbase\pb_public"
|
||||
Write-Host ""
|
||||
Write-Host "🔐 安全提醒:" -ForegroundColor Yellow
|
||||
Write-Host " 1. 请立即登录后台修改默认管理员密码"
|
||||
Write-Host " 2. 加密密钥已保存在服务配置中"
|
||||
Write-Host " 3. 建议配置 RDP 限制、启用 BitLocker"
|
||||
Write-Host ""
|
||||
Write-Host "🛠️ 常用命令:" -ForegroundColor Cyan
|
||||
Write-Host " - 查看状态: C:\pocketbase\scripts\status.bat"
|
||||
Write-Host " - 备份数据: C:\pocketbase\scripts\backup.bat"
|
||||
Write-Host " - 更新代码: C:\pocketbase\scripts\update.bat"
|
||||
Write-Host " - 查看日志: C:\pocketbase\logs\"
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
Main
|
||||
@@ -0,0 +1,596 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# PocketBase 版本
|
||||
PB_VERSION="0.22.0"
|
||||
PB_DIR="/opt/pocketbase"
|
||||
PB_DATA_DIR="${PB_DIR}/pb_data"
|
||||
PB_MIGRATIONS_DIR="${PB_DIR}/pb_migrations"
|
||||
PB_PUBLIC_DIR="${PB_DIR}/pb_public"
|
||||
|
||||
# 颜色输出
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
log_info() { echo -e "${BLUE}[INFO]${NC} $*"; }
|
||||
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $*"; }
|
||||
log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
|
||||
log_error() { echo -e "${RED}[ERROR]${NC} $*"; }
|
||||
|
||||
# 检查是否为 root
|
||||
check_root() {
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
log_error "请使用 root 权限运行 (sudo ./install.sh)"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 检测系统架构
|
||||
detect_arch() {
|
||||
local arch=$(uname -m)
|
||||
case $arch in
|
||||
x86_64) echo "amd64" ;;
|
||||
aarch64|arm64) echo "arm64" ;;
|
||||
armv7l) echo "armv7" ;;
|
||||
*) log_error "不支持的架构: $arch"; exit 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# 安装依赖
|
||||
install_deps() {
|
||||
log_info "安装系统依赖..."
|
||||
apt update && apt upgrade -y
|
||||
apt install -y curl wget unzip nginx certbot python3-certbot-nginx ufw fail2ban
|
||||
log_success "系统依赖安装完成"
|
||||
}
|
||||
|
||||
# 下载并安装 PocketBase
|
||||
install_pocketbase() {
|
||||
local arch=$(detect_arch)
|
||||
local pb_url="https://github.com/pocketbase/pocketbase/releases/download/v${PB_VERSION}/pocketbase_${PB_VERSION}_linux_${arch}.zip"
|
||||
local tmp_dir=$(mktemp -d)
|
||||
|
||||
log_info "下载 PocketBase v${PB_VERSION} (${arch})..."
|
||||
cd "$tmp_dir"
|
||||
wget -q --show-progress "$pb_url" -O pocketbase.zip
|
||||
unzip -q pocketbase.zip
|
||||
|
||||
log_info "安装 PocketBase 到 ${PB_DIR}..."
|
||||
mkdir -p "$PB_DIR"
|
||||
mv pocketbase "$PB_DIR/"
|
||||
chmod +x "$PB_DIR/pocketbase"
|
||||
|
||||
# 创建数据目录
|
||||
mkdir -p "$PB_DATA_DIR" "$PB_MIGRATIONS_DIR" "$PB_PUBLIC_DIR"
|
||||
|
||||
# 复制迁移文件
|
||||
if [[ -d "$(dirname "$0")/../apps/pb_migrations" ]]; then
|
||||
cp -r "$(dirname "$0")/../apps/pb_migrations"/* "$PB_MIGRATIONS_DIR/"
|
||||
log_success "迁移文件已复制"
|
||||
fi
|
||||
|
||||
# 清理
|
||||
cd /
|
||||
rm -rf "$tmp_dir"
|
||||
|
||||
log_success "PocketBase 安装完成"
|
||||
}
|
||||
|
||||
# 生成加密密钥
|
||||
generate_encryption_key() {
|
||||
openssl rand -base64 32 | tr -d '\n'
|
||||
}
|
||||
|
||||
# 创建 systemd 服务
|
||||
create_systemd_service() {
|
||||
local encryption_key=$(generate_encryption_key)
|
||||
|
||||
log_info "创建 systemd 服务..."
|
||||
cat > /etc/systemd/system/pocketbase.service <<EOF
|
||||
[Unit]
|
||||
Description=PocketBase - 云升数码后端服务
|
||||
Documentation=https://pocketbase.io/docs/
|
||||
After=network.target network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=www-data
|
||||
Group=www-data
|
||||
WorkingDirectory=${PB_DIR}
|
||||
ExecStart=${PB_DIR}/pocketbase serve \\
|
||||
--http=127.0.0.1:8090 \\
|
||||
--dir=${PB_DATA_DIR} \\
|
||||
--publicDir=${PB_PUBLIC_DIR} \\
|
||||
--migrationsDir=${PB_MIGRATIONS_DIR} \\
|
||||
--encryptionEnv=PB_ENCRYPTION_KEY
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
StartLimitIntervalSec=60
|
||||
StartLimitBurst=3
|
||||
|
||||
# 安全配置
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
ReadWritePaths=${PB_DATA_DIR} ${PB_PUBLIC_DIR} ${PB_MIGRATIONS_DIR}
|
||||
ProtectKernelTunables=true
|
||||
ProtectKernelModules=true
|
||||
ProtectControlGroups=true
|
||||
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
||||
RestrictNamespaces=true
|
||||
LockPersonality=true
|
||||
MemoryDenyWriteExecute=true
|
||||
SystemCallFilter=@system-service
|
||||
SystemCallErrorNumber=EPERM
|
||||
|
||||
# 环境变量
|
||||
Environment=PB_ENCRYPTION_KEY=${encryption_key}
|
||||
Environment=GOGC=50
|
||||
|
||||
# 资源限制
|
||||
LimitNOFILE=65535
|
||||
LimitNPROC=4096
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
# 设置权限
|
||||
chown -R www-data:www-data "$PB_DIR"
|
||||
chmod 750 "$PB_DATA_DIR" "$PB_MIGRATIONS_DIR" "$PB_PUBLIC_DIR"
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable pocketbase
|
||||
|
||||
log_success "systemd 服务创建完成"
|
||||
log_info "加密密钥: ${encryption_key}"
|
||||
log_warn "请妥善保存加密密钥!迁移服务器时需要它来解密数据。"
|
||||
}
|
||||
|
||||
# 配置 Nginx
|
||||
configure_nginx() {
|
||||
local domain="$1"
|
||||
|
||||
if [[ -z "$domain" ]]; then
|
||||
log_warn "未提供域名,跳过 Nginx 配置"
|
||||
return
|
||||
fi
|
||||
|
||||
log_info "配置 Nginx 反向代理 (域名: ${domain})..."
|
||||
|
||||
cat > /etc/nginx/sites-available/yunsheng-digital <<EOF
|
||||
# 云升数码 - Nginx 配置
|
||||
# 生成时间: $(date)
|
||||
|
||||
# HTTP -> HTTPS 重定向
|
||||
server {
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
server_name ${domain} www.${domain};
|
||||
|
||||
# ACME 挑战路径 (用于 SSL 证书验证)
|
||||
location /.well-known/acme-challenge/ {
|
||||
root /var/www/html;
|
||||
try_files \$uri =404;
|
||||
}
|
||||
|
||||
# 其他请求重定向到 HTTPS
|
||||
location / {
|
||||
return 301 https://\$host\$request_uri;
|
||||
}
|
||||
}
|
||||
|
||||
# HTTPS 主站点
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
listen [::]:443 ssl http2;
|
||||
server_name ${domain} www.${domain};
|
||||
|
||||
# SSL 证书 (由 Certbot 自动填充)
|
||||
# ssl_certificate /etc/letsencrypt/live/${domain}/fullchain.pem;
|
||||
# ssl_certificate_key /etc/letsencrypt/live/${domain}/privkey.pem;
|
||||
|
||||
# SSL 安全配置
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
|
||||
ssl_prefer_server_ciphers off;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_timeout 10m;
|
||||
|
||||
# 安全头
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||
add_header Permissions-Policy "accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()" always;
|
||||
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' data: https://fonts.gstatic.com; img-src 'self' data: https:; media-src 'self' https:; connect-src 'self' wss: https:; frame-ancestors 'self';" always;
|
||||
|
||||
# 根目录
|
||||
root ${PB_PUBLIC_DIR};
|
||||
index index.html;
|
||||
|
||||
# Gzip 压缩
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_min_length 1024;
|
||||
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
|
||||
|
||||
# 静态文件缓存
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|webp|avif|mp4|webm|ogg|mp3|wav|pdf)$ {
|
||||
expires 1y;
|
||||
add_header Cache-Control "public, immutable";
|
||||
access_log off;
|
||||
try_files \$uri =404;
|
||||
}
|
||||
|
||||
# 前端路由 (SPA fallback)
|
||||
location / {
|
||||
try_files \$uri \$uri/ /index.html;
|
||||
}
|
||||
|
||||
# PocketBase API 代理
|
||||
location /api/ {
|
||||
proxy_pass http://127.0.0.1:8090;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
proxy_set_header X-Forwarded-Host \$host;
|
||||
proxy_set_header X-Forwarded-Port \$server_port;
|
||||
|
||||
# 超时设置
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 60s;
|
||||
proxy_read_timeout 60s;
|
||||
|
||||
# 缓冲
|
||||
proxy_buffering on;
|
||||
proxy_buffer_size 128k;
|
||||
proxy_buffers 4 256k;
|
||||
proxy_busy_buffers_size 256k;
|
||||
|
||||
# 文件上传大小限制
|
||||
client_max_body_size 100M;
|
||||
}
|
||||
|
||||
# Realtime WebSocket
|
||||
location /api/realtime {
|
||||
proxy_pass http://127.0.0.1:8090;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade \$http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
|
||||
# WebSocket 超时
|
||||
proxy_read_timeout 86400;
|
||||
proxy_send_timeout 86400;
|
||||
}
|
||||
|
||||
# PocketBase Admin UI
|
||||
location /_/ {
|
||||
proxy_pass http://127.0.0.1:8090;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
|
||||
# 限制 Admin UI 访问 (可选:仅允许特定 IP)
|
||||
# allow 1.2.3.4;
|
||||
# deny all;
|
||||
}
|
||||
|
||||
# 禁止访问隐藏文件
|
||||
location ~ /\. {
|
||||
deny all;
|
||||
access_log off;
|
||||
log_not_found off;
|
||||
}
|
||||
|
||||
# 禁止访问备份文件
|
||||
location ~* \.(bak|backup|sql|log|env|ini)$ {
|
||||
deny all;
|
||||
access_log off;
|
||||
log_not_found off;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
# 启用站点
|
||||
ln -sf /etc/nginx/sites-available/yunsheng-digital /etc/nginx/sites-enabled/
|
||||
rm -f /etc/nginx/sites-enabled/default
|
||||
|
||||
# 测试配置
|
||||
nginx -t
|
||||
|
||||
log_success "Nginx 配置完成"
|
||||
}
|
||||
|
||||
# 申请 SSL 证书
|
||||
setup_ssl() {
|
||||
local domain="$1"
|
||||
local email="$2"
|
||||
|
||||
if [[ -z "$domain" || -z "$email" ]]; then
|
||||
log_warn "未提供域名或邮箱,跳过 SSL 证书申请"
|
||||
log_info "稍后可手动运行: certbot --nginx -d ${domain}"
|
||||
return
|
||||
fi
|
||||
|
||||
log_info "申请 Let's Encrypt SSL 证书..."
|
||||
|
||||
# 先启动 nginx (HTTP 模式)
|
||||
systemctl restart nginx
|
||||
|
||||
# 申请证书
|
||||
certbot --nginx -d "${domain}" -d "www.${domain}" \
|
||||
--non-interactive \
|
||||
--agree-tos \
|
||||
--email "${email}" \
|
||||
--redirect \
|
||||
--hsts \
|
||||
--staple-ocsp \
|
||||
--must-staple
|
||||
|
||||
# 设置自动续期
|
||||
systemctl enable certbot.timer
|
||||
|
||||
log_success "SSL 证书申请完成"
|
||||
}
|
||||
|
||||
# 配置防火墙
|
||||
configure_firewall() {
|
||||
log_info "配置 UFW 防火墙..."
|
||||
|
||||
ufw --force enable
|
||||
ufw default deny incoming
|
||||
ufw default allow outgoing
|
||||
ufw allow ssh
|
||||
ufw allow 80/tcp
|
||||
ufw allow 443/tcp
|
||||
|
||||
# 仅允许本地访问 PocketBase 直接端口
|
||||
ufw allow from 127.0.0.1 to any port 8090
|
||||
|
||||
log_success "防火墙配置完成"
|
||||
}
|
||||
|
||||
# 配置 Fail2Ban
|
||||
configure_fail2ban() {
|
||||
log_info "配置 Fail2Ban..."
|
||||
|
||||
cat > /etc/fail2ban/jail.d/yunsheng-digital.conf <<EOF
|
||||
[DEFAULT]
|
||||
bantime = 3600
|
||||
findtime = 600
|
||||
maxretry = 5
|
||||
backend = systemd
|
||||
|
||||
[nginx-http-auth]
|
||||
enabled = true
|
||||
port = http,https
|
||||
logpath = /var/log/nginx/error.log
|
||||
|
||||
[nginx-limit-req]
|
||||
enabled = true
|
||||
port = http,https
|
||||
logpath = /var/log/nginx/error.log
|
||||
maxretry = 10
|
||||
|
||||
[nginx-badbots]
|
||||
enabled = true
|
||||
port = http,https
|
||||
logpath = /var/log/nginx/access.log
|
||||
maxretry = 3
|
||||
|
||||
[sshd]
|
||||
enabled = true
|
||||
port = ssh
|
||||
logpath = /var/log/auth.log
|
||||
maxretry = 3
|
||||
bantime = 86400
|
||||
EOF
|
||||
|
||||
systemctl enable fail2ban
|
||||
systemctl restart fail2ban
|
||||
|
||||
log_success "Fail2Ban 配置完成"
|
||||
}
|
||||
|
||||
# 创建维护脚本
|
||||
create_maintenance_scripts() {
|
||||
log_info "创建维护脚本..."
|
||||
|
||||
# 备份脚本
|
||||
cat > /usr/local/bin/yunsheng-backup <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
BACKUP_DIR="/backup/yunsheng-digital"
|
||||
DATE=$(date +%Y%m%d_%H%M%S)
|
||||
PB_DATA_DIR="/opt/pocketbase/pb_data"
|
||||
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
|
||||
log_info() { echo -e "\033[0;34m[INFO]\033[0m $*"; }
|
||||
log_success() { echo -e "\033[0;32m[SUCCESS]\033[0m $*"; }
|
||||
|
||||
log_info "开始备份..."
|
||||
|
||||
# 备份 PocketBase 数据 (使用内置 dump 命令)
|
||||
/opt/pocketbase/pocketbase dump \
|
||||
--dir=/opt/pocketbase/pb_data \
|
||||
--output="${BACKUP_DIR}/pocketbase_${DATE}.zip"
|
||||
|
||||
# 备份 Nginx 配置
|
||||
cp -r /etc/nginx/sites-available/yunsheng-digital "${BACKUP_DIR}/nginx_${DATE}.conf"
|
||||
|
||||
# 备份 SSL 证书
|
||||
cp -r /etc/letsencrypt "${BACKUP_DIR}/letsencrypt_${DATE}"
|
||||
|
||||
# 清理 30 天前的备份
|
||||
find "$BACKUP_DIR" -type f -mtime +30 -delete
|
||||
|
||||
log_success "备份完成: ${BACKUP_DIR}/pocketbase_${DATE}.zip"
|
||||
EOF
|
||||
|
||||
chmod +x /usr/local/bin/yunsheng-backup
|
||||
|
||||
# 更新脚本
|
||||
cat > /usr/local/bin/yunsheng-update <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
PROJECT_DIR="/opt/yunsheng-digital"
|
||||
PB_DIR="/opt/pocketbase"
|
||||
PB_PUBLIC_DIR="${PB_DIR}/pb_public"
|
||||
|
||||
log_info() { echo -e "\033[0;34m[INFO]\033[0m $*"; }
|
||||
log_success() { echo -e "\033[0;32m[SUCCESS]\033[0m $*"; }
|
||||
log_error() { echo -e "\033[0;31m[ERROR]\033[0m $*"; }
|
||||
|
||||
cd "$PROJECT_DIR"
|
||||
|
||||
log_info "拉取最新代码..."
|
||||
git pull origin master
|
||||
|
||||
log_info "安装前端依赖..."
|
||||
cd apps/web
|
||||
pnpm install --frozen-lockfile
|
||||
|
||||
log_info "构建前端..."
|
||||
pnpm build
|
||||
|
||||
log_info "部署构建产物..."
|
||||
cp -r dist/* "$PB_PUBLIC_DIR/"
|
||||
|
||||
log_info "重载 Nginx..."
|
||||
systemctl reload nginx
|
||||
|
||||
log_success "更新完成!"
|
||||
EOF
|
||||
|
||||
chmod +x /usr/local/bin/yunsheng-update
|
||||
|
||||
# 状态检查脚本
|
||||
cat > /usr/local/bin/yunsheng-status <<'EOF'
|
||||
#!/usr/bin/env bash
|
||||
|
||||
echo "=== 云升数码 服务状态 ==="
|
||||
echo
|
||||
|
||||
echo "--- PocketBase ---"
|
||||
systemctl status pocketbase --no-pager -l
|
||||
|
||||
echo
|
||||
echo "--- Nginx ---"
|
||||
systemctl status nginx --no-pager -l
|
||||
|
||||
echo
|
||||
echo "--- 磁盘使用 ---"
|
||||
df -h /opt/pocketbase
|
||||
|
||||
echo
|
||||
echo "--- 内存使用 ---"
|
||||
free -h
|
||||
|
||||
echo
|
||||
echo "--- 网络连接 ---"
|
||||
ss -tlnp | grep -E '(80|443|8090)'
|
||||
EOF
|
||||
|
||||
chmod +x /usr/local/bin/yunsheng-status
|
||||
|
||||
log_success "维护脚本创建完成"
|
||||
}
|
||||
|
||||
# 设置定时任务
|
||||
setup_cron() {
|
||||
log_info "设置定时任务..."
|
||||
|
||||
# 每天凌晨 3 点备份
|
||||
(crontab -l 2>/dev/null | grep -v yunsheng-backup; echo "0 3 * * * /usr/local/bin/yunsheng-backup >> /var/log/yunsheng-backup.log 2>&1") | crontab -
|
||||
|
||||
# 每周一凌晨 4 点清理日志
|
||||
(crontab -l 2>/dev/null | grep -v "journalctl"; echo "0 4 * * 1 journalctl --vacuum-time=30d >> /var/log/yunsheng-cleanup.log 2>&1") | crontab -
|
||||
|
||||
log_success "定时任务设置完成"
|
||||
}
|
||||
|
||||
# 主函数
|
||||
main() {
|
||||
echo "=========================================="
|
||||
echo " 云升数码 - 一键部署脚本 v1.0"
|
||||
echo "=========================================="
|
||||
echo
|
||||
|
||||
check_root
|
||||
|
||||
# 获取参数
|
||||
DOMAIN="${1:-}"
|
||||
EMAIL="${2:-}"
|
||||
|
||||
if [[ -z "$DOMAIN" ]]; then
|
||||
read -rp "请输入域名 (例: yunsheng.digital): " DOMAIN
|
||||
fi
|
||||
|
||||
if [[ -z "$EMAIL" ]]; then
|
||||
read -rp "请输入邮箱 (用于 SSL 证书): " EMAIL
|
||||
fi
|
||||
|
||||
log_info "开始部署..."
|
||||
log_info "域名: ${DOMAIN}"
|
||||
log_info "邮箱: ${EMAIL}"
|
||||
|
||||
install_deps
|
||||
install_pocketbase
|
||||
create_systemd_service
|
||||
configure_nginx "$DOMAIN"
|
||||
setup_ssl "$DOMAIN" "$EMAIL"
|
||||
configure_firewall
|
||||
configure_fail2ban
|
||||
create_maintenance_scripts
|
||||
setup_cron
|
||||
|
||||
# 启动服务
|
||||
log_info "启动服务..."
|
||||
systemctl start pocketbase
|
||||
sleep 3
|
||||
systemctl restart nginx
|
||||
|
||||
echo
|
||||
echo "=========================================="
|
||||
log_success "部署完成!"
|
||||
echo "=========================================="
|
||||
echo
|
||||
echo "📋 重要信息:"
|
||||
echo " - 网站地址: https://${DOMAIN}"
|
||||
echo " - 后台管理: https://${DOMAIN}/_/"
|
||||
echo " - 管理员账号: admin@yunsheng.digital"
|
||||
echo " - 管理员密码: YunSheng@2024!Admin"
|
||||
echo " - PocketBase 数据目录: ${PB_DATA_DIR}"
|
||||
echo " - 前端构建目录: ${PB_PUBLIC_DIR}"
|
||||
echo
|
||||
echo "🔐 安全提醒:"
|
||||
echo " 1. 请立即登录后台修改默认管理员密码"
|
||||
echo " 2. 加密密钥已保存在 systemd 服务文件中"
|
||||
echo " 3. 建议配置 SSH 密钥登录,禁用密码登录"
|
||||
echo
|
||||
echo "🛠️ 常用命令:"
|
||||
echo " - 查看状态: yunsheng-status"
|
||||
echo " - 备份数据: yunsheng-backup"
|
||||
echo " - 更新代码: yunsheng-update"
|
||||
echo " - 查看日志: journalctl -u pocketbase -f"
|
||||
echo
|
||||
}
|
||||
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user