From 139a916013249ba97f1e2fcb5364c6c227351724 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Sun, 1 Dec 2019 01:00:42 -0500 Subject: [PATCH] Move PHP-FPM config to separate valet-specific file This allows the valet configuration to stand separately from the default PHP config. This benefits troubleshooting, makes customization of FPM workers and other settings easer and allows for easier uninstallation. Also renames any previously-existing `www.conf` pool config so it doesn't conflict with Valet nor run unnecessary additional processes. --- cli/Valet/PhpFpm.php | 34 +++++++++++++++++++++++---------- cli/stubs/etc-phpfpm-valet.conf | 26 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 cli/stubs/etc-phpfpm-valet.conf diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index fb32381a5..cf6bff19c 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -54,21 +54,35 @@ function updateConfiguration() { info('Updating PHP configuration...'); - $contents = $this->files->get($this->fpmConfigPath()); + $fpmConfigFile = $this->fpmConfigPath(); - $contents = preg_replace('/^user = .+$/m', 'user = '.user(), $contents); - $contents = preg_replace('/^group = .+$/m', 'group = staff', $contents); - $contents = preg_replace('/^listen = .+$/m', 'listen = '.VALET_HOME_PATH.'/valet.sock', $contents); - $contents = preg_replace('/^;?listen\.owner = .+$/m', 'listen.owner = '.user(), $contents); - $contents = preg_replace('/^;?listen\.group = .+$/m', 'listen.group = staff', $contents); - $contents = preg_replace('/^;?listen\.mode = .+$/m', 'listen.mode = 0777', $contents); + $this->files->ensureDirExists(dirname($fpmConfigFile), user()); - $this->files->put($this->fpmConfigPath(), $contents); + // rename (to disable) old FPM Pool configuration, regardless of whether it's a default config or one customized by an older Valet version + $oldFile = dirname($fpmConfigFile) . '/www.conf'; + if (file_exists($oldFile)) { + rename($oldFile, $oldFile . '-backup'); + } + if (false === strpos($fpmConfigFile, '5.6')) { + // for PHP 7 we can simply drop in a valet-specific fpm pool config, and not touch the default config + $contents = $this->files->get(__DIR__.'/../stubs/etc-phpfpm-valet.conf'); + $contents = str_replace(['VALET_USER', 'VALET_HOME_PATH'], [user(), VALET_HOME_PATH], $contents); + } else { + // for PHP 5 we must do a direct edit of the fpm pool config to switch it to Valet's needs + $contents = $this->files->get($fpmConfigFile); + $contents = preg_replace('/^user = .+$/m', 'user = '.user(), $contents); + $contents = preg_replace('/^group = .+$/m', 'group = staff', $contents); + $contents = preg_replace('/^listen = .+$/m', 'listen = '.VALET_HOME_PATH.'/valet.sock', $contents); + $contents = preg_replace('/^;?listen\.owner = .+$/m', 'listen.owner = '.user(), $contents); + $contents = preg_replace('/^;?listen\.group = .+$/m', 'listen.group = staff', $contents); + $contents = preg_replace('/^;?listen\.mode = .+$/m', 'listen.mode = 0777', $contents); + } + $this->files->put($fpmConfigFile, $contents); $contents = $this->files->get(__DIR__.'/../stubs/php-memory-limits.ini'); - $destFile = dirname($this->fpmConfigPath()); + $destFile = dirname($fpmConfigFile); $destFile = str_replace('/php-fpm.d', '', $destFile); $destFile .= '/conf.d/php-memory-limits.ini'; $this->files->ensureDirExists(dirname($destFile), user()); @@ -116,7 +130,7 @@ function fpmConfigPath() return $versionNormalized === '5.6' ? '/usr/local/etc/php/5.6/php-fpm.conf' - : "/usr/local/etc/php/${versionNormalized}/php-fpm.d/www.conf"; + : "/usr/local/etc/php/${versionNormalized}/php-fpm.d/valet-fpm.conf"; } /** diff --git a/cli/stubs/etc-phpfpm-valet.conf b/cli/stubs/etc-phpfpm-valet.conf new file mode 100644 index 000000000..76c4ac78a --- /dev/null +++ b/cli/stubs/etc-phpfpm-valet.conf @@ -0,0 +1,26 @@ +; FPM pool configuration for Valet + +[valet] +user = VALET_USER +group = staff +listen = VALET_HOME_PATH/valet.sock +listen.owner = VALET_USER +listen.group = staff +listen.mode = 0777 + + +php_admin_value[memory_limit] = 128M +php_admin_value[upload_max_filesize] = 128M +php_admin_value[post_max_size] = 128M + +;php_admin_value[error_log] = VALET_HOME_PATH/Log/fpm-php.www.log +;php_admin_flag[log_errors] = on + + + +pm = dynamic +pm.max_children = 5 +pm.start_servers = 2 +pm.min_spare_servers = 1 +pm.max_spare_servers = 3 +