Installing Web Server nginx on a Raspberry Pi

nginx is considered to be a better web server for devices with low amount of memory such as the Raspberry Pi. This is because nginx is event driven rather than process driven which is the technique that Apache uses. This can mean that in some situations, nginx can serve more concurrent clients than Apache particularly if there is only one website and it is delivering static HTML pages.

However, I prefer Apache because it is well known, is relatively easy to configure and there are many control panels and configuration tools available for it. For most situations there is very little difference between the performance and if you are setting up a simple web server then Apache may be suitable.

If you wish to install nginx, the following is a description on how to do this and configure it.

We can install nginx web server with PHP5 and MySQL all at the same time using the following command in the terminal:

sudo apt-get install nginx php5-fpm php5-curl php5-gd php5-cli php5-mcrypt php5-mysql php-apc mysql-server

Answer any prompts that are displayed and enter in a root password for the MySQL database when asked.

The default settings for PHP should be fine but we should make a modification to the configuration file so that the FPM module will only listen to nginx:

sudo nano /etc/php5/fpm/pool.d/www.conf

In this config file un-comment the lines listen.owner and listen.group then Ctrl-x and exit the file.

Configure nginx for your web site

We need to make a configuration file from the default config file and then modify it.

So in terminal we need to:

cd /etc/nginx/sites-available

and then make a copy of the default configuration as follows:

sudo cp default mywebsite

To enable the site to nginx we have to create a symbolic link inside /etc/nginx/sites-enabled by doing the following:

sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/mywebsite

Now we can remove the default web site by:

sudo rm /etc/nginx/sites-enabled/default

and reload to make sure the changes are auctioned:

sudo /etc/init.d/nginx reload

We now have to edit the config file to make sure that the settings are what we want:

sudo nano /etc/nginx/sites-available/mywebsite

Within the server block there will be some setting which you should check and modify.

The first modification is to identify the root of your web site. You may want to leave this as it is but in my configuration I am using a flash drive to store the web site on and hence this line becomes:

root /websites/mywebsite/www

Make sure the following are listed:

index index.php index.html index.htm

and make sure the server name is listed:

server_name mywebsite.local mywebsite.com

where mywebsite.local is the name of your local server in my case paulserver.local and mywebsite.com is the url of the web site.

We can now save and exit this file and then reload the nginx using:

sudo /etc/init.d/nginx reload

We can test our configuration by creating a test html file as follows:

sudo nano /websites/mywebsite/www/index.html

Enter some text then save and quit.

If you go to a browser on your computer and then do http://mywebsitelocal you should an html file that you created.

Set up nginx to use PHP

At the moment the webserver will only output html files.

To do this we edit the configuration file by:

sudo nano /etc/nginx/sites-available/mywebsite

Now, as a separate location block inside the server curly bracket block {}, add:

location ~ [^/].php(/|$) {
fastcgi_split_path_info ^(.+?.php)(/.*)$;

if (!-f $document_root$fastcgi_script_name) {
return 404;
}

fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

We can now test our configuration by creating a PHP file at the root of our web site. In my example this is:

sudo nano /websites/mywebsite/www/index.php

Add the following code:

<?php phpinfo(); ?>

Save and close the file and visit http://mywebsite.local/index.php in your browse to show that it is working correctly.

Leave a Reply