How do I set up per-site php.ini files on a LAMP server using NameVirtualHosts?

Most settings that you would place in your php.ini can also be placed in a .htaccess or in the apache configuration for that site. For example one of my virtual hosts has this in the .htaccess

    php_value display_errors 1
    php_value register_globals 0
    php_value short_open_tag 0
    php_value magic_quotes_runtime 0
    php_value magic_quotes_gpc 0
    php_value session.name "PHPSESSID_SITEBLAH"
    php_value session.gc_maxlifetime 57600
    php_value session.gc_probability 1
    php_value session.gc_divisor 500

You can also specify configuration settings in the Apache configuration file (httpd.conf/apache.conf). In your case you can specify the configuration values within the context.

The following example should set upload_max_filesize to 10M for the virtual host example.com.

<VirtualHost *>
    ServerName example.com
    DocumentRoot /path_to_docroot
    php_value upload_max_filesize 10M
    php_admin_flag allow_url_fopen on
</VirtualHost>

If you do not want to set this value for the whole of the site, but to one particular directory where the application lives, then you can specify the values within a .

<VirtualHost *>
   ServerName example.com
   DocumentRoot /path_to_docroot
  <Location /testApp/>
     php_value upload_max_filesize 10M
     php_admin_flag allow_url_fopen on
  </Location>
</VirtualHost>

See http://php.net/manual/en/configuration.changes.php for more explanation.


Another suggestion is to use mod-fastcgi or mod-fcgi, which run PHP as a separate standalone daemon. Then, for each website, you can define a separate user and php.ini config file.


Yet another option is suPHP which allows you to define a php.ini for each VirtualHost. It also allows you to specify a PHP version for each VirtualHost.