0.创建用户,不要用root运行,这样不安全
- groupadd nginx
- useradd nginx -g nginx
1.安装fastcgi
- [root@localhost conf]# cpan
- Terminal does not support AddHistory.
- cpan shell -- CPAN exploration and modules installation (v1.7602)
- ReadLine support available (try 'install Bundle::CPAN')
- cpan> install FCGI
- cpan> install FCGI::ProcManager
2.生成fcgi服务程序/usr/local/nginx/sbin/cgiwrap-fcgi.pl
- #!/usr/bin/perl
- use FCGI;
- use Socket;
- use FCGI::ProcManager;
- sub shutdown { FCGI::CloseSocket($socket); exit; }
- sub restart { FCGI::CloseSocket($socket); &main; }
- use sigtrap 'handler', \&shutdown, 'normal-signals';
- use sigtrap 'handler', \&restart, 'HUP';
- require 'syscall.ph';
- use POSIX qw(setsid);
- END() { }
- BEGIN() { }
- {
- no warnings;
- *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
- };
- eval q{exit};
- if ($@) {
- exit unless $@ =~ /^fakeexit/;
- }
- &main;
- sub daemonize() {
- chdir '/' or die "Can't chdir to /: $!";
- defined( my $pid = fork ) or die "Can't fork: $!";
- exit if $pid;
- setsid() or die "Can't start a new session: $!";
- umask 0;
- }
- sub main {
- $proc_manager = FCGI::ProcManager->new( {n_processes => 5} );
- $socket = FCGI::OpenSocket( "/usr/local/nginx/cgiwrap-dispatch.sock", 10 )
- ; #use UNIX sockets - user running this script must have w access to the 'nginx'
- $request =
- FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
- &FCGI::FAIL_ACCEPT_ON_INTR );
- $proc_manager->pm_manage();
- if ($request) { request_loop() }
- FCGI::CloseSocket($socket);
- }
- sub request_loop {
- while ( $request->Accept() >= 0 ) {
- $proc_manager->pm_pre_dispatch();
- #processing any STDIN input from WebServer (for CGI-POST actions)
- $stdin_passthrough = '';
- { no warnings; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; };
- if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) ) {
- my $bytes_read = 0;
- while ( $bytes_read < $req_len ) {
- my $data = '';
- my $bytes = read( STDIN, $data, ( $req_len - $bytes_read ) );
- last if ( $bytes == 0 || !defined($bytes) );
- $stdin_passthrough .= $data;
- $bytes_read += $bytes;
- }
- }
- #running the cgi app
- if (
- ( -x $req_params{SCRIPT_FILENAME} ) && #can I execute this?
- ( -s $req_params{SCRIPT_FILENAME} ) && #Is this file empty?
- ( -r $req_params{SCRIPT_FILENAME} ) #can I read this file?
- ) {
- pipe( CHILD_RD, PARENT_WR );
- pipe( PARENT_ERR, CHILD_ERR );
- my $pid = open( CHILD_O, "-|" );
- unless ( defined($pid) ) {
- print("Content-type: text/plain\r\n\r\n");
- print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
- next;
- }
- $oldfh = select(PARENT_ERR);
- $| = 1;
- select(CHILD_O);
- $| = 1;
- select($oldfh);
- if ( $pid > 0 ) {
- close(CHILD_RD);
- close(CHILD_ERR);
- print PARENT_WR $stdin_passthrough;
- close(PARENT_WR);
- $rin = $rout = $ein = $eout = '';
- vec( $rin, fileno(CHILD_O), 1 ) = 1;
- vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
- $ein = $rin;
- $nfound = 0;
- while ( $nfound = select( $rout = $rin, undef, $ein = $eout, 10 ) ) {
- die "$!" unless $nfound != -1;
- $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
- $r2 = vec( $rout, fileno(CHILD_O), 1 ) == 1;
- $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
- $e2 = vec( $eout, fileno(CHILD_O), 1 ) == 1;
- if ($r1) {
- while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
- print STDERR $errbytes;
- }
- if ($!) {
- $err = $!;
- die $!;
- vec( $rin, fileno(PARENT_ERR), 1 ) = 0
- unless ( $err == EINTR or $err == EAGAIN );
- }
- }
- if ($r2) {
- while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
- print $s;
- }
- if ( !defined($bytes) ) {
- $err = $!;
- die $!;
- vec( $rin, fileno(CHILD_O), 1 ) = 0
- unless ( $err == EINTR or $err == EAGAIN );
- }
- }
- last if ( $e1 || $e2 );
- }
- close CHILD_RD;
- close PARENT_ERR;
- waitpid( $pid, 0 );
- } else {
- foreach $key ( keys %req_params ) {
- $ENV{$key} = $req_params{$key};
- }
- # cd to the script
- user nginx;
- worker_processes 1;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #access_log logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #gzip on;
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- root html;
- index index.html index.htm;
- }
- location ~ ^/cgi-bin/.*\.cgi$ {
- gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
- fastcgi_pass unix:/usr/local/nginx/cgiwrap-dispatch.sock;
- fastcgi_index index.cgi;
- fastcgi_param SCRIPT_FILENAME /usr/local/nginx/$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;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http://127.0.0.1;
- #}
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
5.准备测试
- mkdir /usr/local/nginx/cgi-bin
- cat /usr/local/nginx/cgi-bin/index.cgi
- #!/usr/bin/perl
- print "Content-type: text/html\n\n";
- print "<html><body>Hello, world.</body></html>";
- chmod +x /usr/local/nginx/cgi-bin/index.cgi
6.设置拥有者
- chown nginx:nginx -R /usr/local/nginx
7.启动
- sudo -u nginx /usr/local/nginx/sbin/cgiwrap-fcgi.pl >> /usr/local/nginx/logs/cgiwrap.log 2>&1 &
- /usr/local/nginx/sbin/nginx