Turn a Raspberry Pi Zero W into a Mini Web Server with SSH and Ngrok
This guide explains what a Raspberry Pi Zero W is, lists its hardware specs, walks through downloading and flashing Raspbian Lite, enabling head‑less SSH and Wi‑Fi, configuring apt sources, installing and testing nginx, and exposing the server to the internet via an ngrok tunnel.
Raspberry Pi (RPi) is a credit‑card‑sized Linux‑based computer popular among hobbyists; the Zero W model is a miniature version roughly one‑third the size of a Pi 3 B+ while retaining most interfaces.
Hardware specifications of the Zero W
BCM2835 1 GHz processor, 512 MB RAM
BCM43438 Wi‑Fi / Bluetooth chip
Micro‑USB power and OTG ports
Mini‑HDMI port
Composite video and reset pins
CSI camera connector
Micro‑SD slot for the OS
40‑pin GPIO header
Dimensions: 65 mm × 30 mm
The board is powerful enough to run a small website despite its modest CPU and memory.
Preparing the SD card
16 GB or 32 GB SanDisk micro‑SD card
Standard USB‑A to micro‑USB cable (not Type‑C)
SD card formatter (e.g., SDFormatter)
Win32DiskImager (or similar flashing tool)
Raspbian Stretch Lite image (official, no desktop)
Download the raspbian‑stretch‑lite.zip file (≈360 MB), unzip it, and locate the .img file (≈1.7 GB).
Flashing the image
Insert the SD card into a reader, open Win32DiskImager, select the .img file, choose the correct device, and click “Write”. After completion, safely eject the card.
Enabling head‑less SSH and Wi‑Fi
After flashing, the card will have a single boot partition (≈40 MB) visible on Windows. In this partition:
Create an empty file named ssh (no extension) to enable the SSH daemon on first boot.
Create a file named wpa_supplicant.conf with the following content, replacing YOUR_SSID and YOUR_PASSWORD with your Wi‑Fi credentials:
country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="YOUR_SSID"
psk="YOUR_PASSWORD"
}This makes the Zero W connect to your Wi‑Fi automatically, allowing SSH access without a monitor.
Booting and connecting
Insert the SD card into the Zero W, power it via the micro‑USB cable, and wait a few minutes. The board’s LED will flash during boot and stay solid when ready. Locate the Pi’s IP address on your router (e.g., 192.168.0.104) and connect with an SSH client (default user pi, password raspberry).
System optimisation
Because foreign apt mirrors are slow in China, replace them with a domestic mirror (e.g., USTC). Edit /etc/apt/sources.list:
sudo nano /etc/apt/sources.list
# comment existing lines and add:
deb http://mirrors.ustc.edu.cn/raspbian/raspbian/raspbian stretch main contrib non-free rpiAlso edit /etc/apt/sources.list.d/raspi.list:
sudo nano /etc/apt/sources.list.d/raspi.list
# comment existing lines and add:
deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian stretch main uiUpdate the package index and upgrade installed packages:
sudo apt-get update
sudo apt-get upgradeSet the correct timezone (e.g., Asia/Shanghai):
sudo dpkg-reconfigure tzdataEnsuring SSH starts on boot
Method 1: run sudo raspi-config, navigate to “Interfacing Options → SSH”, and enable it.
Method 2: add /etc/init.d/ssh start before the exit 0 line in /etc/rc.local so the service launches automatically after a reboot.
Installing nginx
# install nginx
sudo apt-get install nginx
# start the service
sudo /etc/init.d/nginx start
# restart if needed
sudo /etc/init.d/nginx restart
# stop the service
sudo /etc/init.d/nginx stopOpen a browser on a device in the same LAN and navigate to the Pi’s IP (e.g., http://192.168.0.104) to see the default nginx page.
Exposing the server to the internet
Use an ngrok‑compatible tunnel service. The author tested three providers (ittun, sunny, natapp) and chose the ittun arm binary because it runs on the Zero W. After registering, run the ngrok client (e.g., ngrok http 80) inside a screen session so it stays alive after logout.
When the tunnel is active, the public URL (e.g., http://zerow.ittun.com/) forwards traffic to the Pi’s nginx server, making the site reachable from anywhere.
Note: the tunnel must be restarted manually after power or network loss; automatic startup is a known limitation.
Result
After the above steps, the Zero W runs a lightweight web server, accessible locally and via an ngrok public URL, with about 250 MB free RAM and CPU temperature stable around 37‑39 °C after two days of continuous operation.
Further exploration
The Zero W can be used for many other projects; the Raspberry Pi community provides abundant tutorials and examples.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
