首先:有三种方式(三个不同的脚本)搭建cgi环境,第二种,在运行时,nginx的error里会报错。
FastCGI sent in stderr: “Bad file descriptor at /EBS/fcgi.pl line 125”,第二种脚本的好处是可以起多个进程(process)
方法一
测试页
记得一定要给nginx发送头信息 不然会报504错误的。
#!/usr/bin/perl -w print “Content-type: text/plain\r\n\r\n”;#发送头信息 print “test”;
|
1、安装FCGI模块:
到http://search.cpan.org/网站上下载FCGI模块:
wget http://search.cpan.org/CPAN/authors/id/S/SK/SKIMO/FCGI-0.67.tar.gz
tar -zxvf FCGI-0.67.tar.gz.
cd FCGI-0.67
perl Makefile.PL
make
make install
2、安装 IO 和 IO::ALL模块
下载:
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz
http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.39.tar.gz
安装:
tar -xvzf IO-1.25.tar.gz
cd IO-1.25
perl Makefile.PL
make
make install
tar -xvzf IO-All-0.39.tar.gz
cd IO-ALL-0.39
perl Makefile.PL
make
make install
3、如果需要,安装perl-Getopt和 perl-Socket
(转注:以上三项,使用cpan安装会跟简单一些,cpan怎么安装,请看其它日志)
4、perl cgi脚本
5、启动cgi脚本 (nobody为nginx的运行用户)
vi start_perl_cgi.sh
#!/bin/bash
#set -x
dir=/usr/local/nginx
stop ()
{
#pkill -f $dir/perl-fcgi.pl
kill $(cat $dir/logs/perl-fcgi.pid)
rm $dir/logs/perl-fcgi.pid 2>/dev/null
rm $dir/logs/perl-fcgi.sock 2>/dev/null
echo “stop perl-fcgi done”
}
start ()
{
rm $dir/now_start_perl_fcgi.sh 2>/dev/null
chown nobody.root $dir/logs
echo “$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock” >>$dir/now_start_perl_f
cgi.sh
chown nobody.nobody $dir/now_start_perl_fcgi.sh
chmod u+x $dir/now_start_perl_fcgi.sh
sudo -u nobody $dir/now_start_perl_fcgi.sh
echo “start perl-fcgi done”
}
case $1 in
stop)
stop
;;
start)
start
;;
restart)
stop
start
;;
esac
|
启动
./start_perl_cgi.sh start
6、为Nginx添加FCGI支持(以安装nagios为例)
vi perl_fcgi.conf
gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped fastcgi_pass unix:/usr/local/nginx/logs/nginx-fcgi.sock; fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name
|
以上为perl_fcgi.conf文件内容(转注)
location ~* .*\.cgi$ { rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break; include perl_fcgi.conf; } |
以上为nginx.conf配置文件中的内容(转注)
将请求cgi的url由/nagios/cgi-bin/*.cgi 重写为/*.cgi,这样,即下面的fastcgi_param中的$fastcgi_script_name,这样,可以正确的拼出cgi文件的路径。
重启nginx后,应该就可以访问perl的cgi文件了
配置nginx下的perl的fcgi的原因是我需要在我的机器上安装nagios。
以上为摘抄加自己的一些补充,原作地址为:http://blog.chinaunix.net/u/32831/showart_2011754.html