Tag Archives: mysql

Install WordPress on Raspberry Pi

This post will walk through installing and configuring the software stack (LAMP (Linux, Apache,  MySQL, and PHP)) for running WordPress on a Raspberry Pi

At the time of writing:

  • Raspberry Pi 2, Model B, 1GB RAM
  • Linux: Raspbian 8.0 “Jessie”
  • Apache 2.4.10 (Raspbian)
  • PHP 5.6.17-0+deb8u1
  • MySQL 5.5.44-0+deb8u1 (Raspbian)
  • WordPress 4.4.2

Build and updated from: https://www.raspberrypi.org/learning/lamp-web-server-with-wordpress/worksheet/

Update package lists

sudo apt-get update

Install Apache

First install the apache2 package by typing the following command into the terminal:

sudo apt-get install apache2 -y

5,131 kB of additional disk space will be used

Test the Web Server

By default, Apache puts a test HTML file in the web folder. This default web page is served when you browse to http://localhost/ on the Pi itself, or http://10.2.1.90 (whatever the Pi’s IP address is) from another computer on the network. To find out the Pi’s IP address, type hostname -I at the command line (or read more about finding your IP address) in our documentation.

Browse to the default web page, either on the Pi or from another computer on the network, and you should see the following:

20160208-apache-raspbian-it-works

This means you have Apache working!

Install PHP

PHP is a preprocessor; it’s code that runs when the server receives a request for a web page. It runs, works out what needs to be shown on the page, then sends that page to the browser. Unlike static HTML, PHP can show different content under different circumstances. Other languages are capable of this, but since WordPress is written in PHP, that’s what we need to use this time. PHP is a very popular language on the web; large projects like Facebook and Wikipedia are written in PHP.

Install the PHP and Apache packages with the following command:

sudo apt-get install php5 libapache2-mod-php5 -y

18.6 MB of additional disk space will be used

If you haven’t changed directories to /var/www/html yet do so now.

Test PHP

Create the file index.php:

sudo nano index.php

Put some PHP content in it:

<?php echo "hello world"; ?>

Now save the file (Ctrl+x y) . Next delete index.html because it takes precedence over index.php:

sudo rm index.html

Refresh your browser. You should see “hello world”. This is not dynamic but it is still served by PHP.

20160208-php-raspbian-hello-world

Try something dynamic, for example:

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

Or show your PHP info:

<?php phpinfo(); ?>

20160208-php-raspbian-phpinfo

Install MySQL

MySQL (pronounced My Sequel or My S-Q-L) is a popular database engine. Like PHP, its overwhelming presence on web servers enhanced its popularity. This is why projects like WordPress use it, and why those projects are so popular.

Install the MySQL Server and PHP-MySQL packages by entering the following command into the terminal:

sudo apt-get install mysql-server php5-mysql -y

85.6 MB of additional disk space will be used.

Note: When installing MySQL you will be asked for a root password. You’ll need to remember this to allow your website to access the database.

Download WordPress

You can download WordPress from wordpress.org using the wget command. Helpfully, a copy of the latest version of WordPress is always available at wordpress.org/latest.tar.gz and wordpress.org/latest.zip, so you can grab the latest version without having to look it up on the website. At the time of writing, this is version 4.0.

Navigate to /var/www/html/, and download WordPress to this location. You’ll need to empty the folder first (be sure to check you’re not deleting files you need before running rm); change the ownership of this folder to the pi user too.

Now extract the tarball, move the contents of the folder it extracted (wordpress) to the current directory and remove the (now empty) folder and the tarball to tidy up:

Set up your WordPress Database

To get your WordPress site set up, you need a database. Run the mysql command in the terminal and provide your login credentials (e.g. username root, password password):

mysql -uroot -ppassword

Here I have provided my password (the word password) on the command line; there is no space between -p and your password.

Alternatively you can simply supply an empty -p flag and wait to be asked for a password:

mysql -uroot -p

Now you will be prompted to enter the root user password you created earlier.

Once you’re connected to MySQL, you can create the database your WordPress installation will use:

mysql> create database wordpress;

Note the semi-colon ending the statement. On success you should see the following message:

Query OK, 1 row affected (0.00 sec)

Exit out of the MySQL prompt with Ctrl + D.

WordPress Configuration

You need to find out your Pi’s IP address to access it in the browser, so in a terminal type the command hostname -I.

Navigate to http://YOUR-IP-ADDRESS e.g. http://192.168.1.5 in the web browser on your Pi.

You should see a WordPress error page; this is good! Click the big button marked Create a Configuration File followed by the Let’s go! button on the next page.

Now fill out the basic site information as follows:

Database Name: wordpress
User Name: root
Password: <YOUR PASSWORD>
Database Host: localhost
Table Prefix: wp_pi314159_

Upon successful database connection, you will be given the contents of your wp-config.php file:

Copy this text, return to the terminal on the Pi and edit the file with:

nano wp-config.php

Paste the text into this file, and save and exit with Ctrl + X, then Y for yes and Enter.

Now hit the Run the install button.

Welcome Screen

Now you’re getting close.

Fill out the information: give your site a title, create a username and password, put in your email address and untick the search engines box. Hit the Install WordPress button, then log in using the account you just created.

Now you’re logged in and have your site set up, you can see the website by visiting your IP address in the browser on the Pi or another computer on the network. To log in again (or on another computer), go to http://YOUR-IP-ADDRESS/wp-admin.

Friendly Permalinks

It’s recommended that you change your permalink settings to make your URLs more friendly. To do this, log in to WordPress and go to the dashboard. Go to Settings then Permalinks. Select the Post name option and click Save Changes. After saving, you will be prompted to update your .htaccess file. You probably don’t have one yet, so add one in /var/www/html/ by typing nano .htaccess; note this is a hidden file, so it starts with a dot. Then paste in the contents provided:

Save the file and return to the website homepage. Click on the post title or the sample page link and you’ll probably see a Not Found error page. This is because the rewrite module has not been enabled in Apache. To do this, enter:

sudo a2enmod rewrite

You’ll also need to tell the virtual host serving the site to allow requests to be overwritten. Do this by editing the virtual host file (with root permissions):

sudo nano /etc/apache2/sites-available/000-default.conf

You will need to add a directory block

Notice AllowOverride setting is set to All.

Save the file and then restart Apache with:

sudo service apache2 restart

Once it’s restarted, refresh the page and it should load successfully. Now posts have URLs like /hello-world/ instead of /?p=123, and pages have URLs like /sample-page/ instead of /?page_id=2.

Customization

WordPress is very customizable. By clicking your site name in the WordPress banner along the top of the page (when logged in), you’ll be taken to the Dashboard. From here you can change the theme, add pages and posts, edit the menu, add plugins and lots more. This is just a taster for getting something interesting set up on the Raspberry Pi’s web server.

What Next?

  • PHPmyAdmin – coming soon