Tuesday, June 29, 2010

Lighttpd and php on DD-WRT

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-modgd
     
    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

  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, i.e., 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.

  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:
    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 here, 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 service
    chmod 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 the test 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.

24 comments:

Anonymous said...

To make the router more useful, how can we install MySQL 5 with Drupal 5 or MediaWiki 1.X under DD-WRT?

Anonymous said...

Hi, first of all thanks for all tutorials you have on your site, it is very helpful to me! ;)
Today a was looking for some Rapidshare, etc. download manager which will run thru PHP and I found this one http://www.networkedmediatank.com/showthread.php?tid=16833&page=1 . It seems very promising but I can't get it to work. I configured most of the things but now I'm stucked on SQLlite database which this manager using. Do you have any experiences with this manager or configuring SQLlite? I'm runing this on router TL-WR1043ND using DD-WRT. Thank you

Anonymous said...

It may have changed since this was published, but "opkg install php5-modgd" should be "opkg install php5-mod-gd".

And if you are installing sqlite, you may want to install the php support for it with "opkg install php5-mod-sqlite3"

Anonymous said...

Oh, and in addition to the above, remove the semicolon before "extension=sqlite3.so" in php.ini too.

Anonymous said...

Yeah I tried that but it seems that it need a database to be created but I don't know how to create it...

Anonymous said...

Does anybody know how to install mysql on this router?

Anonymous said...

I was able to install&use the phpXplore:

http://www.webxplorer.org/phpXplorer/www/

Sandydy said...

to install mysql server...
Openwrt contains mysql server in 10.03.1-rc4
1. update /etc/opkg.conf to

src/gz snapshots http://downloads.openwrt.org/backfire/10.03.1-rc4/ar71xx/package
dest root /opt
dest ram /tmp
lists_dir ext /tmp/var/opkg-lists

2. Run opkg update
3. Run opkg install mysql-server
4. modify /opt/etc/my.cnf to set the database variables, recommend to use my-small.cnf example from mysql
5. Run mkdir
6. Run mysql_install_db --defaults-file=/opt/etc/my.cnf --force
7. Replace /opt/etc/init.d/mysqld with...
#!/bin/sh

source /mnt/root/.profile


BIN=mysqld
RUN_D=/var/run
PID_F=$RUN_D/$BIN.pid
COND=$1
[ $# -eq 0 ] && COND="start"

case $COND in
stop)
start-stop-daemon -K -n $BIN
;;
start)
start-stop-daemon -p $PID_F -b -m -S -x $BIN -- --defaults-file=/opt/etc/my.cnf
;;
*)
exit 1
esac
8. set mysqld as service
chmod a+x /opt/etc/init.d/mysqld
ln -s /opt/etc/init.d/mysqld /opt/etc/init.d/S70mysqld
9. start mysqld by /opt/etc/init.d/mysqld start
10. remember to change root password by
mysqladmin --defaults-file=/opt/etc/my.cnf -u root password 'new-password'
11. after this, mysqld should be found in ps command. to enter mysql in console, run mysql --defaults-file=/opt/etc/my.cnf

henschwa said...

Hi!

I tried out this tut and are nearly done.

All packages where installed successfully, but when i tried "php-cgi -v" I got a "Segmentation fault".
Deactivating "mod_fastcgi" made the lighttpd running without php.

How can I fix this? Is the only way to set up new partitions again? Or can there be also a different problem?

Thanks for your support so far!

HeavyP said...

I have followed the instructions on this site and installed transmission since last year. Currently, when I want to add lighttpd and mysql but when I tried to run opkg install I got an error that I have no space left, which probably mean that I have no write permission or so. However, I tried running opkg remove and it worked.

This is the response from opkg install:
"Installing busybox (1.17.3-2) to root...
Collected errors:
* verify_pkg_installable: Only have 0kb available on filesystem /opt/, pkg busybox needs 230
* opkg_install_cmd: Cannot install package busybox.
"

Sorry, but I don't really understand what's going on. What should I do to fix this?

HeavyP said...

I tried again and it worked today for no reason. Now, I have the same problem with henschwa. Segmentation fault...

Mabawsa said...

For Heavy hand henschwa try downgrading ddw-rt to build 15962 when I went for this years build got all sorts of problems including this one. On reversion all was peachy. Hope this helps.

Anonymous said...

When I run /opt/etc/init.d/lighttpd i get a bus error i followed directions to a t so why isnt it working

jointtech said...

mysql_install_db --defaults-file=/opt/etc/my.cnf --force

Could not find help file 'fill_help_tables.sql' in /usr/share/mysql or inside /usr.

any ideas?

Running from /mnt/opt/usr/share/mysql
gives a different error:
Didn't find /usr/lib/mysqld
You should do a 'make install' before executing this script

php/lighthttpd are running great. Thanks for the blog.

injectx said...

Thanks for this great tut!

TPLink WR1043ND said...

Hi,

I tried very hard to install ipkg/opkg but failed miserably. Thus, I need to manually install all the required packages required by lighttpd, php5 and mysql.

Can you please help list down all the required dependencies for the packages that you have recommended in your post?

Currently Installed Packages
==============================
libsqlite3
php5-fastcgi
php5-modgd
lighttpd
lighttpd-mod-fastcgi
lighttpd-mod-simple-vhost

Anonymous said...

With the latest version calling lighttpd produces a bus error. Any suggestions?

Anonymous said...

Hello!
I get an error:

root@DD-WRT root $ /opt/etc/init.d/lighttpd
lighttpd: symbol 'errno': can't handle reloc type 0x2f
root@DD-WRT root $

Ps3+ Team said...

i kept receiving "no input file specified" no matter what i do, anyone with the same problem??

Eric said...

Hi I have followed all the tutorial without encountering any problem except for, when I wanted to start the lightttpd server, I had received an error saying "Bus error"


root@DD-WRT root $ /opt/etc/init.d/lighttpd
Bus error


Any idea how to fix that?

Thank

Eric

Anonymous said...

Hi,

I cannot copy php.ini to /etc/php.ini as /etc is not writable for me. I guess that php-cgi is then not started with the php.ini file.

Eva Smith said...

Content that you just created is extremely fascinating and helpful on behalf of me, I love it. Is also helpful for U.S. all.. Thanks

Read more information :- Brother Printer Technical Support

Lalit Sharma said...

Thanks for sharing this blog.This is very helpful and solving problems easily related to router problems. Good work. Please visit this link ----- Linksys Router Technical Support .

Taiwo Ogunlaja said...

I have router running busybox, it support 32GB SD Card and 500GB of SSD. I want to host my WordPress websites on this router for local users to access. I do not have any linux knowledge. Can anyone assist me on this? I would be ready to pay for this.

Post a Comment