WordPress Pi

I’ve been working on getting my WordPress database back, as it was recently hacked. I am doing my best to get over the total let down from the international hosting corporation, GoDaddy. As the damage is now done, and I need to put my efforts toward database recovery. I am still annoyed that I was trying to get an updated PHP back in April of 2020, and the rep extorted me to get it fixed, or said the devs would get around to it by the end of the year. Thus, they left me unprotected up till Dec of 2020 when my site was attacked by Malware and the database was corrupted. Needless to say I’m looking for an alternative hosting solution, and I’m doing what I can to recover my data.

I was able to make a backup of the old website and I’ve done a lot of footwork to create the WordPress environment in a sandbox. Luckily, we had a Raspberry Pi 4 system that didn’t have a purpose yet. I worked with my personal IT guy to get a WordPress database going on it. We installed a new updated version of raspbian and used the command line to get a platform started for database recovery. The next step is to run a fresh install of WordPress on the pi in the sandbox environment we created; so that the data would only be accessible from my home network. Once I get into WordPress I will use PHPMyAdmin to replace the wp-contents folder with the one I am trying to recover from my backup.

The tutorials I am following and next steps are outlined below and originally came from pimylifeup.com

Raspberry Pi 4 Hardware

  • Raspberry Pi 4 + Case
  • Micro SD Card
  • Power Supply
  • Ethernet Cord / Wifi Dongle

Raspberry Pi 4 Operating System

Raspberry Pi Command Line Tools

  • PuTTY to remote cmd line which allows you to copy and paste from PC
  • VNC Viewer to remote control your pi desktop

Setting up WordPress on Raspberry Pi 4

Before you start this tutorial make sure you have setup Apache and PHP. You will also need to have a MYSQL server running and accessible. As I mentioned earlier, you will need to have your MYSQL root password ready.

2020 PiMyLifeUp

Command Line Script Steps

Step 1: setup Apache

Before we install Apache to our Raspberry Pi, we must first ensure the package list is up to date by running the following two commands.

sudo apt-get update
sudo apt-get upgrade

First, we will need to install the Apache2 package on our Raspberry Pi.

sudo apt install apache2 -y

To be able to make changes to the files within the /var/www/html without using root we need to setup some permissions.

Firstly, we add the user pi (our user) to the www-data group, the default group for Apache2.

Secondly, we give ownership to all the files and folders in the /var/www/html directory to the www-data group.

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

Once you have run that command, you will need to logout and then log back in for the changes to take effect.

You can now make changes to the default web page by running the following command.

This command will use the nano text editor to modify the index.html file.

The web server will serve all files within the /var/www/html/ directory.

nano /var/www/html/index.html
Step 2: setup PHP7 for Apache

Please note that before you start this section, you should be running at least Raspbian Buster. We will need to go ahead and install php7.3 and several other packages to our Raspberry Pi. Run the following command to install all the PHP packages to your Raspberry Pi.

sudo apt install php7.3 php7.3-mbstring php7.3-mysql php7.3-curl php7.3-gd php7.3-zip -y

We can test to see PHP is working by creating a PHP file within the /var/www/html/ directory. Creating a file in this directory will allow it to be processed and displayed when you open it in a web browser.

sudo nano /var/www/html/example.php

In this file, we need to add the following lines on PHP code.

<?php
echo "Today's date is ".date('Y-m-d H:i:s');

Now save the file by pressing Ctrl + X then pressing Y and hitting ENTER.

In your web browser, go to http://192.168.1.103/example.php. Make sure you replace 192.168.1.103 with your Raspberry Pi’s IP Address.

Going to the following URL should display something like the following.

Today's date is 2019-06-28 21:30:45
Step 3: setup Apache Virtual Host

This section was skipped as I’m not looking to host my site with the pi, I am only wondering how to recreate the WordPress environment for data recovery at this time. So, I will circle back to this if need be.

Virtual hosts are an essential part of the way Apache works. Apache uses these Virtualhost files so that it knows how to handle an individual site.

