Raspberry Pi 4 server at home

Date: 2022-10-19

Author: Thomas

Categories: Computers | Coding |

Tags: Raspberry Pi | Hosting | VS Code |


A Raspberry Pi 4 at home

General

I have installed a Raspberry Pi 4 at home with the goal of running it as a kind of server for different things. Here is a list of hardware, software and some points and facts:

Connections

The Raspberry can be connected to the router and share the home network at a 192.168.n.n address. That will work fine, but connection will be lost when we turn off our router at night or when we go away for a longer period.

So I have it directly connected with a cable to the ISP modem, which we never turn off. Port 22 is open in the Raspberry and it is possible to SSH into it from anywhere in the world.

If the Raspberry doesn't want to connect, run the following:

sudo ethtool -s eth0 speed 100 duplex full autoneg off

SSH

You can SSH into the Pi with PuTTY and/or VS Code at 83.177.182.nnn port 22 and private keys ~/.ssh/Windows_Private_Key.ppk for Putty and ~/.ssh/Windows_Private_Key - OpenSSH.ppk for VS Code. A description of this for VS Code – see this post.

The ip from my ISP is not static and changes from time to time ("nnn" above). This is solved automatically with Caddy and Cloudflare – see below – but Putty and VS Code must be updated manually.

Caddy

Caddy is an open source web server of sorts. Does away with Apache, Nginx and databases.

echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list sudo apt update sudo apt install caddy

sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw enable sudo ufw status

sudo systemctl start caddy

If running Caddy as a service, open Caddy config file /etc/caddy/Caddyfile and type the following:

ahlstroem.net { root * / reverse_proxy localhost:1313 }

Hugo

This may install an older version. For the latest Hugo Extended:

```

Replace version with latest

HUGO_VER=0.126.1 wget https://github.com/gohugoio/hugo/releases/download/v$HUGO_VER/hugo_extended_${HUGO_VER}Linux-64bit.tar.gz tar -xvzf hugo_extended${HUGO_VER}_Linux-64bit.tar.gz sudo mv hugo /usr/local/bin/ ```

hugo version

hugo new site myblog cd myblog

git init git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke themes/ananke echo 'theme = "ananke"' >> config.toml

Open a terminal on the Raspberry Pi, cd to the folder from where you want to serve Hugo and run hugo server -D. You can now reach localhost:1313 on the Raspberry Pi on the address it is assigned by your internet provider. In this case Comviq is the provider and the Raspberry address is 83.177.182.215 (this has probably changed, see SSH above).

The hugo serve command is mostly for developing purposes. A more advanced way of building the site is to run the command hugo from the above mentioned folder. Hugo will then build the site under the public folder and the Caddy config file should contain the following:

ahlstroem.net { root * /home/admin/hugo/blog/public file_server }

You can refer to another site by adding another post in the Caddy config file. Add as many sites you want this way.

textochnot.se { root * /home/admin/hugo/blog/content/textochnot.se file_server }

Save and close the file and run sudo systemctl reload caddy.

Cloudflare

If you set the DNS for each of your sites at your internet provider to point at the Raspberry Pi:s ip you can reach all of them. That is all well and good as long as your ISP doesn't change the ip. But probably they will, so we let Cloudflare handle the DNS instead.

Setup auto update of Cloudflare when ip changes

Cloudflare Dynamic DNS IP Updater is a BASH script that updates DDNS at Cloudflare.

What to update when ip changes

Putty:

VSCode:

Cloudflare:

Update at a reboot of the Raspberry

A crontab file at user admin runs at reboot. Be sure to run export EDITOR=nano before crontab -e if you need to edit it.

Send e-mail when ip changes

NeoMutt is used to automatically send an e-mail when the ip changes. The process of setting up NeoMutt can be viewed here: Email on the terminal with mutt. Go into the Cloudflare script mentioned above. Go to the section "Get existing IP" and insert the following lines:

neomutt -s "Raspibolt has a change of IP" thomas.ahlstroem@gmx.com < ~/ip.txt`

neomutt -s "Raspibolt has no change of IP" thomas.ahlstroem@gmx.com < ~/ip.txt`

Replace the e-mail above with an address to where you want to recieve the message.