How to Set Up a Raspberry Pi as a Personal Server

Source | Peng Xiaodai
Link | segmentfault.com/a/1190000021143144

No.1

What is Raspberry Pi?

Raspberry Pi (Chinese name: 树莓派, abbreviated as RPi or RasPi) is a microcomputer designed for learning computer programming, about the size of a credit card, and its system is based on Linux. With the release of Windows 10 IoT, we can also run Windows on Raspberry Pi.
Since its launch, it has been pursued by many computer enthusiasts and makers, and it was once hard to find. Despite its “small” appearance, it has a powerful “heart” with features like video and audio, making it a perfect example of the saying, “A sparrow may be small, but it has all the vital organs.”

1. Understanding in My Words

In my words, Raspberry Pi is a host that you can connect to external displays, keyboards, mice, USB drives, etc. Because it is very small and has many serial ports and external ports, you can directly access many low-level hardware components.

2. Models Available on the Market

The most common model on the market is the Raspberry Pi 3 Model B+, which you can find a lot of on Taobao. The price for just the motherboard (without any peripherals) is around 230+, which is a bit pricey and exceeds my budget, so I continued looking for a cheaper option and finally found a Raspberry Pi for 100+.

3. Raspberry Pi Zero W

Raspberry Pi Zero W is a mini version of Raspberry Pi, only 1/3 the size of the 3B+. After receiving it, you will find that it is indeed super small and super cute. Below is a picture of my actual product, so you can see how mini it really is.
How to Set Up a Raspberry Pi as a Personal Server
You can see that the top is an ordinary black pen, followed by a plug-and-play external WiFi adapter, then a USB card reader, and at the bottom is our protagonist today, the Zero W. It is indeed super small, isn’t it? It perfectly exemplifies the saying, “A sparrow may be small, but it has all the vital organs.”
The main specifications of the Zero W are as follows:
• BCM2835 processor, 1GHz clock speed, 512MB RAM
• BCM43438 WiFi/BT chip
• micro-USB power interface
• micro-USB OTG interface
• miniHDMI port
• Composite video and reset expansion interface
• Fragile CSI camera interface
• micro-SD card slot for the operating system
• 40-pin GPIO expansion interface
• Dimensions: 65mm * 30mm
Don’t be fooled by its single-core CPU and 512MB RAM; you might think it can’t do much, but its performance is actually quite good. It is really a small case to run a website.

4. More About Raspberry Pi

For more Raspberry Pi models or usage tutorials, you can visit the Raspberry Pi Lab website, which has abundant resources.

No.2

Installing the Operating System on Raspberry Pi Zero W

1. Preparation

You may need to prepare the following items in advance:
• A SanDisk memory card of 16GB or 32GB (Note: it is the small type used in older mobile phones)
• An ordinary USB Android data cable (not type-C)
• USB drive formatting tool (recommended: SDFormatter)
• System burning tool (Win32DiskImager)
• Raspberry Pi operating system (can be downloaded from the official website)
I used the Raspbian Stretch Lite system image, which is officially made, and the lite version has no desktop, only a black console, which is small in size and saves performance and memory.
The versions with “desktop” in the name have a desktop UI, which may be more friendly for those unfamiliar with Linux systems, but they are larger in size and consume more resources.
How to Set Up a Raspberry Pi as a Personal Server

2. Step One: Download the System Image

After downloading the system image you need, as shown in the image below:
How to Set Up a Raspberry Pi as a Personal Server
At first, there is only a zip compressed file, which is about 360MB in size. You need to extract it to obtain the folder shown in the image above.
How to Set Up a Raspberry Pi as a Personal Server
Then, enter the folder to see an img image, which is about 1.7GB in size.
PS: This is the official Raspbian image; if it’s another third-party image, the compressed file after extraction may not be an img image. In such cases, please search for a solution on Baidu.

3. Use Win32DiskImager to Write the Image to the Memory Card

How to Set Up a Raspberry Pi as a Personal Server
Insert the memory card into the card reader, then into the computer.
Open the Win32DiskImager software, select the img image, choose your USB drive as the device, and then click write. Once the writing is complete, a success prompt will pop up.
PS: In the image above, I did not select a device because it was not inserted into the card reader; it’s just for demonstration.

4. Modify the Boot Partition Files

Don’t rush to remove the card reader. At this point, we can see that the USB drive only has a partition named boot, which may only be about 40MB in size. Don’t worry because Windows does not recognize the other partitions of the Linux system on the memory card.

4.1 Create the SSH File

