A Practical Introduction to Linux for Web Developers (Transfer)

From the perspective of web development, let me talk about the problems encountered when using Linux, mainly for the operation itself, because the instructions can be found on the Internet, and I will not go into the principle, but try to be practical.

Basic cognition

Why use Linux

At first, I used Linux because the application I needed only provided the Linux version in the tutorial, so I deployed it step by step, and used it naturally, and solved some problems during the process. During this process, I gradually had some questions: Why use Linux as the server instead of the more familiar Windows system we use every day? What is the difference between the two on the server side?

1. Operability

In this regard, there is no doubt that Windows is better, with a user-friendly graphical interface, and there is no big difference in operation between the Windows Server version and the version for the general public. Most people should still be more accustomed to Microsoft's consistent style.

Linux also has a distribution version with a graphical interface, but when used as a server, it loses some of its advantages. The graphical shell program will occupy valuable hardware resources of the server, which is usually not considered.

2. Price cost

Objectively speaking, Linux is open source and free, while Windows Server requires paid authorization. In fact, in most cases, when we purchase cloud servers, this part of the cost has already been amortized.

Tips when Tencent Cloud chooses Windows Server:

3. Stability

Linux is open source, its code is maintained and updated by the best programmers in the world, and bugs are quickly fixed if they are discovered. As for Windows, since I don’t have much experience in using Windows Server, I don’t want to make too many comments, but everyone has a say in the version for ordinary users. Although Microsoft’s programmers are very good, Windows will still frequently push bug fixes to you, and occasionally blue screen and crash

Linux is completely open to users, because it assumes that users know what they are doing. You can operate all the files in the operating system, but Linux will not be responsible for your operations, just like we get root permissions in Android phones. You have obtained the operation permissions of the underlying files, and you can modify the system according to your own ideas, but in this way, the service provider of the phone will no longer provide warranty service, because you have done something beyond their expectations.

On the Windows side, I don’t know if everyone has experienced the situation of being prompted that they do not have permission to operate, or being told that they do not have permission when they want to set permissions. I also encountered the problem that Microsoft’s own software does not have permission to read files and cannot recognize installed applications. This is all because of Windows’ more complicated permission system.

Why put the permission system in the stability section, because the stability of the system will of course be affected when users are free to modify permissions. In Linux operations, we can easily set 777 permissions for files (the highest permission, which will be discussed later), or open all ports of the firewall, which undoubtedly increases the risk of system operation. Therefore, we should be more cautious when designing this part of the operation.

Choose a different distribution

Strictly speaking, Linux is a kernel rather than an operating system. It becomes an operating system with the help of operating system-related components provided by different distributions. Usually, we will simply call it a Linux system, but in fact, due to the nature of open source, there will be a big gap between different branches. This part involves a lot of professional knowledge that I don't understand, and you can find a lot of explanations and comparisons on the Internet, so I won't go into details here. What I want to explain is to pay attention to the prerequisites when looking for instructions and materials, and to distinguish the operations of different systems.

Here is a picture from the rookie tutorial:

Due to the problems of cloud service providers, CentOS is widely used in China, and it is generally a very easy-to-use version. This version is recompiled based on the free RHEL source code, both of which belong to Red Hat. Others such as Debian and Ubuntu are also widely used systems. Each distribution is used by the community it belongs to, and there are also a large number of self-compiled customized versions.

But it should be noted that CentOS 8 has stopped maintenance on December 31, 2021, and Cent OS 7 will stop maintenance on June 30, 2024. This may become a hidden danger and needs to be taken into consideration when choosing a distribution.

Image introduction when selecting a system in Tencent Cloud:

Debian is a stable, convenient, and fast Linux distribution. It has more powerful package management tools than most Linux distributions. It is currently one of the preferred server operating systems for building websites.

Ubuntu is one of the most popular Linux distributions. It is an open-source free software based on the Debian Linux operating system. Its ease of use and stability are excellent, and it has very strong and mature community resources.

