Monday, July 18, 2011

Lighttpd and PHP on DD-WRT (Possibly OpenWRT too)

Busybox httpd is good for simple static pages, now here is a tutorial for installing lighttpd and php with fastcgi support in the router. Here I choose lighttpd over Apache for its light cpu usage and small memory footprint.


Since all our software are based in /opt folder, some modification must be done to get everything working:


  1. Install php5 and lighttpd
    To install php5 with fastcgi support, run:

    opkg install libsqlite3
    opkg install php5-fastcgi
    opkg install php5-mod-gd

     
    Along with php5, the popular libgd module is also installed.
    Copy php.ini to its default location:
    cp /opt/etc/php.ini /etc/php.ini
    After that, test php installation by running:
    php-cgi -v

    You should see the version info and no warning/error message.

    Install lighttpd and some modules
    opkg install lighttpd
    opkg install lighttpd-mod-fastcgi
    opkg install lighttpd-mod-simple-vhost

    *
    If installing sqlite, then also install sqlite php
    opkg install php5-mod-sqlite3

  2. Modify php.ini
    Edit /etc/php.ini

    Find doc_root = "/www" (or whatever) and change it to doc_root = "/opt/www"Find extension_dir = "/usr/lib/php" and change it to extension_dir = "/opt/usr/lib/php"
    Find ;extension=gd.so and remove the leading semicolon, change it to extension=gd.so
    Find ;date.timezone= , remove the leading semicolon and change it to date.timezone = "America/New_York"
    Replace the red part with yours, a full list is available here.

    *If installing sqlite also
    delete the semicolon before ;extension=sqlite3.so

  3. Modify lighttpd.conf
    With our installation, the configuration file for lighttpd is located at /opt/etc/lighttpd/lighttpd.conf. Edit this file:

    First, enable mod_fastcgi and mod_simple_vhost by deleting the highlighted lines' leading #:

    lighttpd-module-enable


    Then
    find server.document-root  and change its value to "/opt/www"
    find #server.port = 81 and change it to server.port = 81

    This will set lighttpd server to listen port 81. If you want to use port 80, make sure to move the DD-WRT's web admin to another port. Details in the Busybox HTTP post, step 1.

    add following at the end of the file:
                   fastcgi.server = ( ".php" => ((
                         "bin-path" => "/opt/usr/bin/php-cgi",
                         "socket" => "/tmp/php.socket"
                     )))

  4. Lighttpd startup script
    Create or edit file /opt/etc/init.d/lighttpd with below. Remove any previous contents if its not empty.

    #!/bin/sh
    source /mnt/root/.profile

    BIN=lighttpd

    LOG_D=/var/log/$BIN
    RUN_D=/var/run
    PID_F=$RUN_D/$BIN.pid
    COND=$1[ $# -eq 0 ] && COND="start"
    case $COND in
    stop)
      killall lighttpd
      killall php-cgi
      ;;
    start)
      mkdir -p $LOG_D
      mkdir -p $RUN_D
      $BIN -f /opt/etc/lighttpd/lighttpd.conf -m /opt/usr/lib/lighttpd
      ;;
    *)
      exit 1
    esac


    Set it as a servicechmod a+x /opt/etc/init.d/lighttpd
    ln -s /opt/etc/init.d/lighttpd  /opt/etc/init.d/S80lighttpd


  5. Test lighttpd server
    First create the home folder
    mkdir /opt/www

    Then create the file /opt/www/phpinfo.php with contents below:

    <?php
    phpinfo();
    ?>

    After that, launch lighttpd
    /opt/etc/init.d/lighttpd

    #to stop the lighttpd server, run
    /opt/etc/init.d/lighttpd stop

    Open browser and navigate to http://router_ip:81/phpinfo.php to check the output.

4 comments:

Hi,

Thanks for your manual. It works perfectly, except for one small detail. In my installation, when php requests date/time info from the system (e.g. date() ), the following output is generated:

Warning: date(): Invalid date.timezone value 'Europe/Amsterdam', we selected the timezone 'UTC' for now. in /opt/www/time.php on line 2

Fatal error: date(): Timezone database is corrupt - this should *never* happen! in /opt/www/time.php on line 2

I assume php can't find the correct database but don't know how to fix it. I have installed zoneinfo-core and zoneinfo-europe.

Any help would be appreciated :)

Regards,

Rick

Look like php just needs an update: http://www.dd-wrt.com/phpBB2/viewtopic.php?p=743879

Your post made me try that for the 2nd time and fixed it! Thanks!

No problem. I wish I had the time to toy with this some more.

Post a Comment