This is the first in a three part tutorial series on how to install and run NGINX on a Raspberry Pi. The first concentrates on installing and configuring Nginx web server and PHP on Raspbian OS, the second talks about installing MySQL server on Raspberry Pi, the third will walk you through installing WordPress on Nginx.

For many, Apache2 has become too bloated, and uses more resources than it needs to. Nginx is a very light-weight web server, that works great on a Raspberry Pi.

Prerequisites

Before doing this tutorial I assume that you are familiar with Terminal, and are proficient and comfortable in command line interface.

Step 1 - Install Nginx

To begin, using terminal or accessing directly, type the following command to ensure everything's up-to-date before installing the required packages:

sudo apt-get update && sudo apt-get upgrade

Chances are there are packages that'll need updating by running this command. If prompted, hit 'Y' to install the updates. Now type the following command to install Nginx:

sudo apt-get install nginx

There we go, we've now got nginx installed. Next, we want to launch nginx and test that it's working ok. Type the following command into the command line interface:

sudo /etc/init.d/nginx start

If all goes well, you should see this message:

image1

If you visit your Raspberry Pi's IP address in your web browser, you should see the default Nginx page, which looks a little something like this:

image2

Great success! You now have Nginx running on your Raspberry Pi. Next, we'll install PHP.

Step 2 - Install PHP

The packages required for PHP to work with Nginx is a little different to those when using PHP with Apache2. PHP runs as a FastCGI interface, so we need to install the PHP FPM package by typing the following command:

sudo apt-get install php5-fpm

Once we've done that we're almost ready to rock. We next need to make a few minor changes to the Nginx configuration to begin using PHP with Nginx.

Step 3 - Configure Nginx and PHP

You're almost there. Similar to Apache2, Nginx works on a virtual hosts principle, but is arguably easier to configure multiple vhosts on Nginx. We'll look at this in a later tutorial.

Next, we're going to adjust the default configuration to allow Nginx to start serving content using PHP. Begin by typing the following command:

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

The Nano editor will appear. If you're more comfortable using VI, use this command instead:

sudo vi /etc/nginx/sites-available/default

Scroll down the configuration file until you find the 'server' block with the following lines contained within it:

#listen 80; ## listen for ipv4; this line is default and implied
 #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

Uncomment them both out by removing the first #, so that they read like this:

listen 80; ## listen for ipv4; this line is default and implied
 listen [::]:80 default_server ipv6only=on; ## listen for ipv6

This allows Nginx to listen on port 80 (the default http port) and listen for both IPv4 and IPv6 requests. Next we need to edit the name of our server to match our domain. In my care, the domain is dingleberrypi.com but be sure to substitute this with your own domain or IP address.

# Make site accessible from http://localhost/
 server_name www.raspipress.com;

Change the index line from this:

 index index.html index.htm;

To this:

 index index.php index.html index.htm;

This allows files with the name index.php to be served up as the default index page, and takes priority over index.html and index.htm. Now we need to tell the configuration how to handle PHP files. Scroll down until you see this:

#location ~ \.php$ {
 # fastcgi_split_path_info ^(.+\.php)(/.+)$;
 # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
 #
 # # With php5-cgi alone:
 # fastcgi_pass 127.0.0.1:9000;
 # # With php5-fpm:
 # fastcgi_pass unix:/var/run/php5-fpm.sock;
 # fastcgi_index index.php;
 # include fastcgi_params;
 #}

Uncomment some of the lines so that it reads like this:

location ~ \.php$ {
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
 
 # # With php5-cgi alone:
 # fastcgi_pass 127.0.0.1:9000;
 
 # # With php5-fpm:
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_index index.php;
 include fastcgi_params;
 }

Be careful to edit the correct lines. We're using a socket, so uncomment the configuration accordingly. Finally, we need to change the PHP configuration to fix a possible security vulnerability. Type the following command to edit the PHP configuration file:

sudo nano /etc/php5/fpm/php.ini

Scroll down (or search for the line using CTRL + W in nano) for:

;cgi.fix_pathinfo=1

Uncomment the line by removing the semicolon from the start, and change the value from 1 to 0. Save and exit nano.

image3

All we need to do now is restart php5-fpm and nginx to make the configuration changes take:

sudo /etc/init.d/php5-fpm reload && sudo /etc/init.d/nginx reload

Step 4 - Test the PHP configuration

