Install a complete WordPress host in a Raspberry Pi

This guide will provide a step-by-step guide to install a complete WordPress host in a Raspberry Pi. In these days I just received my new Raspberry PI Model A+. So, I was excited to test it by building up my new WordPress host home made on it.

First of all, let’s look to this new Raspberry features (Ref https://www.raspberrypi.org/products/raspberry-pi-3-model-a-plus/):

  • Broadcom BCM2837B0, Cortex-A53 (ARMv8) 64-bit SoC @ 1.4GHz
  • 512MB LPDDR2 SDRAM
  • 2.4GHz and 5GHz IEEE 802.11.b/g/n/ac wireless LAN, Bluetooth 4.2/BLE
  • Extended 40-pin GPIO header
  • Full-size HDMI
  • Single USB 2.0 ports
  • CSI camera port for connecting a Raspberry Pi Camera Module
  • DSI display port for connecting a Raspberry Pi Touch Display
  • 4-pole stereo output and composite video port
  • Micro SD port for loading your operating system and storing data
  • 5V/2.5A DC power input

Now let’s move to installation steps.

This guide is intended form medium experienced people.

In order to reach and publish your website to Internet, you will also need:

  • you will need a public IP from your provider and a domain name (also dynamic domain names are good)
  • also to be able to setup your local network to forward ports 80 (for http) and 443 (for https)

What we need

To install a complete WordPress host in a Raspberry Pi we’ll only need the following hardware:

  • Raspberry Pi Model A+ (but also a Pi Zero W work)
  • A Power supply

Step by step guide

First of all we’ll install Raspbian Stretch Lite following my previous article Installing raspbian stretch lite in your raspberry pi…. NOTE: be aware that for Raspberry Pi Model A+ you will need a newer version of OS image (I’ll use 2018-11-13-raspbian-stretch-lite from here)

Next step is installing the so called LAMP stack (Linux + Apache + MariaDB + PHP).

Updating our version

This step is always suggested before every installation:

sudo apt-get update

Installing Apache

This step is quite simple. We’ll install also apache2-utils, which include some useful tools:

sudo apt-get install apache2 apache2-utils

After it’s installed, we can check Apache status:

systemctl status apache2
and you shoul see the folowing:

● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2018-11-28 21:07:39 GMT; 12s ago
Main PID: 14961 (apache2)
CGroup: /system.slice/apache2.service
├─14961 /usr/sbin/apache2 -k start
├─14962 /usr/sbin/apache2 -k start
└─14963 /usr/sbin/apache2 -k start

Enable Apache to start on boot:

sudo systemctl enable apache2

Finally set www-data as user and group assigned to root document:

sudo chown www-data:www-data /var/www/html/ -R

Installing MariaDB

Now let’s go on installing our Database:

sudo apt install mariadb-server mariadb-client

After it’s installed, systemctl to check MariaDB status:

systemctl status mariadb.service

and you should see the following:

● mariadb.service - MariaDB 10.1.37 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2018-11-28 21:19:02 GMT; 1min 0s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 16727 (mysqld)
Status: "Taking your SQL requests now..."
CGroup: /system.slice/mariadb.service
└─16727 /usr/sbin/mysqld

enable MariaDB to automatically start at boot

sudo systemctl enable mariadb.service

Now run the post installation security script:

sudo mysql_secure_installation

answering yes to the following questions in order to enhance security in your database.

Installing PHP

Last step to complete our LAMP server is related to PHP:

sudo apt-get install php7.0 libapache2-mod-php7.0 php7.0-mysql php-common php7.0-cli php7.0-common php7.0-json php7.0-opcache php7.0-readline php7.0-mbstring php7.0-xml php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl

Enable the Apache php7.0 module then restart Apache Web server.

sudo a2enmod php7.0
sudo systemctl restart apache2

Installing WordPress

Download latets WordPress version:

wget https://wordpress.org/latest.zip

extract the zip archive

sudo unzip latest.zip

move the extracted directory (“wordpress”) and all its content to Apache web root (replace example.com with you real domain name)

sudo mv wordpress/ /var/www/example.com

Configure MariaDB for your WordPress Site

Log into MariaDB shell as root

sudo mariadb -u root

create a database for WordPress (I called it wordpress, but you can name it as you prefer):

create database wordpress;

grant all privileges of WordPress database to the user that you will configure in WordPress in next steps:

grant all privileges on wordpress.* to wpuser@localhost identified by 'your-password';

Flush the privileges table for the changes to take effect and then exit

flush privileges;
exit;

Configure WordPress

Go to your WordPress directory and copy the sample configuration file

cd /var/www/example.com/
sudo cp wp-config-sample.php wp-config.php

edit the new config file

sudo nano wp-config.php

Find the following lines and replace the red texts with the database name, username and password you created in the previous step (Configure MariaDB for your WordPress Site).

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

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

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

Save and close the file.
Set the Apache user (www-data) as the owner of the WordPress site directory:

sudo chown www-data:www-data /var/www/example.com/ -R

Create an Apache Virtual Host file for WordPress (change “example.com” with your domain name)

Create a virtual host file for your WordPress site

sudo nano /etc/apache2/sites-available/example.com.conf

Paste the following text into the file:

<VirtualHost *:80> 
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com
  #This enables .htaccess file, which is needed for WordPress Permalink to work. 
  <Directory "/var/www/example.com">
    AllowOverride All
  </Directory>
  ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
  CustomLog ${APACHE_LOG_DIR}/example.com.access.log combined
</VirtualHost>

Save and close the file. Test configurations

sudo apache2ctl configtest

If you see “Syntax OK”, then enable this virtual host and reload Apache for the changes to take effects

sudo a2ensite example.com.conf
sudo systemctl reload apache2.service

Now we can start WordPress Wizard setup in our browser. But, first, we need to make our local PC aware that example.com (or the domain we set in previous steps) link to our raspberry. For this reason, once known our raspberry local IP, we must set our host file redirecting to it. Hosts file can be found in:

  • C:\Windows\System32\drivers\etc\hosts for windows based PC
  • /etc/hosts for linux based PC

by adding (via notepad) the following row to the end (please change <<Your.Raspberry.IP.Here>> with the IP address of your raspberry):

<<Your.Raspberry.IP.Here>> example.com

Finally, you can launch the finalization WordPress wizard: browse “http://example.com/wp-admin/install.php”.

You will be required to set:

  • Site title
  • WordPress administration user
  • WordPress administration password

click Install WordPress button … et voilà, your host is ready and working!

Enjoy!

 

Please follow and like us: