Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4177057
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: 系统运维

2011-06-22 12:32:24

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. END() { }
  12. BEGIN() { }
  13. {
  14.   no warnings;
  15.   *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
  16. };

  17. eval q{exit};
  18. if ($@) {
  19.   exit unless $@ =~ /^fakeexit/;
  20. }
  21. &main;

  22. sub daemonize() {
  23.   chdir '/' or die "Can't chdir to /: $!";
  24.   defined( my $pid = fork ) or die "Can't fork: $!";
  25.   exit if $pid;
  26.   setsid() or die "Can't start a new session: $!";
  27.   umask 0;
  28. }

  29. sub main {
  30.   $proc_manager = FCGI::ProcManager->new( {n_processes => 5} );
  31.   $socket = FCGI::OpenSocket( "/usr/local/nginx/cgiwrap-dispatch.sock", 10 )
  32.   ; #use UNIX sockets - user running this script must have w access to the 'nginx'
  33.   $request =
  34.   FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
  35.   &FCGI::FAIL_ACCEPT_ON_INTR );
  36.   $proc_manager->pm_manage();
  37.   if ($request) { request_loop() }
  38.   FCGI::CloseSocket($socket);
  39. }

  40. sub request_loop {
  41.   while ( $request->Accept() >= 0 ) {
  42.     $proc_manager->pm_pre_dispatch();

  43.     #processing any STDIN input from WebServer (for CGI-POST actions)
  44.     $stdin_passthrough = '';
  45.     { no warnings; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; };
  46.     if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) ) {
  47.       my $bytes_read = 0;
  48.       while ( $bytes_read < $req_len ) {
  49.         my $data = '';
  50.         my $bytes = read( STDIN, $data, ( $req_len - $bytes_read ) );
  51.         last if ( $bytes == 0 || !defined($bytes) );
  52.         $stdin_passthrough .= $data;
  53.         $bytes_read += $bytes;
  54.       }
  55.     }

  56.     #running the cgi app
  57.     if (
  58.       ( -x $req_params{SCRIPT_FILENAME} ) && #can I execute this?
  59.       ( -s $req_params{SCRIPT_FILENAME} ) && #Is this file empty?
  60.       ( -r $req_params{SCRIPT_FILENAME} ) #can I read this file?
  61.     ) {
  62.       pipe( CHILD_RD, PARENT_WR );
  63.       pipe( PARENT_ERR, CHILD_ERR );
  64.       my $pid = open( CHILD_O, "-|" );
  65.       unless ( defined($pid) ) {
  66.         print("Content-type: text/plain\r\n\r\n");
  67.         print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
  68.         next;
  69.       }
  70.       $oldfh = select(PARENT_ERR);
  71.       $| = 1;
  72.       select(CHILD_O);
  73.       $| = 1;
  74.       select($oldfh);
  75.       if ( $pid > 0 ) {
  76.         close(CHILD_RD);
  77.         close(CHILD_ERR);
  78.         print PARENT_WR $stdin_passthrough;
  79.         close(PARENT_WR);
  80.         $rin = $rout = $ein = $eout = '';
  81.         vec( $rin, fileno(CHILD_O), 1 ) = 1;
  82.         vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
  83.         $ein = $rin;
  84.         $nfound = 0;

  85.         while ( $nfound = select( $rout = $rin, undef, $ein = $eout, 10 ) ) {
  86.           die "$!" unless $nfound != -1;
  87.           $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
  88.           $r2 = vec( $rout, fileno(CHILD_O), 1 ) == 1;
  89.           $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
  90.           $e2 = vec( $eout, fileno(CHILD_O), 1 ) == 1;

  91.           if ($r1) {
  92.             while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
  93.               print STDERR $errbytes;
  94.             }
  95.             if ($!) {
  96.               $err = $!;
  97.               die $!;
  98.               vec( $rin, fileno(PARENT_ERR), 1 ) = 0
  99.               unless ( $err == EINTR or $err == EAGAIN );
  100.             }
  101.           }
  102.           if ($r2) {
  103.             while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
  104.               print $s;
  105.             }
  106.             if ( !defined($bytes) ) {
  107.               $err = $!;
  108.               die $!;
  109.               vec( $rin, fileno(CHILD_O), 1 ) = 0
  110.               unless ( $err == EINTR or $err == EAGAIN );
  111.             }
  112.           }
  113.           last if ( $e1 || $e2 );
  114.         }
  115.         close CHILD_RD;
  116.         close PARENT_ERR;
  117.         waitpid( $pid, 0 );
  118.       } else {
  119.         foreach $key ( keys %req_params ) {
  120.           $ENV{$key} = $req_params{$key};
  121.         }

  122.         # 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
  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. #}
  109. }
 
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 "Hello, world. ";

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
阅读(6347) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~