Linux configuration nginx starts automatically at boot&nginx deployment and systemctl controlled startup&The difference between /etc/systemd/system and /lib/systemd/system

1. Method 1

1. Find the /etc/rc.local file and add a startup command in the last line of the file (the default installation directory of nginx is: /usr/local/nginx):

/usr/local/nginx/sbin/nginx

2. Method 2

1. Enter the /etc/systemd/system folder and add the file nginx.service
2. File content:

[Unit]
Description=nginx service
After=network.target

[Service]
User=root
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecStartPre=/bin/sleep 10

[Install]
WantedBy=multi-user.target

3. Turn on the startup

systemctl enable nginx

The difference between /etc/systemd/system and /lib/systemd/system

In Linux operating systems, especially those distributions that use Systemd as the init system, there are two important directories involved in the storage of systemd service unit files:

  1. /usr/lib/systemd/system

    • This directory is used to store service unit files installed by system packages. These files are provided by the package maintainers and may be automatically updated or replaced as the package is upgraded.
    • It is not recommended that users directly edit service files in this directory as changes may be lost during package upgrades.
  2. /etc/systemd/system/

    • This directory is used to store user-defined service unit files. These files are service configurations customized by system administrators according to their own needs and will not be affected by software package upgrades.
    • It is recommended to store customized service unit files in this directory to retain customized configurations.

The relationship between these two directories is complementary and together form the basis of the systemd service unit file. System-built-in services are usually located at /usr/lib/systemd/system, while custom services are located at /etc/systemd/system.

When systemd loads the service configuration, it will first search for the /etc/systemd/system directory. If the corresponding service unit file cannot be found there, it will fall back to /usr/lib/systemd/system to search. This means that if you place a service unit file with the same name in /etc/systemd/system, it will overwrite the system's own service configuration. This design allows users the flexibility to customize and expand system functionality.

Another thing to note is that when you finish editing the service file under /etc/systemd/system and prepare to enable the new configuration, you usually need to run systemctl daemon-reload to notify Systemd reloads the configuration file before using commands such as systemctl start|stop|restart|enable|disable to operate the service.

There are many system directories in the Linux system. In daily use or in a large number of blogs, we often see /etc/systemd/system, /lib/systemd/system and /usr/lib/systemd/system. These three directories are What is the relationship between the three? This article will explain the relationship between the three in detail so that there will be no confusion in subsequent use.

Introduction

The directories /lib/systemd/system and /usr/lib/systemd/system actually point to the same directory. Execute the command in the / directory ll to know:

[root@localhost /]# ll
total 28
lrwxrwxrwx.   1 root root    7 Jul  3 09:43 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Jul 19 12:04 boot
drwxr-xr-x.  19 root root 3080 Jul 19 04:03 dev
drwxr-xr-x.  76 root root 8192 Jul 19 20:07 etc
drwxr-xr-x.   4 root root   39 Jul 19 04:37 home
lrwxrwxrwx.   1 root root    7 Jul  3 09:43 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Jul  3 09:43 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Apr 11  2018 media
drwxr-xr-x.   2 root root    6 Apr 11  2018 mnt
drwxr-xr-x.   3 root root   23 Jul  3 20:33 opt
dr-xr-xr-x. 118 root root    0 Jul 19 04:03 proc
dr-xr-x---.  10 root root 4096 Jul 20 06:31 root
drwxr-xr-x.  27 root root  760 Jul 19 16:00 run
lrwxrwxrwx.   1 root root    8 Jul  3 09:43 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root    0 Jul 19 04:03 sys
drwxrwxrwt.  10 root root 4096 Jul 20 03:37 tmp
drwxr-xr-x.  13 root root  155 Jul  3 09:43 usr
drwxr-xr-x.  21 root root 4096 Jul 19 13:34 var
  • [/usr]/lib/systemd/system/This directory contains the units for software package installation, that is to say, systemd unit files managed through package management commands such as yum, dnf, rpm, etc. are placed in this directory.
[/usr]/lib/systemd/system/
The expectation is that `/lib/systemd/system` is a directory that should only contain
 systemd unit files which were put there by the package manager (YUM/DNF/RPM/APT/etc).
  • /etc/systemd/system/ (unit installed by the system administrator, higher priority)
Files in /etc/systemd/system are manually placed here by the operator of the system for
 ad-hoc software installations that are not in the form of a package. This would 
 include tarball type software installations or home grown scripts.

In general usage scenarios, each Unit (service, etc.) has a configuration file that tells Systemd how to start this Unit.
Systemd reads configuration files from the directory /etc/systemd/system/ by default. However, most of the files stored in it are symbolic links pointing to the directory/usr/lib/systemd/system/, where the real configuration files are stored. The systemctl enable command is used to establish a symbolic link relationship between the above two directories.

