lighttpd with PHP5 Tutorial

Written by macsat

Thursday, 24 November 2005

Modified by ymhee_bcex 31 July 2007

The Tutorial is made using a ASUS WL-500G Deluxe (or WL-500GX and WL-500GD as it is sometimes called). It should however apply to all "OpenWRT Compatible"devices, even though some details like hardware addresses, storage locations and so on could differ if you use other hardware. Check out the General OpenWRT Tutorial Information page for more information.

After following this tutorial you will have a complete running installation of lighttpd with php5 support.
In lighttpd you will have the modules mod-simple-vhost, mod-cgi and mod-status running
In PHP the GD graphics / image module will be installed and configured for use

This tutorial was modified and now includes two changes compared with original:

  1. Kamikaze
  2. Installation is done on the external /opt partitiion. Please see this tutorial for general information how to install packages to /opt partition

Tutorial Index

  1. Prerequisites - What is needed to get started.
  2. Installing lighttpd.
  3. Installing PHP5
  4. Problem: Fatal error: Call to undefined function: preg_match() 

Prerequisites

All you need is :

Installing lighttpd

The version of lighttpd I have chosen to use here, is version 1.4.15 from Kamikaze repository.

Installing can be done as:

ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/lighttpd_1.4.15-1_mipsel.ipk
ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/lighttpd-mod-simple-vhost_1.4.15-1_mipsel.ipk
ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/lighttpd-mod-status_1.4.15-1_mipsel.ipk
ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/lighttpd-mod-cgi_1.4.15-1_mipsel.ipk

Currently (July 29, 2007) lighhtpd is not in Kamikaze 7.07 directory; once it's there you don't need to spell out the full path:

ipkg -d opt install lighttpd lighttpd-mod-simple-vhost lighttpd-mod-status lighttpd-mod-cgi

Now some configuration needs to be done.

First of all, lighhtpd is looking for a file /etc/default/lighttpd, so the easiest is to copy it from where it is installed:

mkdir /etc/default
cp /opt/etc/default/lighttpd /etc/default

Then change /etc/default/lighttpd to point to configuration file:

OPTIONS="-f /opt/etc/lighttpd.conf"

To make sure that lighttpd can find installed modules, create a link to them:

ln -s /opt/usr/lib/lighttpd /usr/lib/lighttpd

All that is needed now is to make a few changes to the main lighttpd configuration file. This file is found in /opt/etc and is called lighttpd.conf

The file is too large to post here with any level of meaningfulness, but I will try to explain what to change to make it run with the modules we installed above.

On the top of the file, the server-modules are defined. Remove comment "#" mark in front of the following three modules:

server.modules = (
"mod_status",
"mod_simple_vhost",
"mod_cgi"
)

The next thing you might want to change is the server-document root. This is /www as default. Personally I use my USB disk on /opt, so I have changed this to /opt/www and keep my httpd files on the USB drive.

If you don't want to disable the built-in httpd server, change the port number of lighhtpd: httpd server:

server.port = 81

Now move a little further down, until you reach the virtual hosts area.

This is most relevant if you own several domains, or have several free domains from places like www.dyndns.org pointing at your IP.

Virtual hosts are handled a bit different in lighttpd than in apache. But I do belive that the handling is very cool :-)
Consider your webpages to be stored in /www and you having to different domains, www.domain1.com and www.domain2.com. Then you should create the two folders and maybe even symlinks for the domains without the "www" prefix:

mkdir -p /www/www.domain1.com
mkdir -p /www.www.domain2.com
ln -s /www/www.domain1.com /www/domain1.com
ln -s /www.www.domain1.com /www/dommain2.com

Now back in the lighttpd.conf file, change the simple-vhost settings to something like:

simple-vhost.server-root = "/www/"
simple-vhost.default-host = "www.domain1.com"
simple-vhost.document-root = "/"

Now - whenever you use www.domain1.com to access your web server, you get the contents of /www/www.domain1.com and when you use www.domain2.com you get contents of /www/www.domain2.com. Also - when you use some other domain name that is pointing to your IP, you get the www.domain1.com contents.

In this way, creating more domains, or subdomains (like http://sub1.domain1.com) all that is needed is to make the appropiate folder in /www

Isnt this cool? ;-)

Next thing in the configfile is the cgi module, allowing you to use php. If you use the cgi version of PHP, this section should look like:

cgi.assign = ( ".php" => "/bin/php" )

This is all that is needed to enable php, once php is installed in CGI mode.

Now that PHP is enabled, you might want to make the server use index.php as the default page when you point your browser to http://www.yourdomain.com/someurl/ that contains an index.php file. To do this, find the server.indexfiles=  part of lighttpd.conf and change to something like:

server.indexfiles = ( "index.html", "index.htm", "index.php")

The last needed change is a bit further down, where you can make the status module part look like:

#### status module
status.status-url = "/status"
status.config-url = "/config"

This gives you the pages you can see in http://your_router_ip/status and http://your_router_ip/config once we get as far as to get the server stated.

That should be it, lighttpd is ready to run, as soon as we get php installed and configured.

Installing PHP

As mentioned above, PHP is installed as a CGI module to the lighttpd server. There is also a fastcgi version available, I dont know if this gives better performance or not.

Installing the CGI version of PHP is done simply by:

ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/php5_5.1.6-1_mipsel.ipk
ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/php5-cgi_5.1.6-1_mipsel.ipk
ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/libjpeg_6b-1_mipsel.ipk
ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/libpng_1.2.8-1_mipsel.ipk
ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/libgd_2.0.33-2_mipsel.ipk
ipkg -d opt install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/php5-mod-gd_5.1.6-1_mipsel.ipk

Again, I am installing from 7.06 directory, and specifying full path because these modules didn't make it to 7.07 distribution yet. I also hope that once that happens php5-mod-gd will be able to find dependencies automatically.

As you can see above, I have installed both PHP and the GD module for PHP. This is mostly to show how to install and enable modules.

The configuration file for PHP is /opt/etc/php.ini
In this file you can make a lot of settings. Setting max memory usage pretty low might be a good idea in OpenWRT due to the low amount of RAM typically in an OpenWRT compatible device.

Make the following changes to point PHP to the external storage:

doc_root = /opt/www
extension_dir = "/opt/usr/lib/php"

Also, create a link to php executable and php.ini:

ln -s /opt/usr/bin/php /usr/bin/php
ln -s /opt/etc/php.ini /etc/php.ini

To enable the gd extention, you should find the place in the file that has this entry:

;extension=gd.so

Remove the ";" so that the line becomes:

extension=gd.so

Now the gd extention will be available to php :-)

To test your new webserver and its PHP capabilities, you need to create a php file and start the server :

echo "<?php phpinfo(); ?>" > /opt/www/phpinfo.php
/etc/init.d/lighttpd start

Now you should be able to point your browser to: http://your_router_ip/phpinfo.php and see the phpinfo page.

If you for some reason get :

No input file specified.

You could try to change :

doc_root = /opt/www
to:
doc_root =

For some reason that helped me here :-)

Problem: Fatal error: Call to undefined function: preg_match()

If you are using PHP Scripts that are containing regular expressions (regex), you might get some error message like the above. To resolve this, you need to install the correct lib and php module:

ipkg install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/libpcre_7.0-1_mipsel.ipk
ipkg install http://downloads.openwrt.org/kamikaze/7.06/brcm-2.4/packages/php5-mod-pcre_5.1.6-1_mipsel.ipk

Now you should be able to run the script.