Hosting WordPress on Raspberry Pi Part 3 – Setup WordPress

We now have a server running Nginx, php5-fpm with MariaDB, now it is time to install WordPress, and how to handle WordPress permalink for Nginx.

Install WordPress

We are ready to install WordPress, which is quite straightforward. First we want to delete the index.php that we previously created for testing php activation as well as the default Nginx index.html file before downloading WordPress.

cd /var/www/html/
sudo rm index.*
sudo wget http://wordpress.org/latest.tar.gz

Upzip the file which will create a folder called wordpress, and we want to move all the content to the current directory, and then delete the empty director left over as well as the downloaded source file.

sudo tar xzf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz
wordpress-installed
WordPress Installed

Next, we’d want to add a robots.txt file to inform crawlers not to crawl certain directories, create the file using:

sudo nano robots.txt

Input the follow lines:

User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/
Disallow: /wp-content/plugins/
Disallow: /wp-content/themes/
Disallow: /admin/

Press Control-x to save file and exit the editor.

The files downloaded belong to user ‘nobody’, we should change the ownership of the html directory to Nginx user www-data:

sudo chown -R www-data: .

Setup a WordPress database

We can now create a database which will be used by WordPress. Login to mariaDB with username and password:

mysql -u username -p

Create a database and then exit the mariaDB.

CREATE DATABASE wordpress_on_p3;
exit

Launch web browser and navigate to http://192.168.0.101. You should see a WordPress weclome page.

wordpress-welcome-page
WordPress Welcome Page

Click the Let's go! button.
Now fill out the basic site information as follows:

wordpress-database-connection
WordPress Database Connection

and click Submit to proceed and click the Run the install button.

wordpress-setup-page
WordPress Setup Page

Fill out the information: give your site a title, create a username and password and enter your email address. Hit the Install WordPress button, then log in using the account you just created.

Now you’re logged in and see the Dashboard of WordPress. To administrate the WordPress site and log in again in future, go to http://192.168.0.101/wp-admin. To see the website, go to http://192.168.0.101.
Further customisation of WordPress are quite straightforward and therefore we will not discuss here.

WordPress Permalinks on Nginx

WordPress generally works out-of-the box on Nginx until when trying to customise the permalinks. When WordPress run on Apache, with mod_rewrite server directive, WordPress will automatically add the required rewrite rules to .htaccess file for permalinks to work. But for Nginx, when WordPress detects that mod_rewrite is not loaded (which is the case with Nginx), it falls back to using PATHINFO permalinks, which inserts an extra ‘index.php’ in front. This can be solved by using the ‘Custom Structure’ option to remove the index.php.

wordpress-permalinks-setting
WordPress permalinks setting

Then we will have to setup the re-write rule manually.

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

Scroll down to find the location / section, and comment out (add a # in front of the line) the original try_files directive and add a new try_files directive as following line.

# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$args;

What this means is that Nginx will check for the existence of a file at the URL ($uri), then for a directory ($uri/). If it doesn’t find a directory or a file, it will redirect to /index.php passing the query string arguments as parameters.

It should look like this after the edits :

nginx-redirect-rule
Nginx Redirect rule

Save the file and reload the configuration settings.

sudo service nginx reload

We now have the WordPress web site setup and be able to create and view contents within our local environment. It is time to create content, and then get a domain name and publish the site for everyone to access it.

Leave a Reply

Your email address will not be published. Required fields are marked *