Connect & Share

How to Install Lighttpd + PHP in Fastcgi Mode

Lighttpd (http://www.lighttpd.net) is a webserver daemon which we widely use for cluster deployment. Lighttpd has a smaller memory footprint as compared to Apache(http://httpd.apache.org) which is widely used on most production servers all over the world. The efficient CPU management features and fast speed from Lighttpd make it a preferred choice for high traffic sites and clusters. We prefer using it as well for our clusters along with php running in FastCGI mode. This combination provides an awesome fast environment for dynamic delivery of PHP based content. The following how to shows instructions on installing Lighttpd+PHP in FastCGI mode.

We have to check for some prerequisites here before doing this installation. I will list them out as below :

  • MySQL Server Installation
  • Openssl Installed
  • Pcre and Pcre-devel packages installed

These are just basic requirements and if you need some other special modules installed these may change as well. I am assuming the OS for the server would be CentOS 5.x in this how to but I do believe this how to should work for Fedora, CentOS or RHEL as well. You can use "yum" to install the above requirments or use "up2date" on RHEL . Lets assume that the prerequisites are already satisfied and we will proceed with the actual installation. We will make a temporary directory for the installation so that we can remove this directory once the installation is successful.

mkdir /usr/src/lighttpd_install

We will download the latest lighttpd source file from their site and uncompress it.

cd /usr/src/lighttpd_install
wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
tar xzf lighttpd-1.4.19.tar.gz
cd lighttpd-1.4.19
./configure --prefix=/usr
make
make install
Once Lighttpd is installed we need to copy the init.d script for Lighttpd and also the spawn-php.sh script to /etc/init.d/ . This spawn-php.sh will be used to spawn php-fcgi processes later after we build PHP.
cp -av doc/rc.lighttpd.redhat /etc/init.d/lighttpd
cp -av doc/spawn-php.sh /etc/init.d/spawn-php.sh
Now we need to build fastcgi on the server.
wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
tar xvf fcgi-2.4.0.tar.gz
cd fcgi-2.4.0
./configure --prefix=/usr
make
make install

Php needs to be built after this step. You can either use php4 or php5 as per your requirement. You need to download the php source from the PHP site (http://www.php.net) into the /usr/src/lighttpd_install before proceeding to the next step. We will be using PHP5 in this case and the php version used for the tutorial is php 5.2.6 . Once you have downloaded the source the steps would be as below: If you have an existing and working php installation on the server, you can use the same configure options for building php again. The following code snippet should get you the existing config line without the options we need for the build

php -i | grep config | sed s/"'"/""/g | sed s/"<.*> "/""/g |\
sed s/"<\/.*>"/""/g | sed s/".* =>  "/""/g | sed s/"'"/""/g |\
sed s/"--with-apxs=.*apxs "/""/g | sed s/".\/configure "/""/g |\
sed s/"--prefix=\/usr\/local "/""/g | sed s/"--with-apxs2=.*apxs "/""/g |\
grep -v ^tidy
Use this output from the above command along with the options below as your configure line to build php as fastcgi
--prefix=/usr/local/php5-fcgi --enable-force-cgi-redirect\
--enable-fastcgi --enable-discard-path
If you do not have an existing php installation, just build php with whatever options you need and add the --enable-fastcgi options as above. The procedure would be as below:
cd /usr/src/lighttpd_install
tar zxf php-5.2.6.tar.gz
./configure ---(add options you need to add or use\
 the config line generated from above)\--prefix=/usr/local/php5-fcgi\
--enable-force-cgi-redirect --enable-fastcgi --enable-discard-path
make
make install
If the installation is successfull you should should see php built with cgi-fcgi mode as below. Please node the php binary name could also be php-cgi.
[root@server ~]# /usr/local/php5-fcgi/bin/php -v
PHP 5.2.6 (cgi-fcgi) (built: May  8 2008 02:38:11)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
    with eAccelerator v0.9.5.2, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
    with the ionCube PHP Loader v3.1.32, Copyright (c) 2002-2007, by ionCube Ltd., and
    with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies
    with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies

After successfully building Lighttpd and PHP in Fastcgi mode, its times to now configure them to work together. In this tutorial we are considering a single site environment and so we will set the configuration files accordingly. We will be adding some more how to's soon which will show the configurations that need to be done for multi site environments or even complex setups like load balancing using Apache and Lighttpd together.

mkdir /etc/lighttpd
cd /usr/src/lighttpd_install/lighttpd-1.4.19
cp -v doc/lighttpd.conf /etc/lighttpd/
vi /etc/lighttpd/lighttpd.conf
We need to make sure the following modules are now enabled in the lighttpd.conf . They just need to be uncommented.
  • mod_rewrite
  • mod_redirect
  • mod_alias
  • mod_access
  • mod_cml
  • mod_auth
  • mod_status
  • mod_setenv
  • mod_fastcgi
  • mod_simple_vhost
  • mod_cgi
  • mod_ssi
  • mod_accesslog
Your server modules section of lighttpd.conf should look as below after the change :
server.modules              = (
                               "mod_rewrite",
                               "mod_redirect",
                               "mod_alias",
                               "mod_access",
                               "mod_cml",
                               "mod_auth",
                               "mod_status",
                               "mod_setenv",
                               "mod_fastcgi",
                               "mod_simple_vhost",
                               "mod_cgi",
                               "mod_ssi",
                               "mod_accesslog" )
The other options that need to be changed are:
server.document-root        = "/home/username/public_html"
The document root can even be in /var/www/html or just anywhere you want . We choose /home/username/public_html for easier ftp access to the location for updates.
server.errorlog             = "/home/username/logs/error.log"

Changing this setting is optional. If you want the user to have access to the error log then change the above option. Otherwise you can leave the setting as it is. Just make sure you have created the file folder /var/log/lighttpd and chowned it to the username you run lighttpd and php as.

accesslog.filename          = "/home/username/logs/access.log"
Again this setting is also optional. If you want the user to have access to the access log then change the above option.
server.port                = 80
The default port is 80. If you want to test or run your lighttpd installation on a non-standard port, you need to change this.
server.bind                = "xxx.xxx.xxx.xxx"
This is the Ip address lighttpd listens on. By default it listens on all IPs. If you want it binded to just one Ip on the server you need specify this option.
server.username            = "username"
server.groupname           = "username"
The username and group which lighttpd will run as. We are going to have the server running in suexec mode when we specify these options. Files created by the webserver will be owned by the username itself.
fastcgi.server = ( ".php" =>
  (( "host" => "127.0.0.1",
     "port" => 1026,
     "bin-path" => "/usr/local/php5-fcgi/bin/php"
  ))

)

In the above section we are specifying how php files will be handled. The bin-path will change as well as the port based on the spawn-php.sh script which we will be configuring next. After all these changes, the lighttpd.conf is all setup for use once you save the file. We will now configure the spawn-php.sh script which we have previously copied to /etc/init.d/

cd /etc/init.d/
vi spawn-php.sh
The following options need to be edited in this script:
SPAWNFCGI="/usr/bin/spawn-fcgi"
The path to spawn-fcgi will be as above as we have configured it with /usr prefix before.
FCGIPROGRAM="/usr/local/php5-fcgi/bin/php"
This path would be the absolute path to the php-fcgi binary. Please note it can be be /usr/local/php5-fcgi/bin/php-cgi as well.
FCGIPORT="1026"
FCGI_WEB_SERVER_ADDRS="127.0.0.1"
These can be left alone as they are unless you want a different Ip or Port specifically.
USERID=username
GROUPID=username
The userid and groupid for the script would be same as the one you have used in the lighttpd.conf before. Save the script and then chmod it to 755 and run it. You should see something like below as the output:
spawn-fcgi.c.197: child spawned successfully: PID: 10985
Now it is time to start lighttpd . Make sure the lighttpd logs folders are created and the user ownerships are set correctly. Once that is done, you can start lighttpd using the command as below:
[root@server ~]#/etc/init.d/lighttpd start
Starting lighttpd: [  OK  ]
Check the status to make sure it is running otherwise check the error log as set in the config file.
[root@server ~]# /etc/init.d/lighttpd status
lighttpd (pid 11210) is running...
This completes the installation steps. To test php you can upload a phpinfo file in the document root folder. A sample phpinfo.php would have the code like : If your installation is running correctly, you should see the server API as CGI/FastCGI as it shows in the image below. If you see the above, you have completed a successful installation of lighttpd and php running as a fastcgi. We hope you liked this how to and it is useful for setting up high performance lighttpd+php fastcgi setups for your site.

6 Responses to How to Install Lighttpd + PHP in Fastcgi Mode

  1. Jamie says:

    Awesome tutorial. I setup my own lighttpd and fastcgi setup within 30 minutes using this. The content loads very fast.

  2. Shaun says:

    Thank you. This how to helped me a lot

  3. gman says:

    hi. after this command “sudo make install” in compiling php it gives me this error ”
    $ sudo make install
    Installing PHP SAPI module: cgi
    Installing PHP CGI binary: /usr/local/php5-fcgi/ –enable-foce-cgi-redirect/bin/
    shtool:install:Error: multiple sources require destination to be directory
    make: *** [install-sapi] Error 1

  4. gman says:

    ok so it’s compiled without errors BUT when i try to run it nothing happens:
    root@gman-desktop:/etc# /etc/init.d/lighttpd start
    root@gman-desktop:/etc# /etc/init.d/lighttpd status
    root@gman-desktop:/etc#

    ..i’ve got no idea why it’s not working so please help me

  5. gman says:

    finally i’ve made it..somehow….but actually not finally because: ok everything’s running except one thing. when i try to run .pl or .fcgi script it gives me this error: 403 – Forbidden ..and yep when i call phpinfo(); – Server API CGI/FastCGI

  6. Nice and detailed guide, you’ve saved me a ton of time thank you!

Leave a Reply