Lighttpd From Source

From DreamHost

Jump to: navigation, search
The instructions provided in this article or section are considered advanced.

You are expected to be knowledgeable in the UNIX shell.
Support for these instructions is not available from DreamHost tech support.
Server changes may cause this to break. Be prepared to troubleshoot this yourself if this happens.
We seriously aren't kidding about this. Read the blinky part again.

The instructions provided in this article or section require shell access unless otherwise stated.

You can use the PuTTY client on Windows, or SSH on UNIX and UNIX-like systems such as Linux or Mac OS X.
Your account must be configured for shell access in the Control Panel.
More information may be available on the article's talk page.

This is only possible on Dreamhost PS, as the terms of service on Dreamhost shared hosting don't allow you to run any extra daemons. Whilst Dreamhost PS already has the option of using Lighttpd as the web server, this is a total conversion for your whole server and is therefor not desirable for a user who wants to run parallel installations.

Most of this is fairly straight forward instructions found in the INSTALL file and the lighttpd wiki tutorial.

Contents

What doesn't work

  • Installation via apt-get (needs root)
  • Compiling the optional dependancy libpcre
    • Note: This ends in an error that I think means run out of memory, but only sometimes. Other times it compiles okay, but lighttpd still can't find it in the configuration script.
      • Note 2: I've been fighting with this many days, finally it worked like this, you just need to have compiled libpcre succesfully ONCE to your personal bin e.g. /home/USER/bin and then do the following:
export PATH=$PATH:/home/USER/bin
./configure --prefix=/home/USER/lighttpd/ --with-pcre --without-bzip2
make 
make install

Get Lighttpd Source

The most recent source packages can be found on the lighttpd download page: http://www.lighttpd.net/download/ You can use wget to download the package directly to your server.

cd ~
wget http://www.lighttpd.net/download/lighttpd-1.4.22.tar.gz

You then untar it

tar -xzf lighttpd-1.4.22.tar.gz

Config/Make

Change to the directory you untared your sources to and run the following commands:

mkdir ~/BIN
./configure --without-pcre --without-bzip2 --prefix=/home/USER/BIN
make
make install

substituting USER with your username. The make command should exit without an error, and as far as I know should always finish with something similar to:

