Raspberry Pi Webserver

This website is hosted on my own webserver built on a Raspberry Pi micro computer. The following basic information and links could be used to set up your own home webserver. Feel free to contact me if you would like further information.

Firstly, have a look at the official Raspberry Pi website to find out what this fantastic little computer is all about.

The Pi runs a custom OS based on Debian Linux called Raspbian. I use an installation of WordPress with a third party theme called The Box. The site is served using Apache with database management from mySQL. PHP is the language used by WordPress to interact with the database. The combination of Linux, Apache, mySQL and PHP is commonly referred to as a LAMP stack server. My domain name is from GoDaddy and dynamic DNS is managed with DNSexit. SSL certification (enabling https and secure server-client connection) is provided by Lets Encrypt.

Setting up a basic home-built webserver is relatively straight forward with a rudimentary knowledge of Linux-based operating systems, the ability to interact with a command line interface and some understanding of networking and domains. However, the maintenance, trouble-shooting and optimisation could turn into a full-time job if you’re not careful – this really needs to be a labour of love rather than a passing phase!

Assuming you have a Raspberry Pi or similar, know how to use it and have bought a domain name, then the following tutorials will set you on the path to your own self-hosted website.

WARNING: Note that to allow others to access your site (from the internet rather than devices on your home network) you need to configure your web host with a static IP address and your home router to allow access to your web host from the outside world. This requires something called port-forwarding and I strongly recommend reading up on this and understanding what it does so you do not leave your home network vulnerable to outside attacks.

With that said, let’s get going!

Install a LAMP stack with WordPress on your Pi and create a website that can be seen on your local network:

https://projects.raspberrypi.org/en/projects/lamp-web-server-with-wordpress

At this point you can choose a WordPress theme, begin writing posts and creating pages, adding photos or other media and generally creating the look and feel of your site. Using WordPress is very intuitive and a million-and-one tutorials exist online should you get stuck.

Allowing clients on the internet to view your website:

Go into the admin settings of your home router (typically accessed through a web page on your home network with address 192.168.0.1 or similar), log in with your username and password (contact your ISP if you don’t know these) and navigate to the menu which allows you to reserve an IP address for a device.

How this looks varies from router to router so you’ll just have to poke around or Google it for your make of router. When you have identified your Pi and reserved it an IP address, any time the router allocates IP addresses to devices on your home network the Pi will always get the same one – a static IP address. This is important because now you are going to direct (and allow in!) any requests from the internet for http or https websites to the static IP address of your Pi, on which lives the webserver.

Find the port forwarding menu on your router (could be in advanced networking settings) and set up rules to allow http requests on port 80 to the IP address of your Pi. If you eventually move your site to https with an SSL certificate, then at that point modify your rule to allow https requests on port 443 to go to your Pi.

Setting up dynamic DNS:

Your home router also has an IP address that identifies it on the internet. Unless you have a special (and likely expensive) deal with your ISP, then it will not have a static IP address – it can change when your router reboots or when your ISP tells it to. This clearly isn’t helpful if you want people to always be directed to your website anytime they type in your domain name in a search bar!

To overcome this we need something called Dynamic Domain Name Servers (DDNS). This sounds complicated but it’s basically just a lookup table, provided by third-party online services, that maps your domain name to an IP address (in this case the IP address of your router, which is then configured to port forward the request to the IP address of your webserver).

If your router’s IP address can change, we need a way of continuously monitoring it’s IP address and updating the look-up tables if it does change (the dynamic bit). This is very simple and I use a company called DNSexit which provides this service free of charge for a single non-commercial domain name. You’ll need to sign up, provide your domain name and follow the instructions to set up a repeating CURL command to check your router’s IP and update the DDNS tables if needed:

https://dnsexit.com/services/free-dynamic-dns-service/

Making your site https rather than http:

By default your LAMP stack installation will create a http site; a site where the connection between the client (the device viewing your site) and the server (your Apache powered weberver) is insecure because it’s not encrypted. This isn’t technically a big deal if you’re not exchanging any sensitive data (this site was http for 8 years before I changed it in 2022!) but moving it to a secure encrypted https site right of the bat will help your Google search rankings, give site visitors piece of mind (no warning messages and open padlock symbols in the search bar) and means you are conforming to current security standards. I highly recommend doing this up front because updating all your site content if you do it later is a time consuming PITA…

https requires something called a SSL certificate and you can get these for free with automatic renewal from an organisation called Lets Encrypt. A bot can be installed to run on your webserver which grabs a certificate every time they expire, installs it in Apache and configures Apache to use https all in one go. Once you’ve done this (and only after you have) you then just need to change your port forwarding, as described above, to use https on port 443 and change your website address URLs in your WordPress general settings to say https rather than http. Follow these tutorials:

https://certbot.eff.org/instructions?ws=apache&os=debianbuster

https://www.wpbeginner.com/wp-tutorials/how-to-add-ssl-and-https-in-wordpress/

Regularly backing up your website:

Given all the hard work you’ve already done to get here, let alone the many years of blogging and content creation you’re about to do, it’s prudent to have a fool-proof backup solution so that your site is never lost for good should something terrible happen, like a corrupted disk…

You can pay for a WordPress plugin to do this for you but the simplest way that I’ve found is to take backup disk images (literally a total copy of the entire disk, OS and all) at regular intervals and store those backups on another hard disk (like a USB drive) connected to the Pi. You can then copy that image onto another machine and to the cloud. That way, if something does go wrong, you can re-flash your Pi hard disk with your latest backup disk image to easily recover your site. I do this with a simple piece of code using the dd command and schedule the code to run in the early hours of every Monday morning using crontab. The code is below but you can use Google to find out how to schedule it using crontab…

sudo dd bs=4M if=/dev/sda | gzip > /home/pi/usb/webserver_backup.gz

Congratulations!

You should now have a working self-hosted WordPress website, accessible in a secure manner from anywhere in the world with DDNS and a back-up solution. What you do with that site, however, is up to you…

Please do contact me if you would like more details on the above or consider beginning a comment thread below so that others may benefit from the conversation.

Raspberry Pi model B+ in a Pimoroni Pibow Coupe case

Leave a Reply

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