jasmine-Jobs

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

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

 

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

 可以看一下:长下面的这样子:

 

一、编译安装nginx源码包。

1. 用xshell将下载的nginx源码包放到/root目录下面。当然你虚拟机可以上网,在虚拟机中用wget下载也是可以的。建议不要这样做,因为虚拟机上网一般比较慢。

 

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

 rpm -qa | grep  zlib-devel

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

 这里用yum 安装这两个软件包,yum安装非常好的一点就是,你只要将包名知道就可以了,不需要包的版本信息及依赖包,而rpm安装,需要包的全名,包括版本信息,后缀名还需要自己安装先关的依赖包等,不是很方便。

 yum install zlib-devel # yum 安装这个软件包。当然了,后面可以带上-y,不需要最后确认安装。

 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 

    

   

   

二、编写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

 

 

         

分类:

技术点:

相关文章: