Host a WordPress blog on a Raspberry Pi

The previous tutorial explained how to setup and run your own web server on a Raspberry Pi. This post will take you one step further and show you how to install a WordPress blog on your server and make it accessible on the world-wide-web.

Welcome to a world without hosting fees!

Content

What you need

  • A Raspberry Pi with a LAMP stack installed. Read this tutorial to complete this step.
  • A registered domain name. More information on this below.

Step 1: Download and install WordPress

By default, the Apache web server is installed in the /var/www/html/ directory on your Raspberry Pi. Change into this directory now. We will create a separate folder for the WordPress files in here.

cd /var/www/html

You can certainly download the newest version of WordPress from their website. However, an easier method is to use the terminal and wget. The WordPress team always keep the URL below up-to-date with the latest software version.

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

Start with extracting the tarball to get access to its files. If you type ls you will now notice that a wordpress folder has been created in the directory.

sudo tar xzf latest.tar.gz

Copy all the extracted files into the html directory for easier access (alternatively you can also leave them in the wordpress folder, but in this case your URL structure might become complicated).

sudo mv wordpress/* ./

Remove the tarball file as we no longer need it.

sudo rm latest.tar.gz

Type ls -lha so that you can glance at the files you just extracted. The default WordPress installation should be visible now, including three directories (in blue below).

As you can see, the directories and files do not yet belong to the correct user-group. First, we need to change the ownership of all these files to the Apache user to ensure PHP can process commands from WordPress later.

sudo usermod -a -G www-data pi
sudo chown -R -f www-data:www-data /var/www/html

Step 2: Install the wordpress database

Before we start with the actual configuration of WordPress, we have to create a database for data storage using MariaDB, which we have installed in the previous tutorial.

Run MariaDB in the Raspberry Pi terminal with the following command. Enter the password you previously created when prompted. A welcome message will appear.

sudo mysql -uroot -p

Now we can create the database with one simple command (do not forget the semicolon!). If successful you should get a Query OK notification.

create database wordpress;

We have to grant all privileges to the root user. The term ‘PASSWORD’ should be replaced with the root password you used previously.

GRANT ALL PRIVILEGES ON wordpress.* TO 'root'@'localhost' IDENTIFIED BY 'PASSWORD';

Finally, flush the database privileges to complete this process.

FLUSH PRIVILEGES;

Exit MariaDB with Ctrl + D and reboot the Raspberry Pi.

sudo reboot

Step 3: Configure your WordPress installation

Now we can go ahead and set up our WordPress installation. A helpful 5 minute setup guide can be found on the official website. However, as we are going to run the blog on our own server we will need a few additional tweaks.

Open the web browser on your Raspberry Pi or a second computer within your network and enter the IP of your Raspberry Pi. Following the conventions in the first tutorial, I will use the IP 192.168.0.555 as an example.

http://192.167.0.555/

You should now see the first page of the WordPress instructions, which asks you to pick your language. In case this page does not appear, try typing the full URL to the config file:

http://192.167.0.555/wp-admin/setup-config.php

Select your preferred language and click continue.

The next page will ask for specific information about your database. Click on Let’s go and enter the following details, as outlined below.

Again, the term <PASSWORD> should be the MariaDB root password you specified.

Database Name:      wordpress
User Name:          root
Password:           <PASSWORD>
Database Host:      localhost
Table Prefix:       wp_

Click Submit and then Run the Install to complete this action.

The following pages will ask for basic information about your website, such as Site title, main user, password and email address of the main user. Fill out the form and click on Install WordPress when you are done.

These settings can be changed later, if needed, but to be able to access your account you will need the username and password, so it is best to make a note of them now.

Your website should now be up and running, and the WordPress account accessible via the following link. Use the details you just entered to log in.

http://192.168.0.555/wp-admin

One important configuration that is best made on a fresh WordPress install is to change the permalinks setting. Permalinks are the permanent URLs to your individual blog posts.

By default, posts will be created with a numerical or plain permalink, but it is recommended to use a more versatile format such as a post-name permalink to help search engines find your website.

The WordPress website has more information about the different formats available, but for the purpose of this tutorial we will use the post-name permalink format.

Start by changing the setting in the WordPress dashboard itself. Log into your account and in the menu select Settings > Permalinks. Click on Post name and Save Changes.

You will also have to slightly modify the Apache settings on your Raspberry Pi so that these changes can be communicated to the server. If you haven’t enabled the rewrite module in the previous tutorial, do so now with the following command.

sudo a2enmod rewrite

The Apache configuration file needs to be udpated for these changes to take effect. Open it with the following command:

sudo nano /etc/apache2/sites-available/000-default.conf

The important bit here is the AllowOverride All. If you do not see the paragraph below in this file, enter it after line 1. If you do see it, but with the AllowOveride function disabled, allow it now.

For now, this line should be only within the VirtualHost for port 80 as we do not have a secure HTTPS connection yet.

<VirtualHost *:80>
    <Directory "/var/www/html">
        AllowOverride All
    </Directory>

Save the file and exit. Then restart Apache.

sudo service apache2 restart

Step 5: Register a domain name

At the moment we are only able to access our brand-new WordPress blog locally on the network. For a connection that is accessible by the world-wide-web you will need to register a domain or a hostname on a free domain and connect it to your home IP.

Keep in mind that many Internet Service Providers assign IPs dynamically and change them from time to time. To avoid disruptions to your self-hosted website it is therefore recommended to use a dynamic DNS service. These services specialise on remote access and allow you to point your IP to a static hostname, while keeping the connection alive. This way, as soon as your IP changes, the dDNS service registers the change and updates the hostname.

One free service I have used in the past is no-ip, but there are many dozens to chose from and they all offer fairly similar starter accounts.

Register a hostname and ensure it points to your dynamic home IP.

Many of these services also come with a dynamic update client that you will have to install on your system to ensure it registers any IP changes.

Once the hostname is registered and successfully connected to your IP, you should be able to access your blog from the web (ensure that you use http:// for the URL).

Step 6: Install HTTPS certificates

The next step is recommended but not strictly necessary for your website to work. You can skip it for now and return to it later if you are happy to use HTTP as your main communications protocol for the moment.

If you do, please be aware that secure authentication and encrypted communication via HTTPS is now standard for all websites. Your visitors might get a warning about an unsafe connection when trying to access your website via HTTP.

LetsEncrypt

The easiest way to get a recognised SSL certificate that will verify your website’s authenticity to the browser is to use the LetsEncrypt Certbot.

For Apache you can use the following command to install it on your Raspberry Pi:

sudo apt-get install python-certbot-apache

Certbot automatically generates a certificate for you and includes it in your Apache configuration with this command. Certbot will also ensure that the certificate is automatically renewed in the future, so you don’t need to worry about this.

sudo certbot --apache

After running these commands, you will be prompted for further information. You need to provide a valid email address to register, agree to the Terms of Service and – most importantly – provide a valid domain name. For this you can use the hostname you registered in the previous step.

The last question is whether you want to automatically redirect http traffic to https. In this case, as we are dealing with a fresh install it is best to use the redirect (option 2), as there is no danger of breaking existing links.

If Certbot gives you an error during the install, you might need to enable port forwarding on your router first. Go to the troubleshooting section to find out how.

The certificate will now be installed on your system and can be found in the following folder: /etc/letsencrypt/live/hostname.domain.com/

You can also test automatic renewal of your certificate with the following command:

sudo certbot renew --dry-run

Visit your website in a browser using HTTPS and look for the lock icon in the URL to verify that the SSL connection is working.

Note: depending on your settings, you might need to repeat step 4 (permalinks) and include an AllowOverride All flag in the Apache Virtual Host for port 443 in your Apache conf file. Configuration details for port 443 should have been added automatically by certbot. In case something went wrong, you can also manually copy the settings from <VirtualHost *:80>.

Congratulations!

You now have a basic website up and running with a secure SSL connection and the recommended permalink structure. If you like, you can go ahead and configure your WordPress blog now. The official website has a lot of helpful documentation to get you started.

Step 7: Troubleshooting

  • Port forwarding

Your website might not be accessible from the internet because ports 80 and 443 are blocked and behind a router. You can open ports on your router by using port forwarding. Access your router’s admin panel and go to the advanced settings. Make sure that both ports are approved for the internal IP address of the Raspberry Pi. You can check whether ports have been successfully opened with this online port check tool.

  • Permalink issues

One issue that I ran into when setting up my first self-hosted WordPress site was an error when trying to switch to permalinks in a post name format. There can be several reasons for this, most likely to do with permissions for your .htaccess file or the apache configuration. A fantastic summary of all of them and how to solve them has been provided on this blog.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.