Rolling my own PHP CGI on DreamHost

So I switched over to DreamHost (warning: affiliate link) as my primary hosting provider. I like them a lot - especially because of the SSH access they grant me - but I’ve had some trouble trying to use their PHP as a CGI.

Note I know this page is all messed up in IE6. It’s because of some really weird thing that’s happening when I include <pre> tags on my page. In Mozilla it looks okay (for now), but it gave me problems, too. Time to stop relying on someone else’s CSS design…

The Issues

I couldn’t get WordPress to work (using 1.3alpha5) with permalinks on.

Textpattern didn’t even display its homepage correctly. The issues have to do with PHP as a CGI not providing the expected path_info.

Solution

After much work, I finally compiled my own PHP, installed it, and have it running. Textpattern now works. A clean install of WordPress went flawlessly - permalinks now work even in CGI mode.

I’ve documented the steps I used to build this below.

The main fix comes from compiling PHP with the --enable-force-cgi-redirect flag. According to other posts on DreamHost’s knowledgebase, this fixes the broken pathing issues when using a CGI.

These instructions assume you are using DreamHost (warning: affiliate link) or some other provider that allows you SSH access and to install your own software.

Setup

  1. First, enable shell access for one of your users via their control panel. It’s in Users -> Users -> Edit.

Get the Source Code

  1. Then download the packages you need -

    Sablotron
    libxml2
    libxslt
    php

    The fastest way to do this is from an SSH session. Here’s my method:

    1. Create a downloads directory.
      mkdir downloads
    2. Then create a src directory inside. That’s where you’ll compile your needed libraries.
      cd downloads
      mkdir src
      cd ..
    3. Use links or ncftp to get each of the source tarballs. For example:
    ~/downalods>links http://www.gingerall.com/charlie/ga/xml/d_sab.xml
   
    And use the browser to download the needed files.
  1. Unpack each of the files you just downloaded.
    ~/downalods>cd src  
    ~/downalods/src>tar -zxvf Sablot-1.0.1.tar.gz  
    ~/downalods/src>tar -zxvf libxml2-2.6.16.tar.gz  
    ~/downalods/src>tar -zxvf libxslt-1.1.12.tar.gz  
    ~/downalods/src>tar -zxvf php-4.3.9.tar.gz
    
(or whatever the latest version numbers are)
  1. configure and make each package, probably in this order

    ~/downalods/src>cd Sablot-1.0.1  
    ~/downalods/src/Sablot-1.0.1>./configure --prefix=$HOME/usr  
    ~/downalods/src/Sablot-1.0.1>make  
    ~/downalods/src/Sablot-1.0.1>make install  
    ~/downalods/src/Sablot-1.0.1>cd ..
    ~/downalods/src>cd libxml2-2.6.16  
    ~/downalods/src/libxml2-2.6.16>./configure --prefix=$HOME/usr  
    ~/downalods/src/libxml2-2.6.16>make  
    ~/downalods/src/libxml2-2.6.16>make install  
    ~/downalods/src/libxml2-2.6.16>cd ..  
    ~/downalods/src>cd libxslt-1.1.12  
    ~/downalods/src/libxslt-1.1.12>./configure --prefix=$HOME/usr --with-libxml-prefix=$HOME/usr/  
    ~/downalods/src/libxslt-1.1.12>make  
    ~/downalods/src/libxslt-1.1.12>make install  
    ~/downalods/src/libxslt-1.1.12>cd ..
~/downalods/src>cd php-4.3.9  
    ~/downalods/src/php-4.3.9>./configure \
--prefix=$HOME/php4 \
--enable-force-cgi-redirect \
--with-config-file-path=$HOME/php4/etc/php.ini \
--enable-discard-path \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--exec-prefix=$HOME/php4 \
--with-mysql \
--enable-bcmath \
--enable-calendar \
--with-jpeg-dir=/usr/lib \
--with-zlib-dir=/usr/lib \
--with-xslt-sablot=$HOME/usr \
--with-expat \
--with-dom \
--with-dom-xslt=$HOME/usr \
--with-dom-exslt=$HOME/usr \
--enable-exif \
--with-gd \
--with-png-dir=/usr/lib \
--with-ttf \
--with-freetype \
--enable-gd-native-ttf \
--enable-sockets \
--enable-wddx \
--with-xmlrpc \
--enable-xslt \
--with-gdbm \
--with-pear

Before I could compile PHP, I had to add a few things to the PHP Makefile “INCLUDES” line. (I know there’s a way to do this with command line arguments, but I didn’t remember how.)

Put these at the beginning of the INCLUDES = line

-I/home/myusername/usr/include -I/home/myusername/usr/include/libxml2 -I/home/myusername/usr/include/libxml2/libxml/
Finally, run
    ~/downalods/src/php-4.3.9>make  
    ~/downalods/src/php-4.3.9>make install  
    ~/downalods/src/php-4.3.9>cd ..  
    

Configuring Apache to use the new PHP binary

  1. Create a cgi-bin directory is the root of one of your domains.
  2. chmod that directory to 755: chmod 755 cgi-bin
  3. Copy the php binary into that directory: >cp ~/php4/bin/php ./cgi-bin
  4. Copy the php.ini-recommended file into your cgi-bin directory and tailor it to your desire
  5. Edit the .htaccess file (or create it if you don’t have one yet) to contain the following lines:
AddHandler php-cgi .php  
    Action php-cgi /cgi-bin/php.cgi
  1. Test out your new installation by running a .php script with the line:

    phpinfo();

Hope this helps!

3 Comments on “Rolling my own PHP CGI on DreamHost”

Joe, July 6th, 2005 at 9:44 am

Are you using a dedicated server solution with dreamhost, or are you getting that kind of support with one of their shared server packages?

jamesvl, July 6th, 2005 at 4:16 pm

It’s actually one of their shared server packages - and one of the reasons I decided to go with them. (I’ve had a lot of web hosts in the past.)

MarcusH, May 17th, 2006 at 4:30 am

Hello James,

Thanks for this info. Looks like I might need to do this soon to get PHPList running on Dreamhost.

I’m interested to know, in light of the recent changes to the PHP implementation on Dreamhost, whether this method is still working for you?

Thanks, Marcus

Leave a Reply

Login Method

OpenID

Anonymous