Nginx支持web界面执行bash|python等系统命令和脚本
· 技术积累 · Python Nginx Shell

通过安装spawn-fcgi和fcgiwrap,配合nginx转发实现web界面执行linux命令shell和python等脚本

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

2020-12-28更新(详细安装到使用过程): [Nginx支持web界面执行bash.python等命令和脚本]

使用说明

1,shell命令 | python命令 | 系统支持的都可以
2,不支持交互式显示 | 不支持动态内容显示
3,请求url,实现执行服务器上的脚本

传参使用:参考: Web CGI with Bash scripts


使用要求:Linux系统


软件安装:nginx安装



准备工作

##关闭防火墙
##(centos6)
service iptables stop
chkconfig iptables off
##(centos7)
systemctl stop firewalld
systemctl disable firewalld

#关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

#已有epel源的跳过此步骤,直接安装依赖开始
#centos6 添加epel yum源
wget -O /etc/yum.repos.d/epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo

#centos7 添加epel yum源 
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

#清除缓存 重新生成缓存
yum clean all
yum makecache

安装依赖包

#安装开发包组和相关的依赖包
yum install dh-autoreconf fcgi fcgi-devel -y

安装spawn-fcgi 和 fcgiwrap

#创建存放包的目录(包下载到哪个目录都可以,这里放在/source/目录下)
mkdir /source/ && cd /source/

#安装spawn-fcgi
#github下载最新代码 https://github.com/lighttpd/spawn-fcgi

本地下载:wget https://www.jinchuang.org/novel/lnmp/spawn-fcgi.zip
解压:unzip spawn-fcgi.zip
安装:
mv spawn-fcgi-master spawn-fcgi
cd spawn-fcgi
./autogen.sh
./configure
make && make install

#安装fcgiwrap
#github下载最新代码 https://github.com/gnosek/fcgiwrap

本地下载:wget https://www.jinchuang.org/novel/lnmp/fcgiwrap.zip
解压: unzip fcgiwrap.zip
安装:
mv fcgiwrap-master fcgiwrap
cd fcgiwrap
autoreconf -i
./configure
make && make install

创建fcgiwrap启动脚本【nginx通过转发请求到这里来执行脚本命令】,脚本启动用户要和你nginx启动用户一致,注意下脚本中2个命令的路径是否和你的一致

cd /etc/init.d/
vim 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

#添加到服务里面(centos6系统执行,centos7跳过此步骤)
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

nginx配置转发 (系统安装好nginx)

#注意下修改为你的目录路径
#location ~ ^/linux-shell/page/script/.*\.(cgi) {  #这里的cgi后缀匹配根据需要修改,后缀自定义即可

# linux-shell 为模板程序目录,放在nginx网站根目录下面
location ~ ^/linux-shell/page/script/ {  #我这里调用的文件是没有后缀的就用这个配置
        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

5,模板目录结构(文章最下面下载模板)
# 下载模板,放在在网站根目录下
# 目录结构:
linux-shell
├── css
│   ├── iconfont.css
│   ├── iconfont.eot
│   ├── iconfont.svg
│   ├── iconfont.ttf
│   ├── iconfont.woff
│   ├── iconfont.woff2
│   ├── page.css
│   └── style.css
├── favicon.ico
├── images
│   ├── b.jpg
│   ├── body.cur
│   ├── b.png
│   ├── content.jpg
│   ├── hua.gif
│   ├── link.cur
│   ├── logo.png
│   ├── nav.jpg
│   └── page.cur
├── index.html
├── js
│   ├── jquery-2.1.1.min.js
│   └── nav.js
└── page
    ├── content
    │   ├── index.html
    │   ├── script.js
    │   └── TweenMax.min.js
    ├── h5
    │   ├── 161
    │   │   └── 161.html
    │   ├── 188
    │   │   └── 188.html
    │   └── local
    │       └── local.html
    └── script
        ├── 161
        │   ├── disk
        │   ├── info
        │   ├── mem
        │   ├── ps
        │   ├── server
        │   ├── ssh
        │   └── uptime
        ├── 188
        │   ├── disk
        │   ├── info
        │   ├── mem
        │   ├── ps
        │   ├── server
        │   ├── ssh
        │   └── uptime
        └── local
            ├── disk
            ├── info
            ├── mem
            ├── ps
            ├── server
            ├── ssh
            └── uptime

shell代码示例文件(查看磁盘使用情况):

#!/bin/bash
echo "Content-Type:text/html;charset=utf-8"
echo "" 

# 自动刷新
#echo "<script>window.setInterval(function(){
#    window.location.reload();
#},1000);</script>"
#echo "<meta http-equiv="refresh" content="60">"

# html页面css样式
echo '<style>
body{color:#cecece;}
.title{color: #FF9800;border-left: 4px solid;padding: 4px;}
pre{font-size:14px;border-left: 4px solid #4CAF50;padding: 5px;}
</style>'

# 定义变量
ip="192.168.x.x"

# 内容代码(命令返回结果放在

<pre>标签中)
echo '<div style="padding-left:10px;">'
echo '<h1 class="title">硬盘使用情况</h1>'
echo '<h5 style="color:#848484;">'
dd=`date`
echo "统计时间: $dd 【当前机器ip: $ip】"
echo '</h5>'
echo '

<pre>'
# 查看磁盘使用(本机)
df -hT
# 查看磁盘使用(远程机器,可以使用ansible|sshpass等远程工具)
sshpass -p "password" ssh root@$ip -o StrictHostKeyChecking=no  'df -hT'
echo '</pre>

'

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


2020-12-26 更新视频演示效果:

2020-12-26 图片预览:

Nginx支持web界面执行bash|python等系统命令和脚本



html模板下载:

linux-shell.zip 如果部署好后访问脚本路径弹出下载,先检查fcgiwrap服务是否正常,再检查nginx匹配规则
# 程序html模板使用:(脚本文件要加执行权限,不然会提示403 错误)

# 脚本文件说明:
# disk            # info           # mem
查看硬盘使用情况    提示信息内容      内存使用情况

# ps           # server            # ssh
系统进程概览    自定义服务进程查看    ssh连接用户情况

# uptime
系统负载cpu和内存使用概览

传参脚本(来源网络文章脚本范例)做了下修改,请求传参示例: http://127.0.0.1/shell?a=xxx&b=xxx&c=xxx

#!/bin/bash

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>传参解析</title>'
echo '</head>'
echo '<body>'

# Save the old internal field separator.
  OIFS="$IFS"

# Set the field separator to & and parse the QUERY_STRING at the ampersand.
  IFS="${IFS}&"
  set - $QUERY_STRING
  Args="$*"
  IFS="$OIFS"

# Next parse the individual "name=value" tokens.

  ARGX=""
  ARGY=""
  ARGZ=""

  for i in $Args ;do

#       Set the field separator to =
        IFS="${OIFS}="
        set $i
        IFS="${OIFS}"

        case $1 in
                # Don't allow "/" changed to " ". Prevent hacker problems.
                a) ARGX="`echo $2 | sed 's|[\]||g' | sed 's|%20| |g'`"
                       ;;
                # Filter for "/" not applied here
                b) ARGY="`echo $2 | sed 's|%20| |g'`"
                       ;;
                c) ARGZ="${2/\// /}"
                       ;;
                *)     echo "<hr>Warning:"\
                            "<br>Unrecognized variable \'$1\' passed by FORM in QUERY_STRING.<hr>"
                       ;;

        esac
  done

  echo '解析参数:' 
  if [[ $ARGX == "" ]] && [[ $ARGY == "" ]] && [[ $ARGZ == "" ]]
  then
      set + $QUERY_STRING
      echo "没有参数传入"
  fi
  echo '<br>'
  echo "$ARGX"
  echo '<br>'
  echo "$ARGY "
  echo '<br>'
  echo "$ARGZ"

