Run PHP as a FastCGI on a MediaTemple DV 3.5 Server

If you found this article, you probably have a reason to want to move away from mod_php.  Mine was because I was trying to operate a Magento install on a MediaTemple DV 3.5 server with periodic problems and outages.  The symptoms were that the site would slow down tremendously, to the point that requests would just hang… no response from the server.  Eventually, I was able to look at server resources in the Plesk interface and this is what I saw:

load_average_100

The CPU usage was through the roof.  And it stayed there too.  In case you’re not familiar with Plesk, you can find the CPU usage by logging into Plesk, then going to Virtuozzo >> Resources.  So I performed various upgrades to the server to try and get this system under control.  First, I installed APC (Alternative PHP Cache) which is a handy little program that caches the compiled PHP opcode to save the computer some processor cycles.  That helped a bit, but still didn’t solve the periodic problem of server slowdowns.  I decided to install php as a FastCGI to free up some resources.  Doing so on Media Temple was not as easy as I’d have liked, and the lack of documentation made it a bit of a challenge.  Here are the steps I took:

  1. You must enable root access on your Media Temple DV account.  This is required, since some of the files you’ll be creating need to go into directories where only root is allowed. Media Temple has a knowledgebase article on the subject.  Basically you click a button on your Media Temple Account Center page.
  2. Enable CGI and FastCGI on the domain where this will be applied.  Here’s how: 1. Log into Plesk  2. Click on Domains on the left side   3. Click the blue VR icon next to your domain   4. Under services check the boxes next to CGI support and FastCGI support  5. Click the OK button at the bottom
  3. Now your domain is ready to run FastCGI.  Log into your server via SSH as root.
  4. Run the commands below (without the quotes.  replace yourdomain.com with your actual domain name)
  5. “cd /var/www/vhosts/yourdomain.com/”
  6. “cp /usr/bin/php-cgi bin/”
  7. “chown -R username:psacln bin” (replace username with your FTP login name for this domain)
  8. “vi conf/vhost.conf”  (using vi or your favorite editor, create this file)
    AddHandler fcgid-script .php

    FCGIWrapper /var/www/vhosts/yourdomain.com/bin/php-cgi .php
    Options ExecCGI
    allow from all
  9. Save the file, then if you’re using SSL on this server, run this command: “cp conf/vhost.conf conf/vhost_ssl.conf”
  10. Now, restart the server: “/etc/init.d/httpd restart”
  11. If you want to see if it really worked, you can create a php page on your server with the line phpinfo();  Look through the page and you should see “Server API: CGI/FastCGI”  if so, you’re in business.
  12. Good luck.

UPDATE: Due to popular demand, I’ve written an article on how to cure 500 errors using this configuration. Click here to read.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Twitter

9 Responses to “Run PHP as a FastCGI on a MediaTemple DV 3.5 Server”

  1. Rahul Bansal  on September 7th, 2009

    Hi Dan,
    Just want to know if this change offloaded server upto some extent or not?
    DV is a big and costly server and I am shocked to acknowledge how pathetic it is performing. :-(
    Thanks,
    -Rahul

  2. Dan  on September 7th, 2009

    I reverted back to mod_php after many complaints of users getting 500 errors. The server ran much faster with fastcgi, but I couldn’t figure out a cure for those 500 errors. Aside from the 500’s, the machine was faster and more stable as a whole. When I get more time, I’d like to figure out those errors, and run fcgi again.

  3. Rahul Bansal  on September 7th, 2009

    Thanks Dan. :-)
    I also ran into 500 errors. I wasted almost 3-4 days on 500 error. :-(

  4. Tada Burke  on September 8th, 2009

    Cool article, thx. One thing is that if you use phpSuExec then that causes 500 Errors. Check to see if you have any htaccess files doing PHP –or– if you have directories set at 777 (should be 755). All PHP is now moved to the php.ini file. Cheers.

  5. Rahul Bansal  on September 11th, 2009

    I got my error fixed. But FastCGI has higher latency for first response.

  6. Niro  on October 7th, 2009

    Rahul How did you fix the 500 errors?

  7. Ivan  on December 7th, 2009

    Rahum bansal,

    thanks for posting how you fixed your error.

    you’re really a nice guy!!

    /sarcasm

  8. Dan  on December 7th, 2009

    I’ll post a new article describing how to fix the 500 errors. It involves tuning up your FastCGI configuration.

  9. GraydonStoner  on April 11th, 2010

    Has anyone here had any luck updating MT to use FastCGI and also get .htaccess files (mod_rewrite) to work properly? Everything works fine until I try to add .htaccess files to my MT account. The server then defaults to the Apache test page rather than rendering the proper file.


Leave a Reply