CentOS is a popular open source Linux distribution that is a recompilation of RHEL (Red Hat Enterprise Linux) source code. (Note: The CentOS community will stop maintaining CentOS 7 on June 30, 2024. It is recommended that you choose the OpenCloudOS 8 image compatible with CentOS 8 instead.)

remote operation

Having said so much, it is better to actually enter the system to operate. When using the cloud server, although the web end will provide a console, the operation and functions cannot fully meet our daily development needs. As long as it can support the SSH protocol (default port 22) software can be used to connect to Linux remotely, and also supports the Telnet protocol. However, because the Telnet protocol itself is not safe, many distributions have closed this connection method.

There are many choices in terms of software, the most simple and straightforward is PuTTY, you can also use SecureCRT, I use XShell a lot, and you can apply for a free home/school version on the official website, which can fully meet the daily development and use needs .

The upload software uses supporting XFTP, and others can also choose WinSCP and so on.

Directory Structure

The default file directory is as follows:

The folder pointed by the arrow represents a soft link, which can be understood as a Windows shortcut. In ftp, you can see a shortcut sign in the lower left corner of the icon.

Some commonly used folders or files can be linked to the outer directory for daily use.

Usually I will create an "app" folder in the root directory to store our web application.

If a directory or file name starts with a dot, it means that the directory or file is a hidden directory or file. lsThat is, when searching ( ) by default , the directory or file is not displayed. Can be used ls -ato view.

install software

package installer

In Windows, there are usually two situations when we want to install a new software. One is to open an exe file or an installer in other formats, and then it will install the file in the directory we specify. At the same time, it will also add information in the registry or create some configuration files in the system disk. There are many specific situations. The other is to decompress a compressed package and open it.

It is similar in Linux. On the one hand, you can install files through the package management tool, which will automatically place the files in a fixed directory or the directory you specify. On the other hand, you can also download the compressed package, decompress it, and then use it according to the specific usage of the software.

However, even the installation packages are different in Linux. When we check the information, we will see files in various formats such as rpm, yum, and apt. They can all install software, but there are differences.

Comparison item rpm yum dpkg apt
series RedHat Department RedHat Department Debian system Debian system
the difference package installer dependency management tool package installer dependency management tool

Among them, the package installation tool is a simple installation tool. If a certain software needs a dependent library, you must install it manually before proceeding. Similar to running some software in Windows system, you need to install the dependent library such as .Net Framework XX first, and the dependency management tool is similar to the front-end npm. It will automatically install the dependent library for you, which is the so-called one-click installation, upgrade and uninstallation tool.

From the above table, we can see that the installation tools of RedHat system and Debian system are different, which is why I emphasized to distinguish the distribution system before executing the command. It is not feasible to execute yum in Debian. Although we can install yum in Debian, this obviously violates the original intention of the design, because Debian already has apt. In addition, it should be noted that even if the dependent library with the same function, the name of the software package may be different, probably because it was developed by different people. Software with the same function, such as the five dependencies required to compile Nginx:

RPM package default installation path:

installation path meaning
/etc/ Configuration file installation directory
/usr/bin/ executable command installation directory
/usr/lib/ The storage location of the function library used by the program
/usr/share/doc/ Basic software manual save location
/usr/share/man/ Where to save help files

environment variable

In addition, it should be noted that the instruction file usually does not affect the whole world, and we need to modify the environment variables ourselves. If you have installed Java in Windows, you may have the impression that after the installation is complete, you can’t use Java commands directly in cmd, but you can only execute them in the corresponding directory. However, by setting environment variables and mapping the command files to the global, you can use Java commands in any folder. The installation script provided by some people will help you add global instructions, and the specific operation needs to be considered by yourself.

I have written the installation process of some commonly used software before, you can refer to the following two articles:

Deploy the Vue+Spring boot project with front-end and back-end separation from scratch on Linux

