nginx编译版systemctl 启动关闭配置
一、配置启动脚本
环境:centos7.8
①脚本编写
[root@web01 /etc/init.d]#cat nginxd
#!bin/bash
#Author:cwy
#Blog:http://xxx.com
#Time:2020-10-26 00:53:39
#Name:nginx.sh
#Version:VI.0
#description:stop/start nginx server
path=/application/nginx/sbin
retval=0
. /etc/init.d/functions
function start(){
if [ `netstat -lntup|grep nginx|wc -l` -eq 0 ];then
$path/nginx
retval=$?
if [ $retval -eq 0 ];then
action $"nginx is started." /bin/true
else
action $"nginx is started." /bin/false
return $retval
fi
else
echo "nginx is running."
return 0
fi
}
function stop(){
if [ `netstat -lntup|grep nginx|wc -l` -eq 1 ];then
$path/nginx -s stop
retval=$?
if [ $retval -eq 0 ];then
action $"nginx is stopped." /bin/true
return $retval
else
action $"nginx is stopped." /bin/false
return $retval
fi
else
echo "nginx is no running."
return $retval
fi
}
case "$1" in
start)
start
retavl=$?
;;
stop)
stop
retval=$?
;;
resart)
stop
sleep 2
start
retval=$?
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit $retval
②赋予执行权限
chmod +x /etc/init.d/nginxd
③测试是否成功
[root@web01 /etc/init.d]#sh nginx.sh start
nginx is running.
[root@web01 /etc/init.d]#sh nginx.sh stop
nginx is stopped.
二、配置server文件
#注意启动脚本与nginx编译安装时的路径
①编写server
[root@web01 /usr/lib/systemd/system]#cat nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/application/nginx/logs/nginx.pid
ExecStart=/etc/init.d/nginxd start
ExecReload=/etc/init.d/nginxd restart
ExecStop=/etc/init.d/nginxd stop
[Install]
WantedBy=multi-user.target
②给权限
chmod +x /usr/lib/systemd/system/nginx.service
③测试
[root@web01 /usr/lib/systemd/system]#systemctl stop nginx.service
[root@web01 /usr/lib/systemd/system]#lsof -i :80
[root@web01 /usr/lib/systemd/system]#systemctl start nginx.service
[root@web01 /usr/lib/systemd/system]#lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 7542 root 6u IPv4 94228 0t0 TCP *:http (LISTEN)
nginx 7544 nginx 6u IPv4 94228 0t0 TCP *:http (LISTEN)
[root@web01 /usr/lib/systemd/system]#systemctl restart nginx.service
[root@web01 /usr/lib/systemd/system]#lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 7569 root 6u IPv4 94560 0t0 TCP *:http (LISTEN)
nginx 7571 nginx 6u IPv4 94560 0t0 TCP *:http (LISTEN)
[root@web01 /usr/lib/systemd/system]#systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2020-10-26 01:25:47 CST; 54s ago
Docs: http://nginx.org/en/docs/
Process: 7552 ExecStop=/etc/init.d/nginxd stop (code=exited, status=0/SUCCESS)
Process: 7561 ExecStart=/etc/init.d/nginxd start (code=exited, status=0/SUCCESS)
Main PID: 7569 (nginx)
CGroup: /system.slice/nginx.service
├─7569 nginx: master process /application/nginx/sbin/nginx
└─7571 nginx: worker process
10月 26 01:25:47 web01 systemd[1]: Stopped nginx - high performance web server.
10月 26 01:25:47 web01 systemd[1]: Starting nginx - high performance web server...
10月 26 01:25:47 web01 nginxd[7561]: nginx is started. [ 确定 ]
10月 26 01:25:47 web01 systemd[1]: Can't open PID file /application/nginx/logs/nginx.pid ...ory
10月 26 01:25:47 web01 systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.
三、我的nginx编译安装路径
可参照该博客安装(方法2):https://blog.csdn.net/qq_42468502/article/details/108815245
[root@web01 /application/nginx]#tree -L 2
.
├── client_body_temp
├── conf
│ ├── extra
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ ├── 5
│ ├── 6
│ └── 8
├── html
│ ├── 50x.html
│ ├── blog
│ ├── index.html
│ └── www
├── logs
│ ├── access.log
│ ├── error.log
│ └── nginx.pid
├── proxy_temp
├── sbin
│ ├── nginx
│ └── nginx.old
├── scgi_temp
└── uwsgi_temp