头图

Hello everyone, Brother Yan's new booklet, the NestJS project , has been launched recently. I have also been lurking in the booklet group for a while, but I noticed that many front-end students are not interested in configuring the Linux-related environment during the actual combat. , encountered different degrees of problems, so I thought of a tutorial to help you quickly master the common commands on Linux and the installation of related middleware.

Seeing this, students who are familiar with the backend should not be in a hurry. Maybe you can learn something you don't know in the text, especially Mysql installation, Systemctl and ufw commands.

Well, the outline of this article is as follows:

Although in theory, I should talk about commands first, and then talk about the specific middleware environment, but from my personal point of view, it is a feeling of sudden realization that I practice first, and then understand the meaning of the relevant commands, so this article will follow the Write in the order above.

You may have noticed that there are no basic commands in the commands above. I think the basic commands can be scanned at a glance, and there is no need for special memory. The basic commands I commonly use are as follows:

  1. top: View system process status.
  2. cd: Change directories.
  3. vim: modify files.
  4. mkdir: Create folders.

Maybe my scene is limited. If I play the server by myself, the basic commands are basically useless except for these four, so I suggest that everyone does not need to spend too much time on this, especially the front-end students.

Finally, the Linux environment version of this article is: Ubuntu 20 LTS . It is strongly recommended that you use Ubuntu (it is really easy to use), and I hope you will develop a good habit of watching after liking it😁😁😁.

Note: The text installation middleware will be installed from the official source, refuse to use the source of the package manager, and keep the same with the official.

Nginx

It turns out that I have always installed Nginx by compiling from source code. Now I think it is more convenient to use the package manager to install + systemctl to manage.

Entering the topic, using the official source to install can be roughly divided into four steps:

  1. Install dependencies.
  2. Generate/import keys.
  3. Set up Nginx source.
  4. Install.
 # 安装依赖
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

# 生成key
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# 设置 Nginx 源
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# 更新 && 安装
sudo apt update
sudo apt install nginx

After the installation is complete, we can use the systemctl start nginx command to start Nginx. Generally speaking, we only need to understand two parts of Nginx: configuration files and common commands.

The configuration file of Nginx is: /etc/nginx/nginx.cnf , the three commonly used commands are as follows:

  1. Nginx -s reload: Reload the configuration file.
  2. Nginx -s stop: Fast shutdown.
  3. Nginx -s quit: Graceful shutdown.

Finally, you can use curl -I 127.0.0.1 to test whether Nginx is normal, and the following response is OK:

 HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Sat, 09 Jul 2022 08:25:06 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 23 May 2022 23:59:19 GMT
Connection: keep-alive
ETag: "628c1fd7-267"
Accept-Ranges: bytes

Nginx installation is relatively simple, and then it will gradually become more complicated.

Node

The installation of Node is different from others. The higher version of Node is actually somewhat incompatible with the Linux version. Taking my current Ubuntu 20 LTS as an example, the version 19/18 under version 20 does not support Node18+, and the specific compatibility A list can be found on its Github: https://github.com/nodesource/distributions/blob/master/README.md.

Apart from version issues, installing Node using the package manager is very simple:

 curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - 
sudo apt-get install -y nodejs

One line of command is enough, where setup\_18.x can be replaced with setup\_17.x or setup\_16.x, according to the version you want, if you want a command that installs the latest version every time, then It can be replaced with: setup\_current.x.

Then enter node -v to view the installed version:

 root@hecs-5778:~ node -v
v18.4.0

The language environment of Node generally does not need to configure self-starting at boot, and can be invoked directly through environment variables, so the steps to configure self-starting at boot will not be shown here\~

JDK/JRE

By the way, why does Java appear here? Because if the front end wants to do some CICD environment, Jenkins will be used, and Jenkins needs the Java runtime environment.

JDK / JRE installations are generally relatively simple and can be done with a single command, but it should be noted that when you search for JDK / JRE on the Linux package manager, a handleless version will usually appear:

 root@hecs-5778:~ sudo apt-cache search jdk-17

