[Raspberry Pi Barbecue 001] Build a server with your own Raspberry Pi from 0: Operation and Maintenance

Yes, I admit that the title is deliciously written...

It is said that the Raspberry Pi 4B 8GB in my house has been stored for two years, and it is still eating dust~ So I want to use it to do some extraordinary things, such as setting up a server. We used GitHub Pages to build websites before, but this time we use the entire real LAMP environment. Not much to say, let's start to build the operating environment~

(Don’t worry about the electricity bill of the Raspberry Pi, it only costs more than 30 yuan a year (provided that the monitor is not plugged in))

1. Preparations

Some students may think that the Raspberry Pi is for eating. In fact, it is a microcomputer with great performance, which is similar to the A7 on the iPhone 5S, but it is enough for a server. In fact, the biggest feature of the Raspberry Pi is its low power consumption. The 4B model with the strongest configuration costs more than 30 yuan a year for electricity (see the picture above), and the Zero 2 W is only an amazing meal of Shaxian snacks (about 10 fast). Use it to set up a server, it can be said to hang a certain cloud server! ! !

1.1 Hardware Purchase

Families without Raspberry Pi look over here. If the Raspberry Pi is used as a server, you can ask your friends in the United Kingdom of Great Britain and Northern Ireland or the United States of America to help you purchase the Raspberry Pi Zero 2 W, the price is only 15 US dollars (equivalent to less than one RMB) red bills). If you think 15 knives is too expensive, you can buy Zero W, the performance will be a little worse, but the price has reached an astonishing cabbage price: 10 US knives (equivalent to ¥ for two cups of tea). But what if you don't have such friends? No way, the Zero 2 W with less than 100 mics abroad will cost 400 yuan in China. Don’t ask me why, it’s all tariffs~

Then you can only enable the second option: Raspberry Pi 400. This is a keyboard computer with the same configuration as the current Raspberry Pi 4B, and the price is also very stable, 535 yuan, which is half of the Raspberry Pi 4B. Although it is expensive, the configuration is about eight times that of the Zero 2 W above.

After choosing the Raspberry Pi host (be sure to pay for the motherboard, don’t buy free accessories, to prevent being cheated), buy an original Wufu Sanan (5V / 3A) on the official website of the Raspberry Pi as the power supply and a flashlight A 16GB TF card (of course, if you buy a bigger one) from Di or Lexar (you can’t buy other brands) can be used as a hard disk , and a casing + small fan can be used as a heat sink (otherwise your Raspberry Pi can be used as a The barbecue plate is exactly the same as the title) and a Micro-HDMI to HDMI (for 400) or a Mini-HDMI to HDMI (for Zero 2 W and Zero W) and a wired keyboard (must have a cable) server hardware ok !

Of course, you can take the above passage from the students who have Raspberry Pi as wind~

1.2 Popularization of software knowledge

Many people also don't have a deep understanding of servers. In simple terms, we visit some webpages (such as Baidu), and it is maintained by the server behind it. In other words, the server is the website (just understand it). After the server is set up, others can visit you. Pie on the site.

The operating system on the server is also different, not the traditional Windows, but many distributions of Linux. Linux, like Windows, is an operating system, but the Linux we use here only has a command line interface, that is, a black interface for typing code line by line. But don't be intimidated by this, trust me, you will get started with Linux in no time. Here we choose Ubuntu Server, one of the distributions of Linux.

As for the server itself, we use a LAMP environment. LAMP here does not refer to desk lamps, but an acronym for Linux (the above-mentioned operating system) + Apache (server software) + MySQL (database software) + PHP (front-end programming software). If someone who doesn’t know much about this aspect knows a little bit, it’s okay if a senior boss wants to use Nginx, but the configuration file needs to be typed by yourself~

Having said so much, the following will start to prepare the server environment~

2. Operating system installation

Before installation, the software we need to prepare is: balenaEtcher, open the link , select the file whose file name format is balenaEtcher-Portable-1.xxexe  to download, and then run it directly.

Then download the Ubuntu Server system image. Open  the Ubuntu download page , slide down to the Ubuntu Server download page (as shown below), and select the green button Download 64-bit. If your pie is 2Bv1.1 and earlier or Zero W, select Download 32-bit.

Since it is a foreign server, it will take some time to download (of course, a few hundred MB is not too big). After downloading, it is an XZ compressed package, which needs to be decompressed first.

 It is normal to get a file with the suffix .img after decompression~

 Run the balenaEtcher just downloaded, select the first item Flash from file;

Select the .img file you just decompressed, and select Open;