Since our Zero W has a mini HDMI interface, but I don’t need a screen, I need to use SSH to connect to the system on the Zero W, so I need to enable SSH on the first boot.
We enter the boot partition and create a file named ssh. Make sure there is no file extension!!!! Also, do not write anything inside it!!

4.2 Create the wpa_supplicant.conf File

Since SSH connection requires an IP address, we need to ensure that the Zero W automatically connects to WiFi on the first boot, allowing it to be on the same local area network as our computer, so we can connect to the system on the Zero W via SSH.
Similarly, in the boot partition, create a file named wpa_supplicant.conf, and write the following content into it and save:
country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="YourWiFiName"
psk="YourWiFiPassword"
}

5. Assemble Our Minimal Host and Connect

Take the memory card out of the card reader and insert it into the Zero W, then connect it to power using an ordinary USB Android data cable (5V1A).
Wait a few minutes; during this time, the indicator light on our Zero W will flash continuously, which is normal. Wait until the indicator light stays on, then check the router for the Raspberry Pi’s IP address.
How to Set Up a Raspberry Pi as a Personal Server
We can see that our Zero W’s IP is 192.168.0.104. Now use an SSH connection tool (recommended: PuTTY) to connect to the Raspberry Pi. The initial account is pi, and the password is raspberry.
How to Set Up a Raspberry Pi as a Personal Server
Connection successful, as shown in the image above. This means our system has been installed correctly.
PS: If you are using a mobile phone to create a hotspot as a router, you can download an app called Android Terminal on your phone, then enter the command ip neigh to check the IP information of devices connected to your phone.

6. Optimize Our Raspberry Pi System

6.1 Modify the Source

Because the foreign sources are slow to connect from within China, we need to change to domestic sources. I modified it to the sources from USTC.

6.1.1 Modify the sources.list File

sudo nano /etc/apt/sources.list
-- Comment out other contents and add the following:
deb http://mirrors.ustc.edu.cn/raspbian/raspbian/raspbian stretch main contrib non-free rpi

6.1.2 Modify the raspi.list File

sudo nano /etc/apt/sources.list.d/raspi.list
-- Comment out other contents and add the following:
deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian stretch main ui


6.1.3 Execute Update

sudo apt-get update
sudo apt-get upgrade

6.2 Modify the Time Zone

sudo dpkg-reconfigure tzdata
Find Asia, then select Shanghai to complete.

6.3 Enable SSH on Boot

First method:
sudo raspi-config
Enter and select interfacing option, then find SSH and press enter to enable it.
Second method:
After starting the SSH service in the terminal command line, if the system is restarted or shut down, the SSH service is turned off by default and needs to be manually started again. To make it easier, you can set the SSH service to start automatically on boot by opening the /etc/rc.local file and adding the following before the exit 0 statement:/etc/init.d/ssh start
I recommend trying both methods; in my case, it worked.

7. 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 stop
Open a browser and visit 192.168.0.104 (your Raspberry Pi IP address), and you should see the Nginx page, indicating that it is installed correctly.
I uploaded my blog, as shown in the image below:
How to Set Up a Raspberry Pi as a Personal Server
The page is displayed normally, but this can only be seen within the local network (LAN). What should I do if I want everyone to access it?

8. NAT Traversal

NAT traversal means exposing the web application on the local network (local) to the public internet through NAT, allowing others to access it.
Currently, NAT traversal is mainly achieved through ngrok and frp, both of which are very useful. In China, free versions of ngrok include ittun, sunny, and natapp. The first two allow custom domain names, while the latter requires a VIP version for custom domain names.
I have tried all three, and I found that the arm version of the ngrok client from sunny does not run on my Raspberry Pi. Both ittun and natapp’s ngrok work fine, and since I need a custom domain name, I used the ngrok_arm version from ittun.
The usage methods for these three are detailed on their official websites, so please check them out.
This is a screenshot when it is running normally. You can access it at http://zerow.ittun.com/…
How to Set Up a Raspberry Pi as a Personal Server
How to Set Up a Raspberry Pi as a Personal Server
Since ngrok needs to run in the background, I used a screen session so it can run in the background. However, I have not yet implemented auto-start, so in case of power failure or network disconnection, I must manually run ngrok, which is a current pain point that hasn’t been resolved.

9. More

Raspberry Pi can do much more than just run a website; there are many features waiting for your development. You can check out the Raspberry Pi Lab for many practical tutorials written by various experts.
The status information for this Zero W is as follows:
How to Set Up a Raspberry Pi as a Personal Server
As shown, I have enabled both Nginx and ngrok services, with 250MB of memory remaining, which is quite comfortable. The CPU temperature is also not high, running between 37-39 degrees Celsius for the past two days.

Leave a Comment