How To Run Lighttpd with Wordpress!

by admin on Wednesday, October 14th, 2009

lighttpd

In this blog post, I will show you how to convert a Wordpress blog to run Lighttpd instead of the default Apache Httpd server (on a Plesk server specifically). You can probably apply most of these instructions to any other web servers running on raw Apache but if you are that good at managing web servers, I will leave the extra hacking to you. As for Plesk, I just find it easier to setup everything like e-mail, DNS, and whatnot but you don’t need to use Plesk at all if you can do it all using command-line.

In my recent conversions, my web servers are now able to handle about 2-3 times more web traffic than before using Lighttpd.

Before, my servers went down every couple days because of too much traffic.

Now, my servers “almost” never go down.

Btw, if you are not on a dedicated server or a VPS(such as MediaTemple), you can’t really install Lighttpd.  You need root access (or SSH) and full access to your web server in order to hack it to run Lighttpd.

THIS GUIDE HAS BEEN TESTED ON THE FOLLOWING WEB HOSTING AND HARDWARE:

MediaTemple dv servers running CentOS, SingleHop Dedicated servers running CentOS64, and Cari.Net dedicated servers running Fedora.  (All servers setup with basic Plesk)

Lighttpd is a web server designed to handle large amounts of web traffic.  Before, I suffered from bad Httpd servers that would just start showing empty web pages, mainly due to lack of server memory.  After switching to Lighttpd, my server’s memory use was about 10 times more efficient.

First, you will need to install some dependencies in order to use Lighttpd right.  There’s simpler ways of installing Lighttpd such as apt-get, yum, or RPM but do not do that as you will actually have to compile the source code. (in order for mod_magnet to work, which is crucial for Wordpress permalinks and WP-Super Cache to work.  If you don’t need those, then go ahead.)

1.Installing Pkgconfig

First you will need to download Pkgconfig to install mod_magnet, which basically is a Lighttpd module that can enable permalinks and mod redirects.  Now, follow my directions as there’s other ways to do it on the web but THEY DON’T WORK! (hence the point of this blog post)

Download latest version of Pkgconfig (This is needed for mod_magnet installation without errors.)

Use wget command to download the latest pkgconfig:

wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.23.tar.gz

Use tar to uncompress the file:

tar xvf pkg-config-0.23.tar.gz

Go into the directory of Pkgconfig:

cd pkg-config-0.23

Compile:

./configure

make

make install

cd ..

You should get a bunch of messages that everything has been installed correctly.

2.Installing Lua

You will also need to download latest version of Lua for mod_magnet to be installed correctly.

Download latest version of Lua (This is needed for mod_magnet)

Use wget command to download the latest Lua:

wget http://www.lua.org/ftp/lua-5.1.4.tar.gz

Use tar to uncompress the file:

tar xvf lua-5.1.4.tar.gz

Go into the directory of Lua:

cd lua-5.1.4

Use the following command for compiling:
(These instructions are for either Fedora or CentOS, if you are on different linux OS, you will need to read the README)

make linux

*Note: If you get readline errors, try: (that means you don’t have readline development packages installed)

yum install readline-devel

*Note: If you get errors like this “/usr/bin/ld: cannot find -lncurses”, try:

yum install ncurses-devel

If everything goes well, do make install:

make install

Next, copy lua.pc to /usr/lib/pkgconfig where your pkgconfig is installed:

cp etc/lua.pc /usr/lib/pkgconfig

Do ranlib command:

ranlib src/liblua.a

Back to working directory:

cd ..

3. Installing Lighttpd

Hopefully you got steps 1 and 2 right, make sure you do because otherwise you won’t be able to compile Lighttpd correctly.  Trust me, I did this for like 2 days without sleep before getting it right.  The instructions on the web are all either vague or hard to follow so just follow my directions and you can do it.

Now for the fun part, download the latest version of Lighttpd:

Download latest version of Lighttpd HERE

Use wget command to download the latest Lighttpd:

wget http://www.lighttpd.net/download/lighttpd-1.4.23.tar.gz

Use tar to uncompress the file:

tar xvf lighttpd-1.4.23.tar.gz

Go into the directory of Lua:

cd lighttpd-1.4.23

Use the following command to configure package for compiling:

./configure –with-lua PKG_CONFIG_PATH=/usr/lib/pkgconfig

Note: Errors you might encounter:

Error:

configure: error: pcre-config not found, install the pcre-devel package or build with –without-pcre

