配置 Nginx 支持 CGI 不指定

jed , 2010-5-13 13:56 , 服务器技术 , 评论(0) , 阅读(2284) , Via 本站原创 | |
Nginx 不能直接执行外部可执行程序,如果要让 Nginx 支持 CGI,可以考虑安装 nginx-fcgi:

http://www.nginx.eu/nginx-fcgi.html

当前版本为 nginx-fcgi-0.4.3,需要用到以下  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://www.nginx.eu/nginx-fcgi/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 nginx:nginx /var/run/nginx-fcgi.sock





注意一定要为 socket 添加 nginx 帐户的权限,否则 cgi 会执行失败。

新建 nginx-fcgi 脚本指令配置,直接从 fastcgi_params 复制模板:



cp /etc/nginx/fastcgi_params /etc/nginx/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$
    {
        root           /work/www/test;
        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即可。

/usr/local/nginx/sbin/nginx -s reload


添加系统

vi /etc/rc.d/rc.local
增加:
/usr/sbin/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
Tags: ,
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]