Dreamhost, PHP and FastCGI

Permalink 3 users found helpful
Those of you who run Concrete5 on Dreamhost are probably underwhelmed with the performance that comes with running PHP as a cgi.

Well, I tooled and toiled and I found an answer that has yielded a big boost. If the next 5 to 7 days go as well as the last 48 hours, I might be willing to say that DreamHost is a viable concrete hosting option.

Now on to the goodies..

In the Dreamhost panel, create or edit the domain you will be hosting your site on. Make sure that both PHP and FastCGI are enabled for this domain.

Install concrete as you normally would for Dreamhost (expand it into the domain directory, and follow the typical instructions).

Once complete, cd into your site directory and do the following.

mkdir cgi-bin
touch cgi-bin/dispatch.fcgi
chmod 744 cgi-bin/dispatch.fcgi


Now, with your favorite editor, edit cgi-bin/dispatch.fcgi and give it the following contents:

#!/bin/sh
export PHP_FCGI_CHILDREN=2
exec /dh/cgi-system/php5.cgi $*


Next, create or edit .htaccess in the root folder of your concrete installation and give it these contents:

Options +ExecCGI
AddHandler fastcgi-script fcg fcgi fpl
AddHandler php5-fastcgi .php
Action php5-fastcgi /cgi-bin/dispatch.fcgi
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>


This is basically the standard pretty url's htaccess file with a few lines at the top that engage fastcgi.

Lastly, make sure the permissions on your htaccess file are secure by running:

chmod 644 .htaccess


The permissions on your two files should look like:

-rw-r--r-- 1 someuser somegroup 309 2009-05-30 09:56 .htaccess
-rwxr--r-- 1 someuser somegroup 69 2009-05-29 23:57 cgi-bin/dispatch.fcgi


That should be it. Just go turn on pretty url's and you're done. Once Concrete has cached all of your pages, you should see very respectable response times on your site!

jereme
 
daniela replied on at Permalink Reply
daniela
Great post, thanks! I look forward to trying it out. Do you have any greater wisdom now that you've run this way for a while?
rileyweiss replied on at Permalink Reply
rileyweiss
Post Redacted
jereme replied on at Permalink Reply
jereme
The FastCGI speed boost has one hitch.

If your site isn't visited frequently enough, the compiled cache of your site will be unloaded, meaning that the next person to arrive will have to endure one slow page load before the site comes up to good speed.

There is a way around this. You can set a scheduled task to hit your site at 5 minute intervals.

SSH into your dreamhost account and edit your cron by typing:

crontab -e


Hopefully you know what editor you are in or are comfortable with VIM or Pico, because this isn't that tutorial.

Add something similar to the following to your cron:

*/5 * * * * curl -shttp://somedomain.com > /dev/null

This will poll the website athttp://somedomain.com every 5 minutes.

For bonus points, determine the IP of the server your Dreamhost account is hosted on and add it to the ignore list for your web statistics analyzer.

Other than that, it's been a great solution for me.
daniela replied on at Permalink Reply
daniela
Thanks again, Jereme. Works like a charm.
dimunation replied on at Permalink Reply
Works like a charm!

After using this on a heavily used site, i ran into an issue with hard memory limits. Setting PHP_FCGI_CHILDREN=2 will launch two child php5.cgi processes for each request that needs to be serviced. Dreamhost uses mod_fcgid, so only one process will ever be used, rendering the children useless. Furthermore, each child was being allocated a portion of the available memory, causing occasional page load issues under heavy load.

On Dreamhost, you should be able to safely remove the PHP_FCGI_CHILDREN line and allow mod_fcgid to manage the processes. You'll notice far less php5.cgi processes running concurrently as a result.
benradler replied on at Permalink Reply
Unfortunately, this worked for ONE of my THREE dreamhosted sites.

Once I modify my .htaccess file appropriately and create the /cgi-bin/dispatch.fgci file, visiting the site returns a blank white page which times out after 60 seconds. Then we get a generic internal server error message. No more info beyond that.

Anyone got any ideas? I followed the instructions word for word, and even set/verified the permissions twice using SSH.
jereme replied on at Permalink Reply
jereme