This short article explains how to specify a custom php.ini for a website running on Apache2 with mod_php. That way, each website can have it’s own php.ini instead of having to use the server’s default one.

1 Preliminary Note

I’m using the website www.example.com here with the document root /var/www/web1/web here.

2 Getting Details About Your PHP Installation

We will now create a small PHP file (info.php) in the document root and call it in a browser. The file will display useful details about our PHP installation, such as the used php.ini file.

nano /var/www/web1/web/info.php

Now we call that file in a browser (e.g. http://www.example.com/info.php):

<img alt="Apache php.ini file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/apache-php-ini.png" ezimgfmt="rs rscb5 src ng ngcb5" height="541" src="data:image/svg xml,” width=”750″>

As you see, the website is currently using the /etc/php/8.1/apache2/php.ini file.

3 Custom php.ini For a WebSite

I will copy the default php.ini (/etc/php/8.1/apache2/php.ini on Debian/Ubuntu; /etc/php.ini on Fedora/CentOS) to the /etc/php/web1/ directory and make www.example.com use the php.ini from the /etc/php/web1/ directory:

Debian/Ubuntu:

mkdir /etc/php/web1/

cp /etc/php/8.1/apache2/php.ini /etc/php/web1/

Fedora/CentOS:

mkdir /etc/php/web1/

cp /etc/php.ini /etc/php/web1/

(You can now modify /var/www/web1/php.ini to your liking.)

Then open the vhost configuration for the www.example.com website and add a PHPINIDir line to it:

[...]
PHPINIDir /etc/php/web1

[...]

PHPINIDir must contain the directory where the php.ini file for the website is located.

Restart Apache afterward:

Debian/Ubuntu:

service apache2 restart

Fedora/CentOS:

service httpd restart

Now call the info.php file again in a browser (http://www.example.com/info.php):

<img alt="using a custom php.ini path with Apache" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/custom-php-ini-path.png" ezimgfmt="rs rscb5 src ng ngcb5" height="226" src="data:image/svg xml,” width=”750″>

The Configuration File (php.ini) Path line should now show the new php.ini.