Sai Yerni Akhil Madabattula
Sai Yerni Akhil's Blog

Sai Yerni Akhil's Blog

Setting up Raspberry pi to run a web server

Setting up Raspberry pi to run a web server

#2Articles1Week

I was bored this weekend, so I searched my shelves and discovered an old Raspberry Pi Zero W (which I never got to use). So I decided to use it as a web server and host some applications on it. I'll take you through the process of setting in this blog post. This is the first in a two-part series.

Pre-requisites

  • A Raspberry Pi with Wifi ( I have a Raspberry Zero W)
  • An SD Card minimum of 8 GB
  • A micro USB cable (to Power up the Pi)
  • A computer with an internet connection.

Installing OS into the Raspberry Pi

You need to download a tool called Raspberry Imager from here. There is also a 45-sec video to guide you through the setup from the official Raspberry Pi.

So, now that you know how to install an OS, how do you pick one for the Pi? I'm going to use it as a web server, so the GUI isn't important to me. Instead of the full desktop version of the Raspberry Pi OS, I've installed the Lite version of it, which is a headless version of the operating system.

image.png

Connecting to Pi

After the OS has been mounted on the SD card. Now is the time to insert the SD card into the Pi and configure the OS to enable us to use the Pi without a monitor. Connect the Pi to the laptop using a USB cable and insert the USB pin into the USB port. Do not unplug the Pi from the laptop yet.

image.png

Things to do -

Make these changes in config.txt, add the line dtoverlay=dwc2 at the end of the file (ref).

image.png

Change the cmdline.txt file add this small text modules-load=dwc2,g_ether in between as shown below. dwc2 is an upstream driver which turns the pi into gadget mode, which can provide us with the ethernet over the USB connection and g_ether is a gadget driver, which will behave as USB over ethernet.

image.png

Tip: take a backup of cmdline and config, if in the process you mess up, you have a way to start fresh

Enabling SSH-ing into the Pi

To be able to ssh, we need to put an empty ssh file(no extensions required). Keep in mind that it should have UNIX file endings if you are creating the file on a Windows PC.

image.png Here is the ssh file I have created using Notepad++.

Connecting to a Wifi network

There is a way to make our Pi automatically connect to the Wifi network we specify. To do so, create a file called wpa_supplicant.conf and paste the contents provided below into it using Unix File endings. These UNIX file endings are causing me a lot of anxiety because I had to spend a lot of time finding out what went wrong.

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

network={
scan_ssid=1
ssid="<Your Wifi Name>"
psk="<Your Wifi Password>"
}

SSH-ing into the Pi

⚠ Don't Unplug the Pi from the PC yet! ⚠

Until now, the most intriguing thing is the steps following. Proceed to the next step using PuTTY or any other ssh client with raspberrypi (If an error occurs, use raspberrypi.local as the hostname). A terminal window appears, prompting us to enter the username which will be pi and the password will be raspberry . If it prompts you with a warning, press 'Yes' and continues.

image.png

Update the packages, using sudo apt update.

Run sudo raspi-config, which will bring you to the menu shown below. We make two improvements here that are important for a server -

  1. Make the Pi auto-login whenever it boots up.
  2. Make the Pi be ssh-able over wifi.

image.png

image.png

Your portable server is now ready! and you can now unplug the Pi from your PC. In the next part, I'll guide you through how to run a hello world app using express.js on the Pi.

Tips -

  1. You can check if your raspberry Pi is able to connect to the Wifi, with help of a tool called Angry IP Scanner. And you'll find the hostname raspberrypi in the list generated.

image.png

Cover Image credits: Me 😀

 
Share this