Advanced PHP configuration
From DreamHost
You are expected to be knowledgeable in the UNIX shell.
[edit] Introduction
This article is intended to bundle up some of the more complex scenarios of PHP configuration into a single place.
If you follow the steps outlined here you will optionally end up with:
- The ability to have a local php binary with a custom php.ini file and to keep that binary and ini file synced with the current Dreamhost php.ini
- The ability to share that local php custom install amongst all your domains in your home directory
- For PHP 5, the ability to wrap that custom install in a fastcgi wrapper
- The ability to run PHP 4 and PHP 5 in a single domain (in different directories)
[edit] Instructions
These instructions will walk through steps to setup PHP5, with options along the way to also set up PHP4 at the same time.
[edit] Step 1 - Set up local PHP directory
First SSH into your home directory, then issue the following commands relevant to your target version/s of PHP.
[edit] PHP 4
mkdir -m 771 ~/php
[edit] PHP 5
mkdir -m 771 ~/php5
[edit] PHP 4 and 5
Run both the above commands.
[edit] Step 2 (optonal) - Create FastCGI wrapper
PHP 5 only.
[edit] PHP 5
cat > ~/php5/php5-wrapper.fcgi << "EOF" #!/bin/bash export PHP_FCGI_CHILDREN=3 exec ./php5.cgi EOF
Then set it to be executable:
chmod 0750 ~/php5/php5-wrapper.fcgi
[edit] Step 3 - Download php-update script
The script can be downloaded directly from http://www.network.net.au/dreamhost/php-update.zip
Current version is 0.1.6
curl http://www.network.net.au/dreamhost/php-update.zip > ~/php-update.zip unzip ~/php-update.zip
Optionally remove the ZIP archive.
rm ~/php-update.zip
[edit] Step 4 - Copy php-update scripts to php directory
[edit] PHP 4
cp ~/LICENSE ~/php/ cp ~/php-update.php ~/php/ cp ~/php-update.sh ~/php/
[edit] PHP 5
cp ~/LICENSE ~/php5/ cp ~/php-update.php ~/php5/ cp ~/php-update.sh ~/php5/
[edit] PHP 4 and 5
Run both the above commands.
[edit] All
Optionally remove the original files.
rm ~/LICENSE rm ~/php-update.php rm ~/php-update.sh
[edit] Step 5 - make the php-update scripts executable
The scripts need to be executable to run.
[edit] PHP 4
chmod 0700 ~/php/php-update.sh chmod 0700 ~/php/php-update.php
[edit] PHP 5
chmod 0700 ~/php5/php-update.sh chmod 0700 ~/php5/php-update.php
[edit] PHP 4 and 5
Run both the above commands.
[edit] Step 6 - run the php-update script
Execute the php-update shell script to create your local versions.
If you want to make any customisations to the php.ini, they should be set in the php-update.php file as described on line 62 (version 0.1.4) prior to execution. If you don't setup your customisations in the php script, then they will be overwritten if Dreamhost's source php.ini file changes.
If you change your customisations at any time, you will need to delete the files php.ini and php.ini.unmodified, and then execute the php-update shell script again.
[edit] PHP 4
cd ~/php ./php-update.sh
After the script has executed read the output and act on any errors. If there were no errors then check the file listing.
ls -lh ~/php
The output should be like:
-rw-r--r-- 1 username pg123456 18K 2004-09-23 06:31 LICENSE -rwx------ 1 username pg123456 7.4K 2004-09-23 06:31 php-update.php -rwx------ 1 username pg123456 2.0K 2004-09-23 06:36 php-update.sh -rwxr-x--- 1 username pg123456 3.3M 2004-09-23 06:36 php.cgi -rw-rw-r-- 1 username pg123456 22K 2004-09-23 06:36 php.ini -rw-rw-r-- 1 username pg123456 22K 2004-09-23 06:36 php.ini.unmodified
[edit] PHP 5
cd ~/php5 ./php-update.sh
After the script has executed read the output and act on any errors. If there were no errors then check the file listing.
ls -lh ~/php5
The output should be like:
-rw-r--r-- 1 username pg123456 18K 2004-09-23 06:31 LICENSE -rwx------ 1 username pg123456 7.4K 2004-09-23 06:31 php-update.php -rwx------ 1 username pg123456 2.0K 2004-09-23 06:31 php-update.sh -rw-rw-r-- 1 username pg123456 45K 2004-09-23 06:38 php.ini -rw-rw-r-- 1 username pg123456 45K 2004-09-23 06:38 php.ini.unmodified -rwxr-x--- 1 username pg123456 53 2004-09-23 06:31 php5-wrapper.fcgi <--- *** FastCGI ONLY *** -rwxr-x--- 1 username pg123456 5.4M 2004-09-23 06:38 php5.cgi
[edit] PHP 4 and 5
Run both the above commands.
[edit] Step 7 - periodically update the php binary and ini files
The php-update script should be run at some interval to check if there is a newer version of the Dreamhost php binary and ini files.
The script actually checks whether the files have changed and only copies them across to your local version if they have.
Add the script to crontab and check for a new file weekly.
To edit your crontab file type:
crontab -e
Paste the following line/s into the editor that appears:
[edit] PHP 4
@weekly /bin/bash $HOME/php/php-update.sh
[edit] PHP 5
@weekly /bin/bash $HOME/php5/php-update.sh
[edit] PHP 4 and 5
@weekly /bin/bash $HOME/php/php-update.sh @weekly /bin/bash $HOME/php5/php-update.sh
Close the editor by pressing CTRL-x, follow the prompts it gives you to save and exit.
[edit] Step 8 - alias the local php directories into your website domains
You can alias this local install into the base directory of as many websites as you like.
Let's assume the website is setup at ~/example.com
[edit] PHP 4
cd ~/example.com ln -s ../php
[edit] PHP 5
cd ~/example.com ln -s ../php5
[edit] PHP 4 and 5
cd ~/example.com ln -s ../php ln -s ../php5
[edit] Step 9 - setup .htaccess files
The .htaccess file will point php scripts in your domain/s to the local versions we have setup.
Assume that you are still working on the domain example.com and that it has not got an .htaccess file setup already. If it does, then you simply need to edit the existing file and not run these commands.
[edit] PHP 4
cat > ~/example.com/.htaccess << "EOF" Options +ExecCGI AddHandler php-cgi .php Action php-cgi /php/php.cgi EOF
[edit] PHP 5
cat > ~/example.com/.htaccess << "EOF" Options +ExecCGI AddHandler php5-cgi .php Action php5-cgi /php5/php5.cgi EOF
[edit] PHP 4 and 5
This method only allows running of one of the two versions in the base directory, then you can over-ride that version on a per directory basis using another .htaccess file.
These instructions will setup PHP 5 for the whole site and then setup PHP 4 in the subdirectory "testPHP4".
Create the .htaccess file for PHP 5 in the site root directory
cat > ~/example.com/.htaccess << "EOF" Options +ExecCGI AddHandler php5-cgi .php Action php5-cgi /php5/php5.cgi EOF
Create a test file for PHP 5
cat > ~/example.com/info.php << "EOF" <?php phpinfo(); ?> EOF
Create the PHP 4 test directory
mkdir ~/example.com/testPHP4
Create the .htaccess file for PHP 4 in the PHP 4 test directory
cat > ~/example.com/testPHP4/.htaccess << "EOF" AddHandler php-cgi .php Action php-cgi /php/php.cgi EOF
Create a test file for PHP 4 in the PHP 4 directory
cat > ~/example.com/testPHP4/info.php << "EOF" <?php phpinfo(); ?> EOF
Visiting the address http://example.com/info.php will show the PHP 5 info page.
Visiting the address http://example.com/testPHP4/info.php will show the PHP 4 info page.
[edit] Step 10 - Secure php.ini and php cgi
Protect your new php.ini and php cgi's
In the file ~/example.com/.htaccess add
<FilesMatch "^php5?\.(ini|cgi)$"> Order Deny,Allow Deny from All Allow from env=REDIRECT_STATUS </FilesMatch>
OR: For even better protection, protect the entire directory instead.
Create the file ~/php5/.htaccess and add:
<FilesMatch ".*"> Order Deny,Allow Deny from All Allow from env=REDIRECT_STATUS </FilesMatch>
[edit] Step 11 (optonal) - FastCGI setup
PHP 5 only.
[edit] PHP 5
To use FastCGI with PHP 5 you need to modify any .htaccess that specify use of the php5.cgi binary this:
Remove the following lines from the relevant .htaccess files...
AddHandler php5-cgi .php Action php5-cgi /php5/php5.cgi
And replace them with these...
AddHandler fastcgi-script fcg fcgi fpl AddHandler php5-fastcgi .php Action php5-fastcgi /php5/php5-wrapper.fcgi
Be careful not to wreck any earlier FastCGI settings in your .htaccess file.
Also make sure that FastCGI is enabled for this domain in the Dreamhost Control Panel.
[edit] See also
[edit] References
Much of the information used to produce this set of instructions came from the following pages:
Thanks go to their authors and contributors.

