Installing lighttpd Web Server in Linux with PHP and MySQL

lighttpd Web Server

After two articles on installing Apache, lets take a look at installing and configuring lighttpd web server on a linux system. lighttpd (pronounced “lighty”) is a web server designed to be secure, fast, standards-compliant and flexible while having a low memory footprint.

Before installation, a few points to remember. These instruction are not for a production environment – this is for a development environment. To install the software, you need root access. You can get that using this command…

su -
[Enter root password]

or by prefixing ‘sudo‘ before each command.

Installing the Server

First install lighttpd and PHP – use your distro’s package management(yum, apt-get) system to do this.

Install lighttpd

You can use this command in Red Hat based systems(Fedora, Cent OS, etc.) to install lighttpd. Debian/Ubuntu systems might the same package name – could someone post the package name of lighttpd in the comments?

yum install lighttpd lighttpd-fastcgi

PHP Logo

Install PHP

If you don’t have PHP, you can install it using the command…

yum install php php-cli php-common

In Debian/Ubuntu systems, the command is…

apt-get install php5-cli php5-common php5-cgi

You can make sure you have php-cgi(needed for working with lighttpd) by running this command…

php-cgi -v

Installing MySQL

If MySQL is not yet installed, use this command to install it…

yum install mysql-server php-mysql mysql mysql-libs

Debian/Ubuntu Command…

apt-get install mysql-client mysql-common mysql-server php5-mysql

Configuring lighttpd

lighttpd configuration is done by editing the text file ‘/etc/lighttpd/lighttpd.conf’.

Enable some lighttpd modules by removing the comment character(#) from the beginning of the line. I have enabled the following modules…

server.modules              = (
                                "mod_rewrite",
                                "mod_redirect",
                                "mod_access",
                                "mod_fastcgi",
                                "mod_accesslog" )

Configure lighttpd’s document root – if you already have Apache, I would recommend using the same document root as Apache(applicable for development servers only).

server.document-root        = "/var/www/"

I find it helpful to enable folder listing in my server.

## virtual directory listings
dir-listing.activate       = "enable"

Turn On FastCGI PHP Support

Now, make sure lighttpd works with PHP – first get the path of php-cgi using the command..

$ whereis php-cgi
php-cgi: /usr/bin/php-cgi #Might be different on your system.

Then add the path to the configuration file of lighttpd…

#### fastcgi module
## read fastcgi.txt for more info
## for PHP don't forget to set cgi.fix_pathinfo = 1 in the php.ini
fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                   "socket" => "/tmp/php.socket",
                                   "bin-path" => "/usr/bin/php-cgi"
                                 )
                               )
                            )

Make sure you uncomment the above lines by removing the ‘#’ characters.

After you are done, save the file. Time to try out the server.

Starting the lighttpd Server

First stop Apache if it is running…

/etc/init.d/httpd stop

Now start lighttpd using the command

/etc/init.d/lighttpd start

Go to your document root(/var/www) and create a php files called ‘info.php’ and put this code inside it…

<?php
phpInfo();

Now fire up a browser and go to localhost – you should see a file listing page with a ‘info.php’ in the list. Click on that link – if you see a PHP information page, your web server is setup correctly.

To make sure MySQL-PHP connection is working, install phpMyAdmin – or write a database connection script – whatever is easier for you.

Related Links

Shameless Plug: If you are a Linux user, you may want to check out my Linux Blog – LinDesk – its about Linux on the Desktop – Articles, Application Reviews and Tutorials about many aspects of Linux included configuration and scripting.

3 Comments

  1. These instruction are not for a production environment – this is for a development environment

    can you explain why? what changes should I make in order to install lighttpd in a production server?

  2. In Debian (and Ubuntu, I guess) fastcgi it’s included in the package “lighttpd”.
    apt-get install lighttpd

  3. This is for both production and development since there’s no way to configure PHP to work with lighttpd.

Comments are closed.