Running Movable Type with FastCGI

Movable Type is a large application with a lot of files. In a CGI context that application is loaded into memory every single time, and there is no ability to leverage a cache across a session or multiple requests.

When using mod_perl of FastCGI, the application is loaded into memory so that it doesn't have to be reparsed and interpreted every single request. This dramatically increases performance.

It also has the advantage of shielding users of more popular sites from spam attacks. Servers can easily become overwhelmed when the MT App is being loaded simultaneously by so many concurrent processes (presumably someone who is posting comment spam to 10 or 20 entries all at once). By keeping MT in memory posting comments happens a lot quicker so that your web server is not tied up as long...

How to detect if you have FastCGI installed

Many systems for security reasons do not make this information publicly available. Also detecting FastCGI can vary depending upon the implementation of FastCGI that you are using. But there are perhaps a few tips to help answering this question:

Ask your Host

Without a doubt the most reliable way to figure this out is by asking your hosting provider. Many hosting providers will list the modules Apache has installed somewhere in the Knowledge Base or Help section.

Try it and find out

Add the following code to an .htaccess file in your MT directory:

<IfModule mod_fastcgi.c>
  FastCgiIpcDir /tmp/fcgi_ipc/
  AddHandler fastcgi-script .fcgi
  FastCGIConfig -autoUpdate -idle-timeout 120 -killInterval 3600 -maxClassProcesses 6 -maxProcesses 15
</IfModule>

Then create a HelloWorld.fcgi file and see if it loads properly. If it does, FastCGI is installed. Any other error and it probably isn't.

The Helloworld.fcgi file could be something as simple as this.

#!/usr/bin/perl
# Hello World Script
# Filename: hello.cgi, or hello.fcgi
print "Content-type: text/html\n\n";
print "Hello world.";

How to Run Movable Type 4 under FastCGI

With Movable Type 4 FastCGI support is integrated into the core product. This means that there is no need to install any special files, specifically the MT::Bootstrap file, into Movable Type. However, users will need to make the following changes:

Configuring Movable Type Automatically

If administrators choose, they can setup Apache to run every existing .cgi file via FastCGI. By choosing this approach, there is no need to modify Movable Type at all. To this, you will need to instruct your web server to route all requests to files with an file extension of .cgi through its FastCGI Handler. On an Apache system, that looks like this:

#AddHandler cgi-script .cgi
AddHandler fastcgi-script .cgi

Once this is done, Movable Type will immediately be running under FastCGI. Some administrators may not want to make this change globally if other CGI applications on their system are not capable of running under FastCGI. In those circumstance, administrator may want to configure Movable Type manually (see below).

Community contributed note from Su: As an alternative, the htaccess rule can be added directly within the MT directory, where it shouldn't affect other applications. (Although it could still break plugins/plugin apps if they don't go through Bootstrap?)

Community contributed note from Delfuego: It's unclear if this will work perfectly; on my setup, mt-check.cgi doesn't run at all under FastCGI. The setups below appear to avoid this by not renaming mt-check.cgi to mt-check.fcgi, thereby avoiding it running under FastCGI. I'm still debugging this, but I think that mt-check.cgi might be totally unable to run under FastCGI... A workaround for this that allows the globally-run-CGIs-under-FastCGI solution to function is to manually set the handler for mt-check.cgi back to the normal CGI handler. Given that all the instructions below ONLY set Apache to run a subset of the MT CGIs under FastCGI, the following directives will reset all the other MT CGIs to run under the standard CGI handler:

<Directory /var/www/mt4>
   ...[other config directives]...
   AddHandler fastcgi-script .cgi
   <FilesMatch "^mt-(add-notify|atom|check|config|feed|testbg|upgrade|wizard|xmlrpc)\.cgi$">
      SetHandler cgi-script
   </FilesMatch>
</Directory>

Configuring Movable Type Manually

Install FastCGI

Install a FastCGI implementation for their web server.

  • Your host may already have FastCGI set up as part of your plan. You should contact support or check their knowledge base to check. Don't rely on scripts posted on-line that claim to test this, as they seem to only check for the Perl module, while the web server itself may not actually be doing anything with it.

  • If it isn't already set up, and the user isn't on some form of dedicated plan, it's probably unlikely they have the access to do this installation.

Copy Your CGI files

Make a copy each of the following .cgi scripts, giving each copy a new file extension .fcgi. For example:

  • Create a copy of mt.cgi named mt.fcgi
  • Create a copy of mt-comments.cgi named mt-comments.fcgi
  • Create a copy of mt-tb.cgi named mt-tb.fcgi
  • Create a copy of mt-view.cgi named mt-view.fcgi
  • Create a copy of mt-search.cgi named mt-search.fcgi

Edit Your MT Configuration

In your Movable Type Configuration File (mt-config.cgi), find the section that contains the lines:

AdminScript mt.cgi
CommentScript mt-comments.cgi
TrackbackScript mt-tb.cgi
SearchScript mt-search.cgi
ViewScript mt-view.cgi

And change it to:

AdminScript mt.fcgi
CommentScript mt-comments.fcgi
TrackbackScript mt-tb.fcgi
SearchScript mt-search.fcgi
ViewScript mt-view.fcgi

You will also need to disable LaunchBackgroundTasks in your mt-config.cgi. Set the value of this config parameter to "0" (zero).

Troubleshooting notes from the community

I was experiencing trouble getting FastCGI installed and operating properly. I even created a helloworld.fcgi and the page would just hang there. Then I consulted a known Apache config that worked and noticed one key difference:

