box.matto.nl

home/

Run busybox httpd with php

For many purposes a light webserver is good enough. This page looks into running httpd from busybox with php and sqlite.

Busybox httpd

The following actions will be explained below.

  • Compile busybox with httpd.
  • Create a directory as document root.
  • Test httpd.

Compile busybox with httpd.

Create a directory as document root.

As an example, create the directory /var/www and put some html-documents into this directory.

Test httpd

Now run

/usr/sbin/httpd -vv -f -h /var/www/

This will run httpd in the forground. Because of the -vv switch messages will be displayed here. Now open a webbrowser to the ip-address of your machine and check the messages.

Install php5-cgi

The following actions will be explained below.

  • Put php5-cgi in /usr/bin and make sure the needed libraries are installed.
  • Create /etc/httpd.conf
  • Check php.ini
  • Test httpd with php.

When ldd is installed on the busybox system this can be used to check if any libraries are missing:

ldd /usr/bin/php5-cgi

If ldd is not installed, then find out which libraries are missing by running /usr/bin/php5-cgi on the commandline.

/etc/httpd.conf

Put the following in /etc/httpd.conf:

*.php:/usr/bin/php5-cgi

Check php.ini

  • Check which ini-file is used
  • Check contents of the ini-file

Check which ini-file is used

Check which ini-file is used by php5-cgi with the following command:

php5-cgi -i | grep ini

Check contents of the ini-file

Check that the ini-file contains the following lines:

cgi.force_redirect = 0
cgi.redirect_status_env ="yes";

Test httpd with php.

Create a test.php-file with phpinfo in it.

cat /var/www/test.php
<?
        phpinfo();
?>

Stop the httpd if it is still running with Ctrl-C and restart it.

/usr/sbin/httpd -vv -f -h /var/www/

Now point your browser to the test.php file.

You should see something like this:

phpinfo in busybox

If the webserver does not show something like this but throws an error message, then check the messages from the -vv switch.

Run the webserver from startup

Now change your startup script to start the httpd at system start. Put something like the following line in your startup-scripts.

/usr/sbin/httpd -h /var/www/

Add SQLite to php

Now that our webserver is up and running and is able to parse php scripts, it is time to add a database to the system.

In many cases we don't need all the power that comes with MySQL. SQLite is a great alternative in those cases.

  • Add libsqlite
  • Add php5-sqlite
  • Restart httpd

Add libsqlite

Add the following files to /usr/lib:

libsqlite.so.0
libsqlite3.so.0

Add php5-sqlite

Add the following files to /etc/php5/cgi/conf.d:

pdo_sqlite.ini
sqlite.ini
sqlite3.ini

Restart the webserver and do some testing

First we make a testfile testdb.php with a small script that creates a database and a table, puts some data in the database and retrieves it.

<?
// create new database (OO interface)
$db = new SQLiteDatabase("testdb");

// create table names and insert sample data
$db->query("BEGIN;
CREATE TABLE names (id INTEGER PRIMARY KEY, name CHAR(80));
INSERT INTO names (name) VALUES('Foo');
INSERT INTO names (name) VALUES('Bar');
COMMIT;");

// retrieve the data
$result = $db->query("SELECT * FROM names");
// iterate through the retrieved rows
while ($result->valid()) {
        // fetch current row
        $row = $result->current();
        print_r($row);
        echo "<br />";
        // proceed to next row
        $result->next();
}

?>

Stop httpd if it is still running.

Now run

/usr/sbin/httpd -vv -f -h /var/www/

And point your browser to testdb.php

See the messages to check everything is OK.

If everything is working OK, kill the webserver and start it without the -vv and without the -f switches.

Create a lightweight LXC Linux container with busybox httpd and php

The instructions above where used to create a lightweight LXC container. The container system is based on busybox with dropbear. The busybox httpd is used as an webserver. This is great for light usage (like developing php applications. Busybox and dropbear were compiled from source while php5-cgi and the needed libraries were copied from a Debian installation.