openjdk-17-jdk - OpenJDK Development Kit (JDK)
openjdk-17-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-17-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-17-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-17-source - OpenJDK Development Kit (JDK) source files

The headless version is a version that does not support graphical interfaces and human-computer interaction devices (mouse, keyboard). It is usually used on the server side, so if the project you deploy is a regular web application, you can directly install the handless version to use it. Compared with the standard version, it introduces fewer dependencies and occupies fewer resources.

After talking about the version, let’s talk about the installation. If you are too lazy to search for commands on Linux, you can directly type the corresponding command, and the Linux package management tool will prompt you to install:

 root@hecs-5778:~ java

Command 'java' not found, but can be installed with:

apt install openjdk-11-jre-headless  # version 11.0.15+10-0ubuntu0.20.04.1, or
apt install default-jre              # version 2:1.11-72
apt install openjdk-16-jre-headless  # version 16.0.1+9-1~20.04
apt install openjdk-17-jre-headless  # version 17.0.3+7-0ubuntu0.20.04.1
apt install openjdk-8-jre-headless   # version 8u312-b07-0ubuntu1~20.04
apt install openjdk-13-jre-headless  # version 13.0.7+5-0ubuntu1~20.04

Next, execute directly: apt install openjdk-17-jre-headless to install.

Of course, it is not recommended to install all software in this way, because what the package manager recommends to you is not necessarily the official source. In addition to this programming language environment, I recommend installing other large-scale software on the server side according to its official website documentation. For example, in the MongoDB documentation, it is stated that the MongoDB installation package in the source that comes with Ubuntu is not the official source installation package.

Since the relevant commands of the programming language are generally added to the environment variables, there is no need to configure the JDK / JRE to configure things such as self-starting at boot, and it can be used directly.

Redis

As an old cache middleware, Redis is now known to more and more front-ends. High performance + high availability is its advantage\~

Redis installation is generally divided into two steps: update Redis source and apt installation, the command is as follows:

 curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis

There are two file paths to know after installation:

  1. Startup script path: /usr/bin/redis-server .
  2. Configuration file path: /etc/redis/redis.conf .

Before officially starting Redis, we usually modify its configuration file, because the default Redis configuration does not support remote access and no password, so we need vim /etc/redis/redis.conf to make the following two configurations Revise:

 bind 0.0.0.0 ::1
requirepass yourpassword

After saving, you can use: systemctl start redis-server Start the Redis service.

If you need to boot automatically, you can set it by command: systemctl enable redis-server .

In this way, Redis can start automatically. By the way, Redis uses port 6379 by default\~

Reference documentation: https://redis.io/docs/getting-started/installation/install-redis-on-linux/

MongoDB

I heard that the first choice for the front-end database is MongoDB. Compared with the old relational database - MySQL, MongoDB is indeed more lightweight, and its performance is much better than MySQL. I also like MongoDB as a back-end. .

The installation of MongoDB is generally divided into two steps: update the MongoDB source and apt installation, the command is as follows:

 wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

sudo apt-get update
sudo apt-get install -y mongodb-org

After MongDB is installed, there are several common directories that need to be known:

  1. Configuration file: /etc/mongod.conf.
  2. Data directory: /var/lib/mongodb.
  3. Log directory: /var/log/mongodb.

After that, we also need to modify the configuration file, because the default MongoDB does not support remote access. After using vim to open the configuration file, modify the bind property:

 net:
  port: 27017
  bindIp: 0.0.0.0

After the modification is completed, you can start MongoDB. Use sudo systemctl start mongod to start it. If you are prompted Failed to start mongod.service: Unit mongod.service not found. you can use sudo systemctl daemon-reload and then run the above startup command again.

If you need to boot automatically, you can use the command: systemctl enable mongod to set it.

In this way, MongoDB can start automatically. MongoDB uses port 27017 by default\~

For the database, I strongly do not recommend using Docker. First, it is troublesome and secondly, it is unnecessary and thankless.

Reference documentation: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/

MySQL

MySQL installation is more complicated, in addition to the configuration file, it also needs to deal with ROOT permissions.

There are generally two ways to install MySQL:

  1. Install by using the MySQL apt repository (note: this is not how the official sources are set up).
  2. Install via the Linux apt package manager.

Since both schemes are introduced in the official website documentation, which means that both schemes can be installed, it will be very convenient for us to directly use the apt package manager that comes with Ubuntu:

 sudo apt-get install mysql-server

Note that this command will help you install the latest MySQL. At this point in time, MySQL 8 is installed. I also strongly recommend not to use versions below 8. The performance improvement brought by MySQL 8 is really huge.

One additional thing that needs to be mentioned here is the data directory initialization of MySQL. If we use the installer and package manager to install, the data directory initialization will generally be performed automatically. If this is not the case, the data directory initialization needs to be performed manually. Otherwise MySQL will not be able to start.

Since we are using apt to install, we don't need to care about the initialization of the data directory, but there are still some things that we need to pay attention to:

default directory

  1. Data directory: /var/lib/mysql.
  2. Log directory: /var/log/mysql.
  3. Configuration file directory: /etc/mysql/mysql.conf.d.

remote access

Remote access needs to be modified in two places: configuration files and user permissions. The configuration files are generally modified using the following commands:

 vim /etc/mysql/mysql.conf.d/mysqld.cnf

bind-address = 0.0.0.0

The default configuration file of MySQL is generally /etc/mysql/mysql.conf.d/mysqld.cnf . To modify user permissions, you need to log in to MySQL to modify the database:

 mysql -u root -p

use mysql;

update user set host='%' where user='root';

flush privileges;

ROOT password :

The initialization password of MySQL is different according to the installation method. The default password is also different. Generally, there are three methods:

  1. You are prompted to set it up during installation.
  2. null.
  3. random code.

If you have already configured a password, you can skip this section. Let’s focus on the case where no password is configured. When you do not have a password configured, you can use mysql -u root -p use an empty password for login verification, if the empty password is not yours To initialize the password, you need to find your initial random password in the MySQL error log. The address of the error log file is generally: /var/log/mysql/error.log , and then reuse mysql -u root -p for login verification.

After logging in, use the following command to view your root user authentication method:

 use mysql;

select user, host, authentication_string, plugin from user where user = 'root'

The effect is generally like this:

 +------+------+-----------------------+-------------+
| user | host | authentication_string | plugin      |
+------+------+-----------------------+-------------+
| root | %    |                       | auth_socket |
+------+------+-----------------------+-------------+
1 row in set (0.00 sec)

The % of Host means remote access is supported. Next, we need to change the password to our custom password. At this time, we should pay attention to what the plugin is in the print. If it is the auth\_socket method, it means that it uses Unix socket for passwordless authentication. , which needs to be replaced, otherwise the password cannot be used for authentication.

Next, you can directly use one line of command to modify the password:

 ALTER USER 'root'@'%' IDENTIFIED with caching_sha2_password BY 'your_password';

Since MySQL 8.0 uses the cache\_sha2\_password method for password authentication by default, I am also consistent with the official here.

Finally, use one line of command to add MySQL to the boot list, MySQL uses port 3306 by default:

 systemctl enable mysql

Reference documentation: https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/

Apt

Before understanding apt, you need to know what the package management tool of the Ubuntu system is. As a Debian-based distribution, Ubuntu system uses the same package management tool as Debian: dpkg, and apt is the command line front end of dpkg. Its function It is used to operate the dbkg package manager. In the Debian system, in addition to the apt command line front end, there is also a command line front end called apt-get, which is older than apt.

apt-get was released in 1998 and was gradually replaced by apt. apt became a standard in Debian8, and became popular after Ubuntu16. Currently, many installation examples of open source projects use apt as the command line front end.

