linux system startup process and nginx startup

Startup process


1. kernel boot

Power on self-test, check the bios configuration, detect the hardware, and power on

2. run init

  • 0: shutdown
  • 1: Single user mode
  • 2: Multi user mode (no file system and network)
  • 3: Multi user mode (command line, default mode)
  • 4: Unused mode
  • 5: Multi user mode (graphical interface)
  • 6: Restart

3. system initialization

4. establish terminal

5. user login

## Shutdown command
init 0
shutdown -h now
shutdown -h 20:20
shutdown -h +10

## Restart command
init 6
shutdown -r now
shutdonw -r 20:20
shutdown -r +10

CentOS7 startup process

1.bios (post)
2.mbr (master boot record)
3.GRUB2 Bootloader (boot menu)
4.Kernel (kernel boot)
5.Systemd (instead of init, SYSTEMd is used)

6. Runlevel target

Operation level:

init ->		# Shutdown
init ->			# Single user mode
init ->		# Multi user mode (no file system and network)
init ->		# Multi user mode (command line)
init ->		# Multi user mode (still not used)		
init ->		# Graphical mode
init ->			# restart
# Get the current default run level
[root@qls ~]# systemctl get-default

# Modify run level
[root@qls ~]# systemctl set-default poweroff

## Modify the default run level using two commands
[root@qls ~]# rm -f /etc/systemd/system/
[root@qls ~]# ln -s /usr/lib/systemd/system/ /etc/systemd/system/

## Related contents
[root@qls ~]# ll /etc/systemd/system (default run level)
[root@qls ~]# ll /usr/lib/systemd/system (runlevel and service startup script)

CentOS7 enters single user mode

At the end of line linux16, add: enforcing=0 init=/bin/bash. After modifying, press Ctrl + X

## Modify default startup method
bash-4.2# mount -o rw,remount /
bash-4.2# Systectl set default (not available)
bash-4.2# rm -f /etc/systemd/system/
bash-4.2# ln -s /usr/lib/systemd/system/ /etc/systemd/system/
bash-4.2# exec /sbin/init

## Change password
bash-4.2# mount -o rw,remount /
bash-4.2# echo 123|passwd --stdin root
bash-4.2# exec /sbin/init

Method 2:

switch_root:/# mount -o rw,remount /sysroot
switch_root:/# chroot /sysroot
sh-4.2# systemctl set-default
sh-4.2# exit
switch_root:/# reboot

nginx related command documents


View the current operating level of the system

N means the last run level 3 is the current run level.

Check whether nginx is turned on automatically. disabled is turned off and enabled is turned on. You can see that it is turning off automatically. If you want to add nginx to the startup automatically, you can enter the command systemctl enable nginx, as shown in the following figure

start-up nginx And view nginx state
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-04-25 04:32:39 CST; 17min ago
 Main PID: 7201 (nginx)
   CGroup: /system.slice/nginx.service
           ├─7201 nginx: master process /app/nginx/sbin/nginx
           └─7202 nginx: worker process
 Can see running It means that the service is started, or if you see a green sign, it means that the service is actually started
 Now you can check nginx Port for
[root@localhost ~]# netstat -lntup|grep nginx
tcp        0      0    *               LISTEN      7201/nginx: master  
Can see nginx The port of is 90, and the default is 80, because I changed it last time nginx The configuration file for changed the port to 90
[root@localhost ~]# netstat -lntup|grep 90
tcp        0      0    *               LISTEN      7201/nginx: master  

Let's change the configuration file to 80. First, find the nginx configuration file. Mine is in /app/nginx

cd /app/nginx/conf/

vim nginx.conf

Then we can reload nginx without stopping the service

[root@localhost nginx]# systemctl reload nginx
[root@localhost nginx]# netstat -lntup|grep nginx
tcp        0      0    *               LISTEN      7201/nginx: master  
[root@localhost nginx]# 

Systemctl reload nginx is to reload the configuration file without stopping the service. It is a common method used by enterprises to ensure the user experience. You can see that the port is changed to 80

Check whether the next service is running

[root@localhost ~]# systemctl is-active nginx
 You can see that the status service is running

We can disable the service

[root@localhost ~]# systemctl mask nginx
Created symlink from /etc/systemd/system/nginx.service to /dev/null.
Take another look at the service
[root@localhost ~]# systemctl is-active nginx
 Into this state
 Cancel the prohibition for the service at this time
[root@localhost ~]# systemctl unmask nginx suppress
[root@localhost ~]# systemctl restart nginx restart service
[root@localhost ~]# systemctl is-active nginx 
active View service running status
[root@localhost ~]# systemctl stop nginx shutdown service
[root@localhost ~]# netstat -lutup|grep nginx viewing port
[root@localhost ~]# ps -ef |grep nginx view service process
root       7466   7165  0 05:16 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# 
[root@localhost ~]# systemctl is-enabled nginx
 Specify view nginx Whether the machine starts automatically

Posted by serverman on Wed, 01 Jun 2022 02:28:43 +0530