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

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

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

验证一下:

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

[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/

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

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

编译安装成功

现在service不受控制,需要

5、编写nginx 服务启动脚本

vim nginx


代码复制如下:

# 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}"
;;


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

测试: