Install WordPress and NGINX on Raspberry Pi

I have been evaluating the idea of spinning up a Digital Ocean VM for my web hosting as a cost effective alternative to shared web hosting. Digital Ocean requires you to know how to understand operating systems, I just happen to be the right guy for the job. I wanted to field test my future setup on my Raspberry Pi to get an idea of how much time it is going to take and what I can do to tune it for maximum performance. All of my sites have been converted to WordPress, my desire was to build a LEMP (Linux Engine-X, MySQL and PHP) stack to accommodate this setup.

The Raspberry Pi that I am using is an original model B. This has 512MB and powered by a 700mhz CPU. The latest version of the Pi was bumped up to 1GB of memory and powered by a quad core 900mhz CPU. The results of this install will vary based on the hardware you are using.

Install nginx

I chose to install nginx (pronounced engine-x) because it is a lightweight web server reknown for it’s awesomeness.

Begin the install by typing the following commands.

1

sudo apt-get install nginx

Start the web nginx web server:

1

sudo/etc/init.d/nginx start

Note the command above will be useful when we are making changes later. What we are doing is controlling the service with a script. Instead of “Start”, you can use “Stop” and “Restart” as well.

Test to make sure the server is running. If you are on the Pi using the graphical interface, browse to http://localhost. If you are remote then browse to the IP of your Pi using the same hostname or IP that you used to ssh into it. Type

1

hostname-i

in case you run out of ideas.

Install MySQL

WordPress requires a database to store all of the configurations and written content within the system.

Kick off the install by typing

1

sudo apt-get install mysql-server

This will start the MySQL services automatically. During the installation you may be asked for a MySQL root password, this is not the same ‘root’ user that is on the operating system. If you do select a password be sure to remember it for the next step.

This would be a good time to also create the database and database user that you will be using for your WordPress installation.

1

2

3

mysql-uroot-p

<enter password here>

Once inside MySQL, create your WordPress database.

1

mysql>create database wordpress;

You can give the database another name other than ‘WordPress’. Next, we will create a WordPress user and assign it full rights to the database.

Above we created the user “wpadmin” which is a localhost user, with the password of “MyP@ssw0rd!”. Be sure to write down the userid and password used here, and again, you are welcome to come up with your own. “\q” quits out of the MySQL command line interface.

In the next line, we granted all available privileges to the entire wordpress database to wpadmin.

Install PHP

WordPress is written in PHP, so we will be installing the package into the operating system. PHP is an interpreted language that occurs on the server’s operating system unlike HTML or JavaScript which is interpreted by the user’s browser.

In this line, we will be installing PHP along with some packages that are necessary for PHP and the MySQL database to speak. We also will install php-imagick which includes the libraries to resize images that are uploaded to WordPress.

1

sudo apt-get install php5-fpm php5-mysql php5-imagick

Next thing we need to do in configure nginx to understand what to do with the PHP code.

This will require editing files in the operating system, I personally am a fan of vi/vim, but you may be more comfortable with using emacs or nano.

Edit the file /etc/nginx/sites-enabled/default

1

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

Scroll down until you find the line that starts with “index”, as of this writing this is line 25. Use CTRL-C in nano to see the current line number. In vi just type “:25”.

Insert index.php right after the word “index”. The line will end up looking like this:

1

index index.php index.html index.htm;

Scroll down until you find

1

# location ~ \.php$ {

This configuration of \.php$ is pretty much all written out, we just need to uncomment all the lines so that the configuration looks like this (credit to Raspberry Pi Documentation for this):

1

2

3

4

5

6

7

8

9

10

11

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;

}

Save and exit out of that file.

Restart nginx

1

sudo/etc/init.d/nginx restart

Let’s take our default index.html file and turn it into a .php file.

The document root of the nginx web server is located at /usr/share/nginx/www/. Go there then rename the default index.html to index.php.

1

sudo mv index.html index.php

Edit the index.php and include this php code:

1

<?phpphpinfo();?>

Save that file and refresh your browser. If you see this in your browser, we are already to install WordPress!

Install WordPress

You should still be in the document root. Type the following command to download the latest version of WordPress

1

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

After that download is complete, unarchive it’s contents.

1

tar-xvf latest.tar.gz

To keep things tidy, you can remove that archive file.

1

rm latest.tar.gz

The next step is to run the web based installation. In your browser, add /wordpress to kick off the install.

From earlier, you will need to know MySQL database name, userid and password. Use “localhost” as your database location.

If WordPress states that it does not have permissions to write to the file system to create wp-config.php, copy what is in the box and edit a new wp-config.php in the WordPress directory.

1

sudo nano/usr/share/nginx/www/wordpress/wp-config.php

Save that file and continue with the installation.

You will not be able to upload images until you create an uploads directory and assign it world write permissions. Complete these steps below:

1

2

sudo mkdir/usr/share/nginx/www/wordpress/wp-content/uploads

sudo chmod777/usr/share/nginx/www/wordpress/wp-content/uploads

Viola, you now have a running WordPress installation on your Raspberry Pi. I was pretty impressed at the speed in comparison to running Apache.

If you prefer to have the WordPress installation run in the root directory, use the following commands to copy the entire contents of the /wordpress directory to its parent (thank you Miles for the suggestion!)

1

2

cd/usr/share/nginx/www/wordpress

sudo mv *.*..

As a side note, you may notice some extra icons I have in my address bar which tells me what software is being used on every website I visit. Check out https://wappalyzer.com/

on a rpi 3: I got down to the place where one sets the location parameters, and an nginx restart fails. I commented out that section, and nginx restarted ok. I uncommented the location line and the ending }, nginx restarts ok. I uncomment the fastcgi_split_path_info, and it wont restart (I’ve gone through each letter multiple times, and compared to the raspberry pi docs you reference). systemctl status doesnt give useful info, and journalctl says no journal files. Great before this!