Project: Raspberry Pi Web Server (Nginx, PHP, WordPress)

I decided last week that I wanted to accomplish two things:

1.) Better familiarize myself with Linux
2.) Create a professional resume website based on a wordpress template

To do so, I decided to join the Raspberry Pi bandwagon. I received the Pi yesterday and as of about 2 minutes ago finally have it up and running. I decided I wanted this project to perform as well as it could given the Pi’s somewhat limited specs but still be robust. In this guide I am am not going to include the steps for MySQL (other than connecting to it via wordpress) as I have a separate MySQL server. I will, however, include my source links so that you can follow their steps. Here’s the process I took in order to get this up and running successfully.

The Hardware

  • Raspberry Pi Model B (512MB RAM / Revision 2)
  • 4 GB SD Card
  • Raspberry Pi Case
  • Micro USB Cable (power)
  • HDMI Cable (although I found out this is not at all necessary)

The Software

This was supposed to be installed with n00b, however, it was actually installed with Raspbian – which is completely fine. With Raspbian installed, you don’t really need to have the HDMI cable as you can do this entirely without a monitor. SSH is installed by default on the latest version of Raspbian.

The Process

Step 1: Download Raspbian

Download and install the latest version of Raspbian so that you have SSH enabled and so that you are all up to date. This can be found at the link below. At this site you can choose to download n00bs but, from what I can tell, that uses a GUI and would require a monitor. If you want to spare the added of expense of the HDMI cable – as well as the time – I’d just select their Raspbian download and go from there.

http://www.raspberrypi.org/downloads

Step 2: Download Win32 Disk Imager

Download Win32 Disk Imager so that you can write that image to the SD Card. This download is located at the link below.

http://www.softpedia.com/get/CD-DVD-Tools/Data-CD-DVD-Burning/Win32-Disk-Imager.shtml

Step 3: Image SD Card

Win32Imager

Image the SD card with the Raspbian Image. This is very simple. Open up the Win32 Disk Imager tool and click the button to browse out to the image. Select the Raspbian image. Click the write button. Depending on the performance of your machine, it could take 5-10 minutes to write the image.

Step 4: Boot Up The Pi

Now go ahead and take the SD card and insert it into the Raspberry Pi. Once it’s inserted, go ahead and power on the Pi. Boot shouldn’t take real long, probably less than a minute or so. Once it’s booted, you’ll need to find the IP address of it from your router or some other means. The easiest is probably just going into your router and looking for new connected devices.

Step 5: Connect to the Pi via SSH

Once you have the IP address of the Pi and it’s booted up, you’ll want to go ahead and connect to it via SSH. To do so, download putty at the link listed below. Putty is pretty easy to setup, once it’s downloaded, by simply adding the discovered IP address to the “Host Name” section of Putty and clicking Open. Just leave the default port and keep it as SSH.

putty

http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe

Step 6: Connect to the Pi

Once you are connected via SSH, it’s time to log in to the Pi and get started. The default username and password for the Pi are as follows:

Username: pi
Password: raspberry

Once logged in, I’d make sure that you have the basic settings configured first that you technically missed by not having a monitor connected. To launch the initial settings, run

raspi-config

That should show you the following screen:

raspi-config

From there update the pi user’s password, change the memory split to 16MB for video, resize the Pi partition and then change to your correct time zone. That sounds like a lot but each of those screens are self explanatory so I won’t go into detail on them.

Once that’s complete select Finished and you’ll probably have to reboot the connection. Once it’s rebooted go ahead and reconnect to the SSH, which will likely have the same DHCP address unless you have crazy short DHCP leases set for some reason.

Step 7: Setup a Static IP address

Prior to installing the web server software, I would suggest assigning the machine a static IP address if you are not using DHCP reservations. This will make it much easier in the long run and will prevent any connection/setup issues with wordpress as it’s dependent on the URL for the site.

To do this, I have found the walk-through at the following link:

http://www.raspberryshake.com/raspberry-pistatic-ip-address/

To make the changes described on that site, go ahead and run the following:

sudo nano /etc/network/interfaces

That will open up a text editor and allow you to modify the settings of the network connections. In that file, change the “iface eth0 inet dhcp” to “iface eth0 inet static” and then add the following lines directly after that one:

address 192.168.1.4
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1

