Different server-environments require diffrent sets of tools. This is especially true in times of small embedded linux computers like the Raspberry PI.

The basic webserver setup for almost every PHP developer ist the same old (Linux,) Apache, MySQL and PHP aka L/XAMP. But most of the time, the apache server is unnecessarily bloated or badly configured.
The problem with this setup is, that the apache and mysql servers are not very user friendly in terms of prototyping. It’s like hammering a nail with a rocket launcher. The apache server can also drain a lot of ressources on the system. I.e. if you use a raspberry pi, apache alone takes about 20-30% of your system ressources in idle mode.

Lighttpd is a lightweight webserver with minimal system ressource occupation and it can easily be setup to server PHP websites.
To show the true (minimal) power of a leightweight server, we now setup a server which can handle PHP websites with a database in the background - without using apache or mysql!


At first, we need to update the repositories to the latest version to avoid conflicts. I’m using an ubuntu 12.10 server with sshd enabled.
Earlier versions might work too for php5 and lighttpd. Raspbian should also work fine, too.

sudo apt-get update
sudo apt-get upgrade

Next, we install all necessary programs to get our lightweight server starting.

sudo apt-get install lighttpd php5 php5-cgi php5-common php-pear php5-sqlite php5-dev vim

After the installation process, we need to edit our lighttpd config to enable the fastcgi mode for php. We also need to add the php file recognition for the server.

sudo vim /etc/lighttpd/lighttpd.conf

br /> Add the following lines to the config.

server.modules = (
# At the end of the file
fastcgi.server = (
	".php" => ((
		"bin-path" => "/usr/bin/php5-cgi",
		"socket" => "/tmp/php.socket"

Next, we need to set our timezone and cgi pathinfo fix. For more information look into the php.ini or in the manual.

sudo vim /etc/php5/cgi/php.ini

br />

cgi.fix_pathinfo = 1
date.timezone = Europe/Berlin
# http://de2.php.net/manual/de/timezones.php

Our server is now configured and needs to be restarted.

sudo /etc/init.d/lighttpd restart

Congratulations! You now have a working lightweight php server with sqlite3 support. You can now open your webbrowser and navigate to your new server.

To test the setup I included a sample php script to show you the power behind the server.

Server Information can be accquired throught the phpinfo function:

sudo vim /var/www/info.php

Next, we add a new directory for our sqlite test:

sudo mkdir sqltest
sudo vim sqltest/info.php

The following php file creates a (file)database with some random records in it. After that, it reads those records back from the db and prints them.

try {
    // Create file "scandio_test.db" as database
    $db = new PDO('sqlite:scandio_test.db');
    // Throw exceptions on error
    $sql = <<<SQL
    message TEXT,
    created_at INTEGER
    $data = array(
        'Test '.rand(0, 10),
        'Data: '.uniqid(),
        'Date: '.date('d.m.Y H:i:s')
    $sql = <<<SQL
INSERT INTO posts (message, created_at)
VALUES (:message, :created_at)
    $stmt = $db->prepare($sql);
    foreach ($data as $message) {
        $stmt->bindParam(':message', $message, SQLITE3_TEXT);
        $stmt->bindParam(':created_at', time());
    $result = $db->query('SELECT * FROM posts');
    foreach($result as $row) {
        list($id, $message, $createdAt) = $row;
        $output = "Id: $id\n";
        $output .= "Message: $message\n";
        $output .= "Created at: ".date('d.m.Y H:i:s', $createdAt)."\n";
        echo $output;
    $db->exec("DROP TABLE posts");
} catch(PDOException $e) {
    echo $e->getMessage();
    echo $e->getTraceAsString();

Next, we set the correct reading and writing permissions for the folder.

sudo chown -R www-data:www-data sqltest/

The output should look something like this:

Id: 1
Message: Test 3
Created at: 14.11.2012 12:35:45
Id: 2
Message: Data: 50a39021b284f
Created at: 14.11.2012 12:35:45
Id: 3
Message: Date: 14.11.2012 12:35:45
Created at: 14.11.2012 12:35:45

You can now open up your browser and navigate to your local sqlite test