天天看點

cmd 順序啟動服務_systemd設定nginx開機自啟動Welcome to nginx!

1、簡介

伺服器每次重新開機,都需要手動啟動一些服務,這不是一個程式員可以忍受的,難怪大家都喜歡寫腳本。CentOS7之後已不再使用chkconfig管理啟動項,而是使用systemd。

Linux系統從啟動到提供服務的過程是這樣,先是機器加電,然後通過MBR或者UEFI加載GRUB,再啟動核心,核心啟動服務,然後開始對外服務。

但是随着移動網際網路的到來,init服務啟動慢的問題顯得越來越突出,許多移動裝置都是基于Linux核心,比如安卓。移動裝置啟動比較頻繁,每次啟動都要等待服務順序啟動,顯然難以接受,systemd就是為了解決這個問題誕生的。在CentOS7中使用,其配置檔案為/usr/lib/systemd/system/ 和 /etc/systemd/system/ 中的檔案。 systemd的設計思路是:盡可能的快速啟動服務;盡可能的減少系統資源占用。

2、systemd使用

systemctl指令主要負責控制systemd系統和服務管理器。基本取代了service和chkconfig指令,雖然service和chkconfig指令依然保留,但是據說已經被閹割過。

曆史上,linux的啟動一直采用init程序,比如

[[email protected] ~]# sudo /etc/init.d/apache start# 或者[[email protected] ~]# service apache start
           

優點

  • 原理簡單,易于了解;依靠shell腳本控制,編寫服務腳本門檻比較低。

這種方法有兩個缺點。

  • 一是啟動時間長。

    init

    程序是串行啟動,隻有前一個程序啟動完,才會啟動下一個程序。
  • 二是啟動腳本複雜。

    init

    程序隻是執行啟動腳本,不管其他事情。腳本需要自己處理各種情況,這往往使得腳本變得很長。

Systemd 就是為了解決這些問題而誕生的。它的設計目标是,為系統的啟動和管理提供一套完整的解決方案。根據 Linux 慣例,字母

d

是守護程序(daemon)的縮寫。Systemd 這個名字的含義,就是它要守護整個系統。使用了 Systemd,就不需要再用

init

了。Systemd 取代了

initd

,成為系統的第一個程序(PID 等于 1),其他程序都是它的子程序。

3、常用指令

systemctl --version # 檢視版本。whereis systemctl # 檢視位置。systemctl list-unit-files # 列出所有可用單元(服務)。systemctl list-units # 列出所有運作中的單元。systemctl --failed # 列出所有失敗的單元。systemctl list-unit-files | grep enable # 檢視自啟動的軟體。systemctl is-enabled mysqld.service # 檢視某個單元是否開機啟動。systemctl status mysqld.service # 檢視某個單元的狀态。systemctl start mysqld.service # 啟動某個單元。systemctl restart mysqld.service # 重新開機某個單元。systemctl stop mysqld.service # 停止某個單元。systemctl daemon-reload # 修改了某個單元的配置檔案後,重載配置檔案。systemctl reload mysqld.service # 重載某個單元。systemctl enable mysqld.service # 設定開機自啟動。systemctl disable mysqld.service # 關閉開機自啟動。systemctl kill mysqld # 殺死單元。
           

4、手動安裝nginx

4.1安裝gcc等編譯環境

[[email protected] ~]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel openssl openssl-devel
           

4.2下載下傳nginx1.12.0并解壓

