, , , , ,

Set up a LAMP server with Mythic Beasts

Reading Time: 7 minutes

In this test, we’ll use it to set up a WordPress website similar to the one we use online. But you could also use LAMP to deliver any kind of website.

Sign up with Mythic Beasts

Start by ordering a Raspberry Pi 4 from Mythic Beasts.

We’re going to use a faster Raspberry Pi 4, to get the extra 4GB RAM for our LAMP server. There’s a monthly charge of £7.25 for a Raspberry Pi 4 web server, which is an excellent deal for a dedicated computer with network storage (our look at non-Raspberry Pi services showed around £27 per month for a Linux machine with four cores).

You can test out the service with a Raspberry Pi 3 for only £5.25 per month (this only has 1GB RAM).

We’re going to stick with the default 10GB storage, although the ‘Disk space’ slider at the bottom is used to select up to 250GB (at 2p per GB).

Click the green Order Now button.

Enter a service name and tick the ‘I agree to the terms and conditions’ box. The other fields are optional. Click Confirm to provision (activate) your Raspberry Pi. Enter your payment details or click Raise Invoice.

Wait for Raspberry Pi to be provisioned and the operating system to be installed. Don’t close the web window.

Generate an SSH key

When provisioning has completed, click ‘Configure your server’ (if you browse away, then you will find the server at
mythic-beasts.com/customer/servers/rpi
).

Mythic Beasts uses SSH keys to provide secure access between your local computer and the remote Raspberry Pi server. If SSH Keygen is new to you, then take a look at passwordless SSH access in the Raspberry Pi documentation.

First, check whether there are already keys on the local computer. Open a Terminal window and enter:

ls ~/.ssh

If you see files named id_rsa.pub or id_dsa.pub then you have keys set up already, so you can skip the ‘Generate new SSH keys’ step below and head to Step 3. To generate new SSH keys, enter the following:

ssh-keygen

Upon entering this command, you will be asked where to save the key. We suggest saving it in the default location (~/.ssh/id_rsa) by pressing ENTER.

You will also be asked to enter a passphrase, which is optional. The passphrase is used to encrypt the private SSH key so that if someone else copied the key, they could not impersonate you to gain access. If you choose to use a passphrase, type it here and press ENTER, then type it again when prompted. Leave the field empty for no passphrase.

Add the key SSH

Take a look a the content of the key:

cat ~/.ssh/id_rsa.pub

The output will start with ‘ssh-rsa’ and end with your hostname ‘pi@raspberrypi’.
Use your mouse to select all the output of the SSH key, then right-click and choose Copy. Now head back to the browser and click ‘configure keys’. Right-click on the large ‘Keys’ text field and choose Paste to enter the SSH key. Click ‘Save changes’.

A secure SSH key generated on your local computer is shared with the remote computer to provide access

SSH access

Scroll down the server window and take a look at the details in SSH access. Here you will see your Username, Host, Port, and Authentication information. You will use this information to connect to your Raspberry Pi server. There is also a Command section, with the Terminal command used to connect directly. Ours looks like this:

ssh -p 5274 root@ssh.magpi01.hostedpi.com

Copy and paste the command into the Terminal and press ENTER.

Respond ‘yes’ at the ‘continue connecting’ prompt and your SSH key will be added to your list of ‘known hosts’.

If you created one, you’ll need to add the SSH key password when prompted. When logged in, the command prompt will change to root@raspberrypi. You are logged in as ‘root’ and in the root user’s home directory ‘/root’.

Install a web server

We’re going to use the Apache web server, which you can install with the following commands:

apt update
apt upgrade -y
apt install apache2 -y

Note that you don’t need to use ‘sudo’ as you are the root user.

Open a web browser and visit the following URL (replacing ‘magpi01’ with the name of your own hosted server: magpi01.hostedpi.com.)

This will display the Apache2 Debian Default webpage with an ‘it works!’ message.

This page is an HTML file located on your remote Raspberry Pi, at /var/www/html/index.html.

Navigate to this directory in the Terminal and have a look at what’s inside:

cd /var/www/html
ls -al cat index.html

Install PHP

We now have the ‘L’ and ‘A’ of our LAMP server: Linux and Apache. The M and P come next: MySQL and PHP.

MySQL is a database system, while PHP is a programming language. You’ll need both to run most content management systems, such as WordPress.

Now is the ideal time to add the software. You can install PHP with the following command:

apt install php -y

And MySQL with this command:

apt install mariadb-server php-mysql -y

Now restart your Apache server to ensure both services are running.

service apache2 restart

Upload content remotely

You don’t need to be logged in to your Raspberry Pi server to edit content in the html directory. Close the connection with:

exit

You can send files directly to the html directory from your local computer using secure copy (scp).

First, we’re going to get a photo (of our cat) and name it cat.jpg. Then create an index.html file.

nano index.html

And enter this basic HTML code:

<html> <head> <title>Siouxsie</title> </head> <body> <p>Our cat, Siouxsie!</p> <img src="cat.jpg"> </body>
</html>

Copy the two files directly to the html directory on your remote Raspberry Pi server:

scp -P 5274index.html cat.jpg root@ssh.magpi01.hostedpi.com:/var/www/html/

…making sure to replace the ‘5274’ and ‘magpi01’ parts with the port and hostname of your Raspberry Pi server. Press F5 to refresh your web browser and view the new webpage design.

Test out PHP

Let’s test that PHP is working, and also take a look at the index page for our website. Create the file index.php:

cd /var/www/html
nano index.php

Put some PHP content in it:

<?php echo "Hello, World!"; ?>

Save the file and close with CTRL+O and CTRL+X.

Now get rid of the index.html file (because it takes precedence over the index.php file):

rm index.html

Reload your website in the web browser and you will see ‘Hello, World!’. This page isn’t dynamic, but it’s created with PHP code.

A LAMP (Linux, Apache, MySQL, and PHP) server can be used to run a WordPress-based website

Install WordPress

Now we’re going to head back into our remote server and do something a little more detailed. We’re going to set up WordPress, a popular CMS (content management system). This makes use of our PHP and MySQL database and is a great option if you’re looking for a powerful website with a minimum of coding.

Log back into your remote Raspberry Pi server.

ssh -p 5274 root@ssh.magpi01.hostedpi.com

Go to your html folder and get rid of all the content.

cd /var/www/html/
rm *

Now download the latest version of WordPress:

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

If you need to install wget, use apt install wget.

Next, extract the WordPress tarball to get at the WordPress files:

tar xzf latest.tar.gz

Move the contents of the extracted wordpress directory to the current directory.

mv wordpress/* .

Tidy up by removing the tarball and the now-empty wordpress directory:

rm -rf wordpress latest.tar.gz

Use ls to view the contents of a WordPress directory inside your html directory. It will include a new index.php file along with various HTML and PHP files.

The WordPress login page running on a Raspberry Pi web server using PHP and HTML

Set up your WordPress database

Although you have the WordPress files, you can’t use your website just yet. First, you need to set up the MySQL database. Enter this command:

mysql_secure_installation

As this is the first time running MariaDB, there is no password, so just press ENTER.

Type in Y and press ENTER to ‘Set root password?’. Type in a password and press ENTER. Make sure you note this root password, as you will need it shortly.

You’ll be asked four questions. Answer ‘y’ to each one: Remove anonymous users, Disallow root login remotely, Remove test database and access to it, and Reload privilege tables now.

When complete, you will see the message ‘All done!’ and ‘Thanks for using MariaDB!’.

Create a database

Now that the database is installed, you need to create a database for WordPress:

Run mysql in the Terminal window:

mysql -u root -p

Enter the root password you created. You should start up the MariaDB monitor and see this command prompt:

MariaDB [(none)]>

Enter this command:

create database wordpress;

If this has been successful, you should see this:

Query OK, 1 row affected (0.03 sec)

Now enter these two commands:

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

Exit the MariaDB monitor using CTRL+D.

Open WordPress

Open the web browser on Raspberry Pi and head to your website: magpi01.hostedpi.com.
You should see a WordPress setup page; click on Let’s Go.

You will see ‘unable to write to wp-config.php file’. Select all the code inside the window and right-click to copy. Now switch to the Terminal window on your remote Raspberry Pi and enter:

nano wp-config.php

Paste the code into the wp-config.php file in Nano, then save and exit (CTRL+O, CTRL+X). Switch back to your web browser, and click ‘Run the installation’.

Fill out the information fields and click the Install WordPress button, then log in using the account you just created.

Facebook Comments

Microphone open/mute control with repurposed racing pedals

Build an e-paper to-do list with Raspberry Pi