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?


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.


  • 2
    phpenmod can enable configs for just a given SAPI if you use -s option, so even FPM or CLI specific config can be kept in /etc/php/7.0/mods-available/ and enabled per-SAPI. – muru Jan 29 '18 at 8:40

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.