Local Hosting: Raspberry Pi as a Server
Raspberry Pi's are incredible devices. Kinda.
The idea of having a tiny 30 dollar full fledged computer is really interesting, but if you are like me, once you buy it, you may find yourself wondering what to do with it. Especially if you already have a home media player (Apple TV) and don't want to make a magic mirror.
Well one thing you can do is host a website! There is something very satisfying about putting together the hardware and building the software for your own website, and raspberry pi lets you do that cheaply and (somewhat) easily.
Time to Buy the Pi
If you have never bought a raspberry pi, you will be amazed by how cheap they are. You can buy a raspberry pi 0 for just five dollars. But don't be foolish. You can't just buy the pi itself. You need to get a case, a power supply, and the accessories to interact with it.
As such, I would recommend getting a kit instead of a pi itself.
I got two. One to act as a development server and one as a production server. The development server is very nice to have because there is a lot of trial and error that goes on when you are setting up your network configuration. It feels much safer to do this on a server that you don't particularly care about rather than one that is actively hosting your website.
For the development server, I am using a raspberry pi zero kit, which at the time of writing this cost 38 dollars, and for the production server I am using a raspberry pi 4 kit, which costs 84 dollars.
Flashing the Pi
The raspberry pi zero comes with a micro SD card with noobs installed on it, so you just need to plug it in and Raspbian, which is a linux distribution, will boot off of it. But for the raspberry pi 4, you will need to set up the bootable micro SD card yourself. I would recommend following the tutorial on raspberry pi's website. But its very simple:
- Download the Raspberry Pi Imager
- Choose Raspbian or Raspberry Pi OS as your operating system
- Write to your SD Card
And with that you should be good to boot from that SD card. Plug your raspberry pi into your TV via HDMI and follow the prompts to complete the installation.
Giving your Raspberry Pi a Static IP Address
For the most part, you will not be hooking up your raspberry pi to a monitor and accessing it that way. Instead you will SSHing into it via a static IP address. Unfortunately, computers do not have one of those out of the box, so you will need to set one up.
As you might expect, your IP address is an address of sorts (shocking), telling your router where to send internet traffic. Think of it like a street address and your router as the mail man. Without an address, he wouldn't be able to deliver the mail.
But unlike a street address, your router will periodically change your IP address. So in order for you to be able to access your raspberry pi consistently, you will need to assign it a static one.
First you need to find your current IP address:
hostname -I
Next you need to edit the dhcpcd.conf
file. Using the following line will open it in nano:
sudo nano /etc/dhcpcd.conf
Next add the following chunks of code to the bottom of the file. If you are using a wired connection add the following:
interface eth0
static ip_address=<<YOUR IP ADDRESS GOES HERE>>/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
If you are using a wifi connection:
interface wlan0
static ip_address=<<YOUR IP ADDRESS GOES HERE>>/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
So what do all these lines mean?
- interface: if its a wifi or ethernet connection
- static ip_address: this is the IP address that you will be using. Put /24 at the end
- static routers: this is the IP address of your router. It usually is
192.168.0.1
- static domain_name_servers: this is IP address of your DNS, which is also most likely the IP address of your router
Now save and quit!
Give your machine a reboot with the reboot
command and then check its IP address with
hostname -I
again to make sure it hasn't changed. If you have done this successfully, you will
now be able to SSH into your machine using your IP address with the following command:
Next you need to enable SSH on your raspberrypi. To do that, open the preferences menu, click over to interfaces, and then select "Enable" next to SSH.
ssh [email protected]
You will be prompted for a password, which will be whatever you entered when setting up the machine. The above line assumes you went with the default (pi) as your username.
Setting Up Apache
To be completely frank, I was astonished by how easy this was. And it will work perfectly right out of the box assuming that you just want to host static files.
Its only two lines!
sudo apt install apache2
sudo service apache2 start
The first installs apache and the second starts the service.
Next cd into the /var/www/html folder, and you will see a file called index.html. This is a welcome page created by apache, and if you type in your IP address into your web browser you will see this page rendered. If you just want to host a static website, ie one where only client side code is used, you can move all your code into this folder, and it will serve it, assuming that you have a index.html inside that html folder.
Keep in mind that at this point, your website will only be available on that IP address as long as you are on your network. To open it up to the world, you need to do some port forwarding, which we will cover later.