nginx自己不支持直接調用shell腳本,咱們能夠經過安裝fastcgi程序,讓nginx把調用shell的http請求交給fastcgi程序去處理,而後nginx 再將結果返回給用戶方式間接調用shell,這裏咱們推薦安裝fcgiwrap這個通用的 fastcgi 進程管理器來幫助nginx 處理shell程序php
1、安裝fcgiwraphtml
# 安裝 epel 源 yum -y install epel-release # 安裝 fcgi 依賴 yum -y install fcgi fcgi-devel # 安裝 fcgiwrap wget https://github.com/gnosek/fcgiwrap/archive/master.zip unzip master.zip cd fcgiwrap-master autoreconf -i ./configure make make install
經過執行以上命令將會把fcgiwrap安裝到/usr/local/sbin/fcgiwrap這個路徑nginx
2、安裝spawn-fcgigit
經過安裝spawn-fcgi方便啓動fcgiwrap程序github
#安裝spawn-fcgi
yum install spawn-fcgi
修改/etc/sysconfig/spawn-fcgi配置文件
vi /etc/sysconfig/spawn-fcgi
修改配置文件爲如下內容shell
# You must set some working options before the "spawn-fcgi" service will work. # If SOCKET points to a file, then this file is cleaned up by the init script. # # See spawn-fcgi(1) for all possible options. # # Example : #SOCKET=/var/run/php-fcgi.sock #OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi" FCGI_SOCKET=/var/run/fcgiwrap.socket FCGI_PROGRAM=/usr/local/sbin/fcgiwrap FCGI_USER=nobody FCGI_GROUP=nobody FCGI_EXTRA_OPTIONS="-M 0700" OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM"
添加spawn-fcgi開機啓動服務apache
chkconfig --levels 235 spawn-fcgi on
啓動spawn-fcgi服務centos
/etc/init.d/spawn-fcgi start
3、配置nginx執行shell調用瀏覽器
修改nginx配置文件bash
vi /usr/local/nginx/conf/nginx.conf
增長如下內容
location ~ ^/.*\.sh { gzip off; root /home/shell; #shell文件存放目錄 fastcgi_pass unix:/var/run/fcgiwrap.socket; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
4、建立shell文件
vi /home/shell/hello.sh
添加如下內容
#!/bin/bash echo "Content-Type:text/html" echo "" echo "hello world!"
添加shell腳本執行權限
chmod -R 755 /home/shell
而後在瀏覽器中輸入http://<ip>:port/hello.sh
便可調用咱們的shell腳本
說明:
一、腳本前三行是必須的,第一行用於指定腳本執行使用的解釋器,第二行和第三行是HTTP協議規範,在發送HTML正文以前要發送MIME頭和空行
二、shell腳本都是使用nobody用戶執行的,因此要確保nobody能夠讀取並執行hello.sh
參考文檔:https://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-centos-6.0-p2