完善 Scert 前后端与部署支持
This commit is contained in:
1 parent
afdb947bea
commit
7f0af703ed
85 files changed
+7530
-800
No files matched your search
+96
-3
@@ -9,6 +9,9 @@ deploy/
|
||||
├── install-linux.sh
|
||||
├── scert
|
||||
├── scert-admin
|
||||
├── web/
|
||||
│ ├── index.html
|
||||
│ └── assets/
|
||||
└── systemd/
|
||||
├── Scert.service
|
||||
├── Scert-admin.service
|
||||
@@ -37,8 +40,8 @@ sudo ./install-linux.sh --source-dir /tmp/scert-linux-amd64
|
||||
|
||||
1. 检查 Linux、root 权限、`systemctl` 和安装产物。
|
||||
2. 创建 `Scert` 系统用户和同名系统用户组,禁止交互式登录。
|
||||
3. 创建 `/scert`、`/scert/conf`、`/scert/data`、`/scert/log`、`/scert/backups`、`/scert/docs` 和证书目录。
|
||||
4. 安装 `scert`、`scert-admin` 以及两个 systemd unit。
|
||||
3. 创建 `/scert`、`/scert/conf`、`/scert/data`、`/scert/log`、`/scert/backups`、`/scert/docs`、`/scert/web` 和证书目录。
|
||||
4. 安装 `scert`、`scert-admin`、Web 静态资源以及两个 systemd unit。
|
||||
5. 设置安装目录、服务目录和配置目录权限,防止 `Scert` 运行用户替换 root 所有的程序文件。
|
||||
6. 为证书根目录生成 systemd drop-in,并执行一次 `systemctl daemon-reload`。
|
||||
|
||||
@@ -64,9 +67,99 @@ systemctl stop Scert-admin
|
||||
|
||||
## 参数说明
|
||||
|
||||
- `--source-dir`:包含两个 Linux 可执行文件的目录,默认是安装器所在目录。
|
||||
- `--source-dir`:包含两个 Linux 可执行文件和 `web/index.html` 的目录,默认是安装器所在目录。
|
||||
- `--certificate-dir`:证书根目录参数,默认 `/ssl`。安装器会创建该目录,写入 `/scert/conf/installation.json`,并生成对应的 `Scert.service.d/10-certificate-path.conf`。首次初始化会读取该记录作为证书目录默认值。
|
||||
|
||||
Linux 安装目录当前固定为 `/scert`。自定义证书根目录必须是规范化的安全绝对路径,只允许字母、数字、点、下划线、连字符和斜杠,不允许空格、`..` 路径片段、符号链接、受保护系统目录或 `/scert` 下的路径;`/var` 下仅允许 `/var/lib/scert` 专用目录。
|
||||
|
||||
安装器是文件部署阶段,不负责替代首次初始化向导,也不代替真实 ACME 申请和远程部署验收。
|
||||
|
||||
## 开发阶段反向代理部署
|
||||
|
||||
开发阶段可以使用测试域名 `web.xuans.com` 验证 Web 页面和反向代理链路。推荐让 Scert 只监听本机端口 `9003`,由 Nginx 对外提供 HTTPS:
|
||||
|
||||
```text
|
||||
公网 HTTPS 443
|
||||
↓
|
||||
Nginx
|
||||
↓ https://127.0.0.1:9003
|
||||
Scert
|
||||
```
|
||||
|
||||
对应配置为:
|
||||
|
||||
```json
|
||||
{
|
||||
"server": {
|
||||
"host": "127.0.0.1",
|
||||
"port": 9003,
|
||||
"https_mode": "proxy"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Nginx 示例:
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
server_name web.xuans.com;
|
||||
|
||||
ssl_certificate /etc/letsencrypt/live/web.xuans.com/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/web.xuans.com/privkey.pem;
|
||||
|
||||
location / {
|
||||
proxy_pass https://127.0.0.1:9003;
|
||||
proxy_ssl_server_name on;
|
||||
proxy_ssl_name web.xuans.com;
|
||||
proxy_ssl_verify off;
|
||||
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 https;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
如果需要 HTTP 自动跳转,再配置并开放 `80`;正常访问只需开放公网 `443`。`9003` 仅供本机 Nginx 使用,不应加入公网防火墙规则。端口可以在配置文件的 `server.port` 修改,修改后需要重启 Scert;如果改为直接对外监听,则还需要同步调整防火墙和安全组规则。
|
||||
|
||||
`web.xuans.com` 仅用于开发阶段测试,不能作为生产环境证书、DNS、ACME 或最终 HTTPS 验收依据。
|
||||
|
||||
## Windows 部署
|
||||
|
||||
Windows 发布目录应包含:
|
||||
|
||||
```text
|
||||
scert.exe
|
||||
scert-admin.exe
|
||||
install-windows.ps1
|
||||
```
|
||||
|
||||
使用管理员 PowerShell 执行:
|
||||
|
||||
```powershell
|
||||
powershell -ExecutionPolicy Bypass -File .\install-windows.ps1 -SourceDirectory .
|
||||
```
|
||||
|
||||
默认目录:
|
||||
|
||||
- 程序目录:`C:\Program Files\Scert`
|
||||
- 配置和运行数据目录:`C:\ProgramData\Scert`
|
||||
- 证书目录:`C:\ProgramData\Scert\ssl`
|
||||
|
||||
安装器会检查 Windows、管理员权限、`sc.exe`、`icacls.exe` 和两个程序产物;创建配置、数据、日志、备份、文档、运行时和证书目录;设置 Windows ACL;使用 `LocalService` 身份注册 `Scert` 与 `Scert-admin` 服务;并写入不含凭据的安装记录。
|
||||
|
||||
安装器不会删除或覆盖已有配置、数据库、证书和备份,不会启动或重启服务,也不会创建公网防火墙规则。`Scert` 注册为自动启动,`Scert-admin` 注册为手动启动;只有确需密码重置时才应由管理员启动 `Scert-admin`,完成后立即停止。
|
||||
|
||||
自定义目录示例:
|
||||
|
||||
```powershell
|
||||
powershell -ExecutionPolicy Bypass -File .\install-windows.ps1 `
|
||||
-SourceDirectory D:\release\Scert\windows-amd64 `
|
||||
-InstallDirectory D:\Apps\Scert `
|
||||
-DataDirectory D:\ScertData `
|
||||
-CertificateDirectory D:\ScertCertificates
|
||||
```
|
||||
|
||||
Windows Service 实机安装、开机启动、停止、升级和 ACL 仍需在授权 Windows 主机上验收。
|
||||
+11
-1
@@ -113,6 +113,7 @@ validate_managed_paths() {
|
||||
"$INSTALL_DIR/log" \
|
||||
"$INSTALL_DIR/backups" \
|
||||
"$INSTALL_DIR/docs" \
|
||||
"$INSTALL_DIR/web" \
|
||||
"$INSTALL_DIR/scert" \
|
||||
"$INSTALL_DIR/scert-admin" \
|
||||
"$INSTALL_METADATA_FILE" \
|
||||
@@ -130,7 +131,7 @@ validate_environment() {
|
||||
[[ "$(uname -s)" == "Linux" ]] || fail "此安装器只支持 Linux"
|
||||
[[ "$(id -u)" -eq 0 ]] || fail "请使用 root 或 sudo 运行安装器"
|
||||
local command_name
|
||||
for command_name in install systemctl getent groupadd useradd id chmod chown cut grep readlink; do
|
||||
for command_name in install systemctl getent groupadd useradd id chmod chown cut grep readlink find; do
|
||||
command -v "$command_name" >/dev/null 2>&1 || fail "找不到系统命令:$command_name"
|
||||
done
|
||||
nologin_shell="$(command -v nologin || true)"
|
||||
@@ -147,6 +148,7 @@ validate_artifacts() {
|
||||
[[ -f "$source_dir/$artifact" ]] || fail "安装产物不存在:$source_dir/$artifact"
|
||||
[[ -x "$source_dir/$artifact" ]] || fail "安装产物不可执行:$source_dir/$artifact"
|
||||
done
|
||||
[[ -f "$source_dir/web/index.html" ]] || fail "缺少前端入口文件:$source_dir/web/index.html"
|
||||
[[ -f "$SCRIPT_DIR/systemd/Scert.service" ]] || fail "缺少 Scert systemd unit"
|
||||
[[ -f "$SCRIPT_DIR/systemd/Scert-admin.service" ]] || fail "缺少 Scert-admin systemd unit"
|
||||
[[ -f "$SCRIPT_DIR/systemd/Scert-admin服务管理说明.md" ]] || fail "缺少 Scert-admin 服务管理说明"
|
||||
@@ -184,6 +186,7 @@ ensure_service_account() {
|
||||
create_directories() {
|
||||
install -d -o root -g "$SERVICE_GROUP" -m 0750 "$INSTALL_DIR"
|
||||
install -d -o root -g root -m 0755 "$INSTALL_DIR/docs"
|
||||
install -d -o root -g root -m 0755 "$INSTALL_DIR/web"
|
||||
install -d -o "$SERVICE_USER" -g "$SERVICE_GROUP" -m 0750 \
|
||||
"$INSTALL_DIR/conf" \
|
||||
"$INSTALL_DIR/data" \
|
||||
@@ -217,6 +220,12 @@ install_binaries() {
|
||||
install -o root -g root -m 0755 "$source_dir/scert-admin" "$INSTALL_DIR/scert-admin"
|
||||
}
|
||||
|
||||
install_web() {
|
||||
cp -a "$source_dir/web/." "$INSTALL_DIR/web/"
|
||||
find "$INSTALL_DIR/web" -type d -exec chmod 0755 {} +
|
||||
find "$INSTALL_DIR/web" -type f -exec chown root:root {} + -exec chmod 0644 {} +
|
||||
}
|
||||
|
||||
install_service_files() {
|
||||
install -o root -g root -m 0644 "$SCRIPT_DIR/systemd/Scert.service" "$SERVICE_DIR/Scert.service"
|
||||
install -o root -g root -m 0644 "$SCRIPT_DIR/systemd/Scert-admin.service" "$SERVICE_DIR/Scert-admin.service"
|
||||
@@ -269,6 +278,7 @@ main() {
|
||||
create_directories
|
||||
write_install_metadata
|
||||
install_binaries
|
||||
install_web
|
||||
install_service_files
|
||||
reload_systemd
|
||||
print_completion
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string]$SourceDirectory = "",
|
||||
[string]$InstallDirectory = (Join-Path $env:ProgramFiles "Scert"),
|
||||
[string]$DataDirectory = (Join-Path $env:ProgramData "Scert"),
|
||||
[string]$CertificateDirectory = (Join-Path $env:ProgramData "Scert\ssl"),
|
||||
[ValidatePattern("^[A-Za-z0-9_.-]+$")]
|
||||
[string]$ServiceName = "Scert",
|
||||
[ValidatePattern("^[A-Za-z0-9_.-]+$")]
|
||||
[string]$AdminServiceName = "Scert-admin"
|
||||
)
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
||||
if ([string]::IsNullOrWhiteSpace($SourceDirectory)) {
|
||||
$SourceDirectory = $scriptDirectory
|
||||
}
|
||||
|
||||
function Test-Administrator {
|
||||
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
||||
$principal = [Security.Principal.WindowsPrincipal]::new($identity)
|
||||
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
||||
}
|
||||
|
||||
function Resolve-RequiredDirectory {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Path,
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Label
|
||||
)
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($Path)) {
|
||||
throw "$Label 不能为空。"
|
||||
}
|
||||
$resolved = [System.IO.Path]::GetFullPath($Path)
|
||||
$root = [System.IO.Path]::GetPathRoot($resolved)
|
||||
if ($resolved -eq $root) {
|
||||
throw "$Label 不能使用磁盘根目录:$resolved"
|
||||
}
|
||||
return $resolved.TrimEnd([System.IO.Path]::DirectorySeparatorChar)
|
||||
}
|
||||
|
||||
function Invoke-Sc {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string[]]$Arguments,
|
||||
[switch]$AllowFailure
|
||||
)
|
||||
|
||||
$output = & sc.exe @Arguments 2>&1
|
||||
if ($LASTEXITCODE -ne 0 -and -not $AllowFailure) {
|
||||
throw "Windows 服务命令执行失败:sc.exe $($Arguments -join ' ')`n$($output -join "`n")"
|
||||
}
|
||||
return $output
|
||||
}
|
||||
|
||||
function Test-ServiceExists {
|
||||
param([Parameter(Mandatory = $true)][string]$Name)
|
||||
& sc.exe query $Name *> $null
|
||||
return $LASTEXITCODE -eq 0
|
||||
}
|
||||
|
||||
function Install-OrUpdateService {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$Name,
|
||||
[Parameter(Mandatory = $true)][string]$DisplayName,
|
||||
[Parameter(Mandatory = $true)][string]$Description,
|
||||
[Parameter(Mandatory = $true)][string]$BinaryPath,
|
||||
[Parameter(Mandatory = $true)][ValidateSet("auto", "demand")][string]$StartMode
|
||||
)
|
||||
|
||||
if (Test-ServiceExists -Name $Name) {
|
||||
Invoke-Sc -Arguments @("config", $Name, "binPath=", $BinaryPath, "start=", $StartMode, "obj=", "NT AUTHORITY\LocalService", "DisplayName=", $DisplayName) | Out-Null
|
||||
}
|
||||
else {
|
||||
Invoke-Sc -Arguments @("create", $Name, "binPath=", $BinaryPath, "start=", $StartMode, "obj=", "NT AUTHORITY\LocalService", "DisplayName=", $DisplayName) | Out-Null
|
||||
}
|
||||
Invoke-Sc -Arguments @("description", $Name, $Description) | Out-Null
|
||||
}
|
||||
|
||||
function Set-DirectoryAcl {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$Path,
|
||||
[Parameter(Mandatory = $true)][ValidateSet("read", "modify")][string]$Mode
|
||||
)
|
||||
|
||||
$localServicePermission = if ($Mode -eq "modify") { "(OI)(CI)(M)" } else { "(OI)(CI)(RX)" }
|
||||
& icacls.exe $Path /inheritance:r /grant:r `
|
||||
"BUILTIN\Administrators:(OI)(CI)(F)" `
|
||||
"NT AUTHORITY\SYSTEM:(OI)(CI)(F)" `
|
||||
"NT AUTHORITY\LOCAL SERVICE:$localServicePermission" *> $null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "设置目录访问控制失败:$Path"
|
||||
}
|
||||
}
|
||||
|
||||
if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) {
|
||||
throw "该安装器只能在 Windows 上运行。"
|
||||
}
|
||||
if (-not (Test-Administrator)) {
|
||||
throw "请使用管理员权限运行 Scert Windows 安装器。"
|
||||
}
|
||||
if (-not (Get-Command sc.exe -ErrorAction SilentlyContinue)) {
|
||||
throw "系统中没有找到 sc.exe,无法注册 Windows 服务。"
|
||||
}
|
||||
if (-not (Get-Command icacls.exe -ErrorAction SilentlyContinue)) {
|
||||
throw "系统中没有找到 icacls.exe,无法设置安全权限。"
|
||||
}
|
||||
|
||||
$source = Resolve-RequiredDirectory -Path $SourceDirectory -Label "安装产物目录"
|
||||
$install = Resolve-RequiredDirectory -Path $InstallDirectory -Label "程序安装目录"
|
||||
$data = Resolve-RequiredDirectory -Path $DataDirectory -Label "数据目录"
|
||||
$certificate = Resolve-RequiredDirectory -Path $CertificateDirectory -Label "证书目录"
|
||||
|
||||
$sourceScert = Join-Path $source "scert.exe"
|
||||
$sourceAdmin = Join-Path $source "scert-admin.exe"
|
||||
if (-not (Test-Path -LiteralPath $sourceScert -PathType Leaf)) {
|
||||
throw "安装产物目录缺少 scert.exe:$sourceScert"
|
||||
}
|
||||
if (-not (Test-Path -LiteralPath $sourceAdmin -PathType Leaf)) {
|
||||
throw "安装产物目录缺少 scert-admin.exe:$sourceAdmin"
|
||||
}
|
||||
|
||||
$configDirectory = Join-Path $data "config"
|
||||
$databaseDirectory = Join-Path $data "data"
|
||||
$logDirectory = Join-Path $data "logs"
|
||||
$backupDirectory = Join-Path $data "backups"
|
||||
$docsDirectory = Join-Path $data "docs"
|
||||
$runtimeDirectory = Join-Path $data "runtime"
|
||||
|
||||
foreach ($directory in @($install, $data, $configDirectory, $databaseDirectory, $logDirectory, $backupDirectory, $docsDirectory, $runtimeDirectory, $certificate)) {
|
||||
New-Item -ItemType Directory -Path $directory -Force | Out-Null
|
||||
}
|
||||
|
||||
$installedScert = Join-Path $install "scert.exe"
|
||||
$installedAdmin = Join-Path $install "scert-admin.exe"
|
||||
Copy-Item -LiteralPath $sourceScert -Destination $installedScert -Force
|
||||
Copy-Item -LiteralPath $sourceAdmin -Destination $installedAdmin -Force
|
||||
|
||||
Set-DirectoryAcl -Path $install -Mode read
|
||||
foreach ($directory in @($data, $configDirectory, $databaseDirectory, $logDirectory, $backupDirectory, $runtimeDirectory, $certificate)) {
|
||||
Set-DirectoryAcl -Path $directory -Mode modify
|
||||
}
|
||||
Set-DirectoryAcl -Path $docsDirectory -Mode read
|
||||
|
||||
$configurationPath = Join-Path $configDirectory "scert.json"
|
||||
$resetCodePath = Join-Path $runtimeDirectory "reset-code"
|
||||
$mainBinaryPath = '"{0}" --config "{1}" serve' -f $installedScert, $configurationPath
|
||||
$adminBinaryPath = '"{0}" --config "{1}" --code-output "{2}"' -f $installedAdmin, $configurationPath, $resetCodePath
|
||||
|
||||
Install-OrUpdateService `
|
||||
-Name $ServiceName `
|
||||
-DisplayName "Scert SSL 证书管理服务" `
|
||||
-Description "Scert SSL 证书申请、续期和部署服务" `
|
||||
-BinaryPath $mainBinaryPath `
|
||||
-StartMode auto
|
||||
|
||||
Install-OrUpdateService `
|
||||
-Name $AdminServiceName `
|
||||
-DisplayName "Scert-admin 临时密码重置服务" `
|
||||
-Description "仅在管理员手动启动时临时激活 Scert 密码重置入口" `
|
||||
-BinaryPath $adminBinaryPath `
|
||||
-StartMode demand
|
||||
|
||||
$metadata = [ordered]@{
|
||||
installed_at = [DateTimeOffset]::Now.ToString("o")
|
||||
install_directory = $install
|
||||
data_directory = $data
|
||||
config_directory = $configDirectory
|
||||
certificate_root = $certificate
|
||||
service_name = $ServiceName
|
||||
admin_service_name = $AdminServiceName
|
||||
service_account = "NT AUTHORITY\LocalService"
|
||||
}
|
||||
$metadataPath = Join-Path $configDirectory "installation.json"
|
||||
$metadata | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath $metadataPath -Encoding UTF8
|
||||
|
||||
Write-Host "Scert Windows 文件安装和服务注册完成。"
|
||||
Write-Host "核心程序:$installedScert"
|
||||
Write-Host "密码重置程序:$installedAdmin"
|
||||
Write-Host "配置目录:$configDirectory"
|
||||
Write-Host "数据目录:$databaseDirectory"
|
||||
Write-Host "日志目录:$logDirectory"
|
||||
Write-Host "备份目录:$backupDirectory"
|
||||
Write-Host "证书目录:$certificate"
|
||||
Write-Host "安装记录:$metadataPath"
|
||||
Write-Host "Scert 主服务已注册为自动启动,但本安装器没有启动或重启服务。"
|
||||
Write-Host "Scert-admin 已注册为手动启动,本安装器不会启动它,也不会创建公网防火墙规则。"
|
||||
Write-Host "首次初始化时请使用配置文件路径:$configurationPath"
|
||||
@@ -26,6 +26,7 @@ systemctl daemon-reload
|
||||
|
||||
```text
|
||||
/scert 安装目录
|
||||
/scert/web Web 管理页面静态资源
|
||||
/scert/conf 配置目录
|
||||
/scert/data 数据目录
|
||||
/scert/log 日志目录
|
||||
|
||||
Reference in new issue
Block a user