How to Prepend a PHP File For a Single Domain

By | November 26, 2010

Sometimes you need to prepend a PHP file, before execution of any file. This can be: website header, or variable declaration, or anything else. In order to do it, we need access to out Virtualhost (which is usually located ad your httpd.conf). If you are using Directadmin, you may find it at /usr/local/directadmin/data/users/. Of course, you need to have root access to modify these values.

Here is our source virtualhost secrion:

ServerName www.lampdocs.com
ServerAlias www.lampdocs.com lampdocs.com
ServerAdmin webmaster@lampdocs.com
DocumentRoot /home/lampdocs/domains/lampdocs.com/public_html
ScriptAlias /cgi-bin/ /home/lampdocs/domains/lampdocs.com/public_html/cgi-bin/
UseCanonicalName OFF
SuexecUserGroup lampdocs lampdocs
CustomLog /var/log/httpd/domains/lampdocs.com.bytes bytes
CustomLog /var/log/httpd/domains/lampdocs.com.log combined
ErrorLog /var/log/httpd/domains/lampdocs.com.error.log


Options +Includes -Indexes
php_admin_flag engine ON

php_admin_flag safe_mode OFF

php_admin_value sendmail_path ‘/usr/sbin/sendmail -t -i -f lampdocs@lampdocs.com’

We will add a string:

php_admin_value auto_prepend_file /var/www/html/docroot.php.

Here is the result:


ServerName www.lampdocs.com
ServerAlias www.lampdocs.com lampdocs.com
ServerAdmin webmaster@lampdocs.com
DocumentRoot /home/lampdocs/domains/lampdocs.com/public_html
ScriptAlias /cgi-bin/ /home/lampdocs/domains/lampdocs.com/public_html/cgi-bin/
php_admin_value auto_prepend_file /var/www/html/docroot.php
UseCanonicalName OFF
SuexecUserGroup lampdocs lampdocs
CustomLog /var/log/httpd/domains/lampdocs.com.bytes bytes
CustomLog /var/log/httpd/domains/lampdocs.com.log combined
ErrorLog /var/log/httpd/domains/lampdocs.com.error.log


Options +Includes -Indexes
php_admin_flag engine ON

php_admin_flag safe_mode OFF

php_admin_value sendmail_path ‘/usr/sbin/sendmail -t -i -f lampdocs@lampdocs.com’

Now reboot Apache and have fun. You can place anything you need in this file. This method does not involve php.ini and allows to prepend a PHP file for a single domain.