Yep, yet another Raspberry Pi post! After a little time off, I have just resumed my work on www.raspberryweather.com. It’s running WordPress on my hard working Raspberry Pi. After adding several tweaks to it, such as caching, I still felt that it’s a little slow sometimes. That is why I decided to step away from the classic LAMP (Linux, Apache, MySQL, PHP) and try out something new. This new option is lighttpd, also nicknamed lighty. It is much easier to set up, compared with nginx and the Quick Cache plugin works flawlessly with it. The increase of speed is noticeable, you should try it yourself.

Start with the well known command – sudo apt-get install lighttpd. With it, your Pi will install lighttpd along with any required dependencies. After installation the server will start and notify you. Same as with Apache, you show the website contents in /var/www map. Take ownership of this map with sudo chown www-data:www-data /var/www. Also allow the group to write to this directory by typing sudo chmod 775 /var/www. To test if the first part was completed successfully, create a simple html file inside /var/www. Hopefully the contents of the html file will be shown.

Now you need to work on the rewrite rules, so your WordPress site will be able to use pretty links. Modify the configuration file – nano /etc/lighttpd/lighttpd.conf. Here is the important part of config for my site along with some comments. You can easily copy and paste it to your config.

#replace raspberryweather with your domain/ip

$HTTP["host"] == "www.raspberryweather.com" {
  server.document-root = "/var/www/"

  server.error-handler-404 = "/index.php"

  url.rewrite-final = (
    # exclude directories from rewriting
    "^/(wp-admin|wp-includes|wp-content|gallery2)/(.*)" => "$0",

    # exclude .php files at root from rewriting
    "^/(.*.php)" => "$0",
    # exclude robots.txt from rewriting
    "/robots.txt" => "$0",
    # exclude xml files also
    "^/(.*.xml)" => "$0",

    # handle permalinks and feeds
    "^/(.*)$" => "/index.php/$1"
  )
}

To test it our, restart the service with  /etc/init.d/lighttpd restart. In your WordPress admin panel, navigate to Settings, Permalinks. Make sure that Custom Structure with /%postname%/ is chosen. Hopefully you will not get any 404 errors while browsing your site.

If you want to give some extra speed, then enable gzip. Again, modify the conf by adding the following code

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ("text/xml","application/x-javascript", "application/javascript", "text/javascript", "text/x-js", "text/css", "text/html", "text/plain", "image/png", "image/gif", "image/jpg", "image/svg+xml", "application/xml")

In my case I had to comment out the existing two lines. If you forget that, you will get an error when restarting lighttpd service. Make sure that var/cache/lighttpd/compress does indeed exist! You need to take ownership of this particular folder as well issue the command chown www-data:www-data /var/cache/lighttpd/compress/. Restart the service and test if all is working as planned. Go to www.whatsmyip.org/http-compression-test/ and type in your IP or domain name.