WordPress on a Raspberry Pi B using Nginx

pi-wordpressI already mentioned I host my girlfriends WordPress site – and this site – on a desktop computer. That desktop computer is an old left over Dell Latitude. For some reason this computer makes quite a bit of noise. Which wouldn’t be a problem if my “server room” wasn’t actually my spare bedroom. I could try to resolve the noise issue – it is the fan making a mess of it – but I’ve never been a hardware guy.  That, and a thread on the NedLinux forum (in dutch, sorry about that :D) about what we are doing with our Raspberry Pi, made me decide to make a web-server out of the Raspberry Pi B I’ve got lying around.

So I started on a journey with the ultimate goal to have a hosting system for a WordPress site which is quit and has an acceptable response time. And keeping in mind the journey, which should be as valuable as the goal.

Having trouble actually finding my Pi I started the journey with a very basic setup on an Alix 2d13 board. Did some pfSense lab work on that board but finished that project some time ago. I installed a Debian system on it with Apache2, PHP5 and MySQL…. it turned out to be fun to do but slooooooow. Exit Alix board, also since I found my Pi in the meantime.

nginxTalking to a co-worker about the project the idea of replacing Apache2 as the actual web server emerged. A http servers like lighthttp an nginx would suffice for my needs, perhaps even better. Lacking any experience with lighthttp or nginx made the journey more interesting. After reading up on the performance of several lightweight http servers I decided to give Nginx a try.

raspbianI decided to install Raspbian on my Pi. Giving me an environment which I’m familiar to with the ease of “burning” an image to an SD card.   This is all pretty well documented on the Raspberry Pi download page. This image is actually being maintained by de Raspberry community, not the Raspbian community.

Raspbian gives you the apt package manger. This makes it easy to install your packages. For my Pi I installed the following packages using apt:

  • nginx
  • php5, php5-cli and php5-fpm
  • mysql-server and phpmyadmin

As a side note: Not really sure where it came from – perhaps it was already there on the Raspbian image – but as it turned out I had Apache 2 installed. I stopped the process and made sure with “# update-rc.d -f apache2 remove” that it wouldn’t start again. You can’t have 2 daemons claiming port 80 and 443.

php-fpm-logoGetting Nginx to serve up PHP generated content turned out to be the biggest issue. There is no php-mod for Nginx. That’s why php5-fpm is installed. Turns out Nginx is a proxy which sends the request to a “PHP CGI script engine”.  Php-fpm is used to spawn the necessary threads.  Nginx and PHP are seperate processes. They communicate either via a unix socket or a TCP connection.

The first thing I did – after getting Nginx to server up static content – was to make sure Nginx would serve up the result of phpinfo(). This sounds trivial – and should be just that – but Nginx is not Apache which I’m familiar with. The Nginx Wiki is full of warnings not the get misinformed by all kinds of blogs giving bogus information on how to do things. So I kinda stuck to the instructions given on their own site.

The page I ended up with on using was Martin’s Nginx, PHP, Primer. I explains quite clearly on how to serve up dynamic content. Only thing which confused me a bit was that Martin includes the file “fastcgi.conf” which Nginx expected to find in “/etc/nginx”. The debian installation I have did have a file named “fastcgi_params”. Nginx did not complain… so I figured that was the one he intended.

Next thing I needed to figure out for myself was the pgp-fpm thing. I wanted to make sure I understood the way the two processes (nginx and php) were talking to each other. Unix socket or TCP.  Turned out this configuration could be found in “/etc/ph[p5/fpm”.  A file “www.conf” in the subdirectory “pool.d” controls such things.

After I corrected the syntax error in  “index.php” (I used the function php_info() instead of phpinfo() (hey… I’m a Perl guy)) it worked as expected. No worries there. On to WordPress.

Writing this blog I’m wondering why I didn’t just paste in the WordPress code. But instead I googled  for “WordPress Nginx” and found some references on the Nginx Wiki. And they have a complete page devoted on that subject. No a big surprise since they state on their homepage that wordpress.com is hosted on nginx.

But anyway… after using that config WordPress just works. An empty WordPress with one article added loads in 3-4 seconds. No plugins enabled. Not to fast I know. The troubles started when I added content and enabled plugins. The aforementioned girlfriends site loads in 10 seconds plus, testing done from within the same subnet. Not really acceptable.  By using the php-apc module I managed to reduce the load time to 8 seconds plus. Impressive…. yes… but not enough.

failedIn conclusion: I did not reach my goal. The Pi B simple does not have the power to support a website with scripted backend. Sure I could have enable a caching plugin. In effect making the site content static for visitors, but not for the writers of the site.  But the journey was fun. Getting Nginx to work was kinda like going back to the basics of things. It made me decide to order a Rasberry Pi 2 model b. A Pi with a quad core 1ghz processor. But that is something for another blog 😀

Leave a Reply

Your email address will not be published. Required fields are marked *