Written by James McDonald

June 1, 2024

CakePHP 5 uses zend.assertions = 1 for development so if you have production and test environments on your server you need separate instance of php-fpm so you can run one with zend.assertions = 1 for development and the other zend.assertions = -1 for production

The following is done on Ubuntu 24.04 LTS

Copy the default php8.3-fpm.service file to a new name and location

cp /etc/systemd/system/multi-user.target.wants/php8.3-fpm.service /etc/systemd/system/php8.3-fpm-test.service

Copy the default php.ini to a create a separate php.ini for the new php-fpm service

cp /etc/php/8.3/fpm/php.ini /etc/php/8.3/fpm/php-test.ini 

Modify the contents of the service file to use the different php-test.ini and a new socket

[Unit]
Description=FPM Service for Test Env
Documentation=man:php-fpm8.3(8)
After=network.target

[Service]
Type=notify
ExecStart=/usr/sbin/php-fpm8.3 --php-ini /etc/php/8.3/fpm/php-test.ini --nodaemonize --fpm-config /etc/php/8.3/fpm/php-fpm-test.conf
ExecStartPost=-/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm-test.sock /etc/php/8.3/fpm/pool-test.d/www-test.conf 83
ExecStopPost=-/usr/lib/php/php-fpm-socket-helper remove /run/php/php-fpm-test.sock /etc/php/8.3/fpm/pool-test.d/www-test.conf 83
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

Modify the contents of the php-test.ini e.g. add stuff you want for a dev setup for example you can up the memory or enable assertions and xdebug.

; other config directives above and below these
memory_limit = 512M

[Assertion]
zend.assertions = 1

Create a pool directory for the new php-fpm instance

sudo mkdir /etc/php/8.3/fpm/pool-test.d

Create a pool file /etc/php/8.3/fpm/poot-test.d/www-test.conf (notice how it is using the different socket and you could change the user/group to be some thing other than www-data too)

[www-test]
user = www-data
group = www-data
listen = /run/php/php8.3-fpm-test.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

Configure nginx to use the now separate default and ‘test’ php-fpm instances. In this example the /live location uses the default php8.3-fpm service and the /test location uses the php8.3-fpm-test service

 location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                rewrite ^ /live/ last;
                try_files $uri $uri/ =404;
        }

        location /live {
                alias /var/www/sites/live/webroot;
                try_files $uri $uri/ @live;
                location ~ \.php$ {
                        include snippets/fastcgi-php.conf;
                        include live_fastcgi_params.conf;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
                }
        }

        location @live {
                rewrite ^ /live/index.php last;
        }

        location /test {
                alias /var/www/sites/test/webroot;
                try_files $uri $uri/ @test;
                location ~ \.php$ {
                        include snippets/fastcgi-php.conf;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass unix:/run/php/php8.3-fpm-test.sock;
                }
        }

        location @test {
                rewrite ^ /test/index.php last;
        }
        location ~ /\.ht {
                deny all;
        }

Enable the service

sudo systemctl enable php8.3-fpm-test.service

You should now have separate php-fpm services with separate php.ini files you can tweak as you like

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

List local disks

GET-CimInstance -query "SELECT * from Win32_DiskDrive" DeviceID Caption Partitions Size Model -------- -------...

Create ISO using Powershell

Usage New-IsoFile -NewIsoFilePath C:\tmp\Hyper-V.iso-sources -ImageName Hyper-V -SourceFilePath 'C:\tmp\Hyper-V'...