Guide to selectively enabling PHP in different vhosts

We often see people asking how to enable PHP for different vhosts, selectively. This can be done if you follow the Apache recommended method of enabling PHP in the first place.

There is a very popular misconception that people should use AddType to enable PHP. This will not work in this scenario and is not recommended.

The PHP module should be loaded by having something similar to this in your httpd.conf:

LoadModule php5_module       modules/libphp5.so

N.B. You need to check the names of the modules you are using, the names above may not match what you have on your system

Once you have added this, you now need to instruct Apache on how to use the module. The Apache recommended way to do this is to use the AddHandler directive. Below is an example of how this can be done :

AddHandler application/x-httpd-php .php .phtml

If you just add this to your main server config, it will use the PHP5 module. This will then apply to all vhosts. All you need to do now is specify which vhost will not use PHP:

<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /usr/local/apache2/htdocs/example1

<VirtualHost *:80>
    ServerName www.example2.com
    DocumentRoot /usr/local/apache2/htdocs/example2
    RemoveHandler .php  

There are several ways to use these options:

  1. Set a global default, i.e. PHP5, then using RemoveHandler specify which vhosts should have PHP disabled.

  2. Set each vhost to use a specific version, i.e. Not using a global default, so PHP is disabled by default.

PHPPerVhost (last edited 2009-09-20 22:13:33 by localhost)