Ubuntu – Correct way to modify php.ini for Apache and/or CLI

16.04Apache2PHP

I want to change and add some PHP directives, some for apache, some for CLI and some for both. As an example, I want to set the default timezone for all server contexts. There are a couple of different ways I can go about this, but I don't know which is the best approach.

I can directly edit the master php.ini files, which are here /etc/php/7.0/apache2/php.ini and here /etc/php/7.0/cli/php.ini, but I don't know whether this file will be overwritten on upgrade. Also for shared settings, such as the timezone, I have to remember to edit two files and keep those changes synchronised.

I can use Apache's php_admin_value, php_admin_flag, php_value and php_flag directives. The advantage here is that the php settings can be applied on a virtual host by virtual host basis and I can also apply them globally to Apache. However, this doesn't help with the CLI settings.

The php directory has a similar structure to Apache, allowing files full of directives to be enabled or disabled with the phpenmod command. I can copy this structure, creating the directory /etc/php/7.0/mydirectives, placing php.ini files within this folder, then creating symbolic links from /etc/php/7.0/apache2/conf.d/ to my php.ini directive files. This allows me to have a single directive file which adds directives to both Apache and CLI version of PHP. The only thing I can't do with this is virtual host by virtual directives.

Any advice on how to do this properly?

Best Answer

FYI, configuration files in /etc/ installed by a package are not overwritten by default; on upgrade if a file changes a prompt is presented with options.

Coming back to the original question, here is how I would do it:

  • Create a config file /etc/php/7.0/mods-available/50-mydirectives.ini and do common configuration there; then use phpenmod mydirectives to enable it.

  • Do apache and cli specific configuration in /etc/php/7.0/apache2/conf.d/ and /etc/php/7.0/cli/conf.d/ respectively.

  • Do virtualhost specific configuration in the virtualhost itself.

With the above configuration will not need to edit system provided php.ini.

Related:

Related Question