Raspberry Pi 4 Web Server running from an SSD card with NGINX and WordPress – PART 1

by Mike McRoberts

A while back I bought a 250Gb WD Blue SN500 M2 SSD to run an InfluxDB and Grafana server and this has been running nicely since November 2019. I then decided recently that I didn’t want to pay the hosting costs for the Medway Makers website any more and that we should try and self-host it using a Raspberry Pi. The Pi 4 with the SSD card I had been using as the InfluxDB and Grafana server was ideal. It was a 4Gb Pi 4 and with the SSD card, it was very fast.

So after a few days of pulling my hair out, reformatting and reinstalling and so on, I was finally able to get the WordPress based site up and running and you are reading it, served from a Raspberry Pi, right now.

So below I am going to give you a tutorial in two parts in how to boot your Raspberry Pi 4 from an external drive to give you a huge speed boost, and then how to install NGINX and WordPress so you can run your blog site from a Pi 4 based web server. This is part one of that tutorial.

First, you will need to go to the Raspberry Pi website to download the latest Raspbian image. These can be found at https://www.raspberrypi.org/downloads/raspbian/ You are looking for Raspbian with Desktop. Download the image onto your PC using either your torrent client or just download the zip file and extract the image once downloaded.

Once you have your image file use your favourite image burning software to burn the image to your SD Card. I personally use Balena Etcher. Once you have your image burnt to the SD Card, plug it into your Raspberry Pi 4 and also attach your SSD drive to one of the USB 3 ports (blue) on the Pi 4. Power up your Pi and boot into the desktop. Follow the instructions as you go to set up your wifi and so on. Then once in the desktop open a command terminal and carry out a full update using:

sudo apt update
sudo apt full-upgrade

Once you have a fully updated Pi you are ready to prepare the SSD drive. In a terminal window type in

sudo fdisk -l

and look for your SSD drive. If you have only one external drive attached it will likely be

/dev/sda

Format your drive by typing

sudo fdisk /dev/sda

You will then get a “Welcome to fdisk” message and a prompt. Type p to see a list of the partitions on the drive. More than likely there will be only one partition, If not delete them all. Now press d to delete the primary partition. Once there are no more partitions on the drive press n to create a new partition. Choose p to make it a primary partition and then 1 to make it the first partition on the drive. Then hit the enter key twice when asked for the start and end sectors to choose the default settings. Finally, press w to write the settings above to the drive.

Now the drive has a single primary partition, you can format the drive with

sudo mkfs.ext4 /dev/sda1

Your drive is nor formatted in the ext4 format and ready to be used. Now go to the desktop and click the Pi logo in the top left then go down to Accessories and inside here you will find the ‘SD Card Copier’. Open this app.

The Raspbian SD Card Copier app

In the “Copy from device” box choose your SD Card and in the “Copy to device” choose your SSD card. Make sure you click on “New partition UUIDs” too. Then click “Start”. Now, wait for the entire contents of the SD Card to be copied over onto the SSD Card. This should only take a few minutes.

Now in a terminal window type the commands

sudo blkid

blkid is the linux command-line utility to locate/print block device attributes. In your terminal window, you should now see the details for the SD Card and the new SSD drive like this

The output from the blkid command

In the image above the top two lines show the boot partition and the rootfs partition of the SD Card and lines 3 and 4 show the boot and rootfs partitions for the SSD drive. As you can see they are identical apart for the PARTUUIDs for the 2 drives and their partitions.

Now leave this terminal window open and open a second terminal window. Now we are going to edit the cmdline.txt so that the Pi boots to the SSD instead of the SD Card. Create a backup of the cmdline.txt file in case you need to restore it later. Use the Linux copy command cp as follows

sudo cp /boot/cmdline.txt /boot/cmdline.txt.bak

Then edit the cmdline.txt file by typing

sudo nano /boot/cmdline.txt

Inside this file, you are looking for the command that starts root=PARTUUID= and this will point at the root partition of the SD Card. To change it over to the root of the SSD drive, copy the PARTUUID of the SSD drive from the first terminal window into here. In my case,as you can see above this is 8cac059a-02 with the first 8 digits being the ID of the drive and 02 being the 2nd partition.

Copy this over the PARTUUID in the cmdline.txt file. Save the file by hitting CTRL + X and then Y and enter.

You have now copied the entire contents of the operating system on the SD card over to the SSD and pointed the cmdline.txt file to boot into the 2nd partition of the SSD drive instead of the SD Card. Finally, reboot your Pi by typing

sudo reboot

Once the Pi has rebooted open up the file manager and look down at the bottom right. This should now indicate the size of the new SSD drive. If so, you are now running from the SSD drive instead of the SD card.

Note that the SD card is still required and must be left in the Pi. However, all the SD card is doing is loading the kernel and after that, it isn’t used. The OS then runs from the SSD. Hopefully, a future firmware update for the Pi 4 will allow true boot from USB without needing an SD card (like the previous versions of the Pi’s). For some reason, this is taking the Pi foundation a very long time to sort out.

In part 2 I shall show you how to install NGINX and get a WordPress site running from your Pi.

dav

2 Comments Add yours

  1. Ken Virapen says:

    Hi Mike,
    Thanks for sharing this setup which I am hoping to duplicate on a Raspberry Pi 4B
    I anxiously await part 2.

    1. Mike says:

      Hoping to do part 2 over this weekend Ken. Glad you like part 1.

Leave a Reply

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