nginx 开启fastcgi 可支持php、python、perl等多种语言
2013-08-23 15:09 zwz2008 阅读(1516) 评论(0) 编辑 收藏 举报需要用到以下 perl 库:(以下版本可能过旧导致下载地址失效可直到上一级目录下载最新文件)
perl-FCGI
perl-Getopt
perl-IO
perl-Socket
安装 perl-FCGI 、perl-FCGI-ProcManager 和 perl-IO-ALL:
wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.67.tar.gz
tar -zxf FCGI-0.67.tar.gz
cd FCGI-0.67
perl Makefile.PL
make && make install
cd ..wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.18.tar.gz
tar -zxf FCGI-ProcManager-0.18.tar.gz
cd FCGI-ProcManager-0.18
perl Makefile.PL
make && make install
cd ..wget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.39.tar.gz
tar zxf IO-All-0.39.tar.gz
cd IO-All-0.39
perl Makefile.PL
make && make install
安装 nginx-fcgi 脚本:
wget http://hily.me/blog/wp-content/uploads/2010/01/nginx-fcgi.txt
mv nginx-fcgi.txt /usr/sbin/nginx-fcgi
chmod +x /usr/sbin/nginx-fcgi
如果不用 sudo 方式运行 nginx-fcgi,请注释掉 nginx-fcgi 脚本中的:
if ( $> == “0″ ) {
print “\n\tERROR\tRunning as a root!\n”;
print “\tSuggested not to do so !!!\n\n”;
exit 1;
}
启动 nginx-fcgi:
nginx-fcgi -l /var/log/nginx/nginx-fcgi.log -pid /var/run/nginx-fcgi.pid -S /var/run/nginx-fcgi.sock
chown www:www /var/run/nginx-fcgi.sock
注意一定要为 socket 添加 nginx 帐户的权限,否则 cgi 会执行失败。
新建 nginx-fcgi 脚本指令配置,直接从 fastcgi_params 复制模板:
cp /usr/local/nginx/conf/fastcgi_params /usr/local/nginx/conf/nginx_fcgi_params
去除尾部的:
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
新建示例 cgi 站点配置:
server {
listen 80;
server_name test.local;location ~ ^/cgi-bin/.*\.cgi$
{
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include nginx_fcgi_params;
fastcgi_read_timeout 5m;
fastcgi_pass unix:/var/run/nginx-fcgi.sock;
}
}
重启 nginx,并添加开机启动
编辑/etc/init.d/perl-fastcgi
#!/bin/sh
#add for chkconfig
#chkconfig: 2345 70 30
nginx-fcgi -l /var/log/nginx/nginx-fcgi.log -pid /var/run/nginx-fcgi.pid -S /var/run/nginx-fcgi.sock
chown www:www /var/run/nginx-fcgi.sock
添加开机启动
1)
chmod +x /usr/sbin/nginx-fcgi
chmod 755 /etc/init.d/perl-fastcgi
chkconfig perl-fastcgi on
2)
编辑 /etc/rc.d/rc.local文件
添加
nginx-fcgi -l /var/log/nginx/nginx-fcgi.log -pid /var/run/nginx-fcgi.pid -S /var/run/nginx-fcgi.sock
chown www:www /var/run/nginx-fcgi.sock
测试脚本(声明和代码之前至少有一空行隔开) :
python
#!/usr/bin/python2.5
# File:/usr/local/apache/cgi-bin/python.py
# -*- coding: utf-8 -*-
print "Content-type: text/html;charset=utf-8\n\n"
print 'Hello World!'
php:
#!/usr/local/php/bin/php -q
# File: File:/usr/local/apache/cgi-bin/i.php
<?php
phpinfo();
?>
perl:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, world.";
SHELL:
#!/bin/bash
# File: /usr/local/apache/cgi-bin/bash.sh
echo -e "Content-type: text/html;charset=utf-8\n\n"
echo CGI/1.0 test script report:$(uuidgen)'<br>'
echo argc is $#. argv is "$*".'<br>'
echo SERVER_SOFTWARE = $SERVER_SOFTWARE '<br>'
echo SERVER_NAME = $SERVER_NAME '<br>'
echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE '<br>'
echo SERVER_PROTOCOL = $SERVER_PROTOCOL '<br>'
echo SERVER_PORT = $SERVER_PORT '<br>'
echo REQUEST_METHOD = $REQUEST_METHOD '<br>'
echo HTTP_ACCEPT = "$HTTP_ACCEPT" '<br>'
echo PATH_INFO = "$PATH_INFO" '<br>'
echo PATH_TRANSLATED = "$PATH_TRANSLATED" '<br>'
echo SCRIPT_NAME = "$SCRIPT_NAME" '<br>'
echo QUERY_STRING = "$QUERY_STRING" '<br>'
echo REMOTE_HOST = $REMOTE_HOST '<br>'
echo REMOTE_ADDR = $REMOTE_ADDR '<br>'
echo REMOTE_USER = $REMOTE_USER '<br>'
echo AUTH_TYPE = $AUTH_TYPE '<br>'
echo CONTENT_TYPE = $CONTENT_TYPE '<br>'
echo CONTENT_LENGTH = $CONTENT_LENGTH