How to Set Up and Optimize a Raspberry Pi Zero W as a Minimal Web Server
This tutorial walks through the basics of Raspberry Pi Zero W hardware, prepares a micro‑SD card with Raspbian Lite, enables SSH and Wi‑Fi on first boot, updates the system sources, installs and configures nginx, and finally exposes the web service to the Internet using ngrok for seamless remote access.
Reading font
Raspberry Pi (RPi) is a credit‑card‑sized Linux‑based single‑board computer; the Zero W model is a miniature version with a 1 GHz BCM2835 CPU, 512 MB RAM, Wi‑Fi/Bluetooth, micro‑USB, mini‑HDMI, GPIO pins and a micro‑SD slot.
1. Prepare the SD card – download the official Raspbian Stretch Lite image, unzip it to obtain a .img file, and write the image to a 16 GB or 32 GB SanDisk micro‑SD card using Win32DiskImager.
2. Enable SSH and Wi‑Fi – after flashing, the boot partition is visible on Windows. Create an empty file named ssh (no extension) to enable SSH on first boot. Then create wpa_supplicant.conf 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"
}3. First‑boot configuration – insert the card into the Zero W, power it via a micro‑USB cable, and locate its IP address on your router (e.g., 192.168.0.104). Connect with SSH (default user pi, password raspberry).
4. System optimisation – replace the default Debian mirrors with a faster domestic source (e.g., USTC) by editing /etc/apt/sources.list and /etc/apt/sources.list.d/raspi.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 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 the timezone to Shanghai with sudo dpkg-reconfigure tzdata, and ensure SSH starts on boot (either via raspi-config → Interfacing Options → SSH, or by adding /etc/init.d/ssh start before exit 0 in /etc/rc.local).
5. Install a web server – install 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 stopAccess the server locally via the Pi’s IP address to verify the default nginx page.
6. Expose the service externally – use a tunnelling tool such as ngrok (or frp). After registering, download the ARM version of ngrok, configure it with your auth token, and run it in a screen session to keep it alive:
ngrok http 80Visit the provided public URL (e.g., http://zerow.ittun.com/) to see the site from anywhere. Note that automatic startup of ngrok is not covered and must be added manually.
The Zero W runs comfortably with ~250 MB free RAM and CPU temperatures around 37‑39 °C even after two days of continuous nginx and ngrok operation.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.