nginx 源码编译安装并编写服务启动脚本

1. 使用xshell将nginx源码包上传到servernginx

image

2. 安装依赖的软件包工具 zlib-devel   pcre-devel   gcc  gcc-c++c++

yum -y install zlib-devel pcere-devel gcc gcc-c++shell

验证一下:vim

image

3. 指定nginx的运行用户 (建立nginx用户不使其登陆系统、-M不建立宿主目录)ide

[root@Cent65 ~]# useradd -s /sbin/nologin -M nginx工具

4. 编译安装nginx测试

[root@Cent65 ~]# tar zxf nginx-1.11.2.tar.gz -C /usr/src/
[root@Cent65 ~]# cd /usr/src/nginx-1.11.2/spa

image

[root@Cent65 nginx-1.11.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginxrest

image

[root@Cent65 nginx-1.11.2]# make -j 4 && make installserver

编译安装成功

image

如今service不受控制,须要

五、编写nginx 服务启动脚本

vim nginx

image

image


代码复制以下:

# chkconfig: 2345 99 20
#description: nginx-server
nginx=/usr/local/nginx/sbin/nginx
case $1 in

        start)
              netstat -anptu | grep nginx
              if [ $? -eq 0 ]
                 then
                    echo "nginx-server is already running"
                  else
                     echo "nginx-server begin start"
                    $nginx
                 fi
         ;;

        stop)

                $nginx -s stop
                 if [ $? -eq 0 ]
                 then
                    echo "nginx-server is stoped"
                 else
                    echo "nginx-server stop fail,try again"
                 fi
;;

        status)
             netstat -anlpt | grep nginx
                 if [ $? -eq 0 ]
                 then
                    echo "nginx-server is running"
                 else
                    echo "nginx-server is stoped"
fi

;;

        restart)
                 $nginx -s reload
                 if [ $? -eq 0 ]
                 then
                     echo "nginx-server is begin restart"
                 else
                     echo "nginx-server restart fail"
                 fi
;;


         *)
         echo "please enter {start restart status stop}"
;;


六、将脚本文件拷贝到/etc/init.d/目录下

image

测试:

image

相关文章
相关标签/搜索