echo '</body>'
echo '</html>'

exit 0

本文最后更新时间 2023-12-12
文章链接地址:
https://me.jinchuang.org/archives/114.html
本站文章除注明[转载|引用|原文]出处外,均为本站原生内容,转载前请注明出处


留言列表

  1. 山东老猫
    山东老猫 Windows 10 Microsoft Edge · 中国上海市电信 · 回复

    点开连接,把脚本文件下载到本地了。下面的是nginx配置:
    server {
    index index.html;
    listen 81;
    server_name 127.0.0.1;
    root /home/www/bash;

    access_log /var/log/nginx/default.log main;
    error_log /var/log/nginx/default.err.log;
    error_page 403 404 /404.html;

    location ~ ^/.*\.(cgi|sh) {
    gzip off;
    fastcgi_pass unix:/tmp/fcgiwrap.socket;
    include fastcgi_params;
    fastcgi_param SCRIPT_NAME $document_root$fastcgi_script_name;
    }

    }

    1. J.C
      J.C Windows 10 Google Chrome · 中国广东省东莞市电信 · 回复

      我用你的nginx配置和指定的目录,访问是ok的也没出现下载文件

  2. 陆
    Windows 10 Firefox · 中国上海市电信 · 回复

    windows配置cgi,NGINXweb界面打开是显示下载了 而不是执行脚本请问为啥啊,是不是cgi哪里没配置好啊,网上找了好多都没解决

    1. J.C
      J.C Windows 10 Google Chrome · 中国上海市电信 · 回复

      windows上没试过,可能匹配请求地址转发规则错了,或者是cgi的服务问题

  3. pyker
    pyker Mac OS X 10.15.7 Google Chrome · 中国广东省东莞市电信 · 回复

    如何curl http://127.0.0.1/ab.sh 传递参数?

    1. J.C
      J.C Windows 10 Google Chrome · 中国上海市电信 · 回复

      我也试过传参但是都不行

      1. pyker
        pyker Mac OS X 10.15.7 Google Chrome · 中国广东省东莞市电信 · 回复

        我这边可以传递参数了,请看看我做的docker镜像https://hub.docker.com/r/ipyker/fcgiwrap-nginx-shell

        1. J.C
          J.C Windows 10 Google Chrome · 中国上海市电信 · 回复

          找到了传参的脚本例子:http://www.yolinux.com/TUTORIALS/BashShellCgi.html

        2. J.C
          J.C Windows 10 Google Chrome · 中国天津市联通 · 回复

          ??? 厉害了!

  4. blitz
    blitz Mac OS X 10.15.2 Google Chrome · 中国天津市电信IDC机房 · 回复

    你好,这个我还是看不懂 能教教我吗 。1057287797@qq.com

留言