Solution:

yum install pcre-devel

Error:

configure: error: zlib-headers and/or libs where not found, install them or build with –without-zlib

Solution:

yum install zlib-devel

Error:

configure: error: bzip2-headers and/or libs where not found, install them or build with –without-bzip2

Solution:

yum install bzip2-devel

Compile

make

make install

*Note: You might get errors with a 64bit system as I did with a CentOS64

ERRORS with CentOS 64:

libtool: link: gcc -shared  .libs/mod_magnet_la-mod_magnet.o .libs/mod_magnet_la-mod_magnet_cache.o   -L/usr/local/lib -llua -lm    -Wl,-soname -Wl,mod_magnet.so -o .libs/mod_magnet.so
/usr/bin/ld: /usr/local/lib/liblua.a(lapi.o): relocation R_X86_64_32 against `luaO_nilobject_’ can not be used when making a shared object; recompile with -fPIC

If you get this you need to re-compile Lua again, go back to the Lua source directory:

cd lua-5.1.4

vi src/Makefile

Change this line:

CFLAGS= -O2 -Wall $(MYCFLAGS)

to this:

CFLAGS= -O2 -Wall $(MYCFLAGS) -fPIC


Compile Lua again using “make linux” and “make install”, then go back to installing lighttpd.
*You might have to do “make clean” before compiling to clean up previous compiles.

You shoud be DONE with compiling/installing Lighttpd here but you still need to setup the config files so that’s next.

4. Setting up Lighttpd.conf, web server directory, and Wordpress permalinks + WP Super Cache

The following steps are necessary for running your website correctly which includes setting your web server directory, Wordpress permalinks, and WP Super Cache with Lighttpd mod_magnet module.

First, make a directory called lighttpd under /etc like this:

mkdir /etc/lighttpd

Next, copy the defauly lighttd.conf file from your uncompressed Lighttpd directory under /doc.

cp doc/lighttpd.conf /etc/lighttpd/lighttpd.conf

Next, use vi to edit the file. (Or you can use your favorite text editor):

vi /etc/lighttpd/lighttpd.conf

Uncomment (or add if it doesn’t exist) the following lines.

“mod_fastcgi”,
“mod_magnet”,

Set the server.document-root directory to /var/www/html/ (or your web server directory):

server.document-root        = “/var/www/html/”

If you are doing this on a live server, you might want to test Lighttpd on server port 81 before you actually make it go live so you don’t mess with the live website:

For testing on a live server, change server.port to 81: (Remember to change this back to port 80 when going live)

server.port                = 81

Find where the following lines are:

#simple-vhost.server-root   = “/srv/www/vhosts/”
#simple-vhost.default-host  = “www.example.org”
#simple-vhost.document-root = “/htdocs/

Under it, add something like this:

$HTTP["host"] =~ “(^|\.)mysite\.com$” {
server.document-root = “/var/www/vhosts/mysite.com/httpdocs”
server.errorlog = “/var/log/lighttpd/error.log”
accesslog.filename = “/var/log/lighttpd/access.log”
server.error-handler-404 = “/var/www/vhosts/mysite.com/error_docs/not_found.html”

magnet.attract-physical-path-to = ( server.document-root + “/rewrite.lua” )

}

Remember, this example is for Plesk web servers where the web directory is usually under /var/www/vhosts.  If you are not using Plesk, use your own directory.

Now, *IMPORTANT*, uncomment the following lines:

#### fastcgi module
## read fastcgi.txt for more info
## for PHP don’t forget to set cgi.fix_pathinfo = 1 in the php.ini
fastcgi.server = ( “.php” =>
( “localhost” =>
(
“socket” => “/var/run/lighttpd/php-fastcgi.socket”,
“bin-path” => “/usr/local/bin/php-cgi”
)
)
)

Change 2 above lines(parameters) to:

“socket” => “/tmp/php-fastcgi.socket”,
“bin-path” => “/usr/bin/php-cgi”

You can also optimize the above section of code for number of children and max request but you can do this after you have verified Lighttpd has been installed correctly, so skip this step and come back.

(Optimizing)

For optimizing add: (see here for optimizing)

“PHP_FCGI_CHILDREN” => “16″,
“PHP_FCGI_MAX_REQUESTS” => “10000″,

Next, you will need to add a file called “rewrite.lua” to the root directory of your Wordpress blog installation:

Copy and paste the following lines and save as “rewrite.lua” for each of your Wordpress blogs hosted on this server.  (Sometimes copying and pasting might corrupt the apostrophes so make sure you copy them exactly!)

function serve_html(cached_page)
if (lighty.stat(cached_page)) then
lighty.env["physical.path"] = cached_page
print(”Serving cached page: ” .. cached_page)
return true
else
return false
end
end

function serve_gzip(cached_page)
if (lighty.stat(cached_page .. “.gz”)) then
lighty.header["Content-Encoding"] = “gzip”
lighty.header["Content-Type"] = “”
lighty.env["physical.path"] = cached_page .. “.gz”
print(”Serving gzipped page: ” .. cached_page .. “.gz”)
return true
else
return false
end
end

attr = lighty.stat(lighty.env["physical.path"])

if (not attr) then
lighty.env["uri.path"] = “/index.php”
lighty.env["physical.rel-path"] = lighty.env["uri.path"]
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]

query_condition = not (lighty.env["uri.query"] and string.find(lighty.env["uri.query"], “.*s=.*”))
user_cookie = lighty.request["Cookie"] or “no_cookie_here”
cookie_condition = not (string.find(user_cookie, “.*comment_author.*”) or string.find(user_cookie, “.*wordpress.*”) or string.find(user_cookie, “.*wp-postpass_.*”))
if (query_condition and cookie_condition) then
accept_encoding = lighty.request["Accept-Encoding"] or “no_acceptance”
cached_page = lighty.env["physical.doc-root"] .. “/wp-content/cache/supercache/” .. lighty.request["Host"] .. lighty.env["request.uri"] .. “/index.html”
cached_page = string.gsub(cached_page, “//”, “/”)
if (string.find(accept_encoding, “gzip”)) then
if not serve_gzip(cached_page) then serve_html(cached_page) end
else
serve_html(cached_page)
end
end
end

Now, change /etc/php.ini and add the following line somewhere at the top:

cgi.fix_pathinfo = 1

Also, make sure SAFE MODE in Php.ini is TURNED OFF:

;
; Safe Mode
;
safe_mode = Off

Also, you need to turn on compression if you want glib compression:

zlib.output_compression = 9

Next, we need to make a directory called “lighttpd” for logging errors under /var/log directory like this:

mkdir /var/log/lighttpd

5. Starting Lighttpd for the first time

Now, try to start your Lighttpd with the following command:

/usr/local/sbin/lighttpd start -f /etc/lighttpd/lighttpd.conf

Now try doing a “top” command and press “M” while in top, you should see bunch of php-fcgi processes and one Lighttpd process.  If that’s the case, congratulations, your web server is now running Lighttpd.  You can also test your website on port 81 by doing http://mysite.com:81.

Now, you will actually need to test it on your live blog to make sure it’s working.

Kill the Lighttpd process by:

Kill {lighttpd PID}

You can find Lighttpd PID while doing the top command.

Go back to editing the file /etc/lighttpd/lighttpd.conf and change the following line:

server.port                = 81

to

server.port                = 80

Next, we will stop the live website intermittently and bring up Lighttpd immediately to start running your websites:

It’s pretty simple to do as you can shut down your Apache by doing:

/sbin/service httpd stop

Then do:

/usr/local/sbin/lighttpd start -f /etc/lighttpd/lighttpd.conf

Immediately check to see if your Wordpress blog is running correctly.  If not, simply kill the Lighttpd process like before and start the httpd server so your live site keeps running:

/sbin/service httpd start

If you are having problems at this point, you might want to go check the errors listed under /var/log/lighttpd.

Most likely, you will probably have some kind of setup config file error so just double check it, keep testing the lighttpd until your live site runs flawlessly.

Of course, it’s best if you can practice this before running on a live site so…

If you have gotten the test to run right, let’s move on.

Next, we need to set up Lighttpd so the web server will only use Lighttpd to run your Wordpress blogs. (and we can turn off resource-wasting Apache)

6. Replacing Apache Httpd permanently with Lighttpd

copy the init.d script from lighttpd-1.x.xx/doc directory to /etc/init.d/lighttpd

for redhat or fedora or CentOS (tested on those):

cp /root/lighttpd-1.4.23/doc/rc.lighttpd.redhat /etc/init.d/lighttpd

try this one if above doesn’t work:

cp /root/lighttpd-1.4.23/doc/rc.lighttpd /etc/init.d/lighttpd

Copy the Lighttpd executable file from /usr/local/sbin/lighttpd to /usr/sbin.  Trust me, just do it.

cp /usr/local/sbin/lighttpd /usr/sbin/.

Now, do the following commands to add lighttpd to your starting processes. (so your Lighttpd will automatically run if web server is rebooted)

chkconfig lighttpd --add
chkconfig  lighttpd  on

Now, let’s turn off Apache’s httpd so it doesn’t run (ever) again:

chkconfig httpd off

Now, check to see if httpd is NOT supposed to start automatically.   It probably is turned off.  Just making sure, double check.

chkconfig --list

A. Errors and Solutions

If lighttpd cannot be found via ps -ax|grep lighttpd

Try:

cp /usr/local/sbin/lighttpd /usr/sbin/.
/etc/init.d/lighttpd start

If it starts fine, you are done!

VERY IMPORTANT – Also make sure to set your WP Super Cache directory to 777!

chmod 777 cache

Usually your cache directory is under myblog/wp-content/cache

Error:

2009-08-24 18:53:32: (log.c.124) opening errorlog ‘/var/log/lighttpd/error.log’ failed: No such file or directory

2009-08-24 18:53:32: (server.c.919) Opening errorlog failed. Going down.

Try:

Make sure you made a new directory /var/log/lighttpd

B. Performance Test

I did a performance test while converting couple of my high-trafficked blogs from Apache to Lighttpd:

Used the ab command:

ab -n 1000 -c 5 http://testsite.com/index.php

Results:

Load Avg. Httpd – 0.70

Load Avg. Lighttpd – 0.34

That’s a whopping 200% improvement in load times!

Blog A

Httpd Home page loading time – 0.774 seconds

Httpd Avg. single page load time – 1.1  seconds

Lighttpd Home page loading time – 0.74 seconds

Lighttpd Avg. single page load time -

Httpd Ab test – 28.52 [#/sec] (mean)

Lighttpd Ab test – 38.34 [#/sec] (mean)

Blog B

Httpd Home page loading time – 0.375 seconds

Httpd Avg. single page load time – 0.42 seconds

Lighttpd Home page loading time – 0.344 seconds

Lighttpd Avg. single page load time – 0.39 seconds

Httpd Ab test – 77.54 [#/sec] (mean)

Lighttpd Ab test – 108.09 [#/sec] (mean)

FINAL THOUGHTS

I actually wrote this HOWTO guide about 3 months ago.  After 3 months of extensive testing on millions of pageviews, Lighttpd has proved to be much better solution for high-trafficked Wordpress blogs.

If you are not dealing with much web traffic (less than 5,000/pageviews per day), you might be better off Apache since it’s easier to use.

For all others who are spending at least $100/month on dedicated/VPS web hosting services, you can expect to shave about half of your web hosting costs.

My web hosting costs went down from $1500 a month to $600 because my web servers now can handle 2-3 times more traffic.  Over a year, that’s about $8,000 in savings.  It made enough of a difference where I could use the money saved for other things in my blogging business.

Why did I made this free tutorial?

Basically to help you save money on web server costs.  At first, I was even thinking of selling my HOWTO guide but then heck, I am a blogger.  I feel I need to give back to the community (the interwebs).

Anyways, if you have trouble with this HOWTO guide, simply leave a comment and I will try to help you as best as I can.  Sometimes, it might take me over a week to reply as I am constantly working on things. (and blogging)  But if it’s emergency or you simply can do it yourself, I can help you to setup your site for a fee.  You can e-mail me @ zedomax [at] gmail.com for a quote.

Enjoy hacking and I hope you save a bunch of money on dedicated/VPS web hosting costs.

EXTRA – /etc/lighttpd/lighttpd.conf settings for MediaWiki installation on /wiki directory

Add the following to lighttpd.conf

url.rewrite-once = (
“(^/wiki/[^:]*[\./].*)” => “$1″,
“^/wiki/([^?]*)(?:\?(.*))?” => “/wiki/index.php?title=$1&$2″,
)

For LocalSettings.php in MediaWiki, try:

$wgScriptPath       = “/wiki”;

$wgScript           = “$wgScriptPath/index.php”;
$wgRedirectScript   = “$wgScriptPath/redirect.php”;

$wgUsePathInfo = false;

More Resources

ab -n 1000 -c 5 http://testsite.com/index.php

Rate


Related News and Resources