diff --git a/README.md b/README.md index 1df963d..1668006 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ `dep sitehost:prepare` -`dep sitehost:prepare:deploy stage=uat --branch=master` +`dep sitehost:phpconfig` `dep sitehost:backup` @@ -115,7 +115,10 @@ host('uat.domain.co.nz') ->set('remote_user', 'uatuser') ->set('sitehost_server_name', 'ch-xxx') ->set('sitehost_stack_name', 'xxxxxxxxx') - ->set('sitehost_restart_mode', 'apache-php'); //Optional + ->set('sitehost_restart_mode', 'apache-php') //Optional + ->set('php_memory_limit', '512M') //Optional - default 512M + ->set('php_post_max_size', '64M') //Optional - default 64M + ->set('php_max_execution_time', '60'); //Optional - default 60 //Production @@ -139,7 +142,7 @@ This will: - Delete public directory which is created on first creation of a Sitehost server, so we can use this path as a symlink - Generates ssh key which you can copy to deployment keys on github project -- Create php default config +- Create/update php server config (memory_limit, post_max_size, max_execution_time) If you are doing a container upgrade on Sitehost then you will want to run this command immediately after @@ -199,6 +202,23 @@ This is only available on certain containers. `->set('sitehost_restart_mode', 'apache-php');` +### PHP server config + +Each deployment runs `sitehost:phpconfig` which updates the managed PHP settings in `~/container/config/php/conf.d/ps-custom.ini`, patching only the relevant lines so any other config in the file is preserved. + +The following settings can be configured globally or per host in your `deploy.php`: + +```php +->set('php_memory_limit', '512M') // default: 512M +->set('php_post_max_size', '64M') // default: 64M +->set('php_max_execution_time', '60') // default: 60 +``` + +You can also run this command manually to update PHP config without a full deployment: + +`dep sitehost:phpconfig` + + ### Docker Deployer comes with ps docker image. diff --git a/ps_silverstripe.php b/ps_silverstripe.php index 156dee5..d2d1929 100644 --- a/ps_silverstripe.php +++ b/ps_silverstripe.php @@ -17,6 +17,11 @@ set('shared_path', '/container/application/shared'); set('sitehost_restart_mode', 'container'); //This can also be set to apache +// PHP server config defaults - override per host in deploy.php +set('php_memory_limit', '512M'); +set('php_post_max_size', '64M'); +set('php_max_execution_time', '60'); + /** * Sitehost - this is the upgrade script from mysql 5.7 to 8 * This will immediately make the changes to the environment @@ -113,19 +118,33 @@ }); /** - * Sitehost + * Sitehost - Write PHP server config settings to ps-custom.ini. + * Values can be overridden per host in deploy.php: + * ->set('php_memory_limit', '256M') + * ->set('php_post_max_size', '32M') + * ->set('php_max_execution_time', '30') */ task('sitehost:phpconfig', function () { - //Update php config to default - if (test('[ ! -f ~/container/config/php/conf.d/ps-custom.ini ]')) { - writeln('No default custom php has been configured'); - writeln('Creating "~/container/config/php/conf.d/ps-custom.ini" and adding defaults'); - run('echo "memory_limit=512M" >> ~/container/config/php/conf.d/ps-custom.ini'); - //TODO: POST_MAX - //TODO: EXECUTION TIME - //TODO: UPLOAD_MAX - } else { - writeln('php has been configured - skipping'); + $ini = '~/container/config/php/conf.d/ps-custom.ini'; + writeln('Updating PHP server config in "' . $ini . '"'); + // Ensure the directory and file exist before patching + run('mkdir -p ~/container/config/php/conf.d && touch ' . $ini); + $settings = [ + 'memory_limit' => get('php_memory_limit'), + 'post_max_size' => get('php_post_max_size'), + 'max_execution_time' => get('php_max_execution_time'), + ]; + foreach ($settings as $key => $value) { + // Escape value for sed replacement (|, \, and & are special in the replacement string). + // strtr() is used instead of str_replace() to avoid double-escaping when the value + // contains multiple metacharacters (str_replace processes needles sequentially). + $sedValue = strtr($value, ['\\' => '\\\\', '|' => '\\|', '&' => '\\&']); + // Escape value for shell echo (handles spaces, quotes, etc.) + $shellValue = escapeshellarg($value); + // Replace the existing line if present, otherwise append + run("grep -q '^{$key}=' {$ini}" + . " && sed -i 's|^{$key}=.*|{$key}={$sedValue}|' {$ini}" + . " || echo {$key}={$shellValue} >> {$ini}"); } }); @@ -491,6 +510,7 @@ // TODO: check if required 'deploy:clear_paths', 'silverstripe:buildflush', 'deploy:publish', + 'sitehost:phpconfig', 'sitehost:restart' ]);