1. 程式人生 > 實用技巧 >linux系統開機流程和啟動nginx

linux系統開機流程和啟動nginx

開機啟動流程

CentOS6

1.核心引導

加電自檢,檢查bios的配置,檢測硬體,開機

2.執行init

  • 0:關機
  • 1:單使用者模式
  • 2:多使用者模式(沒有檔案系統和網路)
  • 3:多使用者模式(命令列,預設模式)
  • 4:沒有使用的模式
  • 5:多使用者模式(圖形化介面)
  • 6:重啟

3.系統初始化

4.建立終端

5.使用者登入

## 關機命令
init 0
halt
shutdown -h now
shutdown -h 20:20
shutdown -h +10
poweroff

## 重啟命令
init 6
reboot
shutdown -r now
shutdonw -r 20:20
shutdown -r +10

CentOS7開機啟動流程

1.BIOS(開機自檢)
2.MBR ( Master Boot Record 主引導記錄)
3.GRUB2 Bootloader(引導選單)
4.Kernel(核心引導)
5.Systemd (不再使用init,改成了systemd)

6.Runlevel-Target (執行級別)

執行級別:

init 0.target -> poweroff.target		# 關機
init 1.target -> rescue.target			# 單使用者模式
init 2.target -> multi-user.target		# 多使用者模式(沒有檔案系統和網路)
init 3.target -> multi-user.target		# 多使用者模式(命令列)
init 4.target -> multi-user.target		# 多使用者模式(還是沒有被使用)		
init 5.target -> graphical.target		# 圖形化模式
init 6.target -> reboot.target			# 重啟
# 獲取當前預設的執行級別
[root@qls ~]# systemctl get-default
multi-user.target

# 修改執行級別
[root@qls ~]# systemctl set-default poweroff

## 使用兩條命令修改預設執行級別
[root@qls ~]# rm -f /etc/systemd/system/default.target
[root@qls ~]# ln -s /usr/lib/systemd/system/poweroff.target /etc/systemd/system/default.target


## 相關目錄
[root@qls ~]# ll /etc/systemd/system   (預設的執行級別)
[root@qls ~]# ll /usr/lib/systemd/system (執行級別和服務啟動指令碼)

CentOS7進入單使用者模式

在linux16 行末,加上:enforcing=0 init=/bin/bash 修改完之後,按Ctrl + X

## 修改預設啟動方式
bash-4.2# mount -o rw,remount /
bash-4.2# systectl set-default(不能用)
bash-4.2# rm -f /etc/systemd/system/default.target
bash-4.2# ln -s /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target
bash-4.2# exec /sbin/init

## 修改密碼
bash-4.2# mount -o rw,remount /
bash-4.2# echo 123|passwd --stdin root
bash-4.2# exec /sbin/init

方法2:

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

nginx相關命令文件

目錄


檢視系統當前執行級別

N代表上次執行級別3是本次執行級別。

檢視nginx是否開機自啟,disabled是關閉,enabled是開啟,可以看到現在是在關閉開機自啟的狀態,如果想新增到開機自啟可以輸入命令systemctl enable nginx,如下圖所示

啟動nginx並檢視nginx狀態
[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
可以看到running代表服務是啟動的,或者看到有綠色的標誌就代表服務實在開啟狀態
這時可以檢視下nginx的埠
[root@localhost ~]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:90              0.0.0.0:*               LISTEN      7201/nginx: master  
可以看到nginx的埠是90,預設是使用80埠,因為上次我改了一下nginx的配置檔案把埠改成了90
[root@localhost ~]# netstat -lntup|grep 90
tcp        0      0 0.0.0.0:90              0.0.0.0:*               LISTEN      7201/nginx: master  

我們把配置檔案改成80,首先找到nginx的配置檔案我的在/app/nginx裡面

cd /app/nginx/conf/

vim nginx.conf

然後我們可以不停止服務,重新載入nginx

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

Systemctl reload nginx 是不停止服務重新載入配置檔案,企業常用手法,確保使用者體驗,這是可以看到埠改成了80

檢視下服務是否執行

[root@localhost ~]# systemctl is-active nginx
active
可以看到這個狀態服務是在執行中的

我們可以把服務禁止執行

[root@localhost ~]# systemctl mask nginx
Created symlink from /etc/systemd/system/nginx.service to /dev/null.
再看下服務
[root@localhost ~]# systemctl is-active nginx
unknown
變成了這種狀態
這時候給服務取消禁止
[root@localhost ~]# systemctl unmask nginx 取消禁止
[root@localhost ~]# systemctl restart nginx 重啟服務
[root@localhost ~]# systemctl is-active nginx 
active 檢視服務執行狀態
[root@localhost ~]# systemctl stop nginx 關閉服務
[root@localhost ~]# netstat -lutup|grep nginx 檢視埠
[root@localhost ~]# ps -ef |grep nginx 檢視服務程序
root       7466   7165  0 05:16 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# 
[root@localhost ~]# systemctl is-enabled nginx
disabled
指定檢視nginx是否開機自啟