Saturday, November 03, 2007

lighttpd+PHP setup on Fedora Core 4

The Apache httpd webserver on my desktop at work is configured to work with Tomcat, on which I develop web applications. For various reasons, there is a high degree of coupling between Apache and Tomcat, with custom httpd.conf and rewrite.conf files. Recently I had a need to build a dynamic page which would not be powered by a Java web application. To do this under Apache, I would have to swap out the custom configs, and swap in the original configs that came with Apache, then restart Apache. To revert back to doing web development (which I would very likely need to do), I would have to swap the config files out in the other direction and restart. So using the built-in Apache server was not going to be very convenient, given my situation.

I had read about the lighttpd webserver when playing with Ruby on Rails in the past. Lighttpd is a very fast and lightweight web server, much like Jetty in the Java application server world. So I decided to install it as an extra webserver on my Fedora Core 4 desktop along with Apache so I could develop my dynamic page on it.

To install lighttpd, I found some useful information in this Digital Media Minute Howto page for Ruby on Rails. Essentially, to install lighttpd, I ran yum, like so:

[root@pluto ~]# yum install lighttpd
[root@pluto ~]# yum install lighttpd-fastcgi

For the actual script, I thought about using Python or Ruby as a CGI script, then ultimately settled on PHP, which was pre-installed on the box as part of the default Fedora Core 4 install. The nice thing about PHP is that it does not need to be set up as an actual CGI program, you can simply embed PHP calls into an HTML page running inside a PHP enabled webserver. This was ideal for me, since my intention was to be able to build the dynamic page and hand it off to someone else to run, so with PHP, all that person would have to do is drop the page into a PHP-enabled server and run it.

To configure lighttpd for PHP, I found this Nixcraft article on Lighttpd/PHP/fastcgi configuration which I pretty much followed to the letter. The only difference between the configuration described and mine was that I reassigned the server.port variable to 81 from the default 80, since Apache was already listening on port 80.

## bind to port (default: 80)
server.port                = 81

To start lighttpd, I use the standard command to manually start it from /etc/init.d. Obviously, if I wanted it started always by default (I didn't), I could use chkconfig (or just symlinks) to do this.

[root@pluto ~]# /etc/init.d/lighttpd restart
Stopping lighttpd:                                         [FAILED]
Starting lighttpd:                                         [  OK  ]

To verify that everything came up correctly, I pointed my browser to http://localhost:81 and I see the "Powered by Lighttpd" start page. Now I actually needed to build my PHP script page. Since I was getting back to PHP after a long time, I decided to build a "Hello World" page to get things moving. From the lighttpd.conf file, I knew that the docroot for lighttpd was on /srv/www/lighttpd/ (see server.document-root), so I build a test.php file in it, which is shown below:

  <head><title>PHP Test</title></head>
    <?php $name="Sujit"; ?>
    Hello <?php echo $name; ?>

And bringing up this page on http://localhost:81 predictably shows "Hello Sujit" on the page.

I hope this post has been useful. About two jobs back, I would routinely write dynamic pages (first in Perl CGI and then in PHP) and test them on my local Apache webserver. However, since then, I became a Java web developer and I always find that the default Apache webserver is repurposed as a dedicated front end to the Java application server. I found it mildly annoying that whenever I needed a vanilla webserver to share content and information (there have been a few occasions), I would have to think of other ways to do it. With lighttpd, I no longer have to. If you are or have been in a similar situation, I hope you will find the installation and configuration instructions for lighttpd and php easy to understand and use.

2 comments (moderated to prevent spam):

sathis kumar(india) said...

please tell me the how to connect the PHP in apache tomcat server and error is showing while opening the local host in web browser
Important note : this is my first project for me

please e-mail me at

Sujit Pal said...

Hi Sathis, I haven't set up PHP with Apache, since my Apache is too closely tied (via rewrites and other configs) to my Tomcat to my main web application. That is why I use lighttpd for all my PHP/scripting work. However, there are any number of guides which provide step-by-step instructions to do this - just search google for "apache php setup" (I just did) and you will get many helpful hits.