Featured image of post Getting Started with RaspberryPi as Home Server

Getting Started with RaspberryPi as Home Server

Initial setup - Installing OS, Docker, Poratiner

Introduction

So, you’ve finally managed to get your hands on a RaspberryPi? That in itself is a huge accomplishment atleast based on the situation at the time of writing. Now it’s time to dive into the world of self-hosting. Let’s get started! Here is a list of things that you’re gonna need to get going. Some of these are not absolutely necessary but definitely recommended.

  • RaspberryPi 4 (Atleast 4GB ram)
  • Power Adapter
  • Case (Recommended to go with something that has active cooling)
  • SSD (Definitely use SSD instead of MicroSD for boot drive especially for our use case)
  • Micro-HDMI adapter (Or just a micro to full size HDMI cable) You can also just buy a kit that comes with all this and much more to get you going.

OS Installation

This part of OS installation is covered by probably any blog related to the RaspberryPi and so I won’t be covering here in detail. You can follow this guide or this video for detailed steps. In my set-up I am using RaspberryPi OS 64bit running on a 240GB SSD. You can go with the lite OS installation if you wish but the desktop environment can come in handy sometimes, especially when trying to troubleshoot some error. Once you are up and running, fire up the terminal if you are at the desktop using HDMI and make sure you do a sudo apt update && sudo apt upgrade before proceeding with the next steps.

Installing Docker

Docker is what I am using to run majority of my apps & services. It may seem complicated at first but trust me, once you start using docker for deploying your services, you won’t look back! For quickly and easily deploying docker we will be using an install script from the pi-hosted series.

To install docker, run

1
wget -qO- https://raw.githubusercontent.com/pi-hosted/pi-hosted/master/install_docker.sh | bash

You can visit the url mentioned above to go through the installation script before running it, which is a good rule to follow anyways. Never blindly run any scripts on your machines. Post installation of docker, reboot your Pi. Now you should be having docker up and running on your machine. You can test this by running docker run hello-world and you should see the following on your terminal

output for “docker run hello-world”

Installing Portainer

Portainer is a container management platform that we’ll be using to manage our docker containers. Installing this is also going to be easy as we will be using a script for this as well. This is basically going to be our first docker container installation!

To install portainer, run

1
wget -qO- https://raw.githubusercontent.com/pi-hosted/pi-hosted/master/install_portainer.sh | bash

Now portainer should be up and running on your Pi. To access portainer, open any browser of your choice and navigate to the-ip-address-of-your-pi:9000 (eg. 192.168.0.125:9000) You should be greeted with the initial setup for portainer. Create a username and password and on the next screen make sure you select ‘Docker’ and click connect. That’s it, you should now be logged in to your portainer dashboard. For more details you can check the official portainer documentation. We are now ready to start installing various apps & services on our little RaspberryPi server!

Extra - Setting up OLED Display

So if you’ve got an OLED display with your Pi then here’s an example script for you to use to display stats on your display. Firstly you need to connect and set-up the display with your Pi. You can follow this guide for the initial set-up. Below is the script I personally use on my display. I am using a larger font in it just so that I can read the information displayed even when I’m not really close to the Pi.

https://github.com/spandan13/orbitsrv-docs/blob/main/OLED-Stats/stats.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Original Script created by: Michael Klements - https://github.com/mklements/OLED_Stats/blob/main/stats.py
# This is a modified version of the above script
# Base on Adafruit CircuitPython & SSD1306 Libraries
# Installation & Setup Instructions - https://www.the-diy-life.com/add-an-oled-stats-display-to-raspberry-pi-os-bullseye/
import time
import board
import busio
import digitalio

from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

import subprocess

# Define the Reset Pin
oled_reset = digitalio.DigitalInOut(board.D4)

# Display Parameters
WIDTH = 128
HEIGHT = 64
BORDER = 5

# Use for I2C.
i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)

# Clear display.
oled.fill(0)
oled.show()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new("1", (oled.width, oled.height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a white background
draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)

font = ImageFont.truetype('PixelOperator.ttf', 55)
font2 = ImageFont.truetype('PixelOperator.ttf', 16)

while True:

    # Draw a black filled box to clear the image.
    draw.rectangle((0, 0, oled.width, oled.height), outline=0, fill=0)

    # Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
    cmd = "top -bn1 -p 1 | fgrep \"Cpu(s)\" | tail -1 | awk -F'id,' -v prefix=\"$prefix\" '{ split($1, vs, \",\"); v=vs[length(vs)]; sub(\"%\", \"\", v); printf \"%s%.1f%%\", prefix, 100 - v }'"
    CPU = subprocess.check_output(cmd, shell = True )
    cmd = "free --giga -h | awk 'NR==2{printf \"Mem: %s/%s\", $3,$2 }'"
    MemUsage = subprocess.check_output(cmd, shell = True )
    cmd = "vcgencmd measure_temp |cut -f 2 -d '='"
    temp = subprocess.check_output(cmd, shell = True )

    # Pi Stats Display
    draw.text((0, 12), "CPU:", font=font2, fill=255)
    draw.text((0, 16), str(CPU,'utf-8'), font=font, fill=255)
    draw.text((0, 0), str(MemUsage,'utf-8'), font=font2, fill=255)
        
    # Display image
    oled.image(image)
    oled.show()
    time.sleep(2)

    
    ##Switch to temps
    # Draw a black filled box to clear the image.
    draw.rectangle((0, 0, oled.width, oled.height), outline=0, fill=0)

    # Pi Stats Display
    draw.text((0, 12), "Temp:", font=font2, fill=255)
    draw.text((0, 16), str(temp,'utf-8') , font=font, fill=255)
    draw.text((0, 0), str(MemUsage,'utf-8'), font=font2, fill=255)

    # Display image
    oled.image(image)
    oled.show()
    time.sleep(2)
    

To make sure your display works even after reboots you need to make sure the script runs automatically whenever the Pi reboots. To do this, open crontab crontab -e and add @reboot python3 /path/to/file/stats.py &

So now you should bu fully set-up with your new RaspberryPi and fully ready to go down the rabbit hole of self-hosting. See you down there!

Built with Hugo
Theme Stack designed by Jimmy