Within this section, we will show you how to create a basic virtual host file on your Raspberry Pi Apache web server. Virtual hosts are Apache’s way of handling multiple websites with each Virtual Host file setting up and configuring a particular domain.

Begin by running the following command to create a basic virtual host file called example.com.conf within the /etc/apache2/sites-available folder.

If you plan on using this for an actual domain name, make sure you swap out example.com with the domain name.

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

Within this file, enter the following text. We will explain each part of the virtual host file as we go along so you can have an idea on how to set up a very basic virtual host.

<VirtualHost *:80>

This line designates the start of the virtual host and that this virtual host should listen on port 80.

For those who do not know port 80 is the default port for http. Likewise, the port utilized for https is 443.

ServerName example.com
ServerAlias www.example.com

Here we add two directives to our virtual host. The first of these directives called ServerName designates the base domain. This server name is used to match the VirtualHost to a domain name.

The second directive, ServerAlias, defines additional domain names that will be matched as if they were the base domain name.

This directive is useful for matching additional names such as a www. subdomain.

DocumentRoot /var/www/example.com/public_html

The DocumentRoot directive defines the directory where all the files will be served from by Apache.

ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined

In these final two directives ErrorLog and CustomLog we specify the locations where we want that log files to be kept.

</VirtualHost>

Finally, we close off the VirtualHost section.

With everything complete, the code should end up looking like what we have below. Of course, using your domain name and not example.com.

<VirtualHost *:80>
      ServerName example.com
      ServerAlias www.example.com
      DocumentRoot /var/www/example.com/public_html
      ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
      CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

Once done, save the file by pressing CTRL + X followed by Y then ENTER.

Let’s now create the folder where we will be storing our HTML files. We will take ownership of this folder for the www-data group as well.

Run the following command to create the folder we need and take ownership of it.

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

Now that we have created our VirtualHost and the folder for it, let’s go ahead and now activate it by running the following command.

This command creates a symlink for our config file between the /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ directories.

sudo a2ensite example.com.conf

Finally, for our new virtual host file to be loaded in, we need to tell the Apache2 service to reload its configuration.

This can be done simply by running the command below.

sudo systemctl reload apache2

You can now point a domain name server (DNS) to the Raspberry Pi’s public IP and have it serve files for the requested domain name. You will need to setup port forwarding to have this work correctly.

Step 4: setup MYSQL

The next step is to install the MySQL server software to your Raspberry Pi.

Installing MySQL to the Raspberry Pi is a simple process and can be done with the following command.

sudo apt install mariadb-server

With the MySQL server software installed to the Raspberry Pi, we will now need to secure it by setting a password for the “root” user.

By default, MySQL is installed without any password set up meaning you can access the MySQL server without any authentication.

Run the following command to begin the MySQL securing process.

sudo mysql_secure_installation

Just follow the prompts to set a password for the root user and to secure your MySQL installation.

For a more secure installation, you should answer “Y” to all prompts when asked to answer “Y” or “N“.

These prompts will remove features that allows someone to gain access to the server easier.

Make sure you write down the password you set during this process as we will need to use it to access the MySQL server and create databases and users for software such as WordPress or PHPMyAdmin.

Now if you want to access your Raspberry Pi’s MySQL server and start making changes to your databases, you can enter the following command.

sudo mysql -u root -p

You will be prompted to enter the password that we just created in step 3 for MySQL’s root user.

Note: Like most Linux password inputs, the text will not show up as you type.

You can now enter MYSQL commands to create, alter, and delete databases. Through this interface, you can also create or delete users and assign them the rights to manage any database.

There are two different ways you can quit out of the MYSQL command line, the first of those is to type “quit;” into the MySQL interface.

The other way of quitting out of the MYSQL command line is to press CTRL + D.

At this point, you will now have successfully setup MySQL on your Raspberry Pi. Our next few sections will go into making better use of this database.

Step 5: create MYSQL Database & User

Before we proceed to create a MySQL user and database on our Raspberry Pi, we must first log back into the MySQL command-line tool.

