Fix OPcache toggle failure - enhance config file discovery for LiteSpeed paths

main
Anthony 2026-02-28 15:18:59 +08:00
parent 0f819513e2
commit d126152db6
2 changed files with 82 additions and 23 deletions

View File

@ -653,7 +653,22 @@ actions:
- cmd[cp]:
user: root
commands:
- php /home/litespeed/mbmanager/scripts/toggle_opcache.php disable
- |
set -euo pipefail
TMP_OUTPUT="$(mktemp)"
cleanup() {
rm -f "${TMP_OUTPUT}"
}
trap cleanup EXIT
php /home/litespeed/mbmanager/scripts/toggle_opcache.php disable > "${TMP_OUTPUT}"
if grep -Eq '"status"[[:space:]]*:[[:space:]]*"error"' "${TMP_OUTPUT}"; then
cat "${TMP_OUTPUT}"
exit 1
fi
cat "${TMP_OUTPUT}"
- restartNodes:
- nodeGroup: "cp"
reboot: true

View File

@ -35,6 +35,7 @@ function find_php_config_files() {
'/etc/php/8.2/cli/conf.d/',
'/etc/php/8.1/cli/conf.d/',
'/etc/php/8.0/cli/conf.d/',
'/usr/local/lsws/lsphp/etc/php.d/',
'/usr/local/lsws/lsphp84/etc/php.d/',
'/usr/local/lsws/lsphp83/etc/php.d/',
'/usr/local/lsws/lsphp82/etc/php.d/',
@ -42,7 +43,41 @@ function find_php_config_files() {
'/usr/local/lsws/lsphp80/etc/php.d/'
];
$possible_files = [
'/usr/local/lsws/lsphp/etc/php.d/10-opcache.ini',
'/etc/php.d/10-opcache.ini'
];
$config_files = [];
foreach ($possible_files as $file) {
if (is_file($file)) {
$config_files[] = $file;
}
}
$loaded_ini = php_ini_loaded_file();
if ($loaded_ini !== false && is_file($loaded_ini)) {
$content = file_get_contents($loaded_ini);
if ($content !== false && preg_match('/^\s*;?\s*opcache\.enable\s*=\s*/mi', $content)) {
$config_files[] = $loaded_ini;
}
}
$scanned_ini = php_ini_scanned_files();
if ($scanned_ini !== false && trim($scanned_ini) !== '') {
foreach (explode(',', $scanned_ini) as $ini_file) {
$ini_file = trim($ini_file);
if ($ini_file === '' || !is_file($ini_file)) {
continue;
}
if (stripos(basename($ini_file), 'opcache') !== false) {
$config_files[] = $ini_file;
}
}
}
foreach ($possible_paths as $path) {
if (is_dir($path)) {
$files = glob($path . '*opcache*');
@ -52,7 +87,7 @@ function find_php_config_files() {
}
}
return $config_files;
return array_values(array_unique($config_files));
}
// Function to modify OPcache configuration
@ -65,28 +100,37 @@ function modify_opcache_config($enable) {
$success = false;
foreach ($config_files as $file) {
if (is_writable($file)) {
if (!is_file($file)) {
continue;
}
$content = file_get_contents($file);
if ($content !== false) {
// Replace opcache.enable setting
if ($enable) {
$content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content);
$content = preg_replace('/^;opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content);
} else {
$content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=0', $content);
if ($content === false) {
continue;
}
// If no opcache.enable line exists, add it
if (!preg_match('/^opcache\.enable\s*=/m', $content)) {
$content .= "\nopcache.enable=" . ($enable ? '1' : '0') . "\n";
$replacement_count = 0;
$updated_content = preg_replace(
'/^\s*;?\s*opcache\.enable\s*=\s*.*$/mi',
'opcache.enable=' . ($enable ? '1' : '0'),
$content,
-1,
$replacement_count
);
if ($updated_content === null) {
continue;
}
if (file_put_contents($file, $content) !== false) {
// If no opcache.enable line exists, add it once.
if ($replacement_count === 0) {
$updated_content = rtrim($updated_content) . "\nopcache.enable=" . ($enable ? '1' : '0') . "\n";
}
if (file_put_contents($file, $updated_content) !== false) {
$success = true;
}
}
}
}
return $success;
}