Quintic
Installing WordPress – Pi4

Installing WordPress on Pi4

I mentioned on the front page about, having installed WordPress on the Pi3 and having integrated my hand-crafted Site into WordPress, that the performance in serving up the pages was slow and definitely CPU limited.  The arrival of the Pi 4, which is advertised as being 2 to 4 times faster than the Pi 3, could provide a solution to the performance issue. Transferring the site on to the Pi 4 would also give me an opportunity to migrate to PHP 7.x.x from 5.x.x. (which also brings some perfomance improvments 🙂 )and to run through my notes on the previous installation process and document what was needed.

Minimum Equipement

  • Raspberry Pi 4 (Rather – this configuration is targetted at the Pi 4)
  • Pi 4 power supply
  • HDMI cable for the Pi 4 – This is not the same HDMI cable used for previous Pi series.
  • Network connection (WiFi or Ethernet)
  • SD card
  • Keyboard, mouse and screen (but only for set-up)
  • PC, with SD card reader and internet connection.

SD Card

If, like me, you have gone for the fastest, largest, reasonably priced SD card you can afford, then you may have bought one larger than 64Gbyte. (For me I purchased a 256Gbyte V30). There is a slight problem with these cards in that they are SDXC cards. The SDXC spec states that they should be formatted as exFAT, which, by default on Windows, they will be. The Raspberry Pi will not work with exFAT partitions, it needs FAT32. To get round this you can format the card on a MAC, which will default to FAT32 or download guiformat.exe on your Windows PC, and use that to format the card.

One other point to note on chosing the card size. The bigger the card the longer it takes to Clone and to Restore from the clone. For a 256 Gbyte micro SD V30 it takes 4 hours to Clone, 6 hours to restore on a MAC Book Pro i7. So whilst Clone and Restore as a principle failsafe is good. For larger SD cards a more sophisticated backup process will be needed.

Overview of steps required:

At a high level the steps are:

  • Pi Configuration
  • LAMP Installation
  • WordPress set-up
  • Web site transfer
  • Certificate transfer

Pi Configuration

Initial Set-up Steps

  • On PC
    • Formatted SD card using ‘SD Card Formatter’
    • Downloaded NOOBS3.1.1.zip
    • Unziped and copied contents to SD card
  • On Raspberry Pi
    • Plugged in Screen to HDMI
    • Plugged in Keyboard and Mouse remote USB dongle
    • Inserted SD card
    • Connected Power

System booted fine, and presented a screen that enabled me to connect to WiFi and install Rasperian. Installation of Rasperian requests

  • Password for user Pi
  • Location, and Keyboard layout

Pi 4 Configuration

  • Create a new user with SUDO privileges, so that we can restrict user Pi to just local login.
  • sudo adduser <name>
    • Password – followed by the following optional prompts
      • Fullname
      • Room Number
      • Work Phone
      • Home Phone
      • Other
    • Is the information correct (Y/N)
  • sudo usermod -aG sudo <name> – to give new user sudo privileges
  • Logout and login as new user. Test sudo privileges (eg sudo visudo)
  • groups pi – to get list of groups that pi is in
  • sudo usermod -aG <group1,group2,…> <name> to add new user to same set of groups. That basically makes <name> == Pi.
  • If you have a Pi 3, then you may need to adjust your screen settings at this point to make maximum use of your screen:
    • sudo cp /boot/config.txt /boot/config.txt.backup
    • sudo nano /boot/config.txt to get full use of screen.
      • Left and Right values -nn
      • Top and Bottom values -mm
    • Note: This does not work on the Pi 4. The overscan controls in config.txt do not have any effect on the Pi 4. This is a known problem, and if you have off-screen image issues with your Pi 4, then, as of  July 2019, as I understand it, there is no solution
  • System>Preferences – These are my preferences.
    • Select medium sized ICONs for top menu
    • Select font Sans and size 10 for text.
  • Keyboard was not correctly configured. Backslash key was being mapped to ‘>’, plus other annomalies To resolve this:
    • sudo raspi-config
    • select option 4 – Localisation Options
    • select Change Keyboard Layout.
    • And now we come to the messy bit. You have to select your keyboard from an extremely long list of options. It, possibly, doesn’t matter which one you choose as long as you get something that matches the number of keys and layout. I am using an  ASUS cordless  107 key QWERTY keyboard. The only ASUS option was for a laptop keyboard – which didn’t sound correct, so I selected the generic sounding Microsoft Office keyboard.
    • Then you have to choose the keyboard Layout, and here it does matter that you get it right. Get this wrong and your key mappings will be completely screwed.  The first issue was that whilst my locale was UK, the selection of layouts was all US, at least that was the appearance. You have to scroll to the bottom and select Other before you get given any none US options. At this point I was able to select English (UK).
    • As an FYI  – In case you don’t know what Colemak, Dvorak, and Workman are – they are non QWERTY layouts. If you have a QWERTY keyboard, do not select one of these.