Run the following command to log in to the MySQL command line. You will be prompted to enter the password for the “root” account that you set up earlier.

sudo mysql -u root -p

Let’s start by creating a MySQL database using the following command.

This command is super simple and is just “CREATE DATABASE” followed by the name that you want to give the database.

In our example, we will be calling this database “exampledb“.

CREATE DATABASE exampledb;

Next, we will create a MySQL user that we will assign to our new database. We can create this user by running the following command.

For this example, we will be calling the user “exampleuser” and giving it the password “pimylifeup“. When creating your own, make sure you replace both of these.

CREATE USER 'exampleuser'@'localhost' IDENTIFIED BY 'pimylifeup';

With the user created, we can now go ahead and grant all privileges to the user so that it can interact with the database.

This command will grant all permissions to our “exampleuser” for all tables within our “exampledb” database.

GRANT ALL PRIVILEGES ON exampledb.* TO 'exampleuser'@'localhost';

The final thing we need to do for both our MySQL database and user to be finalized is to flush the privilege table. Without flushing the privilege table, the new user won’t be able to access the database.

We can do this by running the following command.

FLUSH PRIVILEGES;

If you rather not use the command line to administrate your databases then you can always install PHPMyAdmin instead.

Step 6: setup PHPMyAdmin

You will need to have the password you set up for the root MYSQL account. If you don’t have this, you will not be able to install PHPMyAdmin.

To install the PHPMyAdmin package to our Raspberry Pi, we need to run the command below.

sudo apt install phpmyadmin

PHPMyAdmin will now begin to install to your Pi. It will require your input on various steps along the way. You will be presented with a screen asking the type of web server you want it to run off.

Select the “apache2” option by pressing SPACE and then ENTER. Select this option even if you are using NGINX as we will configure that ourselves latest on.

Next, we will need to configure PHPMyAdmin to connect to our MYSQL server. We will also need set up some details so that we can log in to the PHPMyAdmin software.

To do this select “<Yes>” at the next prompt.

Raspbian PHPMyAdmin setup

It will now ask you to set a password for PHPMyAdmin itself. It is best to set this password to something different to your root SQL password. Doing this will help secure the server.

This password is what PHPMyAdmin will use to connect to the MySQL server.

With the PHPMyAdmin installation process complete, there is one last thing we need to do. PHPMyAdmin by default will block you from logging into the PHPMyAdmin interface using the “root” user.

Instead, you will need to create a new user if you wish to create and access data tables within PHPMyAdmin.

To do this, we will need to first login to the MySQL command line interface using the “root” user with the password you set up.

sudo mysql -u root -p

6. Now run the command below to create a user and permit it to access all databases on the MySQL server. Remember the details you enter here as you will use these to log in to PHPMyAdmin

Make sure you replace “username” with the username of your choice.

Also, replace “password” with a secure password of your choice.

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

7. You can exit out of the MySQL command line interface by typing “quit” in the terminal.

Once done you can proceed to configure PHPMyAdmin for Apache or NGINX.

Step 7: configure Apache for PHPMyAdmin

Before we can load the PHPMyAdmin interface on our Raspberry Pi, we will need to make some configuration changes to Apache.

To get started, we need to edit the “Apache2.conf” file, we can do this by entering the following into the terminal.

sudo nano /etc/apache2/apache2.conf

Now we need to add the following line to the bottom of this file.

This line will include PHPMyAdmin’s configuration and allow it to be loaded in and listened to by Apache.

Include /etc/phpmyadmin/apache.conf

Once done we can save and exit by pressing CTRL + X and then pressing Y then ENTER.

Now we need to restart the Apache service on our Raspberry Pi by running the command below.

We need to do this to flush out its current configuration and make it load in our modified file.

sudo service apache2 restart

To setup NGINX to work with PHPMyAdmin, we need to do is create a link between the phpmyadmin folder and our root HTML directory.

To do this, we need to run the following command on our Raspberry Pi.

