Learn How To Install Lighttpd and PHP on Ubuntu

September 20, 2019

Table of Contents

Lighttpd is an easy, fast, and flexible web-server that runs on low-resources (CPU/RAM) while running on a small server. In this tutorial, I will show how to configure lighttpd with PHP to work on multiple sub-domains by using virtual hosts.

These steps were tested on Ubuntu server.

Install software

Update the server to the latest packages/updates.

apt-get update

Install lighttpd and PHP.

sudo apt-get install lighttpd php5-cgi

Enable the fastcgi module and fastcgi PHP support.

sudo lighty-enable-mod fastcgi 
sudo lighty-enable-mod fastcgi-php

Restart your lighttpd service to apply the fastcgi changes.

sudo service lighttpd force-reload

Configure your website

On your server, edit the /etc/lighttpd/lighttpd.conf file. Some admins prefer uploading a configuration file over FTP instead of SSH editing.

vi /etc/lighttpd/lighttpd.conf
Setup the document root

By default, the document root is /var/www. You may prefer to host your sites out of a different folder, such as /var/websites.

server.document-root        = "/var/www"
server.document-root        = "/var/websites/root"

Note that lighttpd disables the directory listing by default.

Add virtual hosts

Add the following to lighttpd.conf to host a domain or subdomain.

$HTTP["host"] =~ "^mydomain.com$" {
server.document-root = "/var/websites/domain"
$HTTP["host"] =~ "^sub.mydomain.com$" {
server.document-root = "/var/websites/domain/sub"

Bare in mind that the $HTTP line contains a regular expression between quotation marks.

If you would like to disable directory listings for virtual hosts, use the following example.

$HTTP["host"] =~ "^sub.mydomain.com$" {
server.document-root = "/var/websites/domain/sub"
dir-listing.activate = "disable"
Save and restart

Once you have finished adding virtual hosts, save the lighttpd.conf file and restart the lighttpd service.

service lighttpd restart

At this point, lighttpd is serving your PHP pages. Enjoy!

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!