Remote Access

I want to use the Pi4 headless and have FTP available for file transfers.

  • Open System Menu>Preferences>Raspberry Pi Configuration
  • Select Interfaces Tab
  • Enable SSH and VNC

LAMP Instalation

Apache2

  • sudo apt-get update
  • sudo aptget install apache2

Yep – that simple.  As a quick test to check Apache2 is installed and running type http://xxx.xxx.xxx.xxx/ whatever the IP address is of your PI and you should see the following image. To determine the IP address, ifconfig or hostname -I (Note: if you have both WiFi and Ethernet connection, you may have two IP addresses.)

PHP

  • sudo apt-get install php php-xml php-mysql
  • php -v

And the result:

PHP 7.3.4-2 (cli) (built: Apr 13 2019 19:05:48) ( NTS )

As a quick test of the Apache and PHP install.

  • cd /var/www/html
  • sudo rm index.html
  • sudo nano index.php – create a php file with the following content.
    • <?php echo “Hello World “;
    • echo date(‘Y-m-d H:i:s’);
    • echo ” Version: ” . phpversion() ?>

then browse to http://localhost/html/index.php.

(You may want to delete the index.php afterwards. It isn’t must of a security issue, but the cleaner then better)

Maria / MySql DB

As you may already be aware installing MySql on Raspberry Pi actually installs Maria D/B, and not MySql. I will confess that I have not investigated the difference between these two packages nor their relative provences. For me Maria D/B has provided all the functionality I have required and so have stuck with that.

  • sudo mysql_secure_installation – installs MariaDB
  • sudo mysql -u root -p – to create a root user with a password.
    • Remove annoymous user
    • Disallow root login remotely.
    • Remove Test D/B
  • apt-get install emma – Emma is a MySQL GUI. There are others, this one works for me.

WordPress Set-up

Downloading WordPress

  • cd /var/www
  • sudo wget http://wordpress.org/latest.tar.gz
  • sudo tar -xzf latest.tar.gz
  • sudo mv wordpress quintic
  • sudo rm latest.tar.gz

Setting up WordPress D/B

  • sudo mysql -uroot -p#####
  • create database quintic;  – Note the ‘;’ at the end of the command.
  • grant all privileges on quintic.* to ‘root’@’localhost’ identified by ‘#######’; – The ‘######’ is the root password.
  • flush privileges;
  • <CTRL>D to exit

As it happens I decided to divert from a straight Pi 3 -> Pi 4 installation copy. I do have two other web sites – stephendickinson.co.uk and bliki.co.uk. I decidied to try to create a Multi-site installation. So I also created databases called stephendickinson and bliki and created the required directories, copying the contents from quintic.

