55 lines
2.2 KiB
PowerShell
55 lines
2.2 KiB
PowerShell
# 启动 PHP-CGI(FastCGI for nginx)
|
||
# 监听 127.0.0.1:9001-9004,4 个并发子进程
|
||
# 由 nginx upstream 负载均衡
|
||
# 用法:以管理员身份运行 powershell -ExecutionPolicy Bypass -File start-php.ps1
|
||
|
||
# 提权
|
||
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||
Start-Process powershell.exe "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
|
||
exit
|
||
}
|
||
|
||
# 路径
|
||
$PhpDir = "D:\php"
|
||
$PhpCgi = Join-Path $PhpDir "php-cgi.exe"
|
||
$PhpIni = Join-Path $PhpDir "php.ini"
|
||
$LogDir = "D:\SMANAGE\logs"
|
||
if (!(Test-Path $LogDir)) { New-Item -ItemType Directory -Path $LogDir -Force | Out-Null }
|
||
$LogOut = Join-Path $LogDir "php-cgi.out.log"
|
||
$LogErr = Join-Path $LogDir "php-cgi.err.log"
|
||
|
||
# 配置
|
||
$StartPort = 9001
|
||
$NumWorkers = 4
|
||
$MaxRequests = 1000
|
||
|
||
# 杀掉残留 php-cgi
|
||
Get-Process -Name "php-cgi" -ErrorAction SilentlyContinue | Stop-Process -Force
|
||
Start-Sleep -Milliseconds 500
|
||
|
||
# 启动 N 个 worker(stdout/stderr 分开到不同文件)
|
||
for ($i = 0; $i -lt $NumWorkers; $i++) {
|
||
$port = $StartPort + $i
|
||
$env:PHP_FCGI_MAX_REQUESTS = $MaxRequests
|
||
$argList = "-b 127.0.0.1:$port -c `"$PhpIni`""
|
||
$p = Start-Process -FilePath $PhpCgi -ArgumentList $argList -WindowStyle Hidden -PassThru -RedirectStandardOutput $LogOut -RedirectStandardError $LogErr
|
||
Write-Host "[php-cgi] worker $i on 127.0.0.1:$port pid=$($p.Id)" -ForegroundColor Green
|
||
}
|
||
|
||
Start-Sleep -Seconds 1
|
||
$alive = @(Get-Process -Name "php-cgi" -ErrorAction SilentlyContinue)
|
||
Write-Host "[php-cgi] $($alive.Count) of $NumWorkers workers alive" -ForegroundColor Cyan
|
||
|
||
# 写 upstream 配置文件给 nginx include
|
||
$upstreamFile = "D:\SMANAGE\php\php-upstream.conf"
|
||
$portList = ($StartPort..($StartPort + $NumWorkers - 1)) | ForEach-Object { " server 127.0.0.1:$_;" }
|
||
$upstreamContent = @"
|
||
# Auto-generated by start-php.ps1 — DO NOT EDIT
|
||
# Nextcloud FastCGI upstream pool
|
||
upstream php_fcgi_nextcloud {
|
||
$($portList -join "`n")
|
||
}
|
||
"@
|
||
$upstreamContent | Out-File -FilePath $upstreamFile -Encoding UTF8 -Force
|
||
Write-Host "[php-cgi] upstream config -> $upstreamFile" -ForegroundColor Green
|