靳闯博客 记录是一种习惯,分享是一种态度
  • 波浪
  • 波浪
  • 波浪
  • 波浪
Nginx支持web界面执行bash.python等脚本
发表于: | 分类: 技术积累 | 标签: python nginx shell web | 评论:0 | 阅读: 640
一直以来都是在终端执行shell等脚本,那web界面是否也可以这样,当然也是可以的!

参考自 [让Nginx支持CGI执行Bash,Python等脚本]文章,详细的原理和内容请看此篇文章。


安装使用

1,安装spawn-fcgi

下载:去github下载最新代码 https://github.com/lighttpd/spawn-fcgi 
解压:unzip spawn-fcgi-master.zip
安装:
mv spawn-fcgi-master spawn-fcgi
cd spawn-fcgi
./autogen.sh
./configure
make && make install

2,还需要安装fcgi

安装epel源【各种rpm包都比较全的】,我这里直接yum安装就ok
yum install fcgi fcgi-devel -y

3,安装fcgiwrap

下载:去github下载最新代码 https://github.com/gnosek/fcgiwrap
解压: unzip spawn-fcgi-master.zip
安装:
mv fcgiwrap-master fcgiwrap
cd fcgiwrap
autoreconf -i
./configure
make && make install

4,fcgiwrap 启动脚本【nginx通过转发到这个上面来执行脚本命令】,脚本启用用户要和你nginx启动用户一致,注意下脚本中路径

#! /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

5,启动

chmod +x /etc/init.d/fcgiwrap
chkconfig --add fcgiwrap
chkconfig --level 2345 fcgiwrap on

/etc/init.d/fcgiwrap start
Starting fcgiwrap... spawn-fcgi: child spawned successfully: PID: 22416
spawn-fcgi: child spawned successfully: PID: 22417
spawn-fcgi: child spawned successfully: PID: 22418
spawn-fcgi: child spawned successfully: PID: 22419
spawn-fcgi: child spawned successfully: PID: 22420
done

6,nginx配置

#注意下修改为你的目录路径
location ~ ^/cgi-bin/cgi/.*\.(cgi) {  #这里的后缀匹配根据需要修改,可以使用统一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

7,脚本测试

创建目录放置脚本,目录和nginx配置中一致
mkdir cgi-bin 放置首页html
mkdir -p cgi-bin/cgi 放置脚本

8,访问首页 index.html 示例【所有页面的内容每秒刷新一次,保证是最新的数据】
webshellindex.png
查看日志
cgi-bin1.png
9,shell脚本cgi代码 示例

#!/bin/bash
echo "Content-Type:text/html;charset=utf-8"
echo "" 
#前3行必须是这个格式,html代码使用echo,linux命令和脚本语法一样
echo "<meta http-equiv="refresh" content="1">"
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>'

10,python脚本代码示例参考:Python CGI编程

average.png

disk.png



如果这篇文章帮助到了你,我感到十分荣幸!

或许你不想写点什么......

icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif

站点地图 网站地图
豫ICP备17003270号 | Copyright © 2017-2018 💖 靳闯博客
Typecho🍹Sgreen
TOP