Setting PHP.ini Config In Azure App Service PHP 8.1 / NGINX

Following on from my previous blog post showing how to configure PHP 8.1 based NGINX Azure App Services, another common change is wanting to update PHP.ini config to do things such as increase the max upload file size and timezone etc.

When using Apache, PHP options can be set in the .htaccess file at a directory level, however as we know with NGINX this capability is not available and .htaccess files are not supported.

So… to build on the functionality we added in the previous blog post using the App Service startup script, we will create a new PHP.ini file and copy it over to the server, replacing the default before restarting the NGINX service for the custom PHP.ini to take effect.

IMPORTANT: In order for the following config to work properly you must ensure the client_max_body_size parameter in your NGINX config is set equal to or greater than that of your PHP.ini upload_max_filesize & post_max_size as described in this post.

Firstly, lets see where the default PHP.ini is located:

So, as we can see it lives under /usr/local/etc/php/conf.d/php.ini

Next up I took a copy of the default PHP.ini to my custom created startup directory using the following command (note this also creates a new php-ini directory):

cp /usr/local/etc/php/conf.d/php.ini /home/startup/php-ini/php.ini

I then added some custom config to the default leaving me with the following contents:

error_log=/dev/stderr
display_errors=Off
log_errors=On
display_startup_errors=Off
zend_extension=opcache

# Custom

date.timezone= "Europe/London"
upload_max_filesize = 256M
post_max_size = 256M

# End Custom

Now all that’s left is to add a couple of lines into my startup script to copy this custom PHP.ini over to the App Service container, overwriting the default, before restarting NGINX for it to take effect.

NOTE: If you edit / save the startup script on a Windows machine you may experience problems with hidden characters, therefore I recommend using Notepad ++ and the EOL Conversion functionality within when saving.

What this leaves me with for the complete startup.sh including the WordPress modifications in the previous blog post is:

#!/bin/bash

echo "Replacing default nginx configuration for custom config"

cp /home/startup/nginx-conf/nginx.conf /etc/nginx/sites-available/default
 
echo "Replacing default php.ini for custom config"

cp /home/startup/php-ini/php.ini /usr/local/etc/php/conf.d/php.ini

echo "Reloading nginx to apply new custom config"

service nginx reload

So, as was configured in the previous blog post, so long as our startup command is still set to :

/home/startup/startup.sh

Then the script should run and overwrite the PHP.ini config with your custom options. Voila!