二進制編譯安裝nginx並加入systemctl管理服務


一、安裝nginx所需環境

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

二、安裝nginx

① 下載nginx

# wget -c https://nginx.org/download/nginx-1.12.1.tar.gz

② 解壓

# tar -zxvf nginx-1.17.0.tar.gz
# cd nginx-1.17.0

 ③ 使用默認配置

# ./configure

④ 編譯、安裝

# make
# make install

⑤ 啟動nginx

# cd /usr/local/nginx/sbin/
# ./nginx 

其它命令
# ./nginx -s stop
# ./nginx -s quit
# ./nginx -s reload

三、加入systemctl管理服務

1、 進入到  /usr/lib/systemd/system 目錄下,編輯文件 nginx.service

# cd /usr/lib/systemd/system
# vi nginx.service

2、在nginx.service文件中加入以下代碼

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

 

參數說明:

注意:[Service]的啟動、重啟、停止命令全部要求使用絕對路徑

[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是后台運行的形式
ExecStart為服務的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
[Install]運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3

 

3、設置開機啟動

# systemctl enable nginx.service

 

4、取消開機自啟動

# systemctl disable nginx.service

 

5、其他命令

啟動nginx服務

# systemctl start nginx.service 

查看服務當前狀態

# systemctl status nginx

[root@localhost system]# 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 2019-03-23 07:08:49 CST; 7s ago
  Process: 18942 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 18943 (nginx)
   CGroup: /system.slice/nginx.service
           ├─18943 nginx: master process /usr/local/nginx/sbin/nginx
           └─18944 nginx: worker process

Mar 23 07:08:49 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 23 07:08:49 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.

 

 

停止nginx服務

# systemctl stop nginx

 

重新啟動服務

# systemctl restart nginx

 

查看所有已啟動的服務

# systemctl list-units --type=service

[root@localhost ~]# systemctl list-units --type=service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
auditd.service                     loaded active running Security Auditing Service
crond.service                      loaded active running Command Scheduler
dbus.service                       loaded active running D-Bus System Message Bus
getty@tty1.service                 loaded active running Getty on tty1
kdump.service                      loaded active exited  Crash recovery kernel arming
kmod-static-nodes.service          loaded active exited  Create list of required static device nodes for the current kernel
lvm2-lvmetad.service               loaded active running LVM2 metadata daemon
lvm2-monitor.service               loaded active exited  Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling
lvm2-pvscan@8:2.service            loaded active exited  LVM2 PV scan on device 8:2
mariadb.service                    loaded active running MariaDB database server
network.service                    loaded active exited  LSB: Bring up/down networking
NetworkManager.service             loaded active running Network Manager
nginx.service                      loaded active running The nginx HTTP and reverse proxy server
polkit.service                     loaded active running Authorization Manager
postfix.service                    loaded active running Postfix Mail Transport Agent
rhel-dmesg.service                 loaded active exited  Dump dmesg to /var/log/dmesg
rhel-import-state.service          loaded active exited  Import network configuration from initramfs
rhel-readonly.service              loaded active exited  Configure read-only root support
rsyslog.service                    loaded active running System Logging Service
sshd.service                       loaded active running OpenSSH server daemon
systemd-journal-flush.service      loaded active exited  Flush Journal to Persistent Storage
systemd-journald.service           loaded active running Journal Service
systemd-logind.service             loaded active running Login Service
systemd-random-seed.service        loaded active exited  Load/Save Random Seed
systemd-remount-fs.service         loaded active exited  Remount Root and Kernel File Systems
systemd-sysctl.service             loaded active exited  Apply Kernel Variables
systemd-tmpfiles-setup-dev.service loaded active exited  Create Static Device Nodes in /dev
systemd-tmpfiles-setup.service     loaded active exited  Create Volatile Files and Directories
systemd-udev-trigger.service       loaded active exited  udev Coldplug all Devices
systemd-udevd.service              loaded active running udev Kernel Device Manager
systemd-update-utmp.service        loaded active exited  Update UTMP about System Boot/Shutdown
systemd-user-sessions.service      loaded active exited  Permit User Sessions
systemd-vconsole-setup.service     loaded active exited  Setup Virtual Console
tuned.service                      loaded active running Dynamic System Tuning Daemon
wpa_supplicant.service             loaded active running WPA Supplicant daemon

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

35 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
[root@localhost ~]#

 

參考博客:https://blog.csdn.net/qq_36441027/article/details/80636526


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM