I have had my Raspberry Pi 4 for quite some time and the end goal was to use it
This blog post explains my setup for Ghost (which you are now reading from 🙌)
This is part #1 explaining what is needed in order to server a website or any content from home.
What do we need?
So in order to be able to serve our digital content we need a few basic things.
- A working internet connection
- A device to act as a Server (this can be anything from an old laptop to your gaming PC)
- Being able to modify your Router
Plug-in your Raspberry Pi
Regardless of which Raspberry Pi you have, it will need 3 things:
- a USB-C cable to provide power
- an SD-Card
- an Ethernet cable
If you are more experience you can choose to google around and find the different components, otherwise there are a lot of
all-in-one products you can by on amazon.
Connect to your Pi from another computer
This is a bit of a pain, as it depends on your home setup, the version of the Pi and the Operating system you will install on it.
I wont add with detail every approach or step as it has been thoroughly documented online.
The approaches are:
- connect to the Pi using keyboard and screen and after you login enable ssh via command line.
- connect to the Pi using keyaboard, mouse and screen and after you login you can enable ssh via the desktop UI.
- add a specific file inside the sdcard that enables ssh configuration
Connecting from network/remote pc
Afterwards you should be able to connect to your Pi using normal ssh i.e
You could additionally add an ssh-key-pair so that you dont need to enter the password every time. Follow this guide.
Lastly if you care about not having to find the local ip all the time, you can either setup a DNS (which you will most likely) but this will make your connection go through some additional steps, I personally prefer to setup a static IP on my router for the Pi and connect to that. On how to do this please check the guide further down under "Configuring our Router".
Going around the Dynamic IP
The main issue with such a setup is that most home internet connections come with something called a
dynamic ip. An IP is your digital address and for home connections this IP changes in variable frequencies depended on your provider.
Why is this a problem?
Imagine the IP being your home address and you invite friends over every day, but that address changes daily as well! This means that you will have to tell them a new address every day, which might work fine for a few friends but if you want 24/7 uptime then it's going to be a full time job just updating your new address.
Furthermore, your IP looks something like
188.8.131.52 which is not easy to remember and ofcourse we prefer to use named urls to share with our friends.
So in order to translate
czioutas.com we need to set a DNS record. (Imagine that there is a world wide address book that points IPs to Name urls)
But as we mentioned before the IP changes frequently, so you would have to login to your DNS manager and change that value every time 😢.
There is a better way 😎
I use Cloudflare to manage my websites and DNS records, luckily cloudflare has a programable API that allows me to update the IP just with a few lines of code.
I used this script with minor modifications to suit my needs and run it every 5 minutes whilst sitting on the same server that hosts the content you see now.
The logic is pretty simple:
- find my current IP
- check against the registered IP if its any different (do we need to update?)
- call the cloudflare api to update the new IP
Configuring our Router
This may vary a lot depending on your home network setup. The explanation bellow assumes you have a router at home that connects directly to your provider and that the router allows you to do basic configuration over your settings.
If you are for example, using a centralized building connection or you are behind a DMZ this will most likely require further setup.
One crucial point is port forwarding. When the IP resolves to your home IP it will send all traffic to your router regardless of the port. However your router does not know how to forward this to your Pi without explicit definition.
So let's say your website runs on port 80 or 443, you need to go to your router settings and define that tcp connections coming to port 80/443/etc will map to port 80/443/etc. You can mix and match the ports but make sure they make sense for your setup.
This configuration should be available from your Router's admin page. Usually routers are available through 192.168.1.1 (unless specified otherwise, see their manual) and will be available ideally only via LAN and not WLAN.
For example this is how one rule looks on my router.
Static Local Network IP (Optional)
As mentioned earlier, it is not needed for your Pi to have a static IP address, however it helps a lot to not have to look it up.
Once again this might not work with all home network setups but should for the basic ones.
The concept of what we need to do is to bind the MAC-Address of the Pi to a specific IP within the local range.
As "simply" illustrated bellow, my Pi with MAC-Address X is bound to IP Y, this means that if I unplug my Pi from my router, or the router looses power etc, when it comes back online, the Pi will be still accessible via this IP.
You may want to read a bit about MAC-Addresses here (in short is a unique identifier for the "connection" of a piece of hardware).
With all the above, you should have a network that is ready to host your Pi and the Ghost blog. At the moment it might not make 100% why we need these things, but in the next post, in which we configure the Pi itself we will be able to see how they all tie together.
Till next time!