My Broken Config:

<IfModule mod_fastcgi.c>
FastCgiIpcDir /tmp/fcgi_ipc/
AddHandler fastcgi-script .fcgi
</IfModule>

This however worked:

<IfModule mod_fastcgi.c>
FastCgiIpcDir /tmp/fcgi_ipc/
AddHandler fastcgi-script .fcgi
FastCGIConfig -autoUpdate -idle-timeout 120 -killInterval 3600 -maxClassProcesses 6 -maxProcesses 15
</IfModule>

I ran into a trouble on my dev machine, and that was because it didn't have perl module FCGI installed, although it did have modfastcgi. I saw these lines in Apache's errorlog:

[Tue Jan 23 16:04:32 2007] [warn] FastCGI: (dynamic)server "/path/cgi-bin/mt334/mt.fcgi" started (pid 19941)
Can't locate FCGI.pm in @INC (@INC contains: /path/cgi-bin/mt334/extlib
lib /usr/local/lib/perl5/5.8.4/i686-linux /usr/local/lib/perl5/5.8.4 
/usr/local/lib/perl5/site_perl/5.8.4/i686-linux 
/usr/local/lib/perl5/site_perl/5.8.4 /usr/local/lib/perl5/site_perl .)
at /path/cgi-bin/mt334/extlib/CGI/Fast.pm line 22.
BEGIN failed--compilation aborted at /path/cgi-bin/mt334/extlib/CGI/Fast.pm line 22.
Compilation failed in require at lib/MT/Bootstrap.pm line 51.
BEGIN failed--compilation aborted at /path/cgi-bin/mt334/mt.fcgi line 11.
[Tue Jan 23 16:04:32 2007] [warn] FastCGI: (dynamic) server 
"/path/cgi-bin/mt334/mt.fcgi" (pid 19941) terminated by calling exit with status '2'

GLOB reference error

Not a GLOB reference at /usr/lib/perl5/FCGI.pm line 125.

If you are experiencing the above error when rebuilding single entries, ensure that the '''[[LaunchBackgroundTasks]]''' configuration option is either set to 0 or removed entirely from your '''mt-config.cgi''' file.

A way avoiding renaming all those .cgis to .fcgis

I ran into trouble with renaming cgis to fcgis, apache kept going back to the cgi versions and running them the old slow way. So I used apache's FilesMatch directive to tell it which 5 MT cgis should run under !FastCGI. Here's an excerpt from my httpd.conf:

<Directory "/var/www/cgi-bin/mt">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
<FilesMatch "^mt(?:-(?:comments|search|tb|view))?\.cgi$">
    SetHandler fastcgi-script
</FilesMatch>
</Directory>

FastCgiIpcDir /etc/httpd/fastcgi
FastCgiConfig -autoUpdate -idle-timeout 120 -killInterval 3600 -maxClassProcesse
FastCgiServer /var/www/cgi-bin/mt/mt.cgi
FastCgiServer /var/www/cgi-bin/mt/mt-search.cgi
FastCgiServer /var/www/cgi-bin/mt/mt-view.cgi
FastCgiServer /var/www/cgi-bin/mt/mt-tb.cgi
FastCgiServer /var/www/cgi-bin/mt/mt-comments.cgi

I use FastCGIServers as I prefer a resident server for each of the key MT tasks. --Jon Fawbert

Permissions Problems?

After setting up FastCGI and then setting MT up to run under it, I found that my dashboard was telling me that it couldn't write to the static dashboard directory:

Movable Type was unable to write to its 'support' directory. Please create a
directory at this location: /var/www/mt4/mt-static/support/dashboard/stats/0/000/001,
and assign permissions that will allow the web server write access to it.

Turns out that FastCGI needs special configuration if you run your MT site under Apache and suexec, which I do! FastCGI runs under the web server's user and group by default; if you want it to use suexec (and all the relevant suexec users and groups defined in your Apache config files), you need to tell it to run under the same security wrapper as Apache. For me, in Ubuntu, I just had to add this config line to my FastCGI Apache configuration file (/etc/apache2/mods-available/fastcgi.conf):

FastCGIWrapper on

After a restart, all was good!

This page was last updated on 2008-04-09, 09:39.  

3 Notes

"When using mod_perl of FastCGI, the application is loaded into memory so that it doesn't have to be reparsed and interpreted every single request. This dramatically increases performance."

Should
modperl of FastCGI
be replaced by
FastCGI
or maybe by
mod
perl or FastCGI
?

P.S. - I am having trouble finding any documentation for modperl, but came across this page when doing a search on modperl using the 'Search Documentation' box.

I am getting strange italicization when doing a 'preview' of this post --

"perl, but came across this page when doing a search on mod"

is italicized in the preceding paragraph.

I am using Firefox 2.0 (with the latest revision, as of October 9, 2007).

That hello world fcgi doesn't seem to be right. On my system I had to implement it like this:

#!/usr/bin/perl
# Hello World Script
# Filename: hello.cgi, or hello.fcgi
use FCGI;

while ( FCGI::accept >= 0 ) {
  print "Content-type: text/html\n\n";
  print "Hello world.";
}

Without the while loop, the program exits which is a problem for FastCGI.

You get an error like this in your apache log:

"terminated by calling exit with status 0"

Note that this means you also need to have the perl module FCGI installed. Your server could already be set up for FastCGI, but for another language (say, ruby).

Leave a note