Home / Tutorials / Raspberry Pi Tutorial / Setting up a Raspberry Pi Zero Web Server
Raspberry Pi Zero Web Server

Setting up a Raspberry Pi Zero Web Server

I finally got my hands on a Raspberry Pi Zero W and started the usual tinkering. For those unaware, this smaller version of the Raspberry Pi packs a 1 GHz CPU and 512 MB of RAM. But what excites me the most is the WiFI and Bluetooth connectivity that comes with the “W” version. Let's build a Raspberry Pi Zero web server!

Getting Started

The Raspberry Pi Zero W has two micro-usb ports, one mini HDMI port, the usual GPIO pins (but with no pins soldered), micro-SD card slot and display connector.

Raspberry Pi Zero W

You need at least a micro-usb cable to power-up the board, like the older versions. I had no problem powering up the Pi Zero W from my computer’s USB port.

As for the image, you can download the latest from here. As of this writing (February 2018), the latest image is Raspbian Stretch. If you need a gui, you go for the “Raspbian Stretch with Desktop” image. If you love working on a terminal screen (like me), go for the “Raspbian Stretch Lite” image.

After downloading, write the image (.img) file to your 8GB microSD card using Win32 Disk Imager. Using Win32 Disk Image is straightforward:

Writing image for Raspberry Pi Zero W using Win32 Disk Imager

When the image has been written, there is a need to setup WiFi and SSH for us to connect to the Raspberry Pi Zero W using a terminal program like Putty.

Setting up WiFi Connection

For the Raspberry Pi Zero W to connect to WiFi on boot, we need to add a file named wpa_supplicant.conf on the SD card image. On Windows, the partition where the image is mounted is named boot.

Create a text file inside boot and named it wpa_supplicant.text. Inside this file, write the following:

Supply your own WiFi name and password on the ssid and psk fields above.

Save the file then rename it to wpa_supplicant.conf.

Enabling SSH

Next, we need to enable SSH which is disabled by default. Enabling ssh only takes creating an empty file named ssh without any extension.

After setting up WiFi and enabling SSH, we should be able to connect to the Raspberry Pi Zero W using Putty.

But first, I need to know the IP address of my Raspberry Pi Zero W. I downloaded nmap for that.

Once nmap is installed, we can run the following command in the Windows command line on a computer.

Here’s my results for nmap (we have a number of computers in the network):

Raspberry Pi Zero W address as per nmap results

My Raspberry Pi Zero’s address is 192.168.1.31!

Now if you don’t want to use nmap, you can just use raspberrypi.local as the host name for your device. I just like using the Pi's IP address, that's all

Open Putty, type your Raspberry Pi Zero’s IP address (or just raspberrypi.local), leave the port as 22 and then click open.

You’ve successfully SSH’d to your Pi!

Raspberry Pi Zero W SSH screen

 

Creating the Web Server

To setup a web server on the Raspberry Pi Zero W, I downloaded Apache:

If the installation is successful, this is what should be on the browser if you go to the Pi’s address (or use http://raspberrypi.local)

Raspberry Pi Zero Web Server default page

The source for the web page above is found at /var/www/html/ and is named index.html. Let’s replace is with something simpler. CD to the mentioned directory and delete index.html

Next, create a new index.html file

Then paste the following code:

 

This is what http://raspberrypi.local looks now:

Raspberry Pi Zero W Hello World

You now have a Raspberry Pi Zero web server! Next, we'll control some objects using this web server.

 

Check Also

Using RFID with Raspberry Pi Zero

Using RFID cards and tags is a cool way to switch on anything, wirelessly. For …

No comments

  1. Use 39 lines of python + a local htm file on the Pi Zero W!

    https://code-maven.com/static-server-in-python (by Gabor Szabo)

    In the script, plug in the home page path (htm file), port number and pi's static IP address.

    I use a custom homepage with over 200 links (bookmarks, custom search engine forms, weather and live date and time) on a Pi Zero W for all our android and iOS devices.

    The Pi Zero W also handles ad blocking for the household as well! 🙂

Leave a Reply to Cy Vystov Cancel reply

Your email address will not be published. Required fields are marked *