Obviously you will need to substitute your own IP address and other addresses with what I have listed but that’s the syntax of the file. Once you have that complete, save the file by pressing ctrl + o and then hitting enter to confirm and then ctrl + x to close out of the notepad. Those two commands are used frequently throughout this procedure so they are good to remember.

Commands to Remember:
ctrl + o : allows you to save an open file in nano editor
ctrl + x : allows you to close out of the open file in nano editor

Now that you have a static IP, you’re ready to start installing the web server.

Step 8: Clean Up Raspbian

This step I’ll give credit to the following site for, I’m using multiple sites for this complete process because that’s the only way I’ve gotten this to work. These steps go through some basic clean up to free up some space on your SD card.

http://rocketman0112.wordpress.com/2013/09/28/raspberry-pi-nginx-php-basic-webserver/

To do the clean-up, run the following commands:

sudo su
apt-get remove -auto-remove-purge libx11-.*
apt-get autoremove
apt-get autoclean

Now let’s get the device fully updated and ready for the webserver to be installed by running the following commands:

apt-get update
apt-get dist-upgrade

Now that those commands have been ran, you’re ready to install the actual web server components of this project.

Step 9: Install Nginx

I have received these steps from the following site and they have worked pretty perfectly. As stated previously, I did skip the MySQL section as I already had that accounted for, however, I think these steps would work for that as well.

https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04

Now let’s go ahead and install nginx. To do so, run the following (sudo is not required if you have not rebooted since the previous step or your run “sudo su” prior to executing these commands):

sudo apt-get install nginx
sudo service nginx start

It’s always good to take a second here to make sure that this part was successful. Go ahead and open a web browser on your computer you’re connected to the Pi with and enter the IP address of the Pi into the address bar and make sure that you see a confirmation page. It should just say something about Nginx – no errors should be displayed.

Step 10: Install PHP

Now let’s go ahead and install PHP. To do so, run the following command:

sudo apt-get install php5-fpm

Step 11: Configure PHP

First let’s make a change in the php.ini file by opening the file:

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

Now find the line, cgi.fix_pathinfo=1, and change the 1 to a 0. By doing so, this will increase the security of the page by requiring the interpreter to use an exact path instead of “rounding” of sorts the path name.

cgi.fix_pathinfo=0

The next step wasn’t required for me, however, I’m going to include it just because it’s mentioned in the original document. For some reason this setting was already made for me. Open up www.conf by doing as follows:

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

Find the line, liste = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /var/run/php5-fpm.sock.

listen = /var/run/php5-fpm.sock

Now go ahead and save and exit using the same nano commands I explained previously.

Let’s restart php now just to make sure it’s ready to go:

sudo service php5-fpm restart

REMINDER: the sudo is not required at the beginning of every command if you have run “sudo su” at the beginning of all this

Step 12: Configure Nginx

Open up the default virtual host file by running the following:

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