make[3]: Nothing to be done for `all-am'.
make[3]: Leaving directory `/home/USER/lighttpd-1.4.22/tests'
make[2]: Leaving directory `/home/USER/lighttpd-1.4.22/tests'
make[2]: Entering directory `/home/USER/lighttpd-1.4.22'
make[2]: Nothing to be done for `all-am'.
make[2]: Leaving directory `/home/USER/lighttpd-1.4.22'
make[1]: Leaving directory `/home/USER/lighttpd-1.4.22'

There are quite a few more similar entries before this, but I think you get the idea. A screen full of lines should signal the successful end of the make install command also.

Copying The Config

It's assumed that our "etc" directory exists. Whilst that is true, we can't write to it. We will need to make a quick directory structure for our "new etc" directory.

mkdir ~/BIN/etc
mkdir ~/BIN/etc/init.d
mkdir ~/BIN/etc/lighttpd
mkdir ~/BIN/etc/sysconfig

Now to copy over the files

cd ~/lighttpd-1.4.22
sed -e 's/FOO/lighttpd/g' doc/rc.lighttpd > ~/BIN/etc/init.d/lighttpd
chmod a+rx ~/BIN/etc/init.d/lighttpd
cp -p doc/sysconfig.lighttpd ~/BIN/etc/sysconfig/lighttpd
install -Dp ./doc/lighttpd.conf ~/BIN/etc/lighttpd/lighttpd.conf

We will also need a few other directories a bit later on, so we should create them now.

This directory stores all our log files. We can't just throw them in our ~/logs directory because it's read only so that we don't accidentally erase anything. It would also get a bit confusing, having multiple servers logging to the same directory. So, we make a new log directory (and in the spirit of keeping a similar structure to linux file systems, I've used var/log/lighttpd)

mkdir ~/BIN/var
mkdir ~/BIN/var/log
mkdir ~/BIN/var/log/lighttpd

Choose where you want to put your server root (the default place to pull all the files from). This will need to be an existing directory, so go ahead and do something like this:

mkdir ~/lighttpd_root

If you wanted to keep with the linux FS structure, you could do something like

mkdir ~/BIN/var/www
mkdir ~/BIN/var/www/htdocs

Editing The Config

There are a few things that you will need to change here before you can even start the server.

Open up your lighttpd.conf file and change the following lines:

## a static document-root, for virtual-hosting take look at the
## server.virtual-* options
Line  40: server.document-root        = "/home/USER/lighttpd_root/"

## where to send error-messages to
Line  43: server.errorlog             = "/home/USER/BIN/var/log/lighttpd/error.log"

#### accesslog module
Line 119: accesslog.filename          = "/home/USER/BIN/var/log/lighttpd/access.log"

# We didn't install PCRE, so this must be disabled to work.
# Just comment it out using a at the start of each line.
Line 128: #$HTTP["url"] =~ "\.pdf$" {
Line 129: #  server.range-requests = "disable"
Line 130: #}

# Ports 1024 and lower require root priveleges. You must set this to a value higher than 1024
## bind to port (default: 80)
Line 141: server.port = 8080

Make sure config is okay

Lighttpd has a nice utility to make sure our config file has no errors in it.

~/BIN/sbin/lighttpd -t -f ~/BIN/etc/lighttpd/lighttpd.conf

If it says:

Syntax OK

Then you're okay to run the server!

Running

~/BIN/sbin/lighttpd -D -f ~/BIN/etc/lighttpd/lighttpd.conf

This will start lighttpd for testing. The -D switch tells lighttpd not to go to the background like a daemon normally would. You should be able to hit http://www.yourdomain.com:8080/ now and get a 404 - Not Found error page (because there's nothing in your doc root)

When you are satisfied that everything is working, you can use

~/BIN/sbin/lighttpd -f ~/BIN/etc/lighttpd/lighttpd.conf

Lighttpd will now run, even when you close the SSH window. However, any time you restart your server you WILL need to restart the server.

Run On Boot

We don't have access to edit the rc.d files, so we need another way to run the server automatically every boot up. Thankfully, Dreamhost is running the "smart" cron service (aka Vixie Cron). This means that we can set a cron job to run whenever the server boots- perfect.

Go to your web panel, and under Goodies->Cron Jobs, create a new job. Here are the settings:

  • User: The same user you used to install lighttpd
  • Title: Lighttpd Startup
  • Command to run:
~/BIN/sbin/lighttpd -f ~/BIN/etc/lighttpd/lighttpd.conf
  • Use locking: Yes
  • When to run: Server Reboot

Running As Proxy

You can also use the proxy feature that Dreamhost provides us with so that you don't have to give ugly links to people.

Unfortunately, this feature is a bit limiting by itself as it only lets us mirror our :8080 site wherever we put a proxy link. However, the mod_evhost in Lighttpd allows us to change this so that we give a different directory, based on the domain that is hit. So for example: http://www.abc.org/static would be different to http://www.example.com/static. This is exactly what we want. To make this change, create a new folder in your Lighttpd root directory (or anywhere else is fine too, but I like to keep them together).

Now, open up your config file and add mod_evhost to the list of enabled modules. Note that mod_accesslog should already be there, enabled. It's just shown for positioning purposes.

Line 36: "mod_accesslog",
Line 37: "mod_evhost" )

Now we go to the mod_evhost config. Line numbers shown are after making the above modification to the module list.

#
# define a pattern for the host url finding
# %% => % sign
# %0 => domain name + tld
# %1 => tld
# %2 => domain name without tld
# %3 => subdomain 1 name
# %4 => subdomain 2 name
#
Line 279: #evhost.path-pattern        = "/srv/www/vhosts/%3/htdocs/"
Line 280: evhost.path-pattern        = "/home/USER/lighttpd_root/%0/"

After this, you will need to restart the server to apply the config.

Restarting The Server

You must be logged in as the same user you used to start the server instance that is currently running.

We need to get the process id (or pid) of the lighttpd instance. This is done using the ps command

ps -A|grep lighttpd

There should only be 1 process listed and should look something like the following:

7149 ?        00:00:00 lighttpd

If not, try again in a few minutes. If it still doesn't work, repeat the following instruction for each one.

We need to end the process. We won't be particularly polite about it and will use the kill command.

kill 7149

Notice that 7149 is the number from the start of the above ps string.

This has stopped the server. Now you just need to run the server as you would normally.

A Quick Summary

cd ~
wget http://www.lighttpd.net/download/lighttpd-1.4.22.tar.gz
tar -xzf lighttpd-1.4.22.tar.gz

mkdir ~/BIN

cd lighttpd-1.4.22

./configure --without-pcre --without-bzip2 --prefix=/home/USER/BIN
make
make install

mkdir ~/BIN/etc
mkdir ~/BIN/etc/init.d
mkdir ~/BIN/etc/lighttpd
mkdir ~/BIN/etc/sysconfig

sed -e 's/FOO/lighttpd/g' doc/rc.lighttpd > ~/BIN/etc/init.d/lighttpd
chmod a+rx ~/BIN/etc/init.d/lighttpd
cp -p doc/sysconfig.lighttpd ~/BIN/etc/sysconfig/lighttpd
install -Dp ./doc/lighttpd.conf ~/BIN/etc/lighttpd/lighttpd.conf

mkdir ~/BIN/var
mkdir ~/BIN/var/log
mkdir ~/BIN/var/log/lighttpd

mkdir ~/lighttpd_root

mkdir ~/BIN/var/www
mkdir ~/BIN/var/www/htdocs

##### EDIT THE CONFIG FILE #####

~/BIN/sbin/lighttpd -t -f ~/BIN/etc/lighttpd/lighttpd.conf
~/BIN/sbin/lighttpd -D -f ~/BIN/etc/lighttpd/lighttpd.conf

##### ADD THE CRON JOB #####
Personal tools