How to Turn a Raspberry Pi Zero W into a Tiny Web Server with SSH and Ngrok
This step‑by‑step guide shows how to choose a Raspberry Pi Zero W, flash Raspbian Stretch Lite onto a micro‑SD card, enable headless SSH, configure Wi‑Fi, replace apt sources, install and start nginx, and expose the site to the internet using ngrok, while noting performance and troubleshooting tips.
Introduction
The author discovered the Raspberry Pi and decided to document the entire process of turning a low‑cost Zero W model into a functional headless server.
What Is a Raspberry Pi?
Raspberry Pi (RPi) is a credit‑card‑sized Linux‑based computer designed for learning programming. The Zero W variant is about one‑third the size of a 3B+ model, featuring a 1 GHz BCM2835 CPU, 512 MB RAM, Wi‑Fi/BT chip, micro‑USB power and OTG ports, mini‑HDMI, CSI camera connector, 40‑pin GPIO, and a micro‑SD slot.
Preparing the Zero W
Required Materials
16 GB or 32 GB SanDisk micro‑SD card
Standard USB‑A to micro‑USB cable (not Type‑C)
SDFormatter utility
Win32DiskImager utility
Raspbian Stretch Lite image (official)
Flashing the OS Image
Download the official Raspbian Stretch Lite zip (≈360 MB), unzip it to obtain a .img file (~1.7 GB), then write the image to the SD card with Win32DiskImager.
After writing, the card shows a single boot partition (~40 MB) visible on Windows.
Enabling Headless SSH
Create an empty file named ssh (no extension) in the boot partition to enable SSH on first boot.
Configuring Wi‑Fi
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_name"
psk="your_wifi_password"
}First Boot and Basic Setup
Insert the card into the Zero W, power it with a 5 V 1 A USB cable, and wait for the LED to become steady. Locate the device’s IP address on the router (e.g., 192.168.0.104) and SSH in with user pi and password raspberry.
System Optimisation
Changing APT Sources
Replace the default overseas mirrors with the USTC mirror for faster downloads in China.
sudo nano /etc/apt/sources.list
# comment existing lines and add:
deb http://mirrors.ustc.edu.cn/raspbian/raspbian stretch main contrib non-free rpi 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 upgradeSetting Timezone
sudo dpkg-reconfigure tzdata
# select Asia → ShanghaiEnabling SSH on Boot
Method 1: run sudo raspi-config, navigate to Interfacing Options → SSH , and enable it.
Method 2: edit /etc/rc.local and insert /etc/init.d/ssh start before the exit 0 line.
Installing Nginx
# install
sudo apt-get install nginx
# start
sudo /etc/init.d/nginx start
# restart
sudo /etc/init.d/nginx restart
# stop
sudo /etc/init.d/nginx stopVisiting http://192.168.0.104 in a browser shows the default Nginx page, confirming a successful installation.
Exposing the Server to the Internet (Port‑Forwarding)
To make the site reachable from outside the LAN, the author used a tunnelling service. Both ngrok and frp are mentioned; the author chose the ngrok_arm client from the ittun service because it supports custom domains.
Running ngrok in a screen session keeps it alive after logout, but the author notes that automatic startup on reboot is still a missing feature.
Final Remarks
The Zero W now runs nginx and ngrok simultaneously, with about 250 MB free RAM and CPU temperatures staying between 37 °C and 39 °C after two days of continuous operation.
Beyond hosting a simple website, the Raspberry Pi platform offers many more possibilities; readers are encouraged to explore additional tutorials on the Raspberry Pi Lab site.
Java Web Project
Focused on Java backend technologies, trending internet tech, and the latest industry developments. The platform serves over 200,000 Java developers, inviting you to learn and exchange ideas together. Check the menu for Java learning resources.
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.
