linux 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
1026 01:25:47 web01 systemd[1]: Stopped nginx - high performance web server.
1026 01:25:47 web01 systemd[1]: Starting nginx - high performance web server...
1026 01:25:47 web01 nginxd[7561]: nginx is started. [  确定  ]
1026 01:25:47 web01 systemd[1]: Can't open PID file /application/nginx/logs/nginx.pid ...ory
1026 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tony带水!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值