$ sudo systemctl enable [email protected]
# 等同于
$ sudo ln -s '/usr/lib/systemd/system/[email protected]' '/etc/systemd/system/multi-
user.target.wants/[email protected]'

If startup is set in the configuration file, the systemctl enable command is equivalent to activating startup. Correspondingly, the systemctl disable command is used to cancel the symbolic link relationship between two directories, which is equivalent to canceling the startup.

$ sudo systemctl disable [email protected]

Although the units placed in the /etc/systemd/system/ directory are units installed by the system administrator, in actual use, users can customize the service configuration file and place it in this directory to set the priority of the service configuration file. improve.

priority

The use of systemd has greatly improved the operating efficiency of system services, and the file locations of unit generally have three main directories:

Table 1.  Load path when running in system mode (--system).
┌────────────────────────┬─────────────────────────────┐
│Path                    │ Description                 │
├────────────────────────┼─────────────────────────────┤
│/etc/systemd/system     │ Local configuration         │
├────────────────────────┼─────────────────────────────┤
│/run/systemd/system     │ Runtime units
├────────────────────────┼─────────────────────────────┤
│/lib/systemd/system     │ Units of installed packages │
└────────────────────────┴─────────────────────────────┘

The priority of the configuration files in these three directories is from high to low. If the same option is configured in three places, the one with higher priority will overwrite the one with lower priority.

When the system is installed, the unit file will be placed in the/lib/systemd/system directory by default. If we want to modify the system's default configuration, such asnginx.service, there are generally two methods:

  1. Create file in the /etc/systemd/system directory and write our own configuration in it. nginx.service
  2. Create a directory under /etc/systemd/system, create any file ending with .conf in this directory, and then write our own configuration. This approach is recommended. nginx.service.d

/run/systemd/systemThis directory is generally a directory where unit files are dynamically created when the process is running. It is rarely modified unless some parameters are modified when the program is running, that is, at the Session level. Modifications are made here.

Attached is an introduction to systemd

Service setting location:

  • /usr/lib/systemd/system/
  • /run/systemd/system/
  • /etc/systemd/system/ (usually a connection file)

Unified service management: systemd

service configuration file:

  • /usr/lib/systemd/system/*.service: Default configuration files
  • /etc/systemd/system/system/*.service.d/: User personalized settings file will be added to the settings
  • /etc/systemd/system/system/*.service.wants/: link file, other services recommended to be started after starting this service
  • /etc/systemd/system/system/*.service.requires/: link file, services that need to be started in advance before starting the service
  • /run/systemd/generator.late uses the systemd-sysv-generator tool to process the SysV init script (ie: /etc/init.d/*) and automatically generate the .service file

nginx deployment and systemctl control startup

Deploy nginx

Download nginx

  • nginx download address
  • Stable version: the latest stable version, the version recommended for production environments

Install nginx pre-environment

  • yum install gcc-c++
  • yum install -y pcre pcre-devel
  • yum install -y zlib zlib-devel
  • yum install -y openssl openssl-devel

Unzip the nginx installation package and compile

tar -xvf nginx-1.14.0.tar.gz  # 不要解压在/usr/local下
./configure && make && make install		#运行安装配置
/usr/local/nginx/sbin/nginx -t		 # 测试nginx安装是否成功
# 安装成功后nginx的目录为/usr/local/nginx下	
conf/nginx.conf         #nginx 配置文件  
sbin/nginx              #nginx 启动文件  
sbin/nginx -s reload    #nginx 重启命令  

Configure systemctl startup

vim /usr/lib/systemd/system/nginx.service

[Unit]                                                          #对服务的说明
Description=nginx - high performance web server                 #描述服务
After=network.target remote-fs.target nss-lookup.target         #描述服务类别

[Service]                                                       #服务的一些具体运行参数的设置
Type=forking                                                    #后台运行的形式
PIDFile=/usr/local/nginx/logs/nginx.pid                         #PID文件的路径
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf     #启动准备
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf           #启动命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload                                    #重启命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop                                        #停止命令
ExecQuit=/usr/local/nginx/sbin/nginx -s quit                                        #快速停止
PrivateTmp=true                                                                     #给服务分配临时空间

[Install]
WantedBy=multi-user.target                                                          #服务用户的模式

#在启动服务之前,需要先重载systemctl命令
systemctl daemon-reload
systemctl start nginx.service

Check the startup status of nginx

[root@localhost conf]#  systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2023-05-09 11:26:32 CST; 5s ago
  Process: 57306 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 57303 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 57309 (nginx)
    Tasks: 2
   Memory: 968.0K
   CGroup: /system.slice/nginx.service
           ├─57309 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─57310 nginx: worker process

509 11:26:32 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
509 11:26:32 localhost.localdomain nginx[57303]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
509 11:26:32 localhost.localdomain nginx[57303]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
509 11:26:32 localhost.localdomain systemd[1]: Started nginx - high performance web server.

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/135026446