What performs better on a RasPi: Apache, Lighttpd or Nginx?

As I’m hosting my blog on a Raspberry Pi, I did some research on the web about it’s performance as a server. I started this site with Apache and migrated after a while to Lighttpd which already reduced server response time (3.6 seconds, according to Google’s PageSpeed Insights it reached 72/100 points) and improved performance. With an DSL upload speed of only 676 kbit/s one shouldn’t expect miracles anyway. Google’s PageSpeed results, which suggested to make a couple of changes to my site, and a comparison of Lighttpd vs. Nginx convinced me to migrate to Nginx.

Basic Nginx installation

The installation of Nginx was straightforward, since all dependencies, such as php5 or php5-fpm were already present and configured on my server. If you start on a freshly installed Raspbian, a simple apt-get should download all dependencies you need to run Nginx.

sudo apt-get install nginx php5 php5-fpm php-pear php5-common php5-mcrypt php5-mysql php5-cli php5-gd

In order to configure Nginx you must edit two files: /etc/nginx/nginx.conf and /etc/nginx/sites-availabe/default, respectively. For most of the settings in /etc/nginx.conf, you can keep the default values, except:

worker_processes 1;
events {
        worker_connections 256;
        multi_accept on;


keepalive_timeout 5;
types_hash_max_size 2048;
server_tokens off;

In /etc/nginx/sites-availabe/default, you must set your ServerName and tell Nginx to pass pass the PHP scripts to PHP-FPM server listening on unix socket.

upstream php {
        server unix:/var/run/php-fpm.socket;

# Place these lines into the server{} block!
server {
       location ~ \.php$ {
               try_files $uri =404;
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               fastcgi_pass unix:/var/run/php5-fpm.sock;
               fastcgi_index index.php;
               include fastcgi_params;

For better protection, disable access to all .htaccess files with:

location ~ /\.ht {
deny all;

To let Nginx start at boot, update-rc.d enable Nginx and php5-fpm. Don’t forget to disable apache2 or lighttpd in case you’ve been using it before.

$ sudo update-rc.d nginx enable && sudo update-rc.d php5-fpm
$ sudo service php5-fpm start && sudo service nginx start

If not already done you can install and visit your WordPress page at http://localhost/wordpress.

MySQL settings

Several times I observed that my Pi had problems by running out-of-memory. It was caused by the default MySQL settings in /etc/mysql/my.cnf consuming up to 430 M. Therefore I replaced the default Raspbian my.cnf with the file located in /usr/share/doc/mysql-server-5.5/examples/my-small.cnf which is meant for systems with small resources. This reduced the maximum possible RAM usage to 290.7 M. You can check your settings with a Perl script available from http://mysqltuner.com/.

Leave a Reply

Notify of