Running PHPBB3 on a high-performance monster – or: How nginx + php-fpm + php-apc + postgres will serve you well

Running a forum for a large community can be exhausting, especially when you need to build everything from the scratch. But before you could start with setting up the sub forums or installing mods, you need to think of the hosting: Where am I going to host the forums? On a shared webhosting package? Or is using a VPS better? What about the costs? Which forum software should I use? What about performance when having thousands of members?

Jesus, what mess! But there is a solution. In this tutorial I try to show you how to plan everything, make the right decisions and finally come to the conclusion that a VPS with a high-performance setup is more than enough for your purpose.

Choosing the right hosting plan
Running a forum is a real challenge and can drain heavily from your financial reserves. Many people use a small shared hosting offer because of the costs, others want to rent a dedicated machine and think this is necessary because of the thousands of people who could come and visit every month.

But in fact running a good forum only takes a small, but powerful virtual server. A shared hosting plan often is not enough since the provider sets strict limits on performance and traffic. As soon as you come close to those limits, you either pay or get kicked out.

On the other side, there are the people who have some money and decided themselves for a dedicated server. This is not a bad choice, of course. But is this really necessary? Unless you expect something like 10 000 members for your forum or less, a VPS may still serve the purpose.

So, I highly recommend to grab a small VPS server and upgrade the server/transfer your community later (if needed). If you have enough money you might even consider renting a cloud instance which can be scaled without blowing RAM or CPU limits.

I personally would stick with a 13 Euro VPS (approx. 18 Dollar), which leads you to 150 Euro or less server costs per year. And there is still enough room for a cheap domain 🙂

Which operating system?
You have the server? You already purchased a cool domain? Now you are ready to get started? Great! Now we should make our minds up and select a decent OS for you.

Of course there is Windows and Win2008 Web Edition. This operating system is expensive, but works and is supported by a large company. But on the other hand, we don’t want to spend a few hundred Euro just for licensing and stuff like this. So clearly we decide ourselves for a Unix or Linux.

Unix is great, especially the *BSD variants. But if you were able to do your daily work on a BSD machine, you wouldn’t read this article and have already setup your high availability cluster with a self-compiled and optimized kernel 😉

So, there is only one type of operating systems left: Linux. Linux comes along with a huge variety of distributions, and obviously we need something which is stable, fast, easy to handle, supported by many software vendors and has a large community behind it. This leads us to either CentOS as a RedHat clone, Ubuntu Server or Debian (this list is based on my own experience, maybe you are missing OpenSuse or something else here..). Since we don’t want to pay for our OS, this list is very obvious for me.

Let’s look at the operating systems: CentOS sometimes lacks of easy handling (have you upgraded a machine from CentOS X to CentOS Y yet? Jesus, what a *****!), but profits from its big brother RHEL. Ubuntu Server depends on Canonical, but is very easy to be configured etc. Debian sometimes is too conservative, but always stable and runs on almost every hardware on the planet. Well, the differences are not always clear to see and you should select the distribution you are more familiar with. For me, this is either Ubuntu or Debian, and I chose Debian. Better wait longer for updates than have new, but rarely bugged software on my system.

Which software packages?
When thinking of web server software, there are many applications jumping to my mind. There are Apache, Cherokee, Lighthttpd, nginx, Tornado…. All of them are great, but Apache is not that good scalable for huge loads. Cherokee and Tornado are both very innovative, but are the wrong choice for a normal forum software (at least that is my opinion – feel free to share your own by dropping me a mail!). This leads us to decide between either Lighthttpd or nginx. Both of them do great in large setups, both of them have their flaws and both of them are already used by large companies. When it comes down to documentation, the Lighthttpd server clearly is the leader here. On the other hand, there are statistics showing that nginx is more stable (Lighthttpd had so many memory leaks issues during the last 3 years), more popular (this changed just a few months ago, I think) and consumes less CPU resources than the other favorite.

So, let’s go with nginx! Furthermore I used php-fpm, which is far better than the mod_php of Apache you might be familiar with and also slightly better than spawn-fcgi, according to this blog entry. What impressed me the most was the fact that php-fpm has the decent “process management ability  to “graceful” stop and start php workers without losing any queries.” In addition it comes with the “possibility of gradually update the configuration and binary without losing any queries.” (source: http://adityo.blog.binusian.org/?p=428) Furthermore we will use php-apc for caching the content. This helps us to keep the memory consumption of our small VPS very low.

Last but not least is the database software. Which one to choose? Oracle, Postgres, MySQL .. Oracle is too expensive and clearly not necessary here, which leaves Postgres and MySQL on the list. As far as I know, both database systems do great in heavy-load situations. However, Postgres beats MySQL by a better handling for sub queries and joins (at least they are faster). This is why we will choose Postgres for our small VPS setup.

Forum software – cheap, but many features plz!
When thinking of forums, WBB, PHPBB, vBulletin, SMF and Invision Power Board come to my mind. vBulletin and WBB are both commercial forum packages, so that’s a “No way!” from me (although both of them are great, though).  Invision Power Board and SMF are not widely spread, but have their advantages. But for me this is not enough to stand up against PHPBB3, probably one of the most successful PHP applications of all times. There are so many free mods and styles for this internet forum software and it is so simple to install and use – so, why not?

Let’s go! Preparing the VPS
I assume that you installed Debian 6 64 Bit (I tested the following steps with also with a 32 Bit Debian, btw), setup your network interface, have a SSH server and DNS + the other basic stuff works. You are now logged in, located at /root/ and ready to go.

At first we install some useful packages (since I wrote this tutorial for my ego I only install software which I find useful 🙂 ):

apt-get update
apt-get upgrade
apt-get install htop cbm atop dstat dvtm ssh vim bzip2 unzip mc vim lsof

Now let’s extend your sources.list in order to gain access to more packages:

echo deb http://packages.dotdeb.org stable all >> /etc/apt/sources.list
wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -
rm dotdeb.gpg
apt-get update

Installing nginx and co
Now let’s get started with the fancy stuff:

apt-get install nginx php5-fpm php5-pgsql php5-gd php5-curl php-apc postgresql imagemagick

Configuring nginx and our vhost
Now open the configuration file of nginx:

vim /etc/nginx/sites-available/www.domain.tld

Edit this file until it looks like the one below. Please remember to replace www.domain.tld with your own domain name:

server {
listen 80;
server_name domain.tld www.domain.tld;

access_log /var/log/nginx/domain.access_log;
error_log /var/log/nginx/domain.error_log;

root /var/www/www.domain.tld;
index index.php index.htm index.html;

location ~ .php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME /var/www/www.domain.tld$fastcgi_script_name;
include fastcgi_params;
try_files $uri =404;
}
}

