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:
All you need is :
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:
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:
Then change /etc/default/lighttpd to point to configuration file:
To make sure that lighttpd can find installed modules, create a link to them:
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:
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:
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:
Now back in the lighttpd.conf file, change the simple-vhost settings to something like:
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:
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:
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.
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:
Also, create a link to php executable and php.ini:
To enable the gd extention, you should find the place in the file that has this entry:
Remove the ";" so that the line becomes:
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 :
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 :
You could try to change :
For some reason that helped me here :-)
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.