Front-end Linux deployment commands and process records

daemon process

There are many methods for the daemon process, and they are all easy to find. I didn’t write the specific steps, but it is very important for web development, so I must mention it.

When I developed the application for the first time, uploaded it to the server (a lot of steps were omitted in the middle), and started using it OK, I thought everything was fine and turned off the remote tool, and then found that the application could not be accessed. After I learned about it, I realized that I just created a foreground task, just like the window you are currently open in the Windows system. When you close the window, the program will naturally stop running. However, there are still many programs that do not depend on the window to run. The application also becomes a background task, there are many ways, you can refer to the following article:

How to start a Linux daemon

permissions

We all say that the permission issue is very important, but when we searched the information on the Internet, we found that many people are not relentless in changing the permission. Here we take Nginx to create a custom log as an example. The first line of Nginx default configuration is as follows:

#user  nobody;

It means that Nginx is started by the user nobody, so the maximum authority of Nginx is the authority of nobody. Although it is commented out here, the default value is nobody, which can be found in the system.

The default log file of Nginx is /usr/local/nginx/logs/access.logrwxrwxrwx, which can be seen from the figure below. It can be simply said that it is full permission. Anyone can read, write or delete. If we want Nginx to  /usr/local/nginx/logswrite custom logs in this directory, we will get an error of no permission, because the user nobody has read and write permissions to this directory.

I found a very common solution: If nobody has read and write permissions, which user has read and write permissions? Root must exist, so I changed the Nginx user to root. This idea seems reasonable, but this kind of permission operation is not necessary. Looking back, if there is no read and write permission for the directory, it is enough to set the read and write permission for the corresponding user on the directory.

Now let’s go back to the permission setting. In fact, rwx can be easily guessed to represent read (represented by r), write (represented by w) and execute (represented by x, for executable files or directories). Why there are three, see the figure below:

As for why the 777 permission was mentioned at the beginning, and many articles will let you execute it  chmod 777, here:

  • r: means read, the corresponding number is 4;
  • w: means write, the corresponding number is 2;
  • x: indicates execution, the corresponding number is 1

Through the combination of 4, 2, 1, we can get the following permissions

  • 0: no permission, use - to indicate
  • 4: Read permission, represented by w
  • 5: Read and execute permissions, represented by rx
  • 6: Read and write permissions, represented by rw
  • 7: Read, write and execute permissions, represented by rwx

So 777 means the highest authority that should not be abused.

Notice

There are still many scattered points that need to be noted that they are not divided into separate chapters.

capitalization

This point is easy to be overlooked, but it is a headache after encountering this problem. The Windows system file name we are used to is case-insensitive, that is to say, a file with a name has the same name as a file with a name. However, it is case-sensitive in Linux, which needs to be paid attention  a.txt to  A.txt .

Open ports (firewall)

Our web applications often expose some ports to external access. In addition to opening the required ports in the internal firewall of the system, the corresponding ports must also be opened on the cloud server platform. In addition, don't try to save trouble and turn off the firewall entirely.

Encoding format and end of line format

  • Linux's newline, there is only one newline character (LF) at the end of each line  \n, and the end \n of is marked with
  • Windows' newline, at the end of each line is a carriage return (CR)  \r and a line feed (LF)  \n and, \r\n since is identified

Therefore, there may be redundant characters when the files on both sides are transferred to each other.

sudo

In many articles, there will be sudocommands beginning with , its function is to temporarily elevate the authority of the command, so that it can obtain the highest authority, which may improve the success rate of some commands, but if you are already a root user, adding it is useless sudo.

text processing

If you want to directly edit the text in Linux, it will be more complicated than you think, but you can understand it after a few more operations. If conditions permit, I will edit the file in ftp.

Editing instructions can refer to this

Linux file editing commands in detail

Guess you like

Origin blog.csdn.net/wangonik_l/article/details/131705926