Enroll Course

100% Online Study
Web & Video Lectures
Earn Diploma Certificate
Access to Job Openings
Access to CV Builder



Online Certification Courses

How to Host a WordPress Site on Raspberry Pi

How to Host a WordPress Site on Raspberry Pi. 

Why Use WordPress?

If you’re looking for a way to host a website with the minimum of fuss, WordPress isn’t the obvious solution.

But if you really need to use WordPress, or you’re developing a theme or plugins for a WordPress site, then having a Raspberry Pi with the blogging platform installed and ready to use is a great option.

To install WordPress on a Raspberry Pi, you’ll need to set it up as a LAMP server. With Linux, Apache, MySQL, and PHP installed, your Pi will then be able to handle WordPress (and other website software).

Step 1: Set Up Apache Web Server

Start off by installing the Apache webserver. This is software that lets you serve any type of webpage to any browser. It’s all you need to serve an HTML page, static or dynamically created with PHP.

sudo apt install apache2 -y

You’ll need to enter the IP address of the Pi in the address bar of your browser. If you’re using SSH, you’ll know this already; otherwise, enter:
hostname -I

This displays the IP address of your Raspberry Pi. The page you see should be something like this:

You can also check the web page works from your Raspberry Pi using the http://localhost address.

Step 2: Install PHP on Raspberry Pi

Next, it’s time to install PHP. This is a software pre-processor that enables the serving of server-generated web pages rather than static HTML pages. While an HTML page might be written in its entirety, a PHP page will feature calls to other pages, and the database, to populate it with content.

While other server-side platforms are available (such as ASP), PHP is vital here as it is required for WordPress because WordPress itself is written in PHP.

Install with:

sudo apt install php -y

Once this is done, you should test that PHP works. Change directory to /var/www/html/ like this:

cd /var/www/html/

Here, delete the index.html file (the web page you viewed earlier):

sudo rm index.html

Next, create a new file called index.php (nano is installed by default):

sudo nano index.php

Here, add any (or all) of the following code:

<?php echo "hello world"; ?>  <?php echo date('Y-m-d H:i:s'); ?>  <?php phpinfo(); ?>

Save the file, then restart Apache:

sudo service apache2 restart

Refresh the page to see the results.

PHP and Apache are both working. Now it’s time to install the database software, MySQL.

Step 3: Install MySQL on Raspberry Pi

WordPress (and other dynamically generated website software) requires a database in order to store the content, links to images, and manage user access (among many other things). This project uses a fork of MySQL called MariaDB:

sudo apt install mysql-server php-mysql -y

Once this is installed, you will once again need to restart Apache:

sudo service apache2 restart

Step 4: Install WordPress on Raspberry Pi

To install WordPress, you’ll first need to install it. Before doing this, however, delete the contents of the /Html/ directory:

cd /var/www/html/  sudo rm *

The asterisk wildcard (*) deletes everything in the directory thanks to the rm (remove) command.

Next, use wget to download the latest version of WordPress:

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

Once downloaded, extract the contents:

sudo tar xzf latest.tar.gz

The WordPress directory should be created, but you want the contents of it in Html. While you might move these manually using the desktop user interface, it’s simple to do this from the command line:

sudo mv wordpress/*.

Be sure to include the space and period at the end, which refer to the current directory!

Enter ls to confirm the directory is full of WordPress folders and PHP files:

Before proceeding, tidy things up a bit, discarding the downloaded file and the Wordpress directory:

sudo rm -rf wordpress latest.tar.gz

Next, set the Apache user as the owner of the directory:

sudo chown -R www-data: .

Step 5: Configure MySQL

To set up the database, you’ll first need to run the installation command:

sudo mysql_secure_installation

You’ll shortly be prompted to set a root password. Follow the onscreen instructions, making sure you keep a note of the password, as it will be required later.

Once this is one, the following prompts will appear:

  • Remove anonymous users
  • Disallow root login remotely
  • Remove test database and access to it
  • Reload privilege tables now

For each of these, tap Y to confirm. When done, the “All done!” message will be displayed.

Step 6: Create the WordPress Database

To install WordPress, you’ll first need to configure the database. Begin by running the MySQL command, entering the password from earlier.

sudo mysql -uroot -p

This opens the MariaDB monitor. At the prompt, create the database:

create database wordpress;

Note how all commands within the MariaDB monitor end with a “;”. Next, the root user needs database privileges. Use your own password in place of PASSWORD.

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

Follow this by flushing previous database privileges:

FLUSH PRIVILEGES;

Exit the MariaDB database management tool with Ctrl + D.

Step 7: Install and Configure WordPress

Installing WordPress is simple; if you’ve already done it on an existing website, you should know what to do.

Open the website in your browser (as described above). You should see the WordPress setup screen. Select your language, then Continue, and make a note of what is required: the database name, username, password, host, and the table prefix (this is for the database tables).

If you made it this far, you should have named the database “WordPress”, and have a note of the password. The Username is root, and the host localhost. Table Prefix is wp_.

Click Submit, then Run the install, and input the Site Title, along with the Username, and Password for your administrator account. Click Install WordPress, and wait as WordPress is (quickly) set up.

To login to your WordPress installation, go to http://localhost/wp-admin.

Access Your WordPress Site From the Internet

As things stand, you’ll probably only be able to access the site from within your home network. To change this, you’ll need a static IP address, and manage port forwarding from your router to your Raspberry Pi.

Corporate Training for Business Growth and Schools