
A personal web server is like a “cloud” that you own and control, rather than a large corporation.
Having your own cloud has many benefits, including customization, free storage, free internet services, access to open-source software, high security, complete control over your content, the ability to change quickly, and a place to experiment with code. Most of these benefits are invaluable, but financially, they can save you over $100 a month.
Building your own web server with Raspberry Pi
I could have chosen AWS, but I prefer complete freedom and controllable security, plus I can learn how these things are set up.
-
Private web hosting: instead of BlueHost or DreamHost
-
Cloud storage: instead of Dropbox, Box, Google Drive, Microsoft Azure, iCloud, or AWS
-
Self-deployed security
-
HTTPS: Let’s Encrypt
-
Analytics: Google
-
OpenVPN: no proprietary internet connection (estimated $7 a month)
Here’s the list of items I used:
-
Raspberry Pi 3 Model B
-
MicroSD card (recommended 32 GB, compatible SD card for Raspberry Pi[1])
-
USB microSD card reader
-
Ethernet cable
-
Router connected to Wi-Fi
-
Raspberry Pi case
-
Amazon Baseus MicroUSB data cable
-
Apple charger
-
USB mouse
-
USB keyboard
-
HDMI cable
-
Monitor (supports HDMI input)
-
MacBook Pro
Step 1: Start the Raspberry Pi
Download the latest release of Raspbian (the Raspberry Pi operating system). The ZIP file for Raspbian Jessie[2] can be used [Footnote 1]. Unzip or extract the downloaded file and copy it to the SD card. Using Pi Filler[3] can make this process easier. Download Pi Filler 1.3[4] or the latest version. After unzipping or extracting the downloaded file, open it, and you should see a prompt like this:
Pi Filler prompt
Make sure the USB card reader is not plugged in at this time. If it’s already plugged in, eject it first. Click “Continue” to proceed to the next step. You will see a file selection interface; choose the Raspberry Pi system files you previously unzipped. Then you will see another prompt as shown in the image:
USB card reader
Insert the MicroSD card (recommended 32 GB, at least 16GB) into the USB microSD card reader. Then connect the USB reader to your computer. You may rename your SD card to “Raspberry” to distinguish it from other devices. Then click “Continue.” Please ensure your SD card is empty, as Pi Filler will erase all content previously on the SD card during the process. If you want to back up the content on the card, it’s best to do so immediately. When you click “Continue,” the Raspbian OS will be written to the SD card. This process will take about one to three minutes. Once the writing is complete, eject the USB reader, remove the SD card, and insert it into the SD card slot of the Raspberry Pi. Connect the power supply, and the Raspberry Pi will boot up on its own. The default login credentials for the Raspberry Pi are:
-
Username: pi
-
Password: raspberry
When the Raspberry Pi first starts up, a configuration interface titled “Setup Options” will pop up, as shown in the image below [Footnote 2]:
Raspberry Pi software configuration setup
Select the option “Expand Filesystem” and press enter [Footnote 3]. I also recommend selecting the second option “Change User Password“. This is particularly important for security. It also personalizes your Raspberry Pi.
In the options list, select the third option “Enable Boot To Desktop/Scratch” and press enter. This will jump to another interface titled “Choose boot option“, as shown in the image below:
Choose boot option
Select the second option “Desktop log in as user ‘pi’ at the graphical desktop” and press enter [Footnote 4]. After completing this operation, you will return to the previous “Setup Options” interface. If you don’t return to the previous interface, select the “OK” button at the bottom of the current interface and press enter.
Once all these operations are completed, select the “Finish” button at the bottom of the current interface and press enter; it will automatically restart. If it doesn’t restart automatically, use the following command in the terminal to reboot.
$ sudo reboot
After the reboot from the previous step, if everything went smoothly, you will enter a desktop environment similar to the one shown below.
Raspberry Pi desktop
Once you enter the desktop, execute the following commands in the terminal to update the Raspberry Pi firmware.
$ sudo apt-get update
$ sudo apt-get upgrade -y
$ sudo apt-get dist-upgrade -y
$ sudo rpi-update
These operations may take a few minutes. Once completed, the running Raspberry Pi is now up to date.
Step 2: Configure the Raspberry Pi
SSH stands for Secure Shell, a cryptographic network protocol that allows you to securely transfer data between your computer and the Raspberry Pi. You can control your Raspberry Pi from the Mac command line without a display or keyboard.
To use SSH, you first need the IP address of your Raspberry Pi. Open the terminal and type:
$ sudo ifconfig
If you are using Ethernet, look at the eth0
section. If you are using Wi-Fi, look at the wlan0
section.
Find the inet addr
followed by an IP address, such as 192.168.1.115, which is the default IP used in this article.
With this address, type in the terminal:
$ ssh [email protected]
For SSH on PC, see [Footnote 5].
When prompted, enter the default password raspberry
, unless you have changed the password previously.
You are now successfully logged in via SSH.
Remote Desktop
Using a GUI (Graphical User Interface) is sometimes easier than using the command line. On the Raspberry Pi command line (using SSH), type:
$ sudo apt-get install xrdp
xrdp supports Microsoft Remote Desktop clients on Mac and PC.
On Mac, search for “Microsoft Remote Desktop” in the App Store. Download it. (For PC, see [Footnote 6].)
Once installed, search for an application called “Microsoft Remote Desktop” on your Mac and open it; you should see:
Microsoft Remote Desktop
Click “New” to create a new remote connection, and fill in the following configuration in the blank space.
Setting up a remote connection
Closing the “New” window will automatically save it.
You should now see the remote connection listed under “My Desktop.” Double-click it.
After a simple loading process, you should see your Raspberry Pi desktop in the window on your screen, as shown below:
Raspberry Pi desktop
Now, you can control your Raspberry Pi without needing an extra mouse, keyboard, or monitor. This is a more lightweight setup.
Static Local IP Address
Sometimes your local IP address 192.168.1.115 may change. We need to make this IP address static. Enter:
$ sudo ifconfig
From the eth0
or wlan0
section, note the inet addr
(current IP of the Raspberry Pi), bcast
(broadcast IP range), and mask
(subnet mask address). Then enter:
$ netstat -nr
Note the destination
and gateway/network
.
Setting up a local IP address
It should look something like this:
net address 192.168.1.115
bcast 192.168.1.255
mask 255.255.255.0
gateway 192.168.1.1
network 192.168.1.1
destination 192.168.1.0
With this information, you can easily set up a static IP. Enter:
$ sudo nano /etc/dhcpcd.conf
Do not touch /etc/network/interfaces
.
All that’s left is to append this content to the bottom of this file, changing the IP to the desired IP address.
interface eth0
static ip_address=192.168.1.115
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
Once you have set the static internal IP address, you need to restart your Raspberry Pi with the following command:
$ sudo reboot
After rebooting, enter:
$ sudo ifconfig
You should now see your Raspberry Pi’s new static configuration.
Static Global IP Address
If your ISP (Internet Service Provider) has given you a static external IP address, you can skip to the port forwarding section. If not, continue reading.
You have set up SSH, remote desktop, and static internal IP address, so now computers on the local network will know where to find your Raspberry Pi. However, you still cannot access your Raspberry Pi from outside the local Wi-Fi network. You need your Raspberry Pi to be publicly accessible from anywhere on the internet. This requires a static external IP address [Footnote 7].
Contact your ISP and request a static external (sometimes called static global) IP address. This can be a very sensitive process. The ISP has the decision-making power, so I would be very careful in handling this. They may refuse your request for a static external IP address. If they deny your request, do not blame them, as this type of request carries legal and operational risks. They particularly do not want customers running medium or large internet services. They may explicitly ask why you need a static external IP address. It’s best to be honest and tell them you plan to host a low-traffic personal website or similar small non-profit internet service. If all goes well, they should create a ticket and call you back in a week or two.
Port Forwarding
This newly acquired static global IP address assigned by your ISP is used to access the router. The Raspberry Pi still cannot be accessed. You need to set up port forwarding to access the Raspberry Pi.
Ports are virtual pathways for information to travel across the internet. You sometimes need to forward ports to allow computers like the Raspberry Pi to access the internet, as it is behind a network router. A video from the VollmilchTV column on YouTube titled What is TCP/IP, ports, routing, Intranet, firewalls, the internet[5] can help you better understand ports.
Port forwarding can be used for applications like Raspberry Pi web servers or VoIP or peer-to-peer downloads. There are over 65,000 ports[6] available, so you can assign a different port for each internet application you build.
The way to set up port forwarding depends on your router. If you have Linksys, Gabriel Ramirez has a video on YouTube titled How to Connect Your Apache Ubuntu Server to the Internet[7] explaining how to set it up. If you don’t have Linksys, please refer to the documentation that came with your router to customize and define the ports to be forwarded.
You need to forward the SSH and remote desktop ports.
If you think you have configured port forwarding, enter the following command to see if it’s working via SSH:
$ ssh pi@your_global_ip_address
It should prompt you for a password.
Check if port forwarding also works for remote desktop. Open Microsoft Remote Desktop. Your previous remote connection settings should be saved, but you need to update the “PC Name” field with the static external IP address (e.g., 195.198.227.116) instead of the static internal address (e.g., 192.168.1.115).
Now, try connecting via remote desktop. It should load simply and display the Raspberry Pi’s desktop.
Raspberry Pi desktop
Now, the Raspberry Pi is accessible from the internet and is ready for advanced projects.
As a bonus option, you can maintain two remote connections to your Pi. One via the internet and the other via LAN (local area network). It’s easy to set up. In Microsoft Remote Desktop, keep one remote connection named “Pi Internet” and another named “Pi Local.” Configure the “PC Name” for Pi Internet to the static external IP address, such as 195.198.227.116. Configure the “PC Name” for Pi Local to the static internal IP address, such as 192.168.1.115. Now, you can choose to connect globally or locally.
If you haven’t watched Gabriel Ramirez’s How to Connect Your Apache Ubuntu Server to the Internet[8], you can check it out as a transition to the second project. It will show you the technical architecture behind the project. In our case, you are using a Raspberry Pi instead of an Ubuntu server. Dynamic DNS sits between the domain name company and your router, which is a part that Ramirez omits. Aside from this subtlety, the video explains how the system works overall. You may notice that this tutorial covers Raspberry Pi setup and port forwarding, which is server-side or backend. Check out the original source covering domain names, dynamic DNS, Jekyll (static HTML generator), and Apache (web hosting) for more advanced projects, which is client-side or frontend.
Footnotes
[1] I do not recommend starting with the NOOBS operating system. I prefer starting with a fully functional Raspbian Jessie operating system.
[2] If “Setup Options” does not pop up, you can always find it by opening the terminal and executing the command:
$ sudo raspi-config
[3] We do this to use all the space available on the SD card as a complete partition. All this is to expand the operating system to fit the entire space on the SD card and can then be used as storage memory for the Raspberry Pi.
[4] We do this because we want to boot into a familiar desktop environment. If we do not do this step, the Raspberry Pi will enter the terminal instead of the GUI each time.
[5]
PuTTY configuration
Download and run PuTTY[9] or other SSH clients for Windows. Enter your IP address in the field as shown in the image above. Keep the default port as 22. Press enter, and PuTTY will open a terminal window prompting you for your username and password. Fill it in and start your remote work on the Raspberry Pi.
[6] If not installed, please download Microsoft Remote Desktop[10]. Search for Microsoft Remote Desktop on your computer. Run it. When prompted, enter the IP address. Next, an xrdp window will pop up asking you for your username and password.
[7] The router has a dynamically assigned external IP address, so theoretically, it can be accessed from the internet temporarily, but you need the help of your ISP to make it permanently accessible. If not, you will need to reconfigure the remote connection every time you use it.
Original article from Mitchell McLaughlin’s Full-Stack Computer Projects[11].
Author’s Biography:
Mitchell McLaughlin – I am a contributor and developer of open networks. I have a wide range of interests, but I particularly enjoy open-source software/hardware, Bitcoin, and programming. I live in San Francisco, and I have some brief work experience with GoPro and Oracle.
via: https://opensource.com/article/17/3/building-personal-web-server-raspberry-pi-3
Author: Mitchell McLaughlin [12] Translator: chenxinlong Proofreader: jasminepeng
This article was originally compiled by LCTT and honorably launched by Linux China

Recommended Articles
< Swipe left and right to view related articles >





Enter the article ID or long press the QR code to reach directly