Files
SMANAGE/php/fix-nextcloud-ini.ps1
T

124 lines
5.0 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Fix D:\php\php.ini for Nextcloud 34 - 5 changes, idempotent
$ErrorActionPreference = 'Stop'
$iniPath = 'D:\php\php.ini'
if (-not (Test-Path $iniPath)) { throw "$iniPath not found" }
$lines = [System.IO.File]::ReadAllLines($iniPath, [System.Text.Encoding]::UTF8)
$patched = 0
$log = New-Object System.Collections.Generic.List[string]
# 1) [opcache] 段后插入 zend_extension=opcache
$opcacheIdx = -1
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -eq '[opcache]') { $opcacheIdx = $i; break }
}
if ($opcacheIdx -lt 0) { throw '[opcache] section not found' }
$hasZend = $false
for ($j = $opcacheIdx; $j -lt [Math]::Min($opcacheIdx + 3, $lines.Count); $j++) {
if ($lines[$j] -match '^zend_extension=opcache') { $hasZend = $true; break }
}
if (-not $hasZend) {
$newLines = New-Object System.Collections.Generic.List[string]
for ($k = 0; $k -lt $lines.Count; $k++) {
$newLines.Add($lines[$k])
if ($k -eq $opcacheIdx) {
$newLines.Add('zend_extension=opcache')
$patched++
$line1 = $opcacheIdx + 2
$log.Add(" [+] line $line1 : added zend_extension=opcache")
}
}
$lines = $newLines.ToArray()
} else {
$log.Add(' [.] zend_extension=opcache already present, skip')
}
# 2-5) 直接按行内容替换(幂等)
$patterns = @(
@{ Old = 'cgi.fix_pathinfo=0'; New = 'cgi.fix_pathinfo=1' },
@{ Old = 'max_execution_time = 360'; New = 'max_execution_time = 3600' },
@{ Old = 'max_input_time = 360'; New = 'max_input_time = 3600' },
@{ Old = 'opcache.revalidate_freq=1'; New = 'opcache.revalidate_freq=60' }
)
foreach ($p in $patterns) {
$foundIdx = -1
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -eq $p.Old) { $foundIdx = $i; break }
}
if ($foundIdx -ge 0) {
$lines[$foundIdx] = $p.New
$patched++
$lineNum = $foundIdx + 1
$log.Add(" [+] line $lineNum : $($p.Old) -> $($p.New)")
} else {
$alreadyNew = $false
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -eq $p.New) { $alreadyNew = $true; break }
}
if ($alreadyNew) {
$log.Add(" [.] already set to $($p.New), skip")
} else {
$log.Add(" [!] pattern not found: $($p.Old)")
}
}
}
# 写回(不写 BOM)
$utf8 = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllLines($iniPath, $lines, $utf8)
Write-Host '=== 1) 改了 ' $patched ' 处 ===' -ForegroundColor Cyan
foreach ($l in $log) { Write-Host $l }
Write-Host ''
Write-Host '=== 2) 验证 5 处最终值 ===' -ForegroundColor Cyan
$verify = [System.IO.File]::ReadAllLines($iniPath)
$keys = @('^zend_extension=opcache', '^cgi\.fix_pathinfo=', '^max_execution_time', '^max_input_time', '^opcache\.revalidate_freq')
foreach ($k in $keys) {
$m = $verify | Select-String -Pattern $k | Select-Object -First 1
if ($m) { Write-Host (' line ' + $m.LineNumber + ' : ' + $m.Line.Trim()) } else { Write-Host ' NOT FOUND' }
}
Write-Host ''
Write-Host '=== 3) 重启 PHP-CGI(kill + 拉起 4 worker)===' -ForegroundColor Cyan
Get-Process php-cgi -ErrorAction SilentlyContinue | Stop-Process -Force
Start-Sleep -Seconds 2
$alive = @(Get-Process php-cgi -ErrorAction SilentlyContinue)
Write-Host (' kill 后剩余:' + $alive.Count)
$PhpCgi = 'D:\php\php-cgi.exe'
$PhpIni = 'D:\php\php.ini'
$LogDir = 'D:\SMANAGE\logs'
if (-not (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'
for ($i = 0; $i -lt 4; $i++) {
$port = 9001 + $i
$env:PHP_FCGI_MAX_REQUESTS = 1000
$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 (' [start] worker ' + $i + ' on 127.0.0.1:' + $port + ' pid=' + $p.Id)
}
Start-Sleep -Seconds 3
Write-Host ''
Write-Host '=== 4) 验证:进程 / 端口 / 扩展 ===' -ForegroundColor Cyan
Write-Host ' --- 进程 ---'
Get-Process php-cgi -ErrorAction SilentlyContinue | Format-Table Id,StartTime -AutoSize | Out-String | Write-Host
Write-Host ' --- 9001-9004 LISTENING ---'
$listen = netstat -ano | findstr ':900[1-4]' | findstr LISTENING
if ($listen) { $listen | ForEach-Object { Write-Host (' ' + $_) } } else { Write-Host ' (无)' }
Write-Host ' --- 关键扩展加载情况 ---'
$mods = & 'D:\php\php.exe' -m 2>&1 | Out-String
@('pdo_mysql','mysqli','PDO','mbstring','gd','intl','zip','curl','opcache','fileinfo','exif','ldap','openssl','sodium','gmp') | ForEach-Object {
$name = $_
$hit = $false
foreach ($ln in $mods -split "`r?`n") { if ($ln.Trim() -eq $name) { $hit = $true; break } }
if ($hit) { Write-Host (' [v] ' + $name) -ForegroundColor Green } else { Write-Host (' [ ] ' + $name) -ForegroundColor Yellow }
}