微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

二进制编译安装nginx并加入systemctl管理服务

一、安装Nginx所需环境

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

#yum install gcc make -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

#可以不安装定zlib --without

④ 编译、安装

# 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 ~]#

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