Nginx配置CGI

簡介

CGI(Common Gateway Interface)通用網關接口 描述了客戶端和服務器程序之間傳輸數據的一種標準。

快速通用網關接口(Fast Common Gateway Interface/FastCGI)是通用網關接口(CGI)的改進,描述了客戶端和服務器程序之間傳輸數據的一種標準。FastCGI致力於減少Web服務器CGI程式**之間互動的開銷,從而使服務器可以同時處理更多的Web請求。與爲每個請求創建一個新的進程不同,FastCGI使用持續的進程來處理一連串的請求。

Nginx與Apache不同,只是一個代理程序,而不能執行外部程序!

避免報錯先安裝這些

yum install fcgi-devel -y # 沒有會報錯FastCGI library is missing
yum install autoconf automake libtool -y # 安裝autoreconf

安裝spawn-fcgi

項目地址:https://github.com/lighttpd/spawn-fcgi

yum install git -y
git clone https://github.com/lighttpd/spawn-fcgi.git
cd spawn-fcgi
./autogen.sh # 生成configure
./configure
make & make install

安裝fcgiwrap

項目地址:https://github.com/gnosek/fcgiwrap

git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoreconf -i # 生成configure
./configure
make
# 如果make報錯執行
make CFLAGS='-Wno-implicit-fallthrough'
make install

創建管理腳本

cd /etc/init.d/
vim fcgiwrap
#!/bin/bash
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/fcgiwrap/$NAME.pid" #pid位置
FCGI_SOCKET="/var/fcgiwrap/$NAME.socket" #socket位置
FCGI_USER="nginx" #用戶
FCGI_GROUP="nginx" #用戶組
FORK_NUM=2 #進程數量
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 fcgiwrap
mkdir /var/fcgiwrap/
service fcgiwrap start
cd /var/fcgiwrap/
chmod 777 fcgiwrap.socket

配置nginx

cd /etc/nginx
vim nginx.conf
location ~/cgi-bin/.*\.(sh|py|cgi) {
		fastcgi_pass   unix:/var//fcgiwrap/fcgiwrap.socket;
		fastcgi_param  SCRIPT_NAME  $fastcgi_script_name;
		include        fastcgi_params;
}
cd /var/share/nginx/html/ # 進入到你的網站根目錄
mkdir cgi-bin

編寫測試腳本

vim test.sh
#!/bin/bash
echo "context-type:text/html"
echo ""

echo "hello /bin/bash"
vim test1.py
#!/bin/python3

print('context-type:text/html')
print('')

print('hello /usr/bin/python3')
service fcgiwrap start
systemctl restart nginx

更多參考:https://www.cnblogs.com/skynet/p/4173450.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章