sudo ln -s /usr/share/phpmyadmin /var/www/html

You should now be able to access your Raspberry Pi’s PHPMyAdmin interface from a web browser.

To test this, go to the following address in your favorite web browser. Use the user you created earlier on in the tutorial to log in. Do not use your root user as this is disabled by default.

Remember to replace the IP Address with your Raspberry Pi’s IP Address. (If you don’t have it, run the hostname –I command on your Raspberry Pi )

http://192.168.1.108/phpmyadmin
Raspberry Pi PHPMyAdmin Interface

At this point, you should now have PHPMyAdmin up and running on your Raspberry Pi.

You will find this tutorial to be very handy if you decide to set up heavy database applications or websites. For example, WordPress is very reliant on a database, so being able to access and manage it is a must.

Step 8: setup WordPress

Before you start this tutorial make sure you have setup Apache and PHP. You will also need to have a MYSQL server running and accessible. As I mentioned earlier, you will need to have your MYSQL root password ready.

To start, we need to download and extract WordPress to our “/var/www/html” directory on our Raspberry Pi.

We will also need to take ownership of the “/var/www/html” folder with the “www-data” user and group. Doing this will allow PHP to process WordPress without running into any permission errors.

To achieve all of this, we will run a few commands, just type each line below into the terminal.

cd /var/www/html
sudo wget http://wordpress.org/latest.tar.gz
sudo tar xzf latest.tar.gz
sudo mv wordpress/* ./
sudo rm -rf wordpress latest.tar.gz
sudo usermod -a -G www-data pi
sudo chown -R -f www-data:www-data /var/www/html

Now that WordPress is downloaded and extracted to our Raspberry Pi, we will also need to set up a database within MYSQL for WordPress to store its information.

We need to first use the MySQL command to login. Use the –p flag for the password and –u for the username. Make sure your password is written right next to the -p tag with no space.

Note: Replace password with the one you set up for the root user when you setup MySQL

sudo mysql -u root -p

Now that we have logged into the MYSQL server, we can issue a command to create a database for WordPress using this simple line:

create database wordpress;

Now we will create a separate user for this database, this is far safer from a security perspective.

While still in MYSQL issue the following command:

CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'new_password';

Note: Make sure you replace new_user and new_password with details of your choosing, just remember what you choose as you will need to know both when we setup WordPress.

Once we have a new user, we will need to give it permissions to manage our new database.

We can do that with the command below, make sure you swap new_user, with whatever you entered in the previous step.

GRANT ALL ON wordpress.* TO 'new_user'@'localhost';

Now we have created our database and our user that we want to interact with it. We need to quit out of the MYSQL command interface.

We can do that by pressing Ctrl + D.

With that all done, we can now finally finish setting up WordPress, we can start this process by going to your Raspberry Pi’s IP address in a web browser.

Wordpress Language Select

You will be greeted with the following screen asking for you to select a language.

The next page will explain the details you need to have handy to install WordPress. You can just press “lets go” to get to the important screen.

Wordpress Let's Go

You will now need to enter various details so that WordPress can connect to the MySQL database we setup.

  • Database Name – This is the database WordPress will connect to. Earlier in this tutorial we created a database called wordpress.
  • Username – This is the user we created in step 4, make sure you get this detail correct as it needs to connect to the above database.
  • Password – This is the password we set while creating the MYSQL user in step 4, again make sure you enter this correctly as it’s needed to connect to the database.
  • Database Host – Keep this to the default setting, we only need it to connect locally.
  • Table Prefix – Keep this to the default setting as well, there is no real need to change this
Wordpress Database Connection Details

Assuming you entered all the correct details, you will now be able to press “Submit” to successfully install the software, follow the rest of the prompts to finalize the installation.

There is quite a bit more to learn about WordPress such as installing a WordPress theme or setting up plugins. Using a Raspberry Pi is a great way to learn all the basics without investing in an expensive web server.

You should now have WordPress setup on the Raspberry Pi and accessible.

Similar Posts

Leave a Reply

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