1. 程式人生 > >Nginx支持web界面執行bash腳本

Nginx支持web界面執行bash腳本

arc mkdir -p pad 必須 rip log != ati lin

一直以來都是在終端執行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腳本