Nginx支持web界面執行bash腳本
阿新 • • 發佈:2017-11-07
arc mkdir -p pad 必須 rip log != ati lin
一直以來都是在終端執行shell等腳本,那web界面是否也可以這樣,當然也是可以的
本文以centos 6為例安裝配置
- 安裝spawn-fcgi
wget https://github.com/lighttpd/spawn-fcgi/archive/spawn-fcgi-1.6.4.zip
unzip spawn-fcgi-1.6.4.zip && rm spawn-fcgi-1.6.4.zip
mv spawn-fcgi-spawn-fcgi-1.6.4/ spawn-fcgi
cd spawn-fcgi/
./autogen.sh
./configure
make && make install - 安裝fcgiwrap
yum -y install fcgi fcgi-devel nginx
wget https://github.com/gnosek/fcgiwrap/archive/1.1.0.zip
unzip 1.1.0.zip
rm 1.1.0.zip
mv fcgiwrap-1.1.0/ fcgiwrap
cd fcgiwrap/
autoreconf -i
./configure
make && make install - 添加fcgiwrap 啟動腳本
腳本啟用用戶要和你nginx啟動用戶一致,註意下腳本中路徑及啟動用戶
vim /etc/init.d/fcgiwrap
#! /bin/bash ### BEGIN INIT INFO # Provides: fcgiwrap # Required
fcgiwrapchmod +x /etc/init.d/fcgiwrap
- 啟動
chkconfig --add fcgiwrap
chkconfig --level 2345 fcgiwrap on
./etc/init.d/fcgiwrap restart - nginx配置
測試環境編輯/etc/nginx/conf.d/default.conf,添加
location ~ ^/cgi-bin/cgi/.*\.(cgi|sh) { #這裏的後綴匹配根據需要修改,可以使用統一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
mkdir -p /usr/share/nginx/html/cgi-bin 放置首頁html
mkdir -p /usr/share/nginx/html/cgi-bin/cgi 放置腳本 - 腳本測試
vim /usr/share/nginx/html/cgi-bin/cgi/disk.cgi
#!/bin/bash echo "Content-Type:text/html;charset=utf-8" echo "" #前3行必須是這個格式,html代碼使用echo,linux命令和腳本語法一樣 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>‘
disk.cgi訪問:http://your-ip/cgi-bin/cgi/disk.cgi
Nginx支持web界面執行bash腳本