Installing lighttpd/PHP/MySQL on TonidoPlug

Want to turn your plug into a web server? This guide will show you how to install a lightweight, standards-compliant web server (lighttpd) that supports PHP and MySQL, and is flexible enough to power most web apps.

Installation

1. SSH into your plug:

$ ssh root@<ipaddress>

2. After that, you will want to “fix” apt-get by issuing the following command as root:

# mkdir -p /var/cache/apt/archives/partial

3. Install MySQL: Issue the following command as root, and when prompted, supply a password for the MySQL root user:

# apt-get install mysql-server mysql-client

4. Install lighttpd: Issue the following command as root:

# apt-get install lighttpd

After that is done installing, you should be able to connect to your plug from your local network using it’s IP address. Open up a browser, navigate to your plug’s IP to ensure that it’s working. On Ubuntu (which the TonidoPlug runs), the default web directory is /var/www, and the configuration file is /etc/lighttpd/lighttpd.conf.

5. Install PHP: Issue the following command as root to install a FastCGI-enabled version of PHP and MySQL support:

# apt-get install php5-cgi php5-mysql

You may wish to install other php5 modules at this time as well, if you think they’ll be needed by your server. You can always do this later.

After PHP is installed, open up /etc/php5/cgi/php.ini in your favorite text editor (I prefer joe, which is available with a simple apt-get install joe) and add the following line to the bottom of the file:

cgi.fix_pathinfo = 1

To allow PHP to work, you have to make sure that the /tmp directory on your system is world-writable, which the TonidoPlug has as root writable only by default. So, as root, simply issue the command

# chmod 777 /tmp

and this should make /tmp world-writable.

6. Enable PHP support in lighttpd: Issue the following command as root:

# lighttpd-enable-mod fastcgi

This will enable the FastCGI module in lighttpd. Now you need to restart lighttpd to get it to reload the modules.

# /etc/init.d/lighttpd restart

If all goes well, you should now have a server with MySQL and PHP support. You may wish to test this by placing a info.php file in your /var/www directory with the following contents:

info.php
<?php
phpinfo();
?>

Access the file using a web browser pointed to <ipaddress>/info.php, where <ipaddress> is the internal IP address of your TonidoPlug. You should see a large listing of various PHP settings, with a section called MySQL.

Congratulations, you now have a fully working PHP/MySQL web server! Be sure to forward port 80 on your router to your TonidoPlug if you intend to use this to host a website.

Optional Configuration

Below are some optional configuration options to help optimize your server. After each of these, be sure to restart lighttpd with a simple

# /etc/init.d/lighttpd restart

to allow the server to accept the changes.

Using your own domain

If you own a domain that you wish to use, make sure port 80 is forwarded on your router, and that the @ and * records in your DNS entries point to your external IP address. This should allow you to connect to your site from your domain.

Simple V-hosts

If you want to use multiple different domains or subdomains on your server to load different pages, add a section like the following in to your /etc/lighttpd/lighttpd.conf file:

## vhost1
$HTTP["host"] =~ "(^|\.)subdomain\.example\.org$" {
      server.document-root = "/var/www/example.org/subdomain/public_html/"
      accesslog.filename = "/var/www/logs/subdomain.access.log"
}
## vhost2
else $HTTP["host"] =~ "(^|\.)anothersub\.example\.org$" {
      server.document-root = "/var/www/example.org/anothersub/public_html/"
      accesslog.filename = "/var/www/logs/anothersub.access.log"
}

In this example, someone who went to subdomain.example.org would be sent the documents in /var/www/example.org/subdomain/public_html/, and would be logged in the access file /var/www/logs/subdomain.access.log. If someone went to anothersub.example.org however, they’d be sent to whatever site is within /var/www/example.org/anothersub/public_html/ and be logged to the file /var/www/logs/anothersub.access.log. Note that the string to match is a regular expression, and thus as presented, if a user requests a subdomain of one of these subdomains, something like foo.anothersub.example.org, they’d be sent to anothersub.example.org. Special cases like this are left to the reader to figure out.

Using compression and caching to increase performance and reduce bandwidth

All modern web browsers are able to decompress pages sent with various compression algorithms. This not only increases download speed (it’s a much smaller file to deliver), but also decreases bandwidth usage. It’s a win-win. Along with compression, there’s also caching, which tells the browser not to re-download commonly used, rarely modified parts of a page, such as CSS or static image files. Lighttpd has support for both of these faculties, just as any decent web server should.

To enable compression, open your /etc/lighttpd/lighttpd.conf file in your favorite text editor, and find the server.modules section at the top, and make sure that the line that says mod_compress, is uncommented.

Now, a little further down in the file, before your v-hosts (if applicable), add the following sections.

## mimetype mapping
include_shell "/usr/share/lighttpd/create-mime.assign.pl"

and

## compress module
compress.allowed-encodings = ("bzip2", "gzip", "deflate")
compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ("text/plain", "text/html", "application/x-javascript", "text/css", "text/javascript", "text/xml")

This should now compress all text, html, css, xml, and javascript files sent out by your server.

To enable caching, go back to the server.modules section in the conf file, and make sure that mod_expire, is uncommented and placed before mod_compress,. Then add the following lines to the conf file, above your v-hosts.

## etags
$HTTP["url"] =~ "(css|js|png|jpg|ico|gif)$" {
      expire.url = ( "" => "access 7 days" )
}
etag.use-inode = "enable"
etag.use-mtime = "enable"
etag.use-size = "enable"
static-file.etags = "enable"

This will now tell the browser to cache all css/js/png/jpg/ico/gif files for 7 days, unless modified on the server between requests. The server will check if the file has been modified based on the inode of the file, the modified time of the file, or the size of the file.