配置nginx运行fastcgi

0.创建用户,不要用root运行,这样不安全

 

 
 
  1. groupadd nginx  
  2. useradd nginx -g nginx 

1.安装fastcgi

 

 
 
  1. [root@localhost conf]# cpan  
  2. Terminal does not support AddHistory.  
  3. cpan shell -- CPAN exploration and modules installation (v1.7602)  
  4. ReadLine support available (try 'install Bundle::CPAN')  
  5. cpan> install FCGI  
  6. cpan> install FCGI::ProcManager 

2.生成fcgi服务程序/usr/local/nginx/sbin/cgiwrap-fcgi.pl

 

 
 
  1. #!/usr/bin/perl  
  2. use FCGI;  
  3. use Socket;  
  4. use FCGI::ProcManager;  
  5. sub shutdown { FCGI::CloseSocket($socket); exit; }  
  6. sub restart { FCGI::CloseSocket($socket); &main; }  
  7. use sigtrap 'handler', \&shutdown, 'normal-signals';  
  8. use sigtrap 'handler', \&restart, 'HUP';  
  9. require 'syscall.ph';  
  10. use POSIX qw(setsid);  
  11.  
  12. END() { }  
  13. BEGIN() { }  
  14. {  
  15.   no warnings;  
  16.   *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };  
  17. };  
  18.  
  19. eval q{exit};  
  20. if ($@) {  
  21.   exit unless $@ =~ /^fakeexit/;  
  22. }  
  23. &main;  
  24.  
  25. sub daemonize() {  
  26.   chdir '/' or die "Can't chdir to /: $!";  
  27.   defined( my $pid = fork ) or die "Can't fork: $!";  
  28.   exit if $pid;  
  29.   setsid() or die "Can't start a new session: $!";  
  30.   umask 0;  
  31. }  
  32.  
  33. sub main {  
  34.   $proc_manager = FCGI::ProcManager->new( {n_processes => 5} );  
  35.   $socket = FCGI::OpenSocket( "/usr/local/nginx/cgiwrap-dispatch.sock", 10 )  
  36.   ; #use UNIX sockets - user running this script must have w access to the 'nginx'   
  37.   $request =  
  38.   FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,  
  39.   &FCGI::FAIL_ACCEPT_ON_INTR );  
  40.   $proc_manager->pm_manage();  
  41.   if ($request) { request_loop() }  
  42.   FCGI::CloseSocket($socket);  
  43. }  
  44.  
  45. sub request_loop {  
  46.   while ( $request->Accept() >= 0 ) {  
  47.     $proc_manager->pm_pre_dispatch();  
  48.  
  49.     #processing any STDIN input from WebServer (for CGI-POST actions)  
  50.     $stdin_passthrough = '';  
  51.     { no warnings; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; };  
  52.     if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) ) {  
  53.       my $bytes_read = 0;  
  54.       while ( $bytes_read < $req_len ) {  
  55.         my $data = '';  
  56.         my $bytes = read( STDIN, $data, ( $req_len - $bytes_read ) );  
  57.         last if ( $bytes == 0 || !defined($bytes) );  
  58.         $stdin_passthrough .= $data;  
  59.         $bytes_read += $bytes;  
  60.       }  
  61.     }  
  62.  
  63.     #running the cgi app  
  64.     if (  
  65.       ( -x $req_params{SCRIPT_FILENAME} ) && #can I execute this?  
  66.       ( -s $req_params{SCRIPT_FILENAME} ) && #Is this file empty?  
  67.       ( -r $req_params{SCRIPT_FILENAME} ) #can I read this file?  
  68.     ) {  
  69.       pipe( CHILD_RD, PARENT_WR );  
  70.       pipe( PARENT_ERR, CHILD_ERR );  
  71.       my $pid = open( CHILD_O, "-|" );  
  72.       unless ( defined($pid) ) {  
  73.         print("Content-type: text/plain\r\n\r\n");  
  74.         print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";  
  75.         next;  
  76.       }  
  77.       $oldfh = select(PARENT_ERR);  
  78.       $| = 1;  
  79.       select(CHILD_O);  
  80.       $| = 1;  
  81.       select($oldfh);  
  82.       if ( $pid > 0 ) {  
  83.         close(CHILD_RD);  
  84.         close(CHILD_ERR);  
  85.         print PARENT_WR $stdin_passthrough;  
  86.         close(PARENT_WR);  
  87.         $rin = $rout = $ein = $eout = '';  
  88.         vec( $rin, fileno(CHILD_O), 1 ) = 1;  
  89.         vec( $rin, fileno(PARENT_ERR), 1 ) = 1;  
  90.         $ein = $rin;  
  91.         $nfound = 0;  
  92.  
  93.         while ( $nfound = select( $rout = $rin, undef, $ein = $eout, 10 ) ) {  
  94.           die "$!" unless $nfound != -1;  
  95.           $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;  
  96.           $r2 = vec( $rout, fileno(CHILD_O), 1 ) == 1;  
  97.           $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;  
  98.           $e2 = vec( $eout, fileno(CHILD_O), 1 ) == 1;  
  99.  
  100.           if ($r1) {  
  101.             while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {  
  102.               print STDERR $errbytes;  
  103.             }  
  104.             if ($!) {  
  105.               $err = $!;  
  106.               die $!;  
  107.               vec( $rin, fileno(PARENT_ERR), 1 ) = 0  
  108.               unless ( $err == EINTR or $err == EAGAIN );  
  109.             }  
  110.           }  
  111.           if ($r2) {  
  112.             while ( $bytes = read( CHILD_O, $s, 4096 ) ) {  
  113.               print $s;  
  114.             }  
  115.             if ( !defined($bytes) ) {  
  116.               $err = $!;  
  117.               die $!;  
  118.               vec( $rin, fileno(CHILD_O), 1 ) = 0  
  119.               unless ( $err == EINTR or $err == EAGAIN );  
  120.             }  
  121.           }  
  122.           last if ( $e1 || $e2 );  
  123.         }  
  124.         close CHILD_RD;  
  125.         close PARENT_ERR;  
  126.         waitpid( $pid, 0 );  
  127.       } else {  
  128.         foreach $key ( keys %req_params ) {  
  129.           $ENV{$key} = $req_params{$key};  
  130.         }  
  131.  
  132.         # cd to the script 

chmod +x /usr/local/nginx/sbin/cgiwrap-fcgi.pl
 
 
4.修改配置文件,红色粗体为修改部分

 

 

 
 
  1. user nginx;  
  2. worker_processes 1;  
  3. #error_log logs/error.log;  
  4. #error_log logs/error.log notice;  
  5. #error_log logs/error.log info;  
  6. #pid logs/nginx.pid;  
  7. events {  
  8. worker_connections 1024;  
  9. }  
  10. http {  
  11. include mime.types;  
  12. default_type application/octet-stream;  
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
  14. '$status $body_bytes_sent "$http_referer" ' 
  15. '"$http_user_agent" "$http_x_forwarded_for"';  
  16. #access_log logs/access.log main;  
  17. sendfile on;  
  18. #tcp_nopush on;  
  19. #keepalive_timeout 0;  
  20. keepalive_timeout 65;  
  21. #gzip on;  
  22. server {  
  23. listen 80;  
  24. server_name localhost;  
  25. #charset koi8-r;  
  26. #access_log logs/host.access.log main;  
  27. location / {  
  28. root html;  
  29. index index.html index.htm;  
  30. }  
  31. location ~ ^/cgi-bin/.*\.cgi$ {  
  32. gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped  
  33. fastcgi_pass unix:/usr/local/nginx/cgiwrap-dispatch.sock;  
  34. fastcgi_index index.cgi;  
  35. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/$fastcgi_script_name;  
  36. fastcgi_param QUERY_STRING $query_string;  
  37. fastcgi_param REQUEST_METHOD $request_method;  
  38. fastcgi_param CONTENT_TYPE $content_type;  
  39. fastcgi_param CONTENT_LENGTH $content_length;  
  40. fastcgi_param GATEWAY_INTERFACE CGI/1.1;  
  41. fastcgi_param SERVER_SOFTWARE nginx;  
  42. fastcgi_param SCRIPT_NAME $fastcgi_script_name;  
  43. fastcgi_param REQUEST_URI $request_uri;  
  44. fastcgi_param DOCUMENT_URI $document_uri;  
  45. fastcgi_param DOCUMENT_ROOT $document_root;  
  46. fastcgi_param SERVER_PROTOCOL $server_protocol;  
  47. fastcgi_param REMOTE_ADDR $remote_addr;  
  48. fastcgi_param REMOTE_PORT $remote_port;  
  49. fastcgi_param SERVER_ADDR $server_addr;  
  50. fastcgi_param SERVER_PORT $server_port;  
  51. fastcgi_param SERVER_NAME $server_name;  
  52. }  
  53. #error_page 404 /404.html;  
  54. # redirect server error pages to the static page /50x.html  
  55. #  
  56. error_page 500 502 503 504 /50x.html;  
  57. location = /50x.html {  
  58. root html;  
  59. }  
  60. # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  61. #  
  62. #location ~ \.php$ {  
  63. # proxy_pass http://127.0.0.1;  
  64. #}  
  65. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  66. #  
  67. #location ~ \.php$ {  
  68. # root html;  
  69. # fastcgi_pass 127.0.0.1:9000;  
  70. # fastcgi_index index.php;  
  71. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;  
  72. # include fastcgi_params;  
  73. #}  
  74. # deny access to .htaccess files, if Apache's document root  
  75. # concurs with nginx's one  
  76. #  
  77. #location ~ /\.ht {  
  78. # deny all;  
  79. #}  
  80. }  
  81. # another virtual host using mix of IP-, name-, and port-based configuration  
  82. #  
  83. #server {  
  84. # listen 8000;  
  85. # listen somename:8080;  
  86. # server_name somename alias another.alias;  
  87. # location / {  
  88. # root html;  
  89. index index.html index.htm;  
  90. # }  
  91. #}  
  92. # HTTPS server  
  93. #  
  94. #server {  
  95. # listen 443;  
  96. # server_name localhost;  
  97. # ssl on;  
  98. # ssl_certificate cert.pem;  
  99. # ssl_certificate_key cert.key;  
  100. # ssl_session_timeout 5m;  
  101. # ssl_protocols SSLv2 SSLv3 TLSv1;  
  102. # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
  103. # ssl_prefer_server_ciphers on;  
  104. # location / {  
  105. # root html;  
  106. index index.html index.htm;  
  107. # }  
  108. #}  

5.准备测试

 

 
 
  1. mkdir /usr/local/nginx/cgi-bin  
  2. cat /usr/local/nginx/cgi-bin/index.cgi  
  3. #!/usr/bin/perl  
  4. print "Content-type: text/html\n\n";  
  5. print "<html><body>Hello, world.</body></html>";  
  6. chmod +x /usr/local/nginx/cgi-bin/index.cgi 

6.设置拥有者

 

 
 
  1. chown nginx:nginx -R /usr/local/nginx 

 

7.启动

 

 
 
  1. sudo -u nginx /usr/local/nginx/sbin/cgiwrap-fcgi.pl >> /usr/local/nginx/logs/cgiwrap.log 2>&1 &  
  2. /usr/local/nginx/sbin/nginx 

8.测试
 

其中的一些代码来自
不过这个文章中在实现时有些问题,特别是用户权限的问题
end



     本文转自yifangyou 51CTO博客,原文链接:http://blog.51cto.com/yifangyou/617221 ,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值