Configure Apache.

  • sudo chown -R www-data:www-data /var/www/quintic – to give apache ownership of the WordPress directories.
  • We now need to create virtual hosts files for the three directories.
  • cd /etc/apache2/sites-available
  • created quintic.conf, bliki.conf, stephendickinson.conf – All created to re-direct to HTTPS, so will need SSL Certificates for all. Quintic’s conf file detailed below. Those for bliki and stephendickinson are carbon copies.
    • <VirtualHost *:80>
      # The Primary domain for this host
      ServerName quintic.co.uk
      # Optionally have other subdomains also managed by this Virtual Host
      ServerAlias quintic.uk www.quintic.co.uk www.quintic.uk
      DocumentRoot /var/www/quintic
      <Directory /var/www/quintic>
      Require all granted
      #Allow local .htaccess to override Apache configuration settings
      AllowOverride all
      </Directory>
      # Enable RewriteEngine
      RewriteEngine on
      RewriteOptions inherit  # Block .svn, .git
      RewriteRule \.(svn|git)(/)?$ – [F]
      # Recommended: XSS protection
      <IfModule mod_headers.c>
      Header set X-XSS-Protection “1; mode=block”
      Header always append X-Frame-Options SAMEORIGIN
      </IfModule>
      RewriteCond %{SERVER_NAME} =*.quintic.co.uk [OR]
      RewriteCond %{SERVER_NAME} =quintic.co.uk [OR]
      RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
      </VirtualHost>
  • sudo a2ensite quintic/bliki/stephendickinson – this creates links from the conf files to the sites-enabled directory.
  • added .htaccess files to the Quintic, Bliki and StephenDickinson sites to set index.php as the default file to open.
  • Edited apache2.conf to set Quintic as the default site.
  • Added ‘DirectoryIndex quintic/index.php‘  instruction to apache2.conf to prevent  access to  directory /var/www

Note: It is easy to make a typo when creating / editing the .conf files. If you do make an error Apache will not restart. Keep calm. Apart from the apache2.conf file you can just rename the other conf files out of the way whilst you track down the problem. I would however recommend taking a copy of apache2.conf before starting to edit that file – just in case.

All sites are now up and running WordPress. So now I have to determine how to transfer a WordPress site over to a new server.

Transferring WordPress site

I hand-crafted the transfer of the code files.

  • tar -cf -R quintic.tar quintic
  • ftp tar file to Pi4
  • tar -xvf quintic.tar .
  • sudo chown -R www-data:www-data /var/www/quintic – to redo apache2 ownership
  • Edit wp-config.php to set database user, password and table prefix.

Transferring WordPress database

  • mysqldump -u ‘username’ -p ‘password’ quintic > quintic.txt – This exports the database ‘quintic’ to file quintic.txt (Note: mysqldump)
  • ftp file to Pi4
  • mysql -u ‘username’ -p ‘password’ quintic < quintic.txt

Test and Debug

An initial test gave the appearance that everything was working beautifully. However, appearances can and were deceptive. Typing in http://Pi4/ on a browsers on my PC did indeed display the Quintic Home page. When I clicked on any of the links from the HomePage, the requested page was displayed. The problem was all links jumped to https://www.quinitc.co.uk/. And here we hit a problem with testing the site as constructed. I had copied the site verbatum, including the configurations. I had made some changes to relflect the new environment, but not enough changes to allow testing. Not difficult though. All I needed to do was:

  • MySql: In wp_options change all rows with an option_value http://www.quintic.co.uk/ to http://pi4/
  • For TESTING ONLY remove the restricted access to wp-config.php and wp-admin
  • apt-get install php-xml

Obviously, the first two entries need to be ‘undone’ for release, but testing was now fine.

Release Stage

  • Undo testing config changes detailed above
  • Install SSL Certificate from Let’s Encrypt
    • sudo apt-get install python-certbot-apache
    • sudo certbot -apache
  • Install Fail2Ban
  • Configure Firewall to forward ports 80 and 443 to Pi4
  • Config bliki.co.uk and stephendickinson.co.uk on GoDaddy site to use Quintic’s static Ip addresss

Everything appears to be working perfectly, and the Pi4 is noticably faster at serving content than the Pi3. Still a tad slow on site admin and post/page generation, but easily fast enough on delivering web pages.

Leave a Reply