Nginx supports web interface execution bash Python and other commands and scripts

Time:2022-5-16

Original text:Jin Chuang blog – nginx supports web interface execution bash Python and other scripts

instructions

1. Shell command | Python command | whatever the system supports
2. Interactive display is not supported | dynamic content display is not supported
3. Fool operation (click the link once on the page and execute the script content once) | you can set the page to refresh automatically to realize the repeated execution of the script
4. Reference for transmission and use:http://www.yolinux.com/TUTORIALS/BashShellCgi.html

Use requirements

Linux system (CentOS 7 demo is used here)

Software installation

Installing nginx

#Create nginx user
useradd -M -s /sbin/nologin nginx

#Add epelyum source
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

#Install dependent packages
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

#Create relevant source package storage directory
mkdir /source && cd /source

#Download and unzip the dependent source code package
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

#Download nginx configuration, compile and install
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

Add nginx command to system environment

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

Installing 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

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

Create fcgiwrap startup scriptvim /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

Increase execution authority

chmod +x /etc/init.d/fcgiwrap

Start service

#Start nginx
nginx

#Start fcgiwrap
/etc/init.d/fcgiwrap start

Download HTML template

linux-shell.zip

#Put the downloaded HTML template in the root directory of nginx website
cd /usr/local/nginx/html/
wget wget https://ossjc-1252545319.cos.ap-shanghai.myqcloud.com/webfile/zip/linux-shell.zip
unzip linux-shell.zip

#Add execution permission to the script
cd linux-shell/page/script/
chmod +x */*

Template directory structure

Page / script / # command script file directory
Page / H5 / #html page (call script, execute command)
Page / content / # home page default page

#Use of program HTML template: (add execution permission to the script file, otherwise 403 error will be prompted)
cd linux-shell/page/script/
chmod +x */*

Script file name Description:

Disk # view hard disk usage
Info # prompt information content
MEM # memory usage
PS # system process overview
Server # custom service process view
SSH #ssh connection users
Uptime # system load CPU and memory usage overview

Command code script example

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

#Auto refresh
#echo 'window.setInterval(function(){
#    window.location.reload();
#},1000);'
#echo ''

#CSS style of HTML page
echo '
body{color:#cecece;}
.title{color: #FF9800;border-left: 4px solid;padding: 4px;}
pre{font-size:14px;border-left: 4px solid #4CAF50;padding: 5px;}
'

#Define variables
ip="192.168.x.x"

#Content code (the result returned by the command is placed in the label)
echo ''
Echo 'hard disk usage'
echo ''
dd=`date`
Echo "statistical time: $DD [current machine IP: $IP]"
echo ''
echo ''
#View disk usage (native)
df -hT
#Check the disk usage (for remote machines, you can use remote non interface interaction tools such as ansible|sshpass)
sshpass -p "password" ssh [email protected]$ip -o StrictHostKeyChecking=no  'df -hT'
echo ''

Configure nginx forwarding
Modify profile
#Note that it is modified to your directory path
#location ~ ^/linux-shell/page/script/.*\. (CGI) {# the CGI suffix matching here can be modified as needed, and the suffix can be customized

#Linux shell is the template program directory, which is placed under the root directory of nginx website
Location ~ ^ / Linux shell / page / script / {# the file I call here has no suffix, so use this configuration
        fastcgi_pass  unix:/tmp/fcgiwrap.socket;
        #fastcgi_index index.cgi;
        include fastcgi_params;
        fastcgi_param  SCRIPT_NAME        $document_root$fastcgi_script_name;
      }

#Restart nginx:
nginx -s reload

Access Preview

Recommended Today

[ES6 deep analysis] 3: generators

Introducing ES6 generators What is generators? Let’s take a look at an example first. function* quips(name) { yield “hello ” + name + “!”; yield “i hope you are enjoying the blog posts”; if (name.startsWith(“X”)) { yield “it’s cool how your name starts with X, ” + name; } yield “see you later!”; } This […]