To save money, I decided to migrate several of my family’s websites to a VPS. Now, setting up lighttpd is a piece of cake, but because these websites are dynamic, database-backed affairs, you have to do a little more work. I might be a programmer, but I’m not really a Linux expert.
That said, it was relatively painless, and in contrast to some previous experience. Besides, who needs tech support if you have Google and a decent brain in your head?
Why Lighty?
Apache is very well known and full-featured. It’s also rather memory-hungry. Lighttpd has a far smaller memory footprint, but still has a lot of capability; if YouTube and Wikipedia prefer it over Apache, it’ll be just fine for my piddly little sites.
The smaller memory means I can get away with a smaller VPS. I’m using an incredibly inexpensive 128MB instance from Prgmr. Even that is overkill, but then I’m not using the server solely for websites.
Also, configuring lighty is pretty simple. I never had much fun configuring Apache.
Step 0: download all you require
I chose to build lighttpd and PHP from source for more control. For MySQL, I decided to install a package provided by the IUS Community Project (more details on doing this yourself here).
Step 1: get lighttpd working
Follow the guide, obeying the instructions for your particular distribution.
It would be wise to create a unprivileged user for lighttpd to run as. After installing,
addgroup www
adduser -g www -M www
Now let’s create somewhere for the websites to reside:
mkdir /var/www
mkdir /var/www/default
echo hello world >/var/www/default/index.html
chown -R www:www /var/www/default
Finally, setup lighttpd.conf (should reside at /etc/lighttpd/lighttpd.conf) with the following:
server.username = "www"
server.groupname = "www"
server.document-root = "/var/www/default/"
mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png"
)
Run /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf and try connecting to your server. If you get the ‘hello world’ message, everything is rosy. Press Ctrl+C to stop lighttpd and install the other bits.
Don’t forget to /sbin/chkconfig lighttpd on so that lighty runs on server startup.
Step 2: install MySQL
I did this from a package, so nothing more to say here than yum install mysql51 mysql51-server mysql51-devel.
It’s important you install the development package as PHP requires the MySQL libraries/header files for MySQL support.
After installation, run /usr/bin/mysql_secure_installation to finish setting up the database. (Very important!) Follow the prompts and accept their suggestions.
Check you can connect to the database with mysql --user=root --password. If everything’s fine, go create a database and a user account for your PHP website to use later.
Again, /sbin/chkconfig mysqld on.
Step 3: build and install PHP
yum install flex bison
tar xvjf php-5.3.2.tar.bz2
cd php-5.3.2
./configure --with-mysql
make
su make install
cp php.ini-production /usr/local/lib/php.ini
You probably want to pass more options to ./configure depending on what libraries you need support for, or alternatively don’t want support for. A full list is here. Configure will complain if it can’t find PHP’s dependencies, but it’s not too hard to sort out (hooray for package managers!).
Edit your lighttpd.conf to add the following:
server.modules += ( "mod_fastcgi" )
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"socket" => "/tmp/php.socket",
"bin-path" => "/usr/local/bin/php-cgi",
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "16",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"bin-copy-environment" => (
"PATH", "SHELL", "USER"
),
"min-procs" => 1,
"max-procs" => 1,
"idle-timeout" => 20
)
)
)
This sets up FastCGI for use with PHP. You should restart lighttpd to make the changes take effect (/etc/init.d/lighttpd restart) and test if PHP is working in the classic manner (echo <?php phpinfo() ?> >/var/www/default/phpinfo.php). Go to http://
Now go and configure to your heart’s content. Have fun!
Things I do
I use mod_rewrite, mod_redirect and mod_accesslog, among others. As you can see, lighty is quite powerful.
# Redirect subdomainless addresses to their www equivalents
$HTTP["host"] =~ "^([^.]+)\.(com|co\.uk|net)$"{
url.redirect = ( "^/(.*)" => "http://www.%0/$1" )
}
# Rewrite subdomains.domain.com to doc-root/subdomain
$HTTP["host"] =~ "^(.*)\.starfishgames\.co\.uk$" {
server.document-root = "/var/www/starfishgames.co.uk/pages/"
url.rewrite-once = ( "^/(.*)" => "/%1/$1" )
accesslog.filename = "/var/www/starfishgames.co.uk/access.log"
}





Leave a comment