原文:靳闖博客-Nginx支持web界面執行bash.python等腳本
使用說明
1,shell命令 | python命令 | 系統支持的都可以
2,不支持交互式顯示 | 不支持動態內容顯示
3,傻瓜式操作(頁面點擊鏈接一次,執行一次腳本內容)|可以設置頁面自動刷新,實現重復執行腳本
使用要求
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模板
#把下載的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