You may have noticed the conf.d directories in locations like /etc/php/8.0/ or /etc/php/7.4/ and wondered what they are used for; these directories are intended to make the configuration of PHP easier for users by categorizing the different configuration options into individual files, and it also makes it easier to update PHP since you do not need to mess with php.ini when updating or making configuration changes.
The .d simply stands for "directory". In my opinion, having a dot in a directory name is a bit of a bad practice, since some users may confuse the directory for a file. I am fairly technical, and got plenty of Linux experience, yet, I still get this feeling that I am dealing with a file rather than a directory sometimes, and I have accidentally tried to edit directories in a text editor because they contained a dot.
If a file located in conf.d does not have the options you want to edit, then you can simply add them yourself. The options you define in these files will override those in php.ini
Which files should you edit
This is actually rather confusing, since we rarely need to edit these files. But, there are some simple logical rules that makes it easier to remember.
If you use PHP-FPM, then you should edit the files in /etc/php/8.0/fpm/conf.d/, and remember to restart PHP-FPM to reload the configuration:
service php8.0-fpm restart
Note. There might be multiple versions of PHP installed on your system, and sometimes the incorrect version might still be loaded by Apache unintentionally. Remember to disable the old php-fpm configuration and enable the new one after updating.
To make sure the correct version of PHP is loaded, you should check the /etc/apache2/conf-enabled/ directory to see if the correct .conf file is loaded.
Apache conf files can be enabled and disabled easily with these commands:
- a2enconf php8.0-fpm
- a2disconf php8.0-fpm
This will create a symlink in /etc/apache2/conf-enabled/ that points to the corresponding file in /etc/apache2/conf-available/
If you are using mod_php, then you will instead need to edit the files in: /etc/php/8.0/apache2/conf.d/, and then restart apache:
service apache2 restart
More recently, the reload option was added; this allows to reload the configuration without restarting the HTTP server completely.
What do the numbers mean in front of the files?
The numbers in front of the conf.d files is used for prioritizing the order in which the files load. A lower value loads the file before a file that has a higher value. I.e.:
This is similar to how Apache is prioritizing the order in which VHOST configuration files are loaded. But, the point of mentioning this is that it can sometimes bite you, especially when it comes to VHOST files and configuring a default vhost on a server that is hosting multiple domains or has subdomains.
If files do not have numbers in their file names, they will simply be loaded in alphabetical order instead.
Some tutorials mention that you can create a local.ini in the conf.d directory to add your own additions and changes to php.ini without having them overwritten every time PHP is updated. This is not completely accurate, since, in debian/ubuntu at least, the directory will be named after the PHP version when updating.
Also, since this file has no number in the file name, it will be loaded after other files that has — so, to better predict when your configuration is loaded, you may want to name it as 10-local.ini instead.
Updating PHP resets your changes
Placing your changes in the conf.d directory will not prevent changes from getting overwritten. If you update to another version of PHP, you will need to move the configuration file from the old directory to the new version of PHP. I.e. From /etc/php/7.4/fpm/conf.d/ to /etc/php/8.0/fpm/conf.d/ in order to have the changes auto-loaded.
While it does not prevent the changes from getting overwritten, it still makes it much easier to manage than using php.ini. The problem with the php.ini file is that it may contain changes and additions when PHP is updated, so you can not simply copy/paste your old php.ini file into the new PHP version.
It might be a better idea to keep a 10-local.ini somewhere outside of the "version" specific folders, and then create a symbolic link for it in the active conf.d directory — but you would have to do this manually. In any case, either a symbolic link, or moving the conf.d files, should do the trick.