How to Turn a Raspberry Pi Zero W into a Mini Web Server: Step‑by‑Step Guide
This article walks you through understanding what a Raspberry Pi Zero W is, preparing the hardware, flashing Raspbian Lite, configuring SSH and Wi‑Fi, optimizing the system, installing nginx, and exposing the device to the internet using tools like ngrok.
1. Introduction
I discovered the Raspberry Pi and found it fascinating, so I decided to document the whole process.
2. What is a Raspberry Pi?
Raspberry Pi (RPi) is a credit‑card‑sized Linux‑based micro‑computer designed for learning programming. Despite its tiny size, it offers video, audio, GPIO, and many other functions – truly "small but fully featured".
1. My understanding
Raspberry Pi is essentially a tiny host that can connect to a monitor, keyboard, mouse, USB drives, etc., and provides many serial ports and GPIO for low‑level hardware access.
2. Models on the market
The most common model is the 3‑generation B+, priced around 230 CNY for the board only. I looked for cheaper options and found a model for about 100 CNY.
3. Raspberry Pi Zero W
Zero W is a mini version, roughly one‑third the size of a 3B+. It is extremely small and cute.
The top item is a black pen, followed by a plug‑and‑play Wi‑Fi dongle, a USB card reader, and finally the Zero W board.
Zero W specifications:
BCM2835 processor, 1 GHz, 512 MB RAM
BCM43438 Wi‑Fi/BT chip
Micro‑USB power port
Micro‑USB OTG port
Mini‑HDMI port
Composite video and reset pins
CSI camera interface
Micro‑SD slot for OS
40‑pin GPIO header
Dimensions: 65 mm × 30 mm
Despite its single‑core CPU and 512 MB memory, it can comfortably run a small website.
4. More Raspberry Pi models
For additional models and tutorials, visit the Raspberry Pi Lab website.
3. Installing the OS on Raspberry Pi Zero W
1. Preparation
16 GB or 32 GB SanDisk micro‑SD card
Standard USB‑type‑A cable (not Type‑C)
SD card formatting tool (e.g., SDFormatter)
Image writing tool (Win32DiskImager)
Raspberry Pi OS image (download from the official site)
I used the Raspbian Stretch Lite image, which is a minimal, console‑only version that saves space and resources.
2. Download the system image
After downloading the zip file (≈360 MB), extract it to obtain a folder containing an
.imgfile (~1.7 GB).
3. Write the image with Win32DiskImager
Insert the SD card into a reader, open Win32DiskImager, select the
.img, choose the correct device, and click Write. A success dialog appears when finished.
4. Modify files in the boot partition
4.1 Create an empty ssh file
In the
bootpartition, create a file named
sshwith no extension and no content to enable SSH on first boot.
4.2 Create wpa_supplicant.conf
Also in
boot, create
wpa_supplicant.confwith the following content to connect to Wi‑Fi automatically:
<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>5. Assemble and power up
Insert the SD card into the Zero W, connect power via a USB cable, wait for the LED to stay solid, then find the device’s IP address on your router (e.g., 192.168.0.104) and SSH with username
piand password
raspberry.
When using a phone hotspot, you can run ip neigh in a terminal app to discover the device’s IP.
6. Optimize the system
6.1 Change apt sources (use domestic mirrors)
6.1.1 Edit /etc/apt/sources.list
<code>sudo nano /etc/apt/sources.list
# comment other lines, then add:
deb http://mirrors.ustc.edu.cn/raspbian/raspbian/raspbian stretch main contrib non-free rpi
</code>6.1.2 Edit /etc/apt/sources.list.d/raspi.list
<code>sudo nano /etc/apt/sources.list.d/raspi.list
# comment other lines, then add:
deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian stretch main ui
</code>6.1.3 Update packages
<code>sudo apt-get update
sudo apt-get upgrade
</code>6.2 Change timezone
<code>sudo dpkg-reconfigure tzdata
</code>Select Asia → Shanghai.
6.3 Enable SSH on boot
Method 1:
sudo raspi-config→ Interfacing Options → SSH → Enable.
Method 2: Add
/etc/init.d/ssh startbefore
exit 0in
/etc/rc.local.
7. 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>Visit
http://<your_ip>to see the default page, then upload your site.
8. Expose to the internet (port forwarding)
Use tools like ngrok or frp. I tried several services; the free arm version of ittun’s ngrok works on the Zero W. Run it inside a
screensession so it stays alive in the background.
9. Further possibilities
The Raspberry Pi can do much more than host a website; explore tutorials on the Raspberry Pi Lab for additional projects.
My Zero W status (image):
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.