How to Turn a Raspberry Pi Zero W into a Tiny Web Server with SSH and Ngrok
This guide walks you through understanding what a Raspberry Pi Zero W is, preparing the hardware, flashing Raspbian Stretch Lite, configuring SSH and Wi‑Fi, installing Nginx, enabling remote access via Ngrok, and optimizing the system for lightweight web hosting.
Introduction
The author discovered the Raspberry Pi and decided to document the whole process of turning a Raspberry Pi Zero W into a functional mini‑host.
What is a Raspberry Pi?
Raspberry Pi (RPi) is a credit‑card‑sized Linux‑based micro‑computer designed for learning programming. Despite its small size, it supports video, audio, GPIO, and many peripherals.
Understanding in My Words
It is essentially a tiny PC that can connect to a monitor, keyboard, mouse, USB drives, etc., and provides numerous serial and GPIO interfaces for low‑level hardware access.
Available Models
The most common model on the market is the 3B+ version, priced around 230 CNY for the bare board.
Raspberry Pi Zero W
The Zero W is a miniature version, about one‑third the size of a 3B+. It is extremely compact and affordable (≈100 CNY). Below are its key 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 & reset pins
CSI camera interface
Micro‑SD slot for OS
40‑pin GPIO header
Dimensions: 65 mm × 30 mm
Despite its modest CPU and memory, it can comfortably run a small website.
Installing the OS on Zero W
1. Preparation
16 GB or 32 GB SanDisk micro‑SD card
Standard USB‑type A cable (not Type‑C)
SD formatting tool (e.g., SDFormatter)
Image writing tool (Win32DiskImager)
Raspberry Pi OS image (download from the official site)
The author used Raspbian Stretch Lite , a lightweight, console‑only image.
2. Download the Image
After downloading the zip (≈360 MB), extract it to obtain an
.imgfile (~1.7 GB).
3. Write the Image to the SD Card
Insert the card into a reader, open Win32DiskImager, select the
.imgfile, choose the correct device, and click “Write”.
4. Modify the Boot Partition
4.1 Create an empty ssh file
To enable SSH on first boot, create a file named
ssh(no extension, no content) in the
bootpartition.
4.2 Create wpa_supplicant.conf
Add the following content (replace with your Wi‑Fi SSID and password) to let the Pi 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 prepared SD card into the Zero W, connect power via the USB cable, and wait for the LED to become steady. Then locate the Pi’s IP address on your router.
6. System Optimization
6.1 Change Package Sources
Replace the default sources with a faster domestic mirror (e.g., USTC):
<code>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
</code> <code>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
</code> <code>sudo apt-get update
sudo apt-get upgrade
</code>6.2 Set 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 the
exit 0line in
/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>Access
http://<span>your_pi_ip</span>to verify the web server.
8. Enable Internet‑Facing Access (Ngrok)
Use a tunneling service such as ngrok, frp, or domestic alternatives (ittun, sunny, natapp) to expose the local Nginx service to the public internet. The author used the
ngrok_armclient from ittun.
9. Final Status
The Zero W runs both Nginx and Ngrok, with about 250 MB free RAM, CPU temperature stable around 37‑39 °C after two days of operation.
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.