How to Set Up a Headless Raspberry Pi Zero W Web Server with Nginx and Ngrok
This step‑by‑step guide shows how to prepare a Raspberry Pi Zero W, flash a Raspbian Lite image, enable SSH and Wi‑Fi, install and configure Nginx, and expose the device to the internet using Ngrok, complete with commands, configuration files and screenshots.
Introduction
The author discovered the Raspberry Pi and decided to document the whole process of turning a Raspberry Pi Zero W into a tiny headless web server.
What is a Raspberry Pi?
Raspberry Pi (RPi) is a credit‑card‑sized Linux‑based computer. The Zero W is a mini version roughly one‑third the size of a Model 3 B+, featuring 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.
BCM2835 processor, 1 GHz
512 MB RAM
BCM43438 Wi‑Fi/BT chip
Micro‑USB power & OTG
Mini‑HDMI port
Composite video & reset header
CSI camera connector
Micro‑SD card slot
40‑pin GPIO header
Dimensions: 65 mm × 30 mm
Preparation
Before starting, gather the following items:
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 (download from the official Raspberry Pi website)
Download and Extract the OS Image
Download the desired Raspbian image, unzip the .zip file, and locate the .img file (about 1.7 GB).
Write the Image to the SD Card
Insert the micro‑SD card into a reader, launch Win32DiskImager, select the .img file, choose the correct device, and click Write . After completion, a success dialog appears.
Configure the Boot Partition
Create an empty ssh file
In the boot partition, create a file named ssh with no extension and leave it empty. This enables SSH on first boot.
Create wpa_supplicant.conf
Also in boot, create wpa_supplicant.conf with the following content (replace with your Wi‑Fi 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"
}Assemble and Power the Device
Insert the prepared SD card into the Zero W, connect power via the micro‑USB cable (5 V 1 A), and wait for the indicator LED to become steady.
Find the IP Address and SSH In
Check your router’s client list to locate the Pi’s IP (e.g., 192.168.0.104). Then connect with an SSH client (default user pi, password raspberry), for example using PuTTY.
System Optimisation
Replace APT sources with a Chinese mirror
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 ui sudo apt-get update
sudo apt-get upgradeSet timezone to Shanghai
sudo dpkg-reconfigure tzdata
# select Asia → ShanghaiEnable SSH on boot
Method 1: run sudo raspi-config, go to *Interfacing Options → SSH* and enable it.
Method 2: edit /etc/rc.local and add before exit 0:
/etc/init.d/ssh startInstall 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 stopOpen a browser and navigate to the Pi’s IP; the default Nginx welcome page should appear.
Expose the Server to the Internet (Ngrok)
Use a tunneling service such as ngrok (ittun’s ARM build works on the Zero W). Run the client in a screen session so it stays alive after logout. Example command (replace with your token):
./ngrok tcp 80The service provides a public address like zerow.ittun.com that forwards traffic to the Pi.
Current Status
The Pi runs both Nginx and ngrok, with about 250 MB free RAM and CPU temperature stable around 37‑39 °C after two days of operation.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential 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.
