码迷,mamicode.com
首页 > Web开发 > 详细

Nginx支持web界面执行bash脚本

时间:2017-11-07 19:44:37      阅读:446      评论:0      收藏:0      [点我收藏+]

标签:格式   支持   location   统计   user   sed   统一   des   sleep   

一直以来都是在终端执行shell等脚本,那web界面是否也可以这样,当然也是可以的
本文以centos 6为例安装配置

  1. 安装spawn-fcgi
    wget https://github.com/lighttpd/spawn-fcgi/archive/spawn-fcgi-1.6.4.zip
    unzip spawn-fcgi-1.6.4.zip && rm spawn-fcgi-1.6.4.zip 
    mv spawn-fcgi-spawn-fcgi-1.6.4/ spawn-fcgi
    cd spawn-fcgi/
    ./autogen.sh 
    ./configure 
    make && make install
  2. 安装fcgiwrap
    yum -y install fcgi fcgi-devel nginx
    wget https://github.com/gnosek/fcgiwrap/archive/1.1.0.zip
    unzip 1.1.0.zip 
    rm 1.1.0.zip 
    mv fcgiwrap-1.1.0/ fcgiwrap
    cd fcgiwrap/
    autoreconf -i
    ./configure 
    make && make install
  3. 添加fcgiwrap 启动脚本
    脚本启用用户要和你nginx启动用户一致,注意下脚本中路径及启动用户
    vim /etc/init.d/fcgiwrap
    技术分享
    #! /bin/bash
    ### BEGIN INIT INFO
    # Provides:          fcgiwrap
    # Required-Start:    $remote_fs
    # Required-Stop:     $remote_fs
    # Should-Start:
    # Should-Stop:
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: FastCGI wrapper
    # Description:       Simple server for running CGI applications over FastCGI
    ### END INIT INFO
    
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    SPAWN_FCGI="/usr/local/bin/spawn-fcgi"
    DAEMON="/usr/local/sbin/fcgiwrap"
    NAME="fcgiwrap"
    
    PIDFILE="/var/run/$NAME.pid"
    
    FCGI_SOCKET="/tmp/$NAME.socket"
    FCGI_USER="nginx"
    FCGI_GROUP="nginx"
    FORK_NUM=5
    SCRIPTNAME=/etc/init.d/$NAME
    
    case "$1" in
        start)
            echo -n "Starting $NAME... "
    
            PID=`pidof $NAME`
            if [ ! -z "$PID" ]; then
                echo " $NAME already running"
                exit 1
            fi
    
            $SPAWN_FCGI -u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -P $PIDFILE -F $FORK_NUM -f $DAEMON
    
            if [ "$?" != 0 ]; then
                echo " failed"
                exit 1
            else
                echo " done"
            fi
        ;;
    
        stop)
            echo -n "Stoping $NAME... "
    
            PID=`pidof $NAME`
            if [ ! -z "$PID" ]; then
                kill `pidof $NAME`
                if [ "$?" != 0 ]; then
                    echo " failed. re-quit"
                    exit 1
                else
                    rm -f $pid
                    echo " done"
                fi
            else
                echo "$NAME is not running."
                exit 1
            fi
        ;;
    
        status)
            PID=`pidof $NAME`
            if [ ! -z "$PID" ]; then
                echo "$NAME (pid $PID) is running..."
            else
                echo "$NAME is stopped"
                exit 0
            fi
        ;;
    
        restart)
            $SCRIPTNAME stop
            sleep 1
            $SCRIPTNAME start
        ;;
    
        *)
            echo "Usage: $SCRIPTNAME {start|stop|restart|status}"
            exit 1
        ;;
    esac
    fcgiwrap

    chmod +x /etc/init.d/fcgiwrap

  4. 启动
    chkconfig --add fcgiwrap
    chkconfig --level 2345 fcgiwrap on
    ./etc/init.d/fcgiwrap restart
  5. nginx配置
    测试环境编辑/etc/nginx/conf.d/default.conf,添加
        location ~ ^/cgi-bin/cgi/.*\.(cgi|sh) {  #这里的后缀匹配根据需要修改,可以使用统一cgi后缀
            gzip off;
            fastcgi_pass unix:/tmp/fcgiwrap.socket;
            fastcgi_index index.cgi;
            include fastcgi_params;
            fastcgi_param  SCRIPT_NAME $document_root$fastcgi_script_name;
        }

    重启nginx:
    nginx -s reload
    mkdir -p /usr/share/nginx/html/cgi-bin 放置首页html
    mkdir -p /usr/share/nginx/html/cgi-bin/cgi 放置脚本

  6. 脚本测试
    vim  /usr/share/nginx/html/cgi-bin/cgi/disk.cgi
    技术分享
    #!/bin/bash
    echo "Content-Type:text/html;charset=utf-8"
    echo "" 
    #前3行必须是这个格式,html代码使用echo,linux命令和脚本语法一样
    
    echo <div style="padding-left:10px;">
    echo <h1 style="color:red;border-left:4px solid;padding:4px;">硬盘使用情况</h1>
    echo <h5>
    dd=`date`
    echo "统计时间: $dd"
    echo </h5>
    echo <pre style="border-left: 4px solid rgb(12, 40, 245);padding:5px">
    df -hT
    echo </pre>
    disk.cgi

    访问:http://your-ip/cgi-bin/cgi/disk.cgi

    技术分享

Nginx支持web界面执行bash脚本

标签:格式   支持   location   统计   user   sed   统一   des   sleep   

原文地址:http://www.cnblogs.com/Mrhuangrui/p/7800288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!