Nginx启动脚本的编写和测试

Nginx启动脚本的编写

安装nginx:

使用yum搜索nginx的安装包

[root@localhost ~]# yum search nginx

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

================================ N/S matched: nginx =================================

pcp-pmda-nginx.x86_64 : Performance Co-Pilot (PCP) metrics for the Nginx Webserver

 

  Name and summary matches only, use "search all" for everything.

#发现没有我们要的nginx包

所以我们就再官nginx.org上下载

 

前面我写过在官网上选择适合自己系统的软件包

相关技巧可以参考我写的《linux系统软件包常用发布网址和各种linux系统的介绍(自己积累)》这篇文章

 

[root@localhost ~]# tar zxf nginx-1.16.0.tar.gz

[root@localhost ~]# ls

anaconda-ks.cfg  Downloads             nginx-1.16.0         Public     Videos

Desktop          initial-setup-ks.cfg  nginx-1.16.0.tar.gz  Templates

Documents        Music                 Pictures             testing

[root@localhost ~]# cd nginx-1.16.0/

[root@localhost nginx-1.16.0]# pwd

/root/nginx-1.16.0

 

[root@localhost nginx-1.16.0]# yum install gcc openssl pcre-devel -y

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

Package 1:openssl-1.0.1e-60.el7.x86_64 already installed and latest version

Resolving Dependencies

--> Running transaction check

……

Installed:

  gcc.x86_64 0:4.8.5-11.el7            pcre-devel.x86_64 0:8.32-15.el7_2.1          

 

Dependency Installed:

  cpp.x86_64 0:4.8.5-11.el7               glibc-devel.x86_64 0:2.17-157.el7        

  glibc-headers.x86_64 0:2.17-157.el7     kernel-headers.x86_64 0:3.10.0-514.el7   

  libmpc.x86_64 0:1.0.1-3.el7             mpfr.x86_64 0:3.1.1-4.el7                

 

Complete!

 

[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx

checking for OS

 + Linux 3.10.0-514.el7.x86_64 x86_64

checking for C compiler ... found

 + using GNU C compiler

 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)

checking for gcc -pipe switch ... found

checking for -Wl,-E switch ... found

checking for gcc builtin atomic operations ... found

checking for C99 variadic macros ... found

checking for gcc variadic macros ... Found

……

checking for PCRE library ... found

checking for PCRE JIT support ... found

checking for zlib library ... not found

 

./configure: error: the HTTP gzip module requires the zlib library.

You can either disable the module by using --without-http_gzip_module

option, or install the zlib library into the system, or build the zlib library

statically from the source with nginx by using --with-zlib=<path> option.

编译报错排错过程

根据报错:

加上了参数  --without-http_gzip_module

 

[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --without-http_gzip_module

checking for OS

 + Linux 3.10.0-514.el7.x86_64 x86_64

checking for C compiler ... found

 + using GNU C compiler

 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)

checking for gcc -pipe switch ... found

……

Configuration summary

  + using system PCRE library

  + OpenSSL library is not used

  + zlib library is not used

 

  nginx path prefix: "/usr/local/nginx"

  ……

  nginx http scgi temporary files: "scgi_temp"

 

[root@localhost nginx-1.16.0]# make && make install

make -f objs/Makefile

make[1]: Entering directory `/root/nginx-1.16.0'

……

         src/core/nginx.c

……

test -d '/usr/local/nginx/logs' \

         || mkdir -p '/usr/local/nginx/logs'

make[1]: Leaving directory `/root/nginx-1.16.0'

 

加载系统函数库

设定nginx启动命令路径

[root@localhost ~]# cd /etc/init.d/

[root@localhost init.d]# vim nginxd

[root@localhost init.d]# cat nginxd

#!/bin/bash

. /etc/init.d/functions    #加载系统函数库

path=/usr/local/nginx/sbin   #设定nginx启动命令路径

function start(){
         if [ `netstat -antlpe|grep nginx|wc -l` -eq 0 ];then
             $path/nginx
             RETVAL=$?
             if [ $RETVAL -eq 0 ];then
                  action "nginx is started" /bin/true
                  return $RETVAL
             else
                  action "nginx is started" /bin/false
                  return $RETVAL
             fi
         else
             echo "nginx is running"
         fi
}

function stop() {

         if [ `netstat -antlpe|grep nginx|wc -l` -ne 0 ];then
             $path/nginx -s stop
             RETVAL=$?
             if [ $RETVAL -eq 0 ];then
                action "nginx is stoped" /bin/true
                return $RETVAL
            else
                action "nginx is stoped" /bin/false
                return $RETVAL
            fi
        else
            echo "nginx is no running"
            return 0
         fi
}

case "$1" in
    start)
        start
         ;;
    stop)
         stop
         ;;
    restart)
         stop
         sleep 1
         start
         ;;
    *)
         echo $"Usage: $0 {start|stop|restart}"
esac

 

 

[root@localhost init.d]# sh nginxd start

nginx is running

[root@localhost init.d]# sh nginxd stop

nginx is stoped                                            [  OK  ]

[root@localhost init.d]# sh nginxd start

nginx is started                                           [  OK  ]

[root@localhost init.d]# sh nginxd restart

nginx is stoped                                            [  OK  ]

nginx is started                                           [  OK  ]

 

[root@localhost init.d]# chmod +x nginxd

 

[root@localhost init.d]# /etc/init.d/nginxd start

Reloading systemd:                                         [  OK  ]

Starting nginxd (via systemctl):                           [  OK  ]

[root@localhost init.d]# /etc/init.d/nginxd stop

Stopping nginxd (via systemctl):                           [  OK  ]

[root@localhost init.d]# /etc/init.d/nginxd status

Usage: /etc/init.d/nginxd {start|stop|restart}

[root@localhost init.d]# /etc/init.d/nginxd restart

Restarting nginxd (via systemctl):                         [  OK  ]

 

发布了150 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43309149/article/details/104362231