[[email protected] ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz[[email protected] ~]# tar -xzvf nginx-1.18.0.tar.gz[[email protected] ~]# cd nginx-1.18.0[[email protected] nginx-1.8.0]#
           

4.3建立目錄

[[email protected] nginx-1.8.0]# mkdir -p /var/temp[[email protected] nginx-1.8.0]# mkdir -p /var/temp/nginx[[email protected] nginx-1.8.0]# mkdir -p /var/temp/run/nginx[[email protected] nginx-1.8.0]# chmod a+wrx -R temp[[email protected] nginx-1.8.0]# chmod a+wrx -R /var/temp
           

4.4配置編譯選項

[[email protected] nginx-1.8.0]#./configure \--prefix=/usr/local/nginx \--pid-path=/var/temp/run/nginx/nginx.pid \--lock-path=/var/lock/nginx.lock \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--with-http_gzip_static_module \--http-client-body-temp-path=/var/temp/nginx/client \--http-proxy-temp-path=/var/temp/nginx/proxy \--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \--http-scgi-temp-path=/var/temp/nginx/scgi# 切記,pid-path不能設定為/var/run/nginx/nginx.pid。因為CentOS每次重新開機後,都會删除/var/run目錄中的自建目錄和檔案,進而導緻nginx自啟動失敗。
           

4.5編譯安裝

[[email protected] nginx-1.18.0]# make && make install
           

進入/usr/local/nginx檢視檔案是否存在conf、sbin、html檔案夾,若存在則安裝成功

4.6測試啟動

[[email protected] ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           

如果不指定-c,nginx在啟動時預設加載conf/nginx.conf檔案。

4.7測試通路

[[email protected] ~]# curl localhostWelcome to nginx!    body {        width: 35em;        margin: 0 auto;        font-family: Tahoma, Verdana, Arial, sans-serif;    }
           

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer to

href="http://nginx.org/" target="_blank" rel="external nofollow" >nginx.org.Commercial support is available athref="http://nginx.com/" target="_blank" rel="external nofollow" >nginx.com.

Thank you for using nginx.

4.8浏覽器如果不能通路,就打開防火牆或者開端口

關閉防火牆

[[email protected] ~]# systemctl stop firewalld.service
           

開放端口

[[email protected] ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent[[email protected] ~]# firewall-cmd --reload
           

5、設定開機啟動

手動安裝的nginx,該怎樣設定開機自啟動?

5.1在系統服務目錄裡建立nginx.service檔案

[[email protected] ~]# vi /usr/lib/systemd/system/nginx.service # 寫入内容如下[Unit]Description=nginxAfter=network.target  [Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s quitPrivateTmp=true  [Install]WantedBy=multi-user.target
           

5.2解釋

[Unit]:服務的說明Description:描述服務After:描述服務類别[Service]服務運作參數的設定Type=forking是背景運作的形式ExecStart為服務的具體運作指令ExecReload為重新開機指令ExecStop為停止指令PrivateTmp=True表示給服務配置設定獨立的臨時空間注意:[Service]的啟動、重新開機、停止指令全部要求使用絕對路徑[Install]運作級别下服務安裝的相關設定,可設定為多使用者,即系統運作級别為3
           

5.3設定開機自啟動

[[email protected] ~]# systemctl enable nginx.serviceCreated symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
           

5.4檢視nginx狀态

[[email protected] ~]# systemctl status nginx.service● nginx.service - nginx   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor>   Active: inactive (dead)#  很奇怪,明明啟動成功了,為什麼顯示Active: inactive (dead)?
           

5.5殺死nginx重新開機nginx

[[email protected] ~]# pkill -9 nginx[[email protected] ~]# ps -aux|grep nginxroot      29703  0.0  0.1  12112  1108 pts/0    R+   04:10   0:00 grep --color=auto nginx[[email protected] ~]# systemctl start nginx
           

再次檢視狀态,變成了active,搞定。

5.6重新開機伺服器

[[email protected] ~]# reboot
           

5.7再次連接配接後,檢視服務狀态

[[email protected] ~]# systemctl status nginx● nginx.service - nginx   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor>   Active: active (running) since Mon 2020-08-10 04:11:56 EDT; 1min 17s a>  Process: 905 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status> Main PID: 913 (nginx)    Tasks: 2 (limit: 4879)   Memory: 2.5M   CGroup: /system.slice/nginx.service           ├─913 nginx: master process /usr/local/nginx/sbin/nginx           └─917 nginx: worker processAug 10 04:11:56 localhost.localdomain systemd[1]: Starting nginx...Aug 10 04:11:56 localhost.localdomain systemd[1]: Started nginx.
           

看到nginx已經啟動,至此,nginx自啟動配置成功。

6、systemd-analyze檢視啟動耗時

# 檢視啟動耗時[[email protected] ~]# systemd-analyze                                                                                      # 檢視每個服務的啟動耗時[[email protected] ~]# systemd-analyze blame# 顯示瀑布狀的啟動過程流[[email protected] ~]# systemd-analyze critical-chain# 顯示指定服務的啟動流[[email protected] ~]# critical-chain atd.service
           

7、依賴關系

Unit 之間存在依賴關系:A 依賴于 B,就意味着 Systemd 在啟動 A 的時候,同時會去啟動 B。

# 列出一個 Unit 的所有依賴[[email protected] ~]# systemctl list-dependencies nginx.service
           

上面指令的輸出結果之中,有些依賴是 Target 類型,預設不會展開顯示。如果要展開 Target,就需要使用

--all

參數。

[[email protected] ~]# systemctl list-dependencies --all nginx.service
           

如果文章有任何錯誤歡迎不吝賜教,其次大家有任何關于運維的疑難雜問,也歡迎和大家一起交流讨論。關于運維學習、分享、交流,筆者開通了微信公衆号【運維貓】,感興趣的朋友可以關注下,歡迎加入,建立屬于我們自己的小圈子,一起學運維知識。群主還經營一家Orchis飾品店,喜歡的小夥伴歡迎👏前來下單。

掃描二維碼

擷取更多精彩

運維貓公衆号

cmd 順序啟動服務_systemd設定nginx開機自啟動Welcome to nginx!

有需要技術交流的小夥伴可以加我微信,期待與大家共同成長,本人微信:

掃描二維碼

添加私人微信

運維貓部落客

cmd 順序啟動服務_systemd設定nginx開機自啟動Welcome to nginx!

掃碼加微信

最近有一些星友咨詢我知識星球的事,我也想繼續在星球上釋出更優質的内容供大家學習和探讨。運維貓公衆号平台緻力于為大家提供免費的學習資源,知識星球主要緻力于即将入坑或者已經入坑的運維行業的小夥伴。

cmd 順序啟動服務_systemd設定nginx開機自啟動Welcome to nginx!
cmd 順序啟動服務_systemd設定nginx開機自啟動Welcome to nginx!

點選閱讀原文  檢視更多精彩内容!!!