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

原文:靳闯博客-Nginx支持web界面执行bash.python等脚本

使用说明

1,shell命令 | python命令 | 系统支持的都可以
2,不支持交互式显示 | 不支持动态内容显示
3,傻瓜式操作(页面点击链接一次,执行一次脚本内容)|可以设置页面自动刷新,实现重复执行脚本
4,传参使用参考:http://www.yolinux.com/TUTORIALS/BashShellCgi.html

使用要求

Linux系统(这里使用centos7演示)

软件安装

安装nginx

#创建nginx用户
useradd -M -s /sbin/nologin nginx

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

#安装依赖包
yum -y install wget unzip bzip2 zlib zlib-devel pcre pcre-devel \
openssl openssl-devel geoip geoip-devel gd gd-devel  gcc gcc-c++ make libtool

#创建相关源码包存放目录
mkdir /source && cd /source

#下载解压相关依赖源码包
wget  https://www.jinchuang.org/novel/lnmp/pcre-8.35.tar.gz
wget  https://www.openssl.org/source/openssl-1.0.2h.tar.gz
wget  https://www.jinchuang.org/novel/lnmp/zlib-1.2.8.tar.gz
tar xf pcre-8.35.tar.gz
tar xf openssl-1.0.2h.tar.gz
tar xf zlib-1.2.8.tar.gz

#下载nginx配置编译安装
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar xf nginx-1.18.0.tar.gz && cd nginx-1.18.0
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/usr/local/nginx/client_temp \
--http-proxy-temp-path=/usr/local/nginx/proxy_temp \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \
--http-scgi-temp-path=/usr/local/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-mail \
--with-stream \
--with-threads \
--with-file-aio \
--with-poll_module \
--with-select_module \
--with-http_v2_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_ssl_module \
--with-http_geoip_module \
--with-http_slice_module \
--with-http_gunzip_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_image_filter_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_stub_status_module \
--with-mail_ssl_module \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-pcre=/source/pcre-8.35 \
--with-openssl=/source/openssl-1.0.2h \
--with-zlib=/source/zlib-1.2.8
maek && make install

添加nginx命令到系统环境中

echo 'export PATH=$PATH:/usr/local/nginx/sbin' >>/etc/profile && source /etc/profile

安装spawn-fcgi

yum install dh-autoreconf fcgi fcgi-devel-y
cd /source/
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

cd /source/
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启动脚本 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

增加执行权限

chmod +x /etc/init.d/fcgiwrap

启动服务

#启动nginx
nginx

#启动fcgiwrap
/etc/init.d/fcgiwrap start

下载html模板

linux-shell.zip

#把下载的html模板放到nginx网站根目录下
cd /usr/local/nginx/html/
wget wget https://ossjc-1252545319.cos.ap-shanghai.myqcloud.com/webfile/zip/linux-shell.zip
unzip linux-shell.zip

#给脚本增加执行权限
cd linux-shell/page/script/
chmod +x */*

模板目录结构

page/script/    #命令脚本文件目录
page/h5/        #html页面(调用脚本,执行命令)
page/content/   #首页默认页面

# 程序html模板使用:(脚本文件要加执行权限,不然会提示403 错误)
cd linux-shell/page/script/
chmod +x */*

脚本文件名称说明:

disk #查看硬盘使用情况
info #提示信息内容
mem #内存使用情况
ps #系统进程概览
server #自定义服务进程查看
ssh #ssh连接用户情况
uptime #系统负载cpu和内存使用概览

命令代码脚本示例

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

配置nginx转发

修改配置文件

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

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

访问预览

posted @ 2020-12-28 11:07  J丶c  阅读(1575)  评论(0编辑  收藏  举报