Turn a Raspberry Pi Zero W into a Tiny Web Server with SSH and Ngrok
This guide explains how to set up a Raspberry Pi Zero W as a miniature Linux server, flash Raspbian Stretch Lite onto an SD card, enable headless SSH access, configure Wi‑Fi, install and run Nginx, and expose the service to the public internet using Ngrok, while also covering useful system optimizations.
Raspberry Pi (RPi) is a credit‑card‑sized Linux‑based computer; the Zero W model is about one‑third the size of a 3B+ board while still offering a 1 GHz BCM2835 CPU, 512 MB RAM, Wi‑Fi/Bluetooth, micro‑USB power and OTG, mini‑HDMI, GPIO pins, and a micro‑SD slot for the OS.
1. Prepare the SD Card
16 GB or 32 GB SanDisk micro‑SD card
Standard USB‑A to micro‑USB data cable (not Type‑C)
SD card formatter (e.g., SDFormatter)
Win32DiskImager for flashing the image
Raspbian Stretch Lite image (official, no desktop)
Download the raspbian‑stretch‑lite.img (≈1.7 GB) from the Raspberry Pi website, unzip the .zip archive, and locate the .img file.
2. Flash the Image
sudo win32diskimagerInsert the micro‑SD card into a reader, select the .img file in Win32DiskImager, choose the correct device, and click “Write”. After completion, safely eject the card.
3. Enable Headless SSH and Wi‑Fi
After flashing, the card will have a small boot partition (≈40 MB) visible on Windows. Do not remove the card yet.
Create an empty file named ssh (no extension) in the boot partition to enable SSH on first boot.
Create wpa_supplicant.conf in the same partition with the following content (replace with your SSID and password):
country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="YOUR_WIFI_SSID"
psk="YOUR_WIFI_PASSWORD"
}This makes the Zero W connect to your Wi‑Fi automatically, allowing SSH access without a monitor.
4. Boot and Find the IP Address
Insert the card into the Zero W, power it via the micro‑USB cable (5 V 1 A). After a few minutes the LED will become steady. Check your router’s DHCP table to locate the Pi’s IP (e.g., 192.168.0.104).
5. SSH into the Device
Use an SSH client such as PuTTY:
ssh [email protected]
Password: raspberryLogin succeeds, confirming the system is running.
6. System Optimizations
Change APT sources to a domestic mirror (e.g., USTC) for faster downloads:
sudo nano /etc/apt/sources.list
# replace with:
deb http://mirrors.ustc.edu.cn/raspbian/raspbian/raspbian stretch main contrib non-free rpi sudo nano /etc/apt/sources.list.d/raspi.list
# replace with:
deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian stretch main uiUpdate packages: <code>sudo apt-get update sudo apt-get upgrade </code> Set timezone to Shanghai: <code>sudo dpkg-reconfigure tzdata </code> Enable SSH on boot (two methods): Run sudo raspi-config , go to *Interfacing Options → SSH* and enable. Or edit /etc/rc.local and add /etc/init.d/ssh start before the exit 0 line.
7. Install Nginx
sudo apt-get install nginx
sudo /etc/init.d/nginx start # start
sudo /etc/init.d/nginx restart # restart
sudo /etc/init.d/nginx stop # stopBrowse to the Pi’s IP (e.g., http://192.168.0.104 ) to see the default Nginx page.
8. Expose the Service via Ngrok (Intranet Penetration)
To make the web service reachable from the public internet, use a tunneling tool such as Ngrok. The author tested three Chinese‑friendly services (ittun, sunny, natapp) and chose the ittun arm binary because it runs on the Zero W. Download the appropriate ngrok_arm binary, configure it with your auth token, and start a tunnel: <code>./ngrok http 80 </code> The tunnel URL (e.g., http://zerow.ittun.com/ ) forwards traffic to the Pi’s Nginx server. The author runs Ngrok inside a screen session so it stays alive after logout. 9. Performance Note After running Nginx and Ngrok together, the Zero W still has about 250 MB free RAM, CPU temperature stays between 37‑39 °C, and the system runs stably for days.
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.
