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:
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:
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:
and then make a copy of the default configuration as follows:
To enable the site to nginx we have to create a symbolic link inside /etc/nginx/sites-enabled by doing the following:
Now we can remove the default web site by:
and reload to make sure the changes are auctioned:
We now have to edit the config file to make sure that the settings are what we want:
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:
Make sure the following are listed:
and make sure the server name is listed:
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:
We can test our configuration by creating a test html file as follows:
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:
Now, as a separate location block inside the server curly bracket block {}, add:
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:
Add the following code:
Save and close the file and visit http://mywebsite.local/index.php in your browse to show that it is working correctly.