How to specify different php.ini files for each website in Apache

Many times you might feel that you require a different settings for different sites (running on the same server) as specified in php.ini . So you start making changes in your php files using ini_set() or .htaccess to override those settings. In this howto, I will tell you how to solve this problem of having different php settings by having separate php.ini files for each website.

So, lets assume that I have a website with document root as /var/www/html/example/ with domain as www.example.com. For testing purpose you can create a small file in /var/www/html/example/phpinfo.php

<?php
phpinfo();
?>

Now run http://example.com/phpinfo.php it will display current php settings.

Now copy the php.ini file from /etc/php.ini file to your /var/www/html/example.

[root]# cp /etc/php.ini /var/www/html/example #For fedora users

[chia]$ sudo cp /etc/php5/apache2/php.ini /var/www/example # for Debian and Ubuntu

And now, we just need a simple trick to tell apache where to find the php settings for this website. You need to make this small change in the configuration file.

<VirtualHost 10.24.11.2:80>

ServerName example.com

ServerAlias www.example.com

PHPINIDir /var/www/html/example

</VirtualHost>

PHPINIDir should be the directory where the file php.ini resides.

Restart the apache after this

[root]# service httpd restart  # for Fedora/Redhat/CenOS based systems

[chia]$ sudo apache2 restart  # for Debian/Ubuntu based systems

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.