HOWTO: Deploy a Catalyst application using FastCGI and nginx

Note: this is not the canonical way, it just Works For Meâ„¢ – there are, no doubt, vast improvements that could be made. YMMV.

0. ASSumptions

I’m assuming that the server already has the Catalyst framework installed, and any other Perl modules your app requires (that don’t live in the app’s lib directory). For the purpose of example, the application will be called “FooApp”, and will be installed on the server at /home/sites/

I’m assuming you want to run the application server privately (bound only to the localhost address) on port 8100.

1. Upload what you need from your app

You need to make sure you’ve got all of the Catalyst framework

From your application, you only need 4 things:

  1. The ‘lib’ directory (where all of your code is!)
  2. The ‘root’ directory (where all of you static stuff is)
  3. The ‘script’ directory (where the FastCGI wrapper is)
  4. The application config file (okay, you don’t need this if you put the config in you main file)

Upload them all to the /home/sites/ directory. If you’re using the Template Toolkit view, you also need to make sure that the directory for compiled templates exists and is writeable by the nginx user.

2.Set up nginx

There’s nothing particularly special here:

server {
  # Let's have a server alias as well

  access_log  /var/log/nginx/foo.access.log;
  root   /home/sites/;

  # Serve static content statically
  expires +30d;
  location /css {
    add_header Cache-control public;
    root /home/sites/;
  location /js {
    add_header Cache-control public;
    root /home/sites/;
  location /images {
    add_header Cache-control public;
    root /home/sites/;

  # We pass the rest to our FastCGI application server
  location /  {
    # We also set some headers to prevent proxies
    add_header Pragma "no-cache";
    add_header Cache-control "no-cache, must-revalidate, private, no-store";
    expires -1s;

    # Where our FastCGI app server is listening

    include /etc/nginx/fastcgi_params;
    fastcgi_param   SCRIPT_NAME     /;
    fastcgi_param   PATH_INFO       $fastcgi_script_name;

I’ve got all of the stuff that the Static::Simple server was handling served by nginx directly, with nice Expiry: and Cache-control: headers to allow proxy/browser caches to be used (no point serving them the same files again and again)

You should now be able to request the static content (CSS/JavaScript/images) from your site – if not, there’s something wrong with your nginx configuration. Fix it first.

If  you try to run any part of the dynamic (code) side, you should get a nice “502 Bad Gateway” message. That’s fine – it’s what you want, in fact – as you’re not running your app yet. 🙂

3. Run your application server

This involves running the FastCGI wrapper script from the ‘script’ directory you uploaded.

Important: if you use a config file, you need to move it inside of the lib/$appname directory for it to be read.

It’s probably a very good idea to run the FastCGI server manually, with “CATALYST_DEBUG=1”, to make sure the app works properly. Once you’ve confirmed it works, you can add it to the system startup.

Here’s the init.d file I use: (probably very Debian specific)

#! /bin/sh
# Provides:          application-catalyst-foo
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the FastCGI app server for the "FooApp" site
# Description:       The FastCGI application server for the "FooApp" site

OPTS="-l :8100 -n 5"
DESC="'Foo' app server"

test -f $DAEMON || exit 0

set -e

 echo -n "Starting $DESC: "
 start-stop-daemon --start --quiet --pidfile /var/run/$ -d $SITE_HOME \
 --exec /usr/bin/perl --startas $DAEMON --chuid $USER --background --make-pid -- $OPTS
 echo "$NAME."

 echo -n "Stopping $DESC: "
 start-stop-daemon --stop --signal TERM --pidfile /var/run/$
 echo "$NAME."

 echo -n "Reloading $DESC: "
 start-stop-daemon --stop --signal HUP --pidfile /var/run/$
 echo "$NAME."

case "$1" in
 sleep 5
 echo "Usage: $N {start|stop|reload|restart|force-reload}" >&2
 exit 1

exit 0

In “OPTS”, we set the listening port for the app server – this must be the same as the nginx config, otherwise you’re still going to get 502 errors. I’ve set 5 handlers – depending on your app, you may want to increase that number.

Add the initscript to whichever runlevels you want, so it’ll be run on startup, and/or…

4. Set up your favourite process monitor to ensure that the server is always running

I usually use monit – it’s easy enough to set up, using the PID file and init script to check, start and stop the app server.

And that should be that – your Catalyst application should now be up and running through nginx.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.