Apt has slightly more functions than apt-get, but the overall functions are similar. The main highlights are:

  1. There is a progress bar.
  2. Some commands display packages with a color.
  3. Commands are shorter.

Shorter commands are my favorite. Next, let’s take a look at a few common commands. I will no longer compare apt-get in terms of commands, just memorize apt-related commands:

  1. Apt search: used to search for a package, such as: apt search jdk.
  2. Apt install: used to install a package, such as: apt install nginx.
  3. Apt remove: used to remove a package, such as apt remove nginx.
  4. Apt purge: used to delete a package and clear the configuration, such as apt purge nginx.
  5. Apt show: used to display the specific information of a package, such as apt show nginx.
  6. Apt list --installed: List all installed packages.
  7. Apt autoremove: Remove library files and dependencies that are no longer used.

Simply memorizing the above commands is generally enough to use \~

Systemctl

Systemctl is a system service management command. In older Linux distributions, service management is generally a service command, while newer systems are basically Systemctl. Systemctl is compatible with the service command and has more powerful functions.

But for Systemctl, I don’t think our average developers need to know too much, because Systemctl is all about Linux services. We only need to know five common commands to use:

  1. systemctl enable service name: used to set the service to start automatically at boot, this is a big praise, because it is more semantic and the command is shorter.
  2. systemctl disable Service name: It is used to disable the service's boot auto-start.
  3. systemctl start service name: start the service.
  4. systemctl stop service name: Shut down the service.
  5. systemctl restart service name: restart the service.
  6. systemctl status service name: View the status of the service.

In the above, we have seen the systemctl command appear several times. Now you can try the effect of opening/closing the service through these commands to deepen your memory.

ufw

UFW is the abbreviation of Uncomplicated FireWall. From this name, you can also see that this is a firewall command. When you search for a port on the Internet, it is usually the iptables command that appears, and UFW was born to replace it. Become the default firewall management command for Debian systems.

The biggest advantage of UFW's new generation command is that it is easy to use and easy to use. Especially for non-professional operation and maintenance like us, it is enough to remember a few common commands.

UFW is turned off by default in Ubutun. You can check the status with the following commands. Incative is turned off and active is turned on (because the UFW command can only be used under administrator privileges, so I will ignore the sudo prefix below):

 root@hecs-5778:~ ufw status verbose
Status: inactive

Next, it can be turned on with the enable command:

 root@hecs-5778:~ ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

After UFW is opened for the first time, all ports will be closed directly, so that you will not be able to connect to the cloud server through SSH next time, so you must open the SSH port immediately after opening:

 root@hecs-5778:~ ufw allow ssh
Rule added
Rule added (v6)

The ufw allwo command is specially used to open ports. It has some built-in service-to-port mappings, such as the above ufw allow ssh actually corresponds to ssh, which corresponds to port 22, and some commonly used ones, such as http. 80, https corresponds to 443, and the specific rule file is under the /etc/services file.

If some ports are not in the preset file, you can add them directly. For example, we release the default port 6379 of Redis:

 root@hecs-5778:~ ufw allow 6379
Rule added
Rule added (v6)

Finally, if our server is playing by itself, I do not recommend opening the firewall, because it is more troublesome, you can use the disable command to close it:

 root@hecs-5778:~ ufw disable
Firewall stopped and disabled on system startup

write at the end

Written at the end, to tell the truth, part of the content of this article is still very detailed and practical, such as MySQL -ROOT password, I also read a lot of related articles on the Internet when I wrote it, but basically I can't say the point, especially with the latest I haven't seen a single article about the encryption method of . I believe that after reading this article, you should be clear about these things.

Linux is a huge operating system, and you can write a book if you pick it out, so I hope that everyone will study for practical purposes in the early stage, not limited to details.

Well, I don’t know if you have gained anything after reading this article. I hope you will like this article more so that more readers can see the good content.


和耳朵
763 声望5.4k 粉丝