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. We’ve already looked at how to configure the Raspberry Pi to host a static or dynamic website (that is, one that relies on standard, prewritten pages, or a site that uses a database to populate pages).
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.
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
Once installed, Apache will drop a test HTML file into the web folder on your Pi. You should test this from another computer (or smartphone) on your network.
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.
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(); ?>
In order, these commands display:
- The phrase “hello world”
- The current date and time
- PHP info for the installation
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 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
As noted, other database options are available for this type of project. However, for the best results, especially if this is your first time configuring a web server, stick with MySQL.
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 refers 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. 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.