安装spawn-fcgi
#从GitHub下载
git clone https://github.com/lighttpd/spawn-fcgi.git
cd spawn-fcgi
#生成configure
./autogen.sh
#生成Makefile
./configure
#生成执行文件并安装
make
make install
若出现以下错误:
Can't locate FCGI.pm in @INC (you may need to install the FCGI module) (@INC contains: /usr/local/perl/lib/site_perl/5.20.0/x86_64-linux /usr/local/perl/lib/site_perl/5.20.0 /usr/local/perl/lib/5.20.0/x86_64-linux /usr/local/perl/lib/5.20.0 .) at /usr/local/nginx/perl-fcgi.pl line 25.
BEGIN failed--compilation aborted at /usr/local/nginx/perl-fcgi.pl line 25.
start perl-fcgi done
原因:
因为没有安装 fcgi 与fcgi-devel 所导致
解决办法:
wget http://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/f/fcgi-2.4.0-25.el7.x86_64.rpm
rpm -ivh fcgi*
然后在重新安装一次
安装fcgiwrap
#从GitHub下载
git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
#生成configure
autoreconf -i
#生成Makefile
./configure
#生成执行文件并安装
make
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="/opt/fcgiwrap/$NAME.pid" #配置pid目录
FCGI_SOCKET="/opt/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 /etc/init.d/fcgiwrap
创建socket目录
mkdir /opt/fcgiwrap
启动服务: service fcgiwrap start
将socket的权限设成777: chmod 777 /opt/fcgiwrap/fcgiwrap.socket
配置nginx的CGI
在nginx/conf/nginx.conf中添加:
location ~/cgi-bin/.*\.(sh|py|cgi) {
fastcgi_pass unix:/var/run/fcgiwrap/fcgiwrap.socket;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
测试
在nginx根目录下新建目录: mkdir cgi-bin
创建测试shell脚本hello.sh并添加执行权限:
vim hello.sh
#!/bin/bash
echo "Content-Type:text/html;charset=utf-8"
echo ""
echo "Hello,welcomg to weninux.com!"
chmod +x hello.sh
在浏览器中输入地址:weninux.com/cgi-bin/hello.sh,出现hello weninux.com!,测试成功。
一个简单的页面:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cgi
import os
Excel_file = (os.listdir("/usr/local/nginx/html/test/excel"))
print "Content-type:text/html"
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title> excel下载</title>'
print '</head>'
print '<body>'
for File in Excel_file[::-1]:
print '<a href="../test/excel/'+File+'">'+File+'</a><br />'
print '</body>'
print '</html>'