Fundamentals 10 min read

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.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
How to Set Up and Optimize a Raspberry Pi Zero W as a Minimal Web Server

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):

<code>country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="your_wifi_name"
    psk="your_wifi_password"
}
</code>

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 :

<code>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 upgrade
</code>

Set 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:

<code># 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 stop
</code>

Access 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:

<code>ngrok http 80
</code>

Visit 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.

linuxnginxWeb ServerIoTRaspberry PiSSHZero W
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.