Category Archives: WordPress

Setting up WordPress on a Raspberry Pi

This tutorial continues on from my previous tutorial Setting up Raspbian – with a web server in mind and shows you how to get WordPress up and running on a Raspberry Pi. WordPress is one of the most popular blogging/content management systems and is what is powering this site.

Step 1 – Install The Lamp Stack

The LAMP solution stack is a popular software bundle for powering web servers and it’s the setup WordPress requires to operate.

Update the system’s package list and update the installed packages to their latest versions.

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

Install the LAMP stack.

sudo apt-get install apache2 php5 php5-mysql mysql-server

After installation you should be able to test that the installation was successful by visiting the IP address of your Raspberry Pi in your web browser, mine was located at http://192.168.1.200/. A page should display that states that ‘It Works!’.

Step 2 – Install WordPress
Install the WordPress package.

sudo apt-get install wordpress

Create a symbolic link to the directory where WordPress was installed in the ‘www’ directory so Apache can find the files. A symbolic link is similar to a Shortcut except it operates at the level of the file system.

sudo ln -s /usr/share/wordpress /var/www/wordpress

Unzip the MySql installation using the gunzip command.

sudo gunzip /usr/share/doc/wordpress/examples/setup-mysql.gz

Set up the MySQL database.

sudo bash /usr/share/doc/wordpress/examples/setup-mysql -n wordpress localhost

Step 3 – Configure WordPress
Create a symbolic link to WordPress’ config file and make sure it contains the IP address of your Raspberry Pi.

sudo ln -s /etc/wordpress/config-localhost.php /etc/wordpress/config-192.168.1.200.php

Change the ownership of the WordPress directory and set the owner as www-data which is the user that Apache runs as.

sudo chown -R www-data /usr/share/wordpress

Remove the default index file that was created when Apache was installed.

sudo rm /var/www/index.html

Move the WordPress index file from the WordPress folder to the www root.

sudo mv /var/www/wordpress/index.php /var/www/

Edit the WordPress index.php.

sudo nano /var/www/index.php

Change the following line:

<? php require('./wp-blog-header.php'); ?>

This is what it should be – this is where WordPress is installed:

<? php require('./wordpress/wp-blog-header.php'); ?>

Edit the WordPress config file.

sudo nano /var/www/wordpress/wp-config.php

After the line:

<?php

Add the following then close and save:

define('WP_HOME','http://192.168.1.200');
define('WP_SITEURL','http://192.168.1.200/wordpress');

Visit ‘http://192.168.1.200/wordpress’ and set up WordPress!

The steps provide the www-data user with the permissions to modify directories required by WordPress.

Update the ownership of the WordPress directory.

sudo chown -R -f www-data:www-data /usr/share/wordpress

Update the ownership of the wp-content directories.

sudo chown -R -f www-data:www-data /var/lib/wordpress/wp-content/ && sudo chown -R -f www-data:www-data /srv/www/wp-content/

Create a configuration file for configuring the Apache web server.

sudo nano /var/www/.htaccess

Update the ownership of the .htaccess file.

sudo chown www-data:www-data /var/www/.htaccess

Set up permissions for the necessary WordPress directories:

sudo chmod 644 /var/www/.htaccess
sudo chmod 755 /var/www/wordpress/wp-content/
sudo chmod 755 /var/www/wordpress/wp-content/uploads
sudo chmod 755 /var/www/wordpress/wp-content/plugins
sudo chmod 755 /var/www/wordpress/wp-admin

Edit the default sites-available configuration file.

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

Set ‘AllowOverride’ to ‘All’ under /var/www/, save and exit.

Restart the Apache server.

sudo /etc/init.d/apache2 restart

Go to settings, permalinks and choose a setting. I chose ‘Post name’, this will update the .htaccess file accordingly. Permalinks make links to your posts more understandable for both humans and search engines boosting your SEO as your links now make more sense semantically.

Restart Apache.

sudo /etc/init.d/apache2 restart

Install a necessary plugin that allows a symbolically linked folder to be used as the plugins folder.

sudo git clone git://github.com/liggitt/wordpress-plugin-symlink.git /srv/www/wp-content/localhost/plugins/aaa-plugin-symlink

Set up the themes and uploads folders so that WordPress can install and access themes and uploads.

sudo rm -rf /srv/www/wp-content/localhost/themes
sudo ln -s /var/www/wordpress/wp-content/themes /srv/www/wp-content/localhost/themes
sudo rm -rf /srv/www/wp-content/localhost/uploads
sudo ln -s /var/www/wordpress/wp-content/uploads /srv/www/wp-content/localhost/uploads

Setup complete!

Step 4 – Forward router ports

At this point you can only access your WordPress site from your internal network. You will need to forward port 80 on your router to your Raspberry Pi so that your router forwards any traffic sent to port 80 straight to your Raspberry Pi allowing it to be accessible from the outside world.

Step 5 – Set up a Dynamic DNS service

Your WordPress site is now accessible to the outside world by typing in your IP address. This number is hard to remember compared to a domain name and is probably not static so is subject to change. There are a couple of free solutions I would recommend:

I use FreeDNS and it’s very easy to set up. I had previously bought a domain name from a domain provider but just switched to the FreeDNS name servers so that I could manage it from there allowing me to utilise the dynamic DNS features.

There are a number of steps you need to take to allow your domain name to resolve to your WordPress site successfully.

Create a symbolic link to your WordPress configuration file with the domain in the file name replacing ‘DOMAIN’ below.

sudo ln -s /etc/wordpress/config-localhost.php /etc/wordpress/config-DOMAIN.php

Edit the .htaccess file.

sudo nano /var/www/.htaccess

Make sure that requests that don’t contain the ‘www.’ sub-domain are redirected to your domain with ‘www.’ included permanently with a HTTP 301 redirect. This will ensure that links to your site with and without ‘www.’ will be seen as the same website removing the possibility that search engines could class your site as multiple sites potentially hurting its ranking. Put the following at the top of the file, save and exit.

 # Redirect non-www to www: 
 RewriteCond %{HTTP_HOST} !^www\. [NC] 
 RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

If you’re using FreeDNS Make sure that they know your IP address by regularly making a request to the update URL provided. Open the crontab configuration file for configuring the cron scheduler.

crontab -e

Set up the job up to make a request every five minutes using -q –spider so that wget doesn’t produce output and only makes a HEAD request as opposed to an entire GET request which isn’t necessary. Replace ‘UPDATE URL’ with your update url, save and exit:

*/5 * * * * wget -q --spider UPDATE URL

WordPress should now be successfully set up and accessible from the outside world!