My first (headless) server for less than £60
Tags: beginners, raspberrypi, productivity, linux
Published: 2022-04-25 19:44:54

## Introduction

I can't speak for everyone, but the idea of having a local server has been a small dream of mine since I started programing. It took me a while before convincing myself it would be worth it and eventually I started looking at my options.

I explored the idea of purchasing a pre-made server, but, 
there were a few problems for me. 

Firstly, the expense. I wanted to spend as little as I could on the very basics for my server. This would allow me to get a feel for owning a server, and decide if I wanted to invest more money and time into this project later down the line.

Secondly, an excuse to learn more. I'm an enthusiast when it comes to knowing the details of a system, especially ones I intend to build upon.

For these reasons, I wanted to have as much control over my server as I could, and an excuse to build it from the ground up (sort of) was just too perfect an opportunity to pass up.

## The hardware

There are two things I needed to create my server:

|         Hardware       |Price |
|------------------------|------|
|[A raspberry pi 4 model B](https://www.amazon.co.uk/gp/product/B07TGBLK33/ref=ppx_yo_dt_b_asin_title_o01_s02?ie=UTF8&psc=1)|£45.50|
| [128GB SD card](https://www.amazon.co.uk/gp/product/B073JYC4XM/ref=ppx_yo_dt_b_asin_title_o01_s01?ie=UTF8&psc=1)          |£13.99|
| [Raspberry Pi Case](https://www.amazon.co.uk/gp/product/B07XCKNM8J/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1) [Optional]| £10.99|

The first thing I want to point out is that you can save some money here if you choose an SD card with smaller memory, or a different raspberry pi.
The second is that although I've bought a raspberry pi, I'm not limited to using the raspberry OS(s).

## The software
* AOMEI Partition Assistant - To format the SD card to FAT32
* Raspberry Pi Imager - To load the OS's image onto the SD card
* Fing - An App for your mobile which tells you the IP addresses of devices connected to your LAN
* PuTTY - To access the Rasberry Pi via ssh 

## The approach

### Formatting
Firstly, I had to check the formatting of my SD card. Turns out it was formatted using "exfat", which isn't the formatting I need. So, using AOMEI Partition assistant, I inserted the SD card into my PC, and selected it for formatting. I tried to format it with FAT32, however, the formatting kept failing and the error code gave an example of "bad sectors".

I wasn't convinced this was the actual error and so I tried to format my SD card using Windows built-in formatter, knowing it would fail because it can only handle 32GB of memory. But, as I hoped, it gave me a much more reasonable error. The SD card was locked and thus had write protection enabled.

The steps I used to resolve this were:
`Press: <Windows Key> + r`
`Type: 'cmd'`
`Type: 'diskpart'`
`Type: 'disk list'`
`Type: 'select disk <number associated to SD card>`
`Any operation after this point will only apply to the selected disk`
`Type: 'attr disk clear readonly'`

Great, now we have a writeable SD card, we can continue with the formatting.

After selecting the partition of my SD card I wanted to format, I simply had to choose the type of formatting, FAT32, and apply it to the partition.

### Operating system

Now the choice of OS. As I am starting out, I decided to go with [Raspberry Pi OS Lite](https://www.raspberrypi.org/software/operating-systems/), knowing that I can always change it in the future if I wish. Lite uses 423MB of storage, which is significatly less than most distributions I was looking at, and is perfect for a headless server.

Using the Raspberry Pi imager, I was able to run and select both the SD card and the Lite OS without needing to download the OS's image separately - always a win in my mind.

Once that had finished I could go into my SD's directory and see what had been installed.

### Wireless connection config

Follwing the guide on [www.raspberrypi.org](https://www.raspberrypi.org/documentation/configuration/wireless/headless.md), I added a wpa_supplicant.conf file to the boot partition/folder that the Raspberry Pi Imager had just created.

Now I had created the file, I opened it using Notepad++ (but sublime text would work also). Inside the file I typed:

```
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
 ssid="Wireless LAN Name"
 psk="Wireless LAN Password"
}
```

The reason I used Notepad++ was because I read a lot of posts saying that Windows Notepad wasn't using the correct "New Line"/<NL>/'\n' ASCII formatting, which would cause problems when booting up the server. 

Although I did read a few articles saying this had been patched, to avoid risk, I went ahead and downloaded Notepad++, wrote the file and then uninstalled it. Not because I don't like Notepad++, but I'll likely be using Vim as my go to editor from now on.

### SSH connection

Finally, we just need to tell the server to allow an ssh connection. To do this, I followed [these instructions](https://www.raspberrypi.org/documentation/remote-access/ssh/), which says to simply add a file called ssh (with either no extension or using a .txt will work). The contents of the file don't matter either, so I left mine empty.

### Final bits

I went ahead and downloaded Fing on my phone. While it was downloading I plugged in my Raspberry Pi with the SD card inserted, and waited 2 minutes for it to boot just to be sure it would have time to boot, and load etc.

Once 2 minutes had passed, I refreshed the search for local devices on the Fing app and conveniently found a device with a name "raspberry pi". Alongside the name of the device was the IP address.

I copied the IP address into PuTTY's hostname field, created a session for my raspberry pi and connected to my brand new server.

## Error handling

You might notice, that sometimes updating the wpa_supplicant.conf doesn't always work. This can be for a few reasons, most are painful to figure out. So if updating the wpa_supplicant.conf file doesn't work - and you've waited at least 2 mins - you can do the following steps to help debug why and set up your wireless connection.

```bash
# install the network manager CLI
sudo apt-get install network-manager

# start the manager
sudo systemctl start NetworkManager.service
sudo systemctl enable NetworkManager.service

# connect to the wifi
nmcli d wifi connect <SSID> password <PASSWORD>
```

## Conclusion

This was a great experience for me, and one which I am thrilled I managed to complete. There were moments where I worried I had wasted my money, or chose the wrong PI for the job. But now my server sits on my desk, happily storing some basic programs and I have no doubt I will be upgrading it again in the future.

Have a good one!

## Credit
<span>Cover photo by <a href="https://unsplash.com/@harrisonbroadbent?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Harrison Broadbent</a> on <a href="https://unsplash.com/s/photos/raspberry-pi?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>