linux软件管理之------编译安装nginx服务器并手动编写自动化运行脚本

  红帽系列的 linux软件管理分为三类:1. rpm 安装软件。2. yum 安装软件。3. 源码包编译安装。前面两种会在相关专题给出详细讲解。源码包的编译安装是很是关键的,咱们知道linux的相关版本很是多,相关的编译器,解释器也有不少,不少还有最小系统,嵌入式系统等等。同一功能的软件若是只有编译好的软件包,在其它linux的平台上,可能并不能正常安装运行,在此状况下,源码包编译安装出现了。因此本文的重点是以nginx为例,给出源码包编译安装的详细过程,同时带你手工编写自动化运行脚本。html

 

准备工做:nginx源码包,官网地址:http://nginx.org/en/download.html
linux

 能够看一下:长下面的这样子:nginx

 

1、编译安装nginx源码包。shell

1. 用xshell将下载的nginx源码包放到/root目录下面。固然你虚拟机能够上网,在虚拟机中用wget下载也是能够的。建议不要这样作,由于虚拟机上网通常比较慢。vim

 

2. 安装依赖的软件包工具 zlib-devel  pcre-devel,这里用yum安装这两个包。ide

 rpm -qa | grep  zlib-devel函数

 rpm -qa | grep  pcre-devel    #检查这两个包是否已经安装了。工具

 这里用yum 安装这两个软件包,yum安装很是好的一点就是,你只要将包名知道就能够了,不须要包的版本信息及依赖包,而rpm安装,须要包的全名,包括版本信息,后缀名还须要本身安装先关的依赖包等,不是很方便。测试

 yum install zlib-devel # yum 安装这个软件包。固然了,后面能够带上-y,不须要最后确认安装。spa

 yum install pcre-devel -y 

 

  

  

 

3. 指定nginx的运行用户。

  useradd -s /sbin/nologin -M nginx

 

   useradd 添加用户。

  -s /sbin/nologin 指定用户运行的shell。

  -M  再也不home目录下建立该用户的目录。

 

 4. 解包,配置,编译,安装nginx

 

      解包:tar zxf nginx-1.11.2.tar.gz -C /usr/src

    配置:./configure --prefix=/usr/local/nginx --user=nginx --group=nginx

   

     

    编译: make -j 4 

   

    安装:make install 

    

   

   

2、编写nginx启动脚本:

 1. 系统的脚本服务,通常放在这个目录下面:/etc/init.d中,咱们也放到这里。

 2. 编写nginx启动的脚本。

    vim nginx 在文件nginx中编写以下脚本:

 

# description: nginx-server

nginx=/usr/local/nginx/sbin/nginx
case "$1" in
        start)
                netstat -anlpt | grep nginx
            if
                [ $? -eq 0 ]
             then
                echo " the nginx-server is already running"
            else
                echo " ther nginx-server is starting to run"
                $nginx
            fi
         ;;

       stop)
              netstat -anlpt | grep nginx
                if 
                [ $? -eq 0 ]
              then
                   $nginx -s stop
                   if [ $? -eq 0 ]
                      then
                          echo " the nginx-server is stopped " 
                   else
                          echo " failed to stop the nginx-server" 
                  fi
            else
               echo " the nginx-server has stopped you needn't to stop it " 
            fi
         ;;
      restart)
                 $nginx -s reload
             if 
                 [ $? -eq 0 ]
               then
                  echo "the nginx-server is restarting "
              else
                  echo " the nginx-server failed to restart"
             fi
         ;;

        status)
                   netstat -anlpt | grep nginx
             if 
                 [ $? -eq 0 ]
               then
                   echo " the nginx-server is running "
            else
                   echo " the nginx-server is not running ,please try again" 
             fi
       ;;

        status)
                   netstat -anlpt | grep nginx
             if 
                 [ $? -eq 0 ]
               then
                   echo " the nginx-server is running "
            else
                   echo " the nginx-server is not running ,please try again" 
             fi
         ;;
        *)
               echo "please enter { start|stop|status|restart}"
        ;;
esac
View Code

 

     

    

   

  

 3. 给脚本添加权限,并将nginx服务添加到系统服务中:

  

  给脚本添加权限:chmod +x nginx

  将nginx服务添加到系统服务中: chkconfig  –add nginx

  查看nginx的运行级别:        chkconfig  –list nginx 

 

 4. 启动脚本,进行测试:

 

说明:有时候会出现这种错误:

上面的报个小错,当restart时,用lsof –i:80检查一下:发现是刚才的nginx在占用着端口,说明restart中实现该功能的函数有点弱:能够考虑用stop 和 start来替换掉。

 

  声明:本文为博主原创,转载必须注明出处:

http://www.cnblogs.com/jasmine-Jobs/p/5847825.html

相关文章
相关标签/搜索