Files
solution-erp/scripts/setup-sql-db.ps1
pqhuy1987 b1a4571c86
Some checks failed
Deploy SOLUTION_ERP / build-backend (push) Failing after 9s
Deploy SOLUTION_ERP / build-fe-admin (push) Has been cancelled
Deploy SOLUTION_ERP / build-fe-user (push) Has been cancelled
Deploy SOLUTION_ERP / deploy-iis (push) Has been cancelled
[CLAUDE] VPS setup scripts + SSL + runner + FE prod config + master runbook
Scripts moi (PowerShell admin trên VPS Windows Server):
- setup-sql-db.ps1: tao DB SolutionErp + grant db_owner cho vrapp (user shared voi VIETREPORT). Idempotent.
- setup-iis-sites.ps1: app pool SolutionErp-Api (NoManagedCode + AlwaysRunning + no idle) + 3 site (SolutionErp-Api/Admin/User) voi host header, C:\inetpub\solution-erp\{api,fe-admin,fe-user,logs,uploads}. Placeholder index.html + SPA web.config voi URL rewrite fallback + security headers. Firewall rule. ACL grant AppPool identity Modify. Naming prefix SolutionErp-* tranh conflict VIETREPORT.
- setup-ssl.ps1: download win-acme v2.2.9 → issue cert Let's Encrypt 3 domain (api/admin/user.huypham.vn) qua HTTP-01 challenge + auto install IIS binding + HTTP→HTTPS redirect + scheduled task 90d renew.
- setup-gitea-runner.ps1: download act_runner.exe → register voi Gitea git.baocaogiaoduc.vn, install Windows service, labels windows-latest,self-hosted,windows,x64 (cho phep share voi VIETREPORT).

FE production config:
- fe-admin/.env.production + fe-user/.env.production: VITE_API_BASE_URL=https://api.huypham.vn
- fe-admin/src/lib/api.ts + fe-user/src/lib/api.ts: BASE_URL = (import.meta.env.VITE_API_BASE_URL ?? '') + '/api'
  - Dev: empty prefix → /api qua Vite proxy :5443
  - Prod: https://api.huypham.vn/api (cross-origin CORS da config AllowedOrigins)

Docs:
- docs/guides/vps-setup.md MOI (master runbook): prereq, 4 script chay theo thu tu, set 5 Gitea secrets, first deploy, appsettings.Production.json pattern (file hoac user-secrets), smoke test 3 curl, post go-live checklist (doi admin password, rotate secrets chat-exposed, backup schedule, disable Swagger prod, monitor logs), table co-existence VIETREPORT
- CLAUDE.md root: add vps-setup.md reference

Gitea repo da setup (extern):
- https://git.baocaogiaoduc.vn/vietreport-admin/solution-erp (private)
- Secrets set via API: IIS_HOST=103.124.94.38, IIS_USER=Administrator, DB_CONNECTION (voi vrapp password), JWT_SECRET placeholder
- CON THIEU: IIS_PASSWORD (Windows admin — user cung cap), JWT_SECRET real value (64-char tu vps-jwt-key.txt — user update qua Gitea UI)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:37:34 +07:00

75 lines
2.5 KiB
PowerShell

# Setup SQL Server DB cho SOLUTION_ERP trên VPS chia sẻ với VIETREPORT.
# Tạo database + grant quyền cho user vrapp (đã có sẵn trên server).
# Idempotent: chạy lại không phá gì.
#
# Usage (chạy trên VPS với admin privilege):
# .\setup-sql-db.ps1 -SaPassword 'your-sa-password'
#
# Prereq:
# - SQL Server cài sẵn
# - Login vrapp đã tồn tại (dùng chung với VIETREPORT)
# - sqlcmd CLI available (đi kèm SQL Server)
param(
[Parameter(Mandatory=$true)] [string]$SaPassword,
[string]$Server = ".",
[string]$Database = "SolutionErp",
[string]$AppUser = "vrapp"
)
$ErrorActionPreference = 'Stop'
function Invoke-Sql($query) {
$output = sqlcmd -S $Server -U sa -P $SaPassword -Q $query -b 2>&1
if ($LASTEXITCODE -ne 0) {
throw "SQL fail (exit $LASTEXITCODE):`n$output"
}
return $output
}
Write-Host "==> Check SQL Server reachable"
Invoke-Sql "SELECT @@VERSION" | Select-Object -First 2
Write-Host "`n==> Check login '$AppUser' exists"
$check = Invoke-Sql "SELECT name FROM sys.sql_logins WHERE name = '$AppUser'"
if ($check -notmatch $AppUser) {
Write-Error "Login '$AppUser' KHONG ton tai. Tao tay truoc: CREATE LOGIN [$AppUser] WITH PASSWORD='...'"
exit 1
}
Write-Host " OK"
Write-Host "`n==> Create database '$Database' (if not exists)"
Invoke-Sql @"
IF DB_ID(N'$Database') IS NULL
BEGIN
CREATE DATABASE [$Database];
PRINT 'Created database $Database';
END
ELSE PRINT 'Database $Database already exists';
"@
Write-Host "`n==> Grant db_owner cho '$AppUser' tren '$Database'"
Invoke-Sql @"
USE [$Database];
IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = '$AppUser')
BEGIN
CREATE USER [$AppUser] FOR LOGIN [$AppUser];
PRINT 'Created user $AppUser in DB';
END
ELSE PRINT 'User $AppUser already exists in DB';
ALTER ROLE db_owner ADD MEMBER [$AppUser];
PRINT 'Added $AppUser to db_owner';
"@
Write-Host "`n==> Verify kết nối với vrapp (test login)"
Write-Host " (skip — tự test khi app chạy)"
Write-Host "`n==> Setup backup schedule"
Write-Host " Chạy: schtasks /Create /TN 'SolutionErp SQL Backup' /TR 'pwsh -File C:\solution-erp\scripts\backup-sql.ps1 -Server . -Database $Database' /SC DAILY /ST 02:00 /RU SYSTEM /F"
Write-Host " (hoặc tạo tay qua Task Scheduler)"
Write-Host "`n✅ SQL Server setup DONE" -ForegroundColor Green
Write-Host " Database: $Database"
Write-Host " App user: $AppUser (db_owner)"
Write-Host " Connection string production: Server=localhost;Database=$Database;User Id=$AppUser;Password=***"