Attention: Don’t forget to add “try_files $uri =404;”! This one closes a security vulnerability which lets attackers parse normal image and text files as PHP scripts.

Ok, let’s continue by doing the necessary stuff on the filesystem layer:

ln -s /etc/nginx/sites-available/www.domain.tld /etc/nginx/sites-enabled/www.domain.tld
mkdir -p /var/www/www.domain.tld

Configure php-fpm to work with Postgres
We need to edit the right php.ini in order to let PHP talk with Postgres and vice versa:

vim /etc/php5/fpm/php.ini

Search the file for [Postgres] and add a new line directly below:

extension=pgsql.so

Now restart php-fpm and start nginx itself:

/etc/init.d/php5-fpm restart
/etc/init.d/nginx start

Downloading PHPBB3
Now let us take care of the forum itself:

cd /var/www/www.domain.tld
wget http://www.phpbb.com/files/release/phpBB-3.0.9.tar.bz2
tar jxf phpBB-3.0.9.tar.bz2
rm phpBB-3.0.9.tar.bz2
mv phpBB3/* .
mv phpBB3/.htaccess .
rmdir phpBB3/

Prepare Postgres for PHPBB3
Before installing PHPBB3 we should prepare Postgres:

su postgres -c psql
\connect template1
alter user postgres with password 'yourSecretPasswordHere';
CREATE USER "www-data" WITH PASSWORD 'yourOtherSecretPasswordHere';
CREATE DATABASE phpbb3;
GRANT ALL PRIVILEGES ON DATABASE phpbb3 TO "www-data";
ALTER DATABASE phpbb3 OWNER TO "www-data;
\q
/etc/init.d/postgresql restart

Optional: Installing phppgadmin
If you have used MySQL before, there might be a high chance that you already know PHPMyAdmin, the great web-based tool for managing MySQL databases. There is a direct equivalent for Postgres, although it lacks some features of its bigger brother:

mkdir /var/www/www.domain.tld/db_admin
cd db_admin
wget http://downloads.sourceforge.net/phppgadmin/phpPgAdmin-5.0.2.tar.bz2?download
tar jxf *
rm phpPgAdmin-5.0.2.tar.bz2\?download
mv phpPgAdmin-5.0.2/* .
rmdir phpPgAdmin-5.0.2/

If you want to you can now open up your browser, visit www.domain.tld/db_admin, login with the www-data user and view your two databases. From now on, you can manage all the database stuff with this web application. Secure the db_admin directory with chmod 000 or use a htpasswd file in order to protect it from unauthorized access.

Installing PHPBB3 – finaly!

cd /var/www/www.domain.tld
chmod 777 config.php

Now open your browser, visit www.domain.tld and follow the installation instructions. Make sure to perform the following actions when you completed all the steps from the PHPBB3 wizard:

rm /var/www/www.domain.tld/install -r
chmod 640 config.php

Now go and browse your awesome new forums on your high-performance monster! 🙂

Bonus: View apc statistics
If you want to check if apc is really working you could..

gunzip /usr/share/doc/php-apc/apc.php.gz
cp /usr/share/doc/php-apc/apc.php /var/www/www.domain.tld/db_admin/

Now call www.domain.tld/db_admin/apc.php with your browser and view the statistics 🙂

That’s it!
That’s it, we made it! We just installed a high-performing monster in a matter of minutes by using free open source software. And it was so easy!
Of course we missed a view things, such as setting up the rewriting stuff for PHPBB3 and do some performance tuning. However, you are now ready to go and have fun with your low-budget setup.

PS: The missing stuff will be added in future tutorials. So be sure to visit this blog from time to time 🙂

4 thoughts on “Running PHPBB3 on a high-performance monster – or: How nginx + php-fpm + php-apc + postgres will serve you well

  1. Hey, thanks for your comment and also for your interest in my blog post. Since there is still someone interested in this topic, I will add new content (performance tuning, maybe also rewriting) in the future. Be sure to visit from time to time so you don’t miss the updates! And also please feel free to share your experience with this setup with me; maybe you also have some useful hints for me which could be published on my blog?

  2. yes I am also interested in this topic. Add some performance tuning tips and please add URL rewriting tips.

    Thanks

Comments are closed.