Insert the TF card into the card reader (it must be USB 3.0, otherwise the speed is as slow as a tortoise) and then insert the card reader into the computer, select the second Select Target, find your TF card, tick it, remember not here Choose another USB flash drive, or you won’t have time to cry if the USB flash drive is formatted~

 Finally, back up the important data in your TF card (don't use the new one), and start Flash!

At this time, the recording will start. It is normal to write more than 10MB per second here. If it is lower than this speed, you should check whether your USB interface or card reader is USB 3.0. And in the end, if it says Flash failed, then congratulations you bought a miscellaneous card, and quickly change to a SanDisk or Lexar~ 

Verify that this step can be Skip~; 

 If this interface appears, you're done!

3. SSH connection and initialization

3.1 Raspberry Pi initialization

Connect the Raspberry Pi to the power supply, and then wait for a while until Ubuntu prompts ubuntu login: , connect the wired keyboard, enter ubuntu and press Enter, and then enter ubuntu again and press Enter (the ubuntu entered here is not displayed for some reasons, just touch typing ), then enter ubuntu again and press Enter (still not displayed), then enter the password you want to set for the Raspberry Pi (still not displayed) press Enter, enter the new password again and press Enter (still not displayed), ok!

Next time, if you want to log in, use ubuntu as the user name and use the one you just created~

Then enter the following code and press Enter once~

sudo -i
nano /etc/netplan/50-cloud-init.yaml

After opening the nano editor, add the following code and pay attention to the indentation. There should be four spaces in front of the first line as shown below, and the next few lines should be indented according to the following. Fill in the real wifi name and wifi password:

     wifis:
         wlan0:
             dhcp4: true
             optional: true
             access-points:
                 "wifi名":
                     password:
                         "wifi密码"

 Then press the following shortcut keys to save the settings:

Ctrl + X
y
回车

Then enter the following code and press Enter once~

systemctl enable wpa_supplicant
systemctl start wpa_supplicant
netplan generate
systemctl daemon-reload
netplan apply

Congratulations, Raspberry Pi initialization is complete! You can turn off the display, after all, the display consumes too much power~ Also, from this moment on, your Raspberry Pi is ready to be turned on all the time, the server cannot be turned off~

3.2 SSH connection to Raspberry Pi

SSH is a remote connection protocol. We use SSH here. On the one hand, it is to save the electricity bill of the monitor (much more expensive than the Raspberry Pi). On the other hand, because Ubuntu Server has no desktop environment and browser tools, we set up the Raspberry Pi for convenience (such as Change the source) These steps that involve a lot of resources are more convenient, otherwise your hands will be useless~

Before connecting the Raspberry Pi to the power supply, we must first install the SSH connection software  PuTTY , select the string of links behind 64-bit x86 to download PuTTY~

The installation process will not be repeated, just click Next all the way.

Open the browser 192.168.1.1 to open the router configuration interface (each router may be different, mine is Mercury, but generally the same) click the device list;

Find the IP address under ubuntu, that is the IP address of the Raspberry Pi~

 Open PuTTY, enter the IP address just noted down in the box pointed by the red arrow, and then click Open~

 Point Accept;

 Enter ubuntu and press Enter, enter the password you set just now and press Enter (not displayed), the interface as shown below is ok! Congratulations, the server system is partially built!

3.3 Source change

We will need to download a lot of software to build the server later, and these software need to be downloaded from the foreign ports.ubuntu.com software source. Since foreign servers are very slow, we have to switch to domestic sources~

The software from the domestic source is exactly the same as the software from the foreign source, so there is no need to worry that the software cannot be downloaded after changing the source~

PuTTY enters the following command:

sudo -i
nano /etc/apt/sources.list

Add # in front of each line in the file to comment them out (the ones that have already been added are not used), and then add the following code (right click in the PuTTY window after copying to paste):

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse

(Think about it, if you don't have SSH, you will be exhausted typing these codes)

(If you have a newer version of Ubuntu Server when you read this article, please visit this link to manually select your Ubuntu version and copy~)

These codes replace the software source in ubuntu with the domestic Tsinghua source. Enter the following shortcut key to save and exit~

Ctrl + X
y
回车

 If you encounter Malformed and report an error, you can refer to this article: link

Then enter the following code to update the software sources:

apt-get update && apt-get upgrade

When the Raspberry Pi prompts the following text, enter y and press Enter;

Need to get 245 MB of archives.
After this operation, 77.8 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

 If you feel that the download speed of the Raspberry Pi connected to Wi-Fi is slow, you can plug in a Gigabit network cable (of course, your model must have a network cable port), and the download speed will be so fast that it will fly without affecting the SSH connection.

When this interface appears in your PuTTY, press Tab and press Enter;

 When this command prompt reappears on your ubuntu, it means that the source change is successful!

4. Server LAMP environment installation

4.1 Control your own server

How do you control your own server if you don't have Linux development experience? Here is an article I wrote before, which contains some simple Linux instructions, Linux common instructions https://rpiws.github.io/2022/03/31/%E6%A0%91%E8%8E% 93%E6%B4%BE%E5%85%A5%E6%89%8B004%EF%BC%9A%E7%BB%88%E7%AB%AF%E5%9F%BA%E6%9C%AC% E5%91%BD%E4%BB%A4

There is also a simple use of the nano editor, which I wrote before:

Simple use of Nano editor https://rpiws.github.io/2022/04/02/%E6%A0%91%E8%8E%93%E6%B4%BE%E5%85%A5%E6%89% 8B005%EF%BC%9A%E7%BB%88%E7%AB%AF%E7%BC%96%E8%BE%91%E5%99%A8-nano/

4.2 Install LAMP environment

First install the LAMP package:

apt-get install apache2 apache2-utils libapache2-mod-php php php-curl php-mysql mysql-server

 (I suddenly remembered the bitter history of setting up LAMP on Windows before, it took three hours)

If you want to enter the mysql command line, you can enter mysql -uroot without a password~

Then shallow test that Apache and PHP are working properly.

Restart the Apache server:

apachectl restart

Enter the following three lines of code:

rm /var/www/html/index.html
touch /var/www/html/index.php
echo "<?php phpinfo(); ?>" >> /var/www/html/index.php

The browser accesses the IP address of the Raspberry Pi, and the following figure is displayed as a success~

 You can also scroll to the bottom to verify whether PHP is connected to MySQL. If the following figure appears, the connection is successful.

congratulations! Everything is ready, the only thing we need is Dongfeng, the server has been set up, what we need to do now is how to make the server public for others to see.

5. Intranet penetration

Zongsuo porridge juice, the intranet IP address of the Raspberry Pi can only be used by people connected to this router, your friends want to access it? There are no doors~

The network is divided into internal network and external network. The internal network refers to your router. The content of the internal network can only be accessed by connecting to your router. The extranet refers to a website that is open to every user and can be accessed from anywhere. There are two ways to allow your Raspberry Pi to access the external network: purchase a public network IP and internal network penetration. We can rule out the first method very quickly, because ordinary home users can’t buy it even if they have money, because the public network IP is only sold for enterprises~ Then we only have the second method: intranet penetration.

Intranet penetration, as the name suggests, is to map your intranet content to the external network. In simple terms, a tunnel is added between the internal network and the external network, and others can access your intranet server from the external network through the tunnel. The most important thing is that intranet penetration does not require a penny, it is free~

The intranet penetration tool we use here is Sunny-Ngrok, and the intranet penetration will start below~

5.1 Sunny-Ngrok deployment

The reason why we choose Sunny-Ngrok here is because it is the only free intranet penetration server in the world that supports fixed domain names. Other intranet penetration servers, such as Sunny-Ngrok's father ngrok, either add money, or change your domain name for you later, and you cannot bind your own domain name, which is very troublesome for others to access. Open  Sunny-Ngrok , select Register, and follow the steps to register;

After logging in, click Real-name authentication, and then perform real-name authentication. Since this real-name authentication mechanism needs to call a third-party API, it may require a little mane. After handing in the mannequin, click on the real-name authentication, and then follow the operation until the following picture is displayed.

Then click Tunnel Management - Open Tunnel, choose the free server at the bottom of the beautiful country, and agree no matter what;

 Then fill in http for the tunnel protocol (I made a mistake here and filled in https), fill in the name and front domain name casually, leave all the latter blank, click OK, and click OK again;

The browser copies the tunnel ID (where I coded);

  Open PuTTY and enter the following code, the last line of the tunnel id you just copied should be changed to real;

sudo -i
cd /opt
wget https://www.ngrok.cc/sunny/linux_arm.zip?v=2.1
mv 'linux_arm.zip?v=2.1' linux_arm.zip
unzip linux_arm.zip
cd linux_arm
screen -S sunny
./sunny clientid 你刚刚复制的隧道 id

The first line raises the right, the second ~ fifth lines download and decompress the Sunny-Ngrok client. The sixth line creates a new screen background program, because we want Sunny-Ngrok to run in the background, and we have to do other things in the foreground. The seventh line starts the intranet penetration program. The program in the red circle is the link to the external network after our internal network penetration. Copy it and open it in the browser. If the PHP page just now appears, it means the penetration is successful.

 After the penetration is successful, don’t forget to press Ctrl + A + D in PuTTY to exit the screen background, let Sunny-Ngrok run in the background, and enter screen -r sunny when you need to restore. If you want to stop intranet penetration, first enter the Sunny-Ngrok page, and then press Ctrl + C. 

6. Apply for domain name and DNS resolution

At this point, the construction of our server operation and maintenance part has come to an end. But it's not quite over yet. You said that your website can't be opened, isn't it all in vain? And every time your friends visit your website, they have to enter such a long list, how troublesome is that? And Sunny-Ngrok's native opening speed is indeed a bit slow. But unfortunately, both of these need ¥. But good people can't help half of it. After 7749 days of tossing, I finally found a free domain name application website and a free DNS resolution website - Freenom and Cloudflare.

After having your own domain name and DNS resolution, others can quickly access your website by only entering a small string of domain names. Nice or not? Let's start tossing~

6.1 Freenom Free Domain Name Application

Open  the Freenom domain name search website (whether Freenom and Sunny-Ngrok have the same DNS resolution, the slow speed is the same), and search for the domain name you want to register. There are only five free domain name suffixes: .tk .ml .ga .cf .gq, and the others are ¥. (You found that without Freenom, you can buy some domain names that we usually can’t buy, such as .org .me) For example, we want to register biteit.tk here, and click Check Availability after filling in;

When you see Yes, biteit.tk is available! , and there is a 0.00 USD next to it, which means that you can buy this domain name at a price of 0 yuan, which is equal to free when rounded up (it seems to be free). Click Checkout to go to the purchase page.

If the purchase page shows that there is no domain name, then apply again according to the method just now. If there is a domain name, change the 3 Months @ FREE on the right to 12 Months @ FREE, that is, apply for one year. It can be renewed for free when it expires. Click Continue.

 Fill in your email address here to register a Freenom account. It is recommended to fill in a foreign email address, such as icloud yahoo gmail outlook. I filled in outlook. I'm not sure if I can receive the registration email with my domestic mailbox. If the blue button does not work, you can try to switch to Firefox or use a mobile browser to access freenom.

Then register an account according to the above requirements. If you are asked to fill in the company, you can fill it in casually. Remember to keep an eye on your mailbox and check the Freenom registration email in time. After logging in, check the agreement agreement and click Complete Order. If you don’t move, you can go online scientifically and recommend a grapefruit accelerator. If you talk too much, the article will not pass the review. 

 Open this link , if you see the domain name you just registered, it means your domain name registration is successful!

Congratulations, you already have your own domain name, and friends only need to enter this domain name to visit your website in the future. Of course, we have to do the second step before this: apply for free DNS resolution. If there is no DNS to resolve your domain name, it is useless~

6.2 Cloudflare free DNS resolution

DNS resolution is a hub for others to visit your website. Its function is to connect the domain name with the server. Most domestic DNS resolutions require ¥ and filing (7 days for filing). After unremitting efforts, I finally found a free DNS resolution website in the United States: Cloudflare.

To access  the Cloudflare Dashboard , a login is required. If you don't have an account, register one first;

Click the blue "Add Site";

 Enter the domain name you just registered;

Select the Free package below; 

 

 Open the link below again and select Manage Domain, the domain name you just applied for;

Domain name management interface https://my.freenom.com/clientarea.php?action=domains

Select Nameserver in the Management Tools column;

Select Use custom nameservers, and fill in the fields under freenom with the two servers under "Replace with Cloudflare's nameservers" under the Cloudflare control panel.

Click Change Nameservers and wait for a while until you receive an email from Cloudflare in your mailbox, which means that the DNS resolution is successful. So how to make Sunny-Ngrok hook up with the domain name you just applied for? If you use other intranet penetration servers, you must add more than 100 meters per year if you want to hook up. If you don’t pay this meter, it will change the default domain name for you later. But Sunny-Ngrok doesn't! Open Cloudflare, click DNS on the left, and click Add Record.

 Select CNAME as the type, fill in the domain name you just registered for the name, fill in free.idcfengye.com for the content, and click OK;

Open the Sunny-Ngrok console , select Tunnel Management, select Edit, you may need to log in again;

Select the custom domain name for the domain name type, fill in the domain name you just registered in the custom domain name column, and click OK to modify;

Open PuTTY, first screen -r sunny to enter the Sunny-Ngrok background, then Ctrl + C to stop intranet penetration, and then ./sunny clientid tunnel id to restart. Press Ctrl + A + D to exit the background, and the browser visits the domain name you just registered, and you will be surprised to find that you can access it! Congratulations, don't rush to match the BGM, good luck~

 At this point, the operation and maintenance part of our Raspberry Pi server is completed. This series has been updated for a long time, and the next issue will start to use the Raspberry Pi as a private cloud~

Guess you like

Origin blog.csdn.net/raspi_fans/article/details/126562772