15 Jul
2009

Moving From Apache to Lighttpd

Comment Posted in Development

Recently I moved away from Apache and to Lighttpd for my web servers to better handle load, especially in a low-memory environment like a VPS.  Resources were somewhat scattered when I was trying to get it set up the way I wanted it so I wanted to document the process.

Getting Started

This tutorial assumes you’re running Debian or Ubuntu.  Most commands in this tutorial are going to require root privileges, so sudo or su to your heart’s content.  Lets get started.

First lets get all the packages we’ll need for the install:

apt-get install lighttpd php5-cgi build-essential php5-dev bzip2

Note: at time of writing spawn-fcgi was part of the Lighttpd package, but in SVN was moved out to its own project. You might need to download the spawn-fcgi package (at this point I don’t know what its name will be, but do an apt-cache search spawn-fcgi and you should find it easily).

If you didn’t have Apache on your system previously, you’ll need to create a group and user for the various services to run as.  I stuck with the default Apache one “www-data”.

Setting Up Spawn-fcgi

The standard configuration I saw referenced when setting up Lighttpd was to have it spawn off PHP processes as needed. Lighttpd would control how many processes were spawned, how many children each process supported, etc. With a minor amount of tweaking one is able to get a go-between set up between the PHP processing and the server. I prefer this set-up because then if something needs to change with your PHP configuration you don’t need to take down the whole web server. Plus, if you want to be paranoid, you can further sandbox PHP and have it run as a user independent from Lighttpd.

The first thing that has to be done is to create a script that will spawn off a Spwn-fcgi process with the appropriate arguments that we can reference later:

vi /usr/bin/php-fastcgi
#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -f /usr/bin/php5-cgi

Save and close the file then you need to make it executable:

chmod +x /usr/bin/php-fastcgi

When this script is executed it will set spawn-fcgi to listen on the local IP and listen on port 9000.  It will spawn off 5 PHP processes and have them run as the www-data user.  You can tweak the port/user/number of children as needed.  The one you might want to play around with is the number of processes; I found 5 to handle plenty of load (90k views in a day) within a 512meg VPS.

Now we need something to kick off this script when the server starts, and a way to restart it when we make changes.  We’ll make an init script:

vi /etc/init.d/fastcgi
#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0

case "$1" in
    start)
          $PHP_SCRIPT
          RETVAL=$?
  ;;
    stop)
          killall -9 php5-cgi
          RETVAL=$?
  ;;
    restart)
          killall -9 php5-cgi
          $PHP_SCRIPT
          RETVAL=$?
  ;;
    *)
       echo "Usage: fastcgi {start|stop|restart}"
       exit 1
  ;;
esac    
exit $RETVAL

Save and close the file and then run:

chmod +x /etc/init.d/fastcgi
update-rc.d fastcgi defaults 

This script will allow you to call /etc/init.d/fastcgi stop|start|restart like you would Apache/Lighttpd/Mysql/any other daemon.  The last command ran, update-rc.d, will add this script to the normal startup scripts so when you restart the machine PHP is brought back online without having to do anything manually.

Go ahead and kick off the daemon now, we’ll need it for Lighttpd in the next section:

/etc/init.d/fastcgi start

Hooking Spawn-fcgi up with Lighttpd

With all the PHP work out of the way we can hook up Lighttpd now. Go ahead and open up it’s configuration file.

vi /etc/lighttpd/lighttpd.conf

First we need to have it load the fast cgi module.  Find the module section towards the top of the file (you’ll see a bunch of “mod_xxxxxx"s) and make sure mod_fastcgi is in that list and not commented out.

Next we need to tell Lighttpd how to access the PHP processor, in our case spawn-fcgi.  If you remember from earlier we set it up to listen on the local IP and port 9000, that’s all the info we need to give Lighttpd. So add the following to the bottom of the configuration:

fastcgi.server = ( ".php" =>
  ( "localhost" =>
    ( "host"        =>     "127.0.0.1",
      "port"        =>     9000
    )
  )
)

While you’re in here make sure you update the web root to point to where your site is currently living — probably /var/www.

Save and close and you’re done.  Restart the Lighttpd daemon to pick up the changes:

/etc/init.d/lighttpd restart

Now you have a web server with PHP running.  To test this create the following file in your web root and load it through a browser:

eAccelerator

Now that everything is running through Lighttpd lets get caching up and running using eAccelerator (similar to APC).  eAccelerator provides post-compile caching of your PHP code which will greatly increase performance of your site. It’s one of the first things I lay down when I’m setting up a PHP install. As of writing the latest version is 0.9.5.3, make sure to check the homepage for any updates and use the latest version.  It is a manual install, but goes very smoothly:

cd /tmp
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
tar xvfj eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
phpize
./configure
make
make install

With eAccelerator installed we just need to configure it.  Open up its configuration file…

vi /etc/php5/conf.d/eaccelerator.ini

And add the following to it:

extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/var/cache/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

Save and close the file. Anything you add inside the conf.d folder in the PHP config will automatically be loaded when PHP goes to run.  Now you need to create the cache folder for eAccelerator (as specified the path in the configuration file you just created).

mkdir /var/cache/eaccelerator
chmod 777 /var/cache/eaccelerator

And that’s it, eAccelerator is ready to go.  Restart the PHP process:

/etc/init.d/fastcgi restart

With that in place you’ll see a dramatic difference in the number of requests per second your server can handle.

You can confirm that is it working by going back to the PHP info page you created above.  Do a search for the string “eAccelerator” on the page and you’ll see a section on it.

Done!

And with that you have a working Lighttpd server in place of Apache, with PHP accelerator and everything. I’ll be taking this further with virtual hosts, server-side compression, how to replace .htaccess files, and content expiration in a future post.

Next Entry
Assassin's Creed 2 »

Previous Entry
« Slimming The Server

Leave A Comment

(Won’t give it out, promise)
(Markdown enabled)