
A personal web server is a “cloud” that you own and control, rather than a large company.
Having your own cloud has many benefits, including customization, free storage, free internet services, access to open-source software, high security, complete control of your content, the ability to change things quickly, a place to experiment with code, and more. Most of these benefits are immeasurable, 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, and 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 a list of items I used:
-
Raspberry Pi 3 Model B
-
MicroSD card (recommended 32 GB, SD card compatible with 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: Boot up Raspberry Pi
Download the latest release of Raspbian (the operating system for Raspberry Pi). The ZIP package for Raspbian Jessie[2] can be used [footnote 1]. Extract or unzip the downloaded file and copy it to the SD card. Using Pi Filler[3] can make these processes easier. Download Pi Filer 1.3[4] or the latest version. After extracting or unzipping the downloaded file, open it, and you should see a prompt like this:
Pi Filler prompt
Make sure the USB reader is not plugged in at this time. If it is plugged in, eject it first. Click “Continue” to proceed to the next step. You will see an interface allowing you to select a file, choose the Raspberry Pi system files you extracted earlier. Then you will see another prompt as shown in the picture:
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 can 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, you should 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 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 cable to power the Raspberry Pi. The Raspberry Pi will start up on its own. The default login credentials for Raspberry Pi are:
-
Username: pi
-
Password: raspberry
When the Raspberry Pi first boots up, a configuration interface titled “Setup Options” will pop up, just like the image below [footnote 2]:
Raspberry Pi software configuration setup
Select the option “Expand Filesystem” and press enter [footnote 3]. At the same time, I also recommend choosing the second option “Change User Password“. This is especially important for security. It also personalizes your Raspberry Pi.
Select the third option “Enable Boot To Desktop/Scratch” and press enter. This will take you to another interface titled “Choose boot option“, just like 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 do not 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 reboot. If it does not reboot automatically, use the following command in the terminal to reboot.
$ sudo reboot
After the reboot from the previous step, if all steps went smoothly, you will enter a desktop environment similar to the one below.
Raspberry Pi desktop
Once you are in the desktop, execute the following commands in the terminal to update the Raspberry Pi’s 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, your Raspberry Pi will be up to date.
Step 2: Configure Raspberry Pi
SSH stands for Secure Shell, which is an encrypted network protocol that allows you to securely transfer data between your computer and Raspberry Pi. You can control your Raspberry Pi from the command line on your Mac 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.
Look for 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 the following command 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 the command line. Type the following on the command line of the Raspberry Pi (using SSH):
$ sudo apt-get install xrdp
xrdp supports the Microsoft Remote Desktop client for 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” in your Mac and open it, you should see:
Microsoft Remote Desktop
Click “New” to create a new remote connection, filling 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 brief loading time, you should see your Raspberry Pi desktop in the window on your screen, as follows:
Raspberry Pi desktop
Now, you can control your Raspberry Pi without needing an extra mouse, keyboard, or display. 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. Type:
$ sudo ifconfig
From the eth0
or wlan0
section, note the inet addr
(current IP of Raspberry Pi), bcast
(broadcast IP range), and mask
(subnet mask address). Then type:
$ 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. Type:
$ sudo nano /etc/dhcpcd.conf
Do not modify /etc/network/interfaces
.
The remaining task is to append the following content to the bottom of this file, replacing the IP with 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, type in the terminal:
$ sudo ifconfig
You should now see your new static configuration on your Raspberry Pi.
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, please continue reading.
You have set up SSH, remote desktop, and a static internal IP address, so now computers on the local network will know where to find your Raspberry Pi. But you still cannot access your Raspberry Pi from outside your 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 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 there are legal and operational risks involved with this type of request. They especially do not want customers running medium or large internet services. They may explicitly ask why you need a static external IP address. It is best to be honest, 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 within 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 is still inaccessible. You need to set up port forwarding to access the Raspberry Pi.
Ports are virtual pathways through which information travels on the internet. Sometimes you need to forward ports so that computers like your Raspberry Pi can access the Internet because it is behind a network router. A video on VollmilchTV’s channel on YouTube titled What is TCP/IP, ports, routing, Intranet, firewall, 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 to each internet application you build.
The way to set up port forwarding depends on your router. If you have Linksys, Gabriel Ramirez has a YouTube video titled How to connect your Apache Ubuntu server to the internet[7] that explains how to set it up. If you do not have Linksys, please read the documentation that came with your router to customize and define the ports to forward.
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 is working via SSH:
$ ssh pi@your_global_ip_address
It should prompt you for a password.
Check if port forwarding is also working for remote desktop. Open Microsoft Remote Desktop. Your previous remote connection settings should have been 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 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 between global or local connections.
If you haven’t seen the video by Gabriel Ramirez titled How to connect your Apache Ubuntu server to the internet[8], you can check it out as a tutorial for transitioning to the second project. It will show you the technical architecture behind the project. In our case, you are using Raspberry Pi instead of an Ubuntu server. Dynamic DNS sits between the domain name company and your router, which is a detail that Ramirez omitted. 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 the original sources covering domain names, dynamic DNS, Jekyll (static HTML generator), and Apache (web hosting) for more advanced projects, which are 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 of this is to expand the operating system to fit the entire space on the SD card, which 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 another SSH client 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 them in and start your remote work on the Raspberry Pi.
[6] If not installed, download Microsoft Remote Desktop[10]. Search for Microsoft Remote Desktop on your computer. Run it. When prompted, enter the IP address. Next, a window will pop up prompting you for your username and password.
[7] Routers have dynamically assigned external IP addresses, so in theory, it can be accessed from the internet temporarily, but you need your ISP’s help to make it permanently accessible. If not, you will need to reconfigure the remote connection each time you use it.
The original article is from Mitchell McLaughlin’s Full-Stack Computer Projects[11].
Author’s Bio:
Mitchell McLaughlin – I am a contributor and developer for the open web. I have a wide range of interests, but I particularly enjoy open-source software/hardware, Bitcoin, and programming. I live in San Francisco and have had some brief experiences working 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 is originally compiled by LCTT, presented by Linux China
Recommended Articles
< Swipe left and right to view related articles >



Enter article ID or long press the QR code to reach