Make the following changes to that file:

    • Add index.php to the index line
    • Change the server_name from local host to your domain name or IP address (replace the example.com in the configuration) – if you’re like me and wondering if you can use your domain name that you haven’t actually setup yet, yes you can. You can still access the website via your IP address for the initial configuration.
    • Change the correct lines in the “location ~\.php$ {” section

The file should look as follows:

server {
        listen   80;

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        server_name example.com;

        location / {
                try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }

}

Then save and exit the file.

Step 13: Create a php Info Page

This is a step used to be able to validate our php configuration and is useful in making sure this is working at this point. Create a new file by doing the following:

sudo nano /usr/share/nginx/www/info.php

Add in the following line:

<?php
phpinfo();
?>

Now save and exit the file.

Restart Nginx for the changes to take effect:

sudo service nginx restart

To check on the php configuration go ahead and go to http://youripaddress/info.php and you should see the generic php info page.

 Step 14: Download WordPress

These steps I have taken from a sub site of the previous one located here:

https://www.digitalocean.com/community/articles/how-to-install-wordpress-with-nginx-on-ubuntu-12-04

If you’re like me and more of a GUI person than a terminal person, I’d preface this particular step by making sure you’re in your home directory (which is the default when you sudo su). Once that’s confirmed, go ahead and download wordpress by:

wget http://wordpress.org/latest.tar.gz

Once it’s downloaded you’ll want to unzip it by running the following:

tar -xzvf latest.tar.gz

Step 15: Create the WordPress Database and User

If you’re keeping MySQL on your Pi, I’d revert back to the main article and follow their steps directly as I’m going to just follow my exact steps here.

I started by logging into my MySQL console and executing the following command that created the wordpress database for me:

CREATE DATABASE wordpress;

Once that was created I then needed to create a new user:

CREATE USER wordpress@localhost;

Then I set a password for the new user:

SET PASSWORD FOR wordpress@localhost= PASSWORD(‘password’);

Please don’t use “password” as your password for the sake of my sanity.

Now you go ahead and give that new user the correct privileges followed by flushing the privileges:

GRANT ALL PRIVILEGES ON wordpress.* TO wordpress@localhost IDENTIFIED BY ‘password’;

FLUSH PRIVILEGES

One additional thing I had to do, since I have a separate MySQL server, is to make sure that in the MySQL console I have the “Limit Connectivity to Hosts Matching” field set to the wildcard % instead of just localhost:

MySQL User Setting

Step 16: Configure WordPress

You’ll begin by copying the wordpress config file which creates a new one. I had a little issue with the command listed, but it’s likely just because I’m really a novice Linux user. They have it as:

cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php

However, I had to make sure that I was in the wordpress directory (where I decompressed the files in the home directory) and run the command like this:

cp wp-config-sample.php wp-config.php

Once that’s copied, go ahead and ope that wordpress config file:

sudo nano wp-config.php

That will only work if you’re still in the wordpress directory – which you likely are. Now, within that file, go ahead and update the following settings with your information:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');

In my case I had to update the server field as well to the name of my server. Once that’s done save and exit the file.

Step 17: Copy the Files

We need to start by creating the www directory in the /var directory to make this a little easier. Do this by running the following:

sudo mkdir -p /var/www

Now let’s go ahead and copy the wordpress folder. I had to back out one directory from the wordpress folder I was already in from the previous steps, and then ran the following:

sudo cp -r /wordpress/* /var/www

Now let’s switch to the web directory so that we can assign it the correct permissions

cd /var/www

Give ownership of the directory to the nginx user (which, for me, was “pi” as that’s the default username for Raspbian):

sudo chown www-data:www-data * -R
sudo usermod -a -G www-data pi

Step 18: Set Up Nginx Server Blocks

Now we need to configure the WordPress virtual host. Create a new file for the host by copying the format from the default config:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wordpress

Now let’s open the WordPress virtual host:

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

Make the following changes to that file:

    • Change the root to “/var/www/”
    • Add index.php to the index line
    • Change the server_name from localhost to your domain name (I used my domain name) or IP address (replace the example.com in the configuration)
    • Change the “try_files $uri $uri/ /index.html;” line to “try_files $uri $uri/ /index.php?q=$uri&$args;” to enable WordPress Permalinks with nginx
    • Uncomment the correct lines in “location ~ \.php$ {” section

These changes should result in a file resembling:

server {
        listen   80;

        root /var/www;
        index index.php index.html index.htm;

        server_name 192.34.59.214;

        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                try_files $uri =404;
                #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;
                 }

}

Now save and exit

Step 19: Activate the Server Block

Technically the WordPress config is complete at this point, however, we need to activate the server block by creating a symbolic link:

sudo ln -s /etc/nginx/sites-availalbe/wordpress /etc/nginx/sites-enabled/wordpress

We then need to delete the default nginx server block:

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

Now let’s install the php5-mysql connector:

sudo apt-get install php5-mysql

And now let’s restart the services:

sudo service nginx restart
sudo service php5-fpm restart

Step 20: Complete WordPress Installation

To complete the installation you just need to open a web browser and go to http://youripaddress/wp-admin/install.php and then just fill out the required information.

Step 21: Increase the Max Upload Size

I found this information from the following source:

http://oopsmonk.blogspot.com/2013/06/nginx-error-413-request-entity-too-large.html

Nginx appears to set the default max upload size to 2 MB. That isn’t even enough to upload a single theme for wordpress. So, to make this work, I went ahead and just disabled the max upload size. To do this, you need to modify the nginx.conf file by doing:

sudo nano /etc/nginx/nginx.conf

Within that file, under the “http { Basic settings” part of the file you need to add the line “client_max_body_size 0” or replace 0 with a number and MB after it to set a specific size limit. The file should look like:

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        client_max_body_size 10M;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;
...
}

Now just save and exit the file and you’re good to go.

Step 22: Celebrat

Now that should be everything. If everything completed successfully you should have an up and running wordpress blog on your Raspberry Pi running now.