Now all that's left to do is to test to ensure PHP and Nginx are correctly configured. Navigate to the default document root:

cd /usr/share/nginx/www

Create a file called index.php:

sudo nano index.php

Add the following line to the file, save and exit nano:

<?php phpinfo(); ?>

Visit your raspberry pi in your web browser by typing its domain name or IP address, and you should see the PHP info page:

image4

That's it! We've installed, configured and tested PHP5 and Nginx. Grab a brew, you've earned it! Any questions or comments, feel free to add them below.

Next, we'll look at installing MySQL on Raspberry Pi and creating a new database and database user for WordPress to use.

In this tutorial, I'm going to go through the steps to install MySQL server on your Raspberry Pi. This is part 2 in a 3 part tutorial which will ultimately see WordPress running from Nginx on your Raspberry Pi.

Prerequisites

I'm assuming that you know your way round command line interface and are comfortable using it. I'm also assuming that you've followed the previous tutorial on how to install Nginx and PHP on Raspberry Pi.

Step 1 - Important, check your firmware

Before we begin, there is a bug either in Raspbian or in the Firmware which prevents a successful install of MySQL Server on Raspbian. Type the following command to find which version of the firmware you have installed:

sudo uname -a

If your firmare reads this:

Linux raspberrypi 3.12.20+ #687 PREEMPT Fri May 30 16:39:11 BST 2014 armv6l GNU/Linux

Then we will need to roll it back to the April firmware in order to prevent the install from failing. Type this command:

sudo rpi-update f6eef32dd6388c3b04dbf462bd324d93281bf397

If your firmware does not appear to be Friday May 30, skip to step 2. The Firmware update utility will begin running. Important, make sure you have backed up everything just in case the firmware fails to update. The process takes approximately 5 minutes, after which you will see the following message:

image5

When the reboot message is displayed, go ahead and reboot your Raspberry Pi by typing:

sudo reboot

It may take a bit longer than usual to start back up, but yet again just let it do it's thing and you should be back up and running.

Step 2 - Install MySQL server

Now we're ready to start the installation. First we need to ensure everything's up-to-date:

sudo apt-get update && sudo apt-get upgrade

Install any out of date packages when prompted. Next we're going to install the actual MySQL server by typing:

sudo apt-get install mysql-server --fix-missing

I tend to use the -fix-missing flag when installing mysql-server as in the past I've found some packages were missing. You should be prompted for a root password:

image6

Choose a secure password, and keep it safe. You will need this later to create additional users for WordPress. You will be prompted to confirm your password:

image7

 

Once you've confirmed your password, MySQL server should finish installing and the service should start.

Now we need to install a couple of extra packages, php5-mysql and mysql-client. The php5-mysql package allows connections to be made to MySQL Server through PHP, and mysql-client allows us to connect to our local MySQL server through the CLI:

sudo apt-get install mysql-client php5-mysql

That's it. Now all we need to do is create an additional user and create our database, ready for when we install WordPress.

Step 3 - Create a Database and a MySQL user for WordPress

We need to connect to our local MySQL instance and create a user and a database. Type the following command:

mysql -uroot -hlocalhost -p

You will be prompted for the root password. Go ahead and type the password that you gave for the root user in step 2. You should now see something that looks like this:

image8

 

I'm going to call my database wpdb, but you can call yours whatever you want. Just substitute wpdb with the name of your choice:

CREATE DATABASE wpdb;

This will create the database. With MySQL, ensure you end each command with a semicolon. I'm going to call my database user wpuser but yet again, call yours anything you like. Also choose a strong password and substitute password_here with the password of your choice for the new user:

CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password_here';

This creates the user that our WordPress installation will use to connect to its database. We've created the user with the host localhost as we will only be connecting to MySQL server on the local machine (the Raspberry Pi). Now we need to give the user wpuser access rights to the database wpdb. Type the following:

GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'localhost';

Finally, flush the privileges for the changes to take effect:

FLUSH PRIVILEGES;

We'll quickly test this to ensure we can login using our newly created user on our new database. Press CTRL + C to exit MySQL Client. Login using your new credentials:

mysql -uwpuser -hlocalhost wpdb -p

When prompted, type the password that you assigned to your new user. You should be logged into the server using these new credentials.

Conclusion

So, let's recap what we've done so far. We've rolled back our firmware if required, we've installed MySQL Server, MySQL client and the package to allow WordPress to connect to our local database, and finally created a new user and database ready for WordPress.