<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<channel>
	<title>flyinweb's blog - WEB服务器</title>
	<link>http://www.517sou.net/Folder/webserver/Index.aspx</link>
	<language>zh-CN</language>
	<webMaster>shanyiwan@msn.com(flyinweb)</webMaster>
	<pubDate>Mon, 15 Jun 2009 19:31:16 GMT</pubDate>
	<copyright>Copyright 2007-2009. All rights reserved.</copyright>
	<generator>Bitrac Free Version</generator>
	<description>桃李无言，下自成蹊</description>
	<image>
		<title>flyinweb&apos;s blog</title>
		<url>http://www.517sou.net/Client/Banner.gif</url>
		<link>http://www.517sou.net/</link>
		<description>桃李无言，下自成蹊</description>
	</image>
	<item>
		<link>http://www.517sou.net/Article/Apache-compile-fails-and-its-solution.aspx</link>
		<title>Apache编译失败及其解决方案</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Fri, 13 Jan 2012 02:47:45 GMT</pubDate>
		<description>&lt;p&gt;1、checking for SSL_set_cert_store... no&lt;br /&gt;configure: error: ... Error, SSL/TLS libraries were missing or unusable&lt;/p&gt;&lt;p&gt;安装openssl,在编译参数中添加--with-ssl=/usr/local/ssl （ssl安装路径，根据安装的实际路径设置）&lt;/p&gt;</description>
		<guid>http://www.517sou.net/Article/Apache-compile-fails-and-its-solution.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/753/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/Apache-compile-fails-and-its-solution.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/753/Feeds.ashx</wfw:commentRss>
	</item>
	<item>
		<link>http://www.517sou.net/Article/Nginx-as-an-IMAP-and-POP3-proxy.aspx</link>
		<title>Nginx as an IMAP/POP3 proxy</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Thu, 12 Jan 2012 03:55:12 GMT</pubDate>
		<description>&lt;p&gt;At Gigahost we are managing a lot of mailboxes for our users.&lt;/p&gt;&lt;p&gt;At the moment these are all located on one high speced server with the outgoing &lt;span class=&quot;caps&quot;&gt;SMTP&lt;/span&gt; split to another server.&lt;/p&gt;&lt;p&gt;We allow our users to connect via both &lt;span class=&quot;caps&quot;&gt;IMAP&lt;/span&gt; and POP3 and support &lt;span class=&quot;caps&quot;&gt;STARTTLS&lt;/span&gt; on ports 110/143 and &lt;span class=&quot;caps&quot;&gt;SSL&lt;/span&gt;/&lt;span class=&quot;caps&quot;&gt;TLS&lt;/span&gt; on ports 993/995.&lt;/p&gt;&lt;p&gt;Since we are constantly adding new users and these in turn add new mailboxes we are running out of options as to upgrade the current server. Hosting mailboxes via Courier, Dovecot or similar is very IO intensive and therefore in the long run disk IO becomes a problem.&lt;/p&gt;&lt;p&gt;The solution to this is ofcourse to scale the setup to more servers. Some hosting providers do this by simply adding users to a new mail server eg. &lt;code&gt;mail2.example.com&lt;/code&gt;, &lt;code&gt;mail3.example.com&lt;/code&gt; and so on.&lt;/p&gt;&lt;p&gt;What we would like to do is use a reverse proxy so that the user always connects to &lt;code&gt;mail.gigahost.dk&lt;/code&gt; and the proxy ensures that the user is send to the correct server.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Enter nginx&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://wiki.nginx.org/Main&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#0066cc&quot;&gt;nginx&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; is mostly known as the reverse proxy that drives sites such as youtube.com, wordpress.com, hulu.com, github.com and many many more.&lt;/p&gt;&lt;p&gt;But nginx can also act as an &lt;span class=&quot;caps&quot;&gt;IMAP&lt;/span&gt;/POP3 proxy and does quite a good job at it.&lt;/p&gt;&lt;p&gt;Using nginx you can authenticate the mail user before she/he reaches the mailserver and specify i) if the user can be authenticated, ii) what server the user should be send to. You can infact also alter the username and do other magic stuff.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;excerpt from nginx.conf&lt;/strong&gt;&lt;/p&gt;&lt;pre class=&quot;brush: plain&quot;&gt;
                http {
                  perl_modules  perl/lib;
                  perl_require  mailauth.pm;
            
                  server {
                    location /auth {
                      perl  mailauth::handler;
                    }
                  }
                }
            
                mail {
                  auth_http  127.0.0.1:80/auth;
                  auth_http_header X-NGX-Auth-Key &amp;quot;some secret&amp;quot;;
            
                  imap_auth plain login cram-md5;
                  pop3_auth plain apop cram-md5;
                }
              &lt;/pre&gt;&lt;p&gt;In the above excerpts you will see that I use the embedded perl module in nginx (you must add this a compile time). This serves up the &lt;code&gt;mailauth.pm&lt;/code&gt; script on port 80.&lt;/p&gt;&lt;p&gt;Be aware that the embedded perl parser blocks the current nginx process – so you might consider running a few and ensure that the script executes fast.&lt;/p&gt;&lt;p&gt;The &lt;code&gt;auth_http&lt;/code&gt; setting in the nginx config is where the magic happens. This points to the &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; server that handles the authentication and find the server the connection should be proxied to.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;mailauth.pm&lt;/strong&gt;&lt;/p&gt;&lt;pre class=&quot;brush: perl&quot;&gt;
                package mailauth;
                use Digest::HMAC_MD5 qw/ hmac_md5_hex /;
                use nginx;
                use DBI;
                use URI::Escape;
                my $dsn=&amp;quot;DBI:mysql:database=postfix;host=10.0.0.1&amp;quot;;
                our $dbh=DBI-&amp;gt;connect_cached($dsn, &apos;mail-proxy&apos;, &apos;p@ssword&apos;, {AutoCommit =&amp;gt; 1, mysql_auto_reconnect =&amp;gt; 1});
              
                our $auth_ok;
                our $protocol_ports={};
                $protocol_ports-&amp;gt;{&apos;pop3&apos;}=110;
                $protocol_ports-&amp;gt;{&apos;imap&apos;}=143;
                $protocol_ports-&amp;gt;{&apos;smtp&apos;}=25;
              
                sub handler {
                  if (!$dbh-&amp;gt;ping()) {
                    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
                    $dbh=DBI-&amp;gt;connect_cached($dsn, &apos;mail-proxy&apos;, &apos;p@ssword&apos;, {AutoCommit =&amp;gt; 1, mysql_auto_reconnect =&amp;gt; 1});
                    printf STDERR &amp;quot;%4d/%02d/%02d %02d:%02d:%02d [notice] : MySQL server connection lost. Reconnecting.\n&amp;quot;, $year+1900,$mon+1,$mday,$hour,$min,$sec;
                  }
                  
                  my $r = shift;
              
                  my $auth_method = $r-&amp;gt;header_in(&amp;quot;Auth-Method&amp;quot;);
                  my $username = uri_unescape($r-&amp;gt;header_in(&amp;quot;Auth-User&amp;quot;));
                  my $password = uri_unescape($r-&amp;gt;header_in(&amp;quot;Auth-Pass&amp;quot;));
                  my $salt = $r-&amp;gt;header_in(&amp;quot;Auth-Salt&amp;quot;);
              
                  our $sth=$dbh-&amp;gt;prepare(&amp;quot;select clear from users where email=? limit 1&amp;quot;); 
                  $sth-&amp;gt;execute($username);
                  my $hash=$sth-&amp;gt;fetchrow_hashref();
                  my $real_password = $hash-&amp;gt;{&apos;clear&apos;};
              
                  # Authorize user
                  if (($auth_method eq &amp;quot;plain&amp;quot; &amp;amp;&amp;amp; $password eq $real_password) or
                    ($auth_method eq &amp;quot;cram-md5&amp;quot; &amp;amp;&amp;amp; $password eq hmac_md5_hex($salt, $real_password))) {
                    # Auth OK, find mail server
                    our $sth=$dbh-&amp;gt;prepare(&amp;quot;select destination_mailstore from transport where domain=? limit 1&amp;quot;); 
                    my $domain = $r-&amp;gt;header_in(&amp;quot;Auth-User&amp;quot;);
                    $domain =~ s/^.*@//; # remove @ and everything before 
                    $sth-&amp;gt;execute($domain);
                    my $hash=$sth-&amp;gt;fetchrow_hashref();
                    my $mailserver = $hash-&amp;gt;{&apos;destination_mailstore&apos;};
                    $mailserver =~ s/smtp://;
              
                    $r-&amp;gt;header_out(&amp;quot;Auth-User&amp;quot;, $username);
                    $r-&amp;gt;header_out(&amp;quot;Auth-Pass&amp;quot;, $real_password);
                    $r-&amp;gt;header_out(&amp;quot;Auth-Status&amp;quot;, &amp;quot;OK&amp;quot;);
                    $r-&amp;gt;header_out(&amp;quot;Auth-Server&amp;quot;, $mailserver);
                    $r-&amp;gt;header_out(&amp;quot;Auth-Port&amp;quot;, $protocol_ports-&amp;gt;{$r-&amp;gt;header_in(&amp;quot;Auth-Protocol&amp;quot;)});
                    
                    # Shared secret to ensure that the request comes from this script
                    $r-&amp;gt;header_out(&amp;quot;X-NGX-Auth-Key&amp;quot;, &amp;quot;some secret&amp;quot;);
                  } else {
                    $r-&amp;gt;header_out(&amp;quot;Auth-Status&amp;quot;, &amp;quot;Invalid login or password&amp;quot;);
                  }
              
                  $r-&amp;gt;send_http_header(&amp;quot;text/html&amp;quot;);
              
                  return OK;
                }
              
                1;
                __END__
              &lt;/pre&gt;&lt;p&gt;The above script supports both plain and &lt;span class=&quot;caps&quot;&gt;CRAM&lt;/span&gt;-MD5 authentication. The request headers set by nginx are visible as &lt;code&gt;header_in(&amp;quot;...&amp;quot;)&lt;/code&gt; and the response headers that the script sets are &lt;code&gt;header_out&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;It should be pretty self-explanatory what the script does and how. The main feature here is ofcourse setting the &lt;code&gt;Auth-Server&lt;/code&gt; response header to the mailserver where you would like to point the user.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span class=&quot;caps&quot;&gt;UPDATE&lt;/span&gt; 2011/02/08:&lt;/strong&gt;&lt;br /&gt;nginx sends the &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; headers for the auth script &lt;em&gt;urlencoded&lt;/em&gt;. Therefore it is emparative that they be decoded so passwords like &lt;code&gt;my little p%ony&lt;/code&gt; works.&lt;/p&gt;&lt;p&gt;I’ve updated the script here to make use of the Perl uri library (you might have to install this).&lt;/p&gt;&lt;p&gt;Also I’ve added a small check to ensure that the MySQL connection is still alive and if not reconnect.&lt;/p&gt;&lt;p&gt;It seems that either my Perl script or the nginx embedded Perl module suffers from memory leaks.&lt;/p&gt;&lt;p&gt;Now, the easy way to fix this would be to run a &lt;code&gt;/etc/init.d/nginx restart&lt;/code&gt; every so often. However, that would of course suck.&lt;/p&gt;&lt;p&gt;So I started looking into alternative ways, using FastCGI to serve the authentication script.&lt;/p&gt;&lt;p&gt;The normal &lt;code&gt;fcgiwrapper&lt;/code&gt; in Debian was way to slow though. Handling only about 30 requests/sec.&lt;/p&gt;&lt;p&gt;Enter &lt;a href=&quot;http://search.cpan.org/~onlyjob/FCGI-Daemon-0.20111014/lib/FCGI/Daemon.pm&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#0066cc&quot;&gt;&lt;span class=&quot;caps&quot;&gt;FCGI&lt;/span&gt;-Daemon&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; by Dmitry Smirnov. It works by keeping the processes alive not respawning Perl on every request.&lt;/p&gt;&lt;p&gt;With this I was able to achieve 2500-3000 request/sec. More than enough to handle &lt;span class=&quot;caps&quot;&gt;IMAP&lt;/span&gt;/POP3 authentications.&lt;/p&gt;&lt;p&gt;I’ve included an updated authentication script for use with this.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;auth.pl&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;&lt;pre class=&quot;brush: perl&quot;&gt;
               #!/usr/bin/perl
            
               use Digest::HMAC_MD5 qw/ hmac_md5_hex /;
               use DBI;
               use URI::Escape;
               use CGI;
            
               print &amp;quot;Content-type: text/html\n&amp;quot;;
            
               my $q = CGI-&amp;gt;new;
               my $auth_shared_secret = $q-&amp;gt;http(&amp;quot;X-NGX-Auth-Key&amp;quot;);
            
               # Shared secret to ensure that the request comes from nginx
               if ( $auth_shared_secret ne &amp;quot;your secret&amp;quot; ) {
                 print &amp;quot;Auth-Status: Authentication failed.\n\n&amp;quot;;
                 print STDERR &amp;quot;Wrong X-NGC-Auth-Key $auth_shared_secret&amp;quot;;
                 exit(0);
               }
            
               my $dsn = &amp;quot;DBI:mysql:database=postfix;host=1.2.3.4&amp;quot;;
               our $dbh =
                 DBI-&amp;gt;connect_cached( $dsn, &apos;mailproxy&apos;, &apos;p@ssw0rd&apos;,
                   { AutoCommit =&amp;gt; 1, mysql_auto_reconnect =&amp;gt; 1 } );
            
               our $auth_ok;
               our $protocol_ports = {};
               $protocol_ports-&amp;gt;{&apos;pop3&apos;} = 110;
               $protocol_ports-&amp;gt;{&apos;imap&apos;} = 143;
               $protocol_ports-&amp;gt;{&apos;smtp&apos;} = 25;
            
               if ( !defined $dbh || !$dbh-&amp;gt;ping() ) {
                   ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
                     localtime(time);
                   $dbh =
                     DBI-&amp;gt;connect_cached( $dsn, &apos;mailproxy&apos;, &apos;p@ssw0rd&apos;,
                       { AutoCommit =&amp;gt; 1, mysql_auto_reconnect =&amp;gt; 1 } );
                   printf STDERR
               &amp;quot;%4d/%02d/%02d %02d:%02d:%02d [notice] : MySQL server connection lost. Reconnecting.\n&amp;quot;,
                     $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
               }
            
               my $auth_method = $q-&amp;gt;http(&amp;quot;Auth-Method&amp;quot;);
               my $username    = uri_unescape( $q-&amp;gt;http(&amp;quot;Auth-User&amp;quot;) );
               my $password    = uri_unescape( $q-&amp;gt;http(&amp;quot;Auth-Pass&amp;quot;) );
               my $salt        = $q-&amp;gt;http(&amp;quot;Auth-Salt&amp;quot;);
            
               our $sth = $dbh-&amp;gt;prepare(&amp;quot;select clear from users where email=? limit 1&amp;quot;);
               $sth-&amp;gt;execute($username);
               my $hash          = $sth-&amp;gt;fetchrow_hashref();
               my $real_password = $hash-&amp;gt;{&apos;clear&apos;};
            
               # Authorize user
               if (
                   ( $auth_method eq &amp;quot;plain&amp;quot; &amp;amp;&amp;amp; $password eq $real_password )
                   or (   $auth_method eq &amp;quot;cram-md5&amp;quot;
                       &amp;amp;&amp;amp; $password eq hmac_md5_hex( $salt, $real_password ) )
                 )
               {
            
                   # Auth OK, find mail server
                   our $sth = $dbh-&amp;gt;prepare(
                       &amp;quot;select destination_mailstore from transport where domain=? limit 1&amp;quot;);
                   my $domain = $q-&amp;gt;http(&amp;quot;Auth-User&amp;quot;);
            
                   # remove @ and everything before
                   $domain =~ s/^.*@//;
                   $sth-&amp;gt;execute($domain);
                   my $hash       = $sth-&amp;gt;fetchrow_hashref();
                   my $mailserver = $hash-&amp;gt;{&apos;destination_mailstore&apos;};
                   $mailserver =~ s/smtp://;
            
                   print &amp;quot;Auth-User: $username\n&amp;quot;;
                   print &amp;quot;Auth-Pass: $real_password\n&amp;quot;;
                   print &amp;quot;Auth-Status: OK\n&amp;quot;;
                   print &amp;quot;Auth-Server: $mailserver\n&amp;quot;;
                   $auth_port = $protocol_ports-&amp;gt;{ $q-&amp;gt;http(&amp;quot;Auth-Protocol&amp;quot;) };
                   print &amp;quot;Auth-Port: $auth_port\n&amp;quot;;
               }
               else {
                   print &amp;quot;Auth-Status: Authentication failed.\n&amp;quot;;
               }
            
               print &amp;quot;\n&amp;quot;;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;pre&gt;
#!/usr/bin/perl
use Digest::HMAC_MD5 qw/ hmac_md5_hex /;
use DBI;
use URI::Escape;
use CGI;
print &amp;quot;Content-type: text/html\n&amp;quot;;
my $q = CGI-&amp;gt;new;
my $auth_shared_secret = $q-&amp;gt;http(&amp;quot;X-NGX-Auth-Key&amp;quot;);
# Shared secret to ensure that the request comes from nginx
if ( $auth_shared_secret ne &amp;quot;your secret&amp;quot; ) {
  print &amp;quot;Auth-Status: Authentication failed.\n\n&amp;quot;;
  print STDERR &amp;quot;Wrong X-NGC-Auth-Key $auth_shared_secret&amp;quot;;
  exit(0);
}
my $dsn = &amp;quot;DBI:mysql:database=postfix;host=1.2.3.4&amp;quot;;
our $dbh =
  DBI-&amp;gt;connect_cached( $dsn, &apos;mailproxy&apos;, &apos;p@ssw0rd&apos;,
    { AutoCommit =&amp;gt; 1, mysql_auto_reconnect =&amp;gt; 1 } );
our $auth_ok;
our $protocol_ports = {};
$protocol_ports-&amp;gt;{&apos;pop3&apos;} = 110;
$protocol_ports-&amp;gt;{&apos;imap&apos;} = 143;
$protocol_ports-&amp;gt;{&apos;smtp&apos;} = 25;
if ( !defined $dbh || !$dbh-&amp;gt;ping() ) {
    ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
      localtime(time);
    $dbh =
      DBI-&amp;gt;connect_cached( $dsn, &apos;mailproxy&apos;, &apos;p@ssw0rd&apos;,
        { AutoCommit =&amp;gt; 1, mysql_auto_reconnect =&amp;gt; 1 } );
    printf STDERR
&amp;quot;%4d/%02d/%02d %02d:%02d:%02d [notice] : MySQL server connection lost. Reconnecting.\n&amp;quot;,
      $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
}
my $auth_method = $q-&amp;gt;http(&amp;quot;Auth-Method&amp;quot;);
my $username    = uri_unescape( $q-&amp;gt;http(&amp;quot;Auth-User&amp;quot;) );
my $password    = uri_unescape( $q-&amp;gt;http(&amp;quot;Auth-Pass&amp;quot;) );
my $salt        = $q-&amp;gt;http(&amp;quot;Auth-Salt&amp;quot;);
our $sth = $dbh-&amp;gt;prepare(&amp;quot;select clear from users where email=? limit 1&amp;quot;);
$sth-&amp;gt;execute($username);
my $hash          = $sth-&amp;gt;fetchrow_hashref();
my $real_password = $hash-&amp;gt;{&apos;clear&apos;};
# Authorize user
if (
    ( $auth_method eq &amp;quot;plain&amp;quot; &amp;amp;&amp;amp; $password eq $real_password )
    or (   $auth_method eq &amp;quot;cram-md5&amp;quot;
        &amp;amp;&amp;amp; $password eq hmac_md5_hex( $salt, $real_password ) )
  )
{
    # Auth OK, find mail server
    our $sth = $dbh-&amp;gt;prepare(
        &amp;quot;select destination_mailstore from transport where domain=? limit 1&amp;quot;);
    my $domain = $q-&amp;gt;http(&amp;quot;Auth-User&amp;quot;);
    # remove @ and everything before
    $domain =~ s/^.*@//;
    $sth-&amp;gt;execute($domain);
    my $hash       = $sth-&amp;gt;fetchrow_hashref();
    my $mailserver = $hash-&amp;gt;{&apos;destination_mailstore&apos;};
    $mailserver =~ s/smtp://;
    print &amp;quot;Auth-User: $username\n&amp;quot;;
    print &amp;quot;Auth-Pass: $real_password\n&amp;quot;;
    print &amp;quot;Auth-Status: OK\n&amp;quot;;
    print &amp;quot;Auth-Server: $mailserver\n&amp;quot;;
    $auth_port = $protocol_ports-&amp;gt;{ $q-&amp;gt;http(&amp;quot;Auth-Protocol&amp;quot;) };
    print &amp;quot;Auth-Port: $auth_port\n&amp;quot;;
}
else {
    print &amp;quot;Auth-Status: Authentication failed.\n&amp;quot;;
}
print &amp;quot;\n&amp;quot;;
&lt;/pre&gt;</description>
		<guid>http://www.517sou.net/Article/Nginx-as-an-IMAP-and-POP3-proxy.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/752/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/Nginx-as-an-IMAP-and-POP3-proxy.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/752/Feeds.ashx</wfw:commentRss>
	</item>
	<item>
		<link>http://www.517sou.net/Article/Creating-Certificate-Authorities-and-self-signed-SSL-certificates.aspx</link>
		<title>Creating Certificate Authorities and self-signed SSL certificates</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Sat, 07 Jan 2012 08:35:20 GMT</pubDate>
		<description>&lt;div class=&quot;textPlain&quot;&gt;Following is a step-by-step guide to creating your own CA (Certificate Authority) -- and also self-signed SSL server certificates -- with openssl on Linux. Self-signing is the simpler route to take, but making one&apos;s own CA allows the signing of multiple server certificates using the same CA and involves only a few extra steps. &lt;br /&gt;&lt;br /&gt;After using openssl to generate the necessary files, you&apos;ll need to integrate them into Apache. This process differs between Linux distros and versions of Apache. Additional references exist at the end of this document. My instructions for &lt;a href=&quot;http://www.tc.umn.edu/~brams006/selfsign_ubuntu.html&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#0066cc&quot;&gt;Setting up SSL: Ubuntu and Apache 2&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; are kept most current, and will carry you through to completion. &lt;br /&gt;&lt;br /&gt;Making a homemade CA or self-signed certificate will cause the client web browser to prompt with a message whether to trust the certificate signing authority (yourself) permanently (store it in the browser), temporarily for that session, or to reject it. The message &amp;quot;web site certified by an unknown authority... accept?&amp;quot; may be a business liability for general public usage, although it&apos;s simple enough for the client to accept the certificate permanently. &lt;br /&gt;&lt;br /&gt;Whichever route you take, you&apos;ll save the periodic expense of paying a recognized signing authority. This is purely for name recognition -- they&apos;ve paid the major browser producers to have their CA pre-loaded into them. So if you&apos;re on a budget, have a special need or small audience, this may be useful.&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;b&gt;Before you start&lt;/b&gt;&lt;br /&gt;You need &lt;a href=&quot;http://www.apache.org/&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#0066cc&quot;&gt;Apache&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; and &lt;a href=&quot;http://www.openssl.org/&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#0066cc&quot;&gt;openssl&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;. Compiling them from source, handling dependencies, etc. is beyond the scope of this document. You can consult their documentation, or go with a mainstream Linux distro that will do the preliminary work for you. &lt;br /&gt;&lt;br /&gt;Now you need to decide whether you&apos;ll make a CA (Certificate Authority) and sign a server certificate with it -- or just self-sign a server certificate. Both procedures are detailed below.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textPlain&quot;&gt;&lt;b&gt;(1A) Create a self-signed certificate.&lt;/b&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;Complete this section if you do NOT want to make a CA (Certificate Authority). If you want to make a CA, skip 1A entirely and go to 1B instead. &lt;br /&gt;&lt;br /&gt;Some steps in this document require priviledged access, and you&apos;ll want to limit access to the cert files to all but the root user. So you should su to root and create a working directory that only root has read/write access to (for example: mkdir certwork, chmod 600 certwork). Go to that directory. &lt;br /&gt;&lt;br /&gt;Generate a server key: &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHighlight&quot;&gt;openssl genrsa -des3 -out server.key 4096&lt;/div&gt;&lt;p&gt;&lt;br /&gt;Then create a certificate signing request with it. This command will prompt for a series of things (country, state or province, etc.). Make sure that &amp;quot;Common Name (eg, YOUR name)&amp;quot; matches the registered fully qualified domain name of your box (or your IP address if you don&apos;t have one). I also suggest not making a challenge password at this point, since it&apos;ll just mean more typing for you. &lt;br /&gt;&lt;br /&gt;The default values for the questions ([AU], Internet Widgits Pty Ltd, etc.) are stored here: /etc/ssl/openssl.cnf. So if you&apos;ve got a large number of certificate signing requests to process you probably want to carefully edit that file where appropriate. Otherwise, just execute the command below and type what needs to be typed: &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHighlight&quot;&gt;openssl req -new -key server.key -out server.csr&lt;/div&gt;&lt;p&gt;&lt;br /&gt;Now sign the certificate signing request. This example lasts 365 days: &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHighlight&quot;&gt;openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt&lt;/div&gt;&lt;p&gt;&lt;br /&gt;Make a version of the server.key which doesn&apos;t need a password: &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHighlight&quot;&gt;openssl rsa -in server.key -out server.key.insecure&lt;br /&gt;mv server.key server.key.secure&lt;br /&gt;mv server.key.insecure server.key&lt;/div&gt;&lt;p&gt;&lt;br /&gt;These files are quite sensitive and should be guarded for permissions very carefully. Chown them to root, if you&apos;re not already sudo&apos;d to root. I&apos;ve found that you can chmod 000 them. That is, root will always retain effective 600 (read) rights on everything. &lt;br /&gt;&lt;br /&gt;Now that you&apos;ve just completed Step 1A, skip ahead to Step 2. &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHeader&quot;&gt;(1B) Generate your own CA (Certificate Authority).&lt;/div&gt;&lt;p&gt;&lt;br /&gt;Complete this section if you want to make a CA (Certificate Authority) and sign a server certificate with it. The steps for making a server certificate are also included here. If you&apos;d rather one-time self-sign a server certificate, skip this step entirely and go to 1A instead. &lt;br /&gt;&lt;br /&gt;Some steps in this document require priviledged access, and you&apos;ll want to limit access to the cert files to all but the root user. So you should su to root and create a working directory that only root has read/write access to (for example: mkdir certwork, chmod 600 certwork). Go to that directory. &lt;br /&gt;&lt;br /&gt;In this step you&apos;ll take the place of VeriSign, Thawte, etc. You&apos;ll first build the CA key, then build the certificate itself. &lt;br /&gt;&lt;br /&gt;The Common Name (CN) of the CA and the Server certificates must NOT match or else a naming collision will occur and you&apos;ll get errors later on. In this step, you&apos;ll provide the CA entries. In a step below, you&apos;ll provide the Server entries. In this example, I just added &amp;quot;CA&amp;quot; to the CA&apos;s CN field, to distinguish it from the Server&apos;s CN field. Use whatever schema you want, just make sure the CA and Server entries are not identical. &lt;br /&gt;&lt;br /&gt;CA:&lt;br /&gt;Common Name (CN): www.somesite.edu CA&lt;br /&gt;Organization (O): Somesite&lt;br /&gt;Organizational Unit (OU): Development &lt;br /&gt;&lt;br /&gt;Server:&lt;br /&gt;Common Name (CN): www.somesite.edu&lt;br /&gt;Organization (O): Somesite&lt;br /&gt;Organizational Unit (OU): Development &lt;br /&gt;&lt;br /&gt;If you don&apos;t have a fully qualified domain name, you should use the IP that you&apos;ll be using to access your SSL site for Common Name (CN). But, again, make sure that something differentiates the entry of the CA&apos;s CN from the Server&apos;s CN. &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHighlight&quot;&gt;openssl genrsa -des3 -out ca.key 4096&lt;br /&gt;openssl req -new -x509 -days 365 -key ca.key -out ca.crt&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHeader&quot;&gt;Generate a server key and request for signing (csr).&lt;/div&gt;&lt;p&gt;&lt;br /&gt;This step creates a server key, and a request that you want it signed (the .csr file) by a Certificate Authority (the one you just created in Step #1B above.)&lt;br /&gt;&lt;br /&gt;Think carefully when inputting a Common Name (CN) as you generate the .csr file below. This should match the DNS name, or the IP address you specify in your Apache configuration. If they don&apos;t match, client browsers will get a &amp;quot;domain mismatch&amp;quot; message when going to your https web server. If you&apos;re doing this for home use, and you don&apos;t have a static IP or DNS name, you might not even want worry about the message (but you sure will need to worry if this is a production/public server). For example, you could match it to an internal and static IP you use behind your router, so that you&apos;ll never get the &amp;quot;domain mismatch&amp;quot; message if you&apos;re accessing the computer on your home LAN, but will always get that message when accessing it elsewhere. Your call -- is your IP stable, do you want to repeat these steps every time your IP changes, do you have a DNS name, do you mainly use it inside your home or LAN, or outside? &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHighlight&quot;&gt;openssl genrsa -des3 -out server.key 4096&lt;br /&gt;openssl req -new -key server.key -out server.csr&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHeader&quot;&gt;Sign the certificate signing request (csr) with the self-created Certificate Authority (CA) that you made earlier.&lt;/div&gt;&lt;p&gt;&lt;br /&gt;Note that 365 days is used here. After a year you&apos;ll need to do this again. &lt;br /&gt;&lt;br /&gt;Note also that I set the serial number of the signed server certificate to &amp;quot;01&amp;quot;. Each time you do this, especially if you do this before a previously-signed certificate expires, you&apos;ll need to change the serial key to something else -- otherwise everyone who&apos;s visited your site with a cached version of your certificate will get a browser warning message to the effect that your certificate signing authority has screwed up -- they&apos;ve signed a new key/request, but kept the old serial number. There are a couple ways to rectify that. crl&apos;s (certificate revocation list) is one method, but beyond the scope of the document. Another method is for all clients which have stored the CA certificate to go into their settings and delete the old one manually. But for the purposes of this document, we&apos;ll just avoid the problem. (If you&apos;re a sysadmin of a production system and your server.key is compromised, you&apos;ll certainly need to worry.) &lt;br /&gt;&lt;br /&gt;The command below does a number of things. It takes your signing request (csr) and makes a one-year valid signed server certificate (crt) out of it. In doing so, we need to tell it which Certificate Authority (CA) to use, which CA key to use, and which Server key to sign. We set the serial number to 01, and output the signed key in the file named server.crt. If you do this again after people have visited your site and trusted your CA (storing it in their browser), you might want to use 02 for the next serial number, and so on. You might create some scheme to make the serial number more &amp;quot;official&amp;quot; in appearance or makeup but keep in mind that it is fully exposed to the public in their web browsers, so it offers no additional security in itself. &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHighlight&quot;&gt;openssl x509 -req -days 365 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;To examine the components if you&apos;re curious: &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHighlight&quot;&gt;openssl rsa -noout -text -in server.key&lt;br /&gt;openssl req -noout -text -in server.csr&lt;br /&gt;openssl rsa -noout -text -in ca.key&lt;br /&gt;openssl x509 -noout -text -in ca.crt&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHeader&quot;&gt;Make a server.key which doesn&apos;t cause Apache to prompt for a password.&lt;/div&gt;&lt;p&gt;&lt;br /&gt;Here we create an insecure version of the server.key. The insecure one will be used for when Apache starts, and will not require a password with every restart of the web server. But keep in mind that while this means you don&apos;t have to type in a password when restarting Apache (or worse -- coding it somewhere in plaintext), it does mean that anyone obtaining this insecure key will be able to decrypt your transmissions. Guard it for permissions VERY carefully.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHighlight&quot;&gt;openssl rsa -in server.key -out server.key.insecure&lt;br /&gt;mv server.key server.key.secure&lt;br /&gt;mv server.key.insecure server.key&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;These files are quite sensitive and should be guarded for permissions very carefully. Chown them to root, if you&apos;re not already sudo&apos;d to root. I&apos;ve found that you can chmod 000 them. That is, root will always retain effective 600 (read) rights on everything. &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;textHeader&quot;&gt;(2) Copy files into position and tweak Apache.&lt;/div&gt;&lt;p&gt;&lt;br /&gt;Some professors like to pause for a moment after a long lecture, and do a little recap. It&apos;s a good pedagogical tool, so let&apos;s do so here. If you took route 1A above, you should have four files in a working directory: &lt;br /&gt;&lt;br /&gt;server.crt: The self-signed server certificate.&lt;br /&gt;server.csr: Server certificate signing request.&lt;br /&gt;server.key: The private server key, does not require a password when starting Apache.&lt;br /&gt;server.key.secure: The private server key, it does require a password when starting Apache. &lt;br /&gt;&lt;br /&gt;If you took route 1B and created a CA, you&apos;ll have two additional files: &lt;br /&gt;&lt;br /&gt;ca.crt: The Certificate Authority&apos;s own certificate.&lt;br /&gt;ca.key: The key which the CA uses to sign server signing requests. &lt;br /&gt;&lt;br /&gt;The CA files are important to keep if you want to sign additional server certificates and preserve the same CA. You can reuse these so long as they remain secure, and haven&apos;t expired. &lt;br /&gt;&lt;br /&gt;At a bare minimum, the following considerations must now be addressed: &lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;You&apos;ll need a virtual host and document root set up for the SSL instance.&lt;/li&gt;&lt;li&gt;You&apos;ll need to turn on the SSL engine and enable/load the SSL module.&lt;/li&gt;&lt;li&gt;Apache must reference server.crt and server.key somewhere in its configuration.&lt;/li&gt;&lt;li&gt;Apache must be listening to a port for which SSL is enabled (443 is default).&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The particulars differ between Linux distros and versions of Apache. I&apos;m only able to keep the &lt;a href=&quot;http://www.tc.umn.edu/~brams006/selfsign_ubuntu.html&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#0066cc&quot;&gt;Setting up SSL: Ubuntu and Apache 2&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; documentation current due to time constraints. Those steps should apply broadly to Debian-based distros with little or no modification. Red Hat and openSUSE commentary is kept online here for historical purposes.&lt;/p&gt;</description>
		<guid>http://www.517sou.net/Article/Creating-Certificate-Authorities-and-self-signed-SSL-certificates.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/748/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/Creating-Certificate-Authorities-and-self-signed-SSL-certificates.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/748/Feeds.ashx</wfw:commentRss>
	</item>
	<item>
		<link>http://www.517sou.net/Article/httpd-apr_sockaddr_info_get-failed-for-hostname.aspx</link>
		<title>httpd: apr_sockaddr_info_get() failed for HOSTNAME</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Fri, 09 Dec 2011 02:10:25 GMT</pubDate>
		<description>&lt;p&gt;If you get this error when starting Apache 2 here is the fix…&lt;br /&gt;&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;br /&gt;&lt;em&gt;Performing sanity check on apache22 configuration:&lt;br /&gt;httpd: apr_sockaddr_info_get() failed for &lt;/em&gt;&lt;/span&gt;&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;span style=&quot;font-weight: bold; text-decoration: underline&quot;&gt;someserver1.host-name.net&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;br /&gt;httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName&lt;br /&gt;Syntax OK&lt;br /&gt;Starting apache22.&lt;br /&gt;httpd: apr_sockaddr_info_get() failed for &lt;/em&gt;&lt;/span&gt;&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;span style=&quot;font-weight: bold; text-decoration: underline&quot;&gt;someserver1.host-name.net&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;br /&gt;httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName&lt;/em&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;The repair is very easy, just fix your hosts file locate at `/etc/hosts`.&lt;/p&gt;&lt;p&gt;First, determing your hostname of the machine that you are on…&lt;/p&gt;&lt;p&gt;#&amp;gt; hostname&lt;br /&gt;&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;span style=&quot;font-weight: bold; text-decoration: underline&quot;&gt;someserver1.host-name.net&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Then open your hosts file…&lt;/p&gt;&lt;p&gt;#&amp;gt; vi /etc/hosts&lt;/p&gt;&lt;p&gt;Then change all the host items to match&lt;br /&gt;&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;br /&gt;::1 localhost.&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;span style=&quot;font-weight: bold; text-decoration: underline&quot;&gt;someserver1.host-name.net&lt;/span&gt;&lt;/em&gt;&lt;/span&gt; localhost&lt;br /&gt;127.0.0.1 localhost.&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;span style=&quot;font-weight: bold; text-decoration: underline&quot;&gt;someserver1.host-name.net&lt;/span&gt;&lt;/em&gt;&lt;/span&gt; localhost&lt;br /&gt;192.1.0.123 &lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;span style=&quot;font-weight: bold; text-decoration: underline&quot;&gt;someserver1.host-name.net&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;span style=&quot;font-weight: bold; text-decoration: underline&quot;&gt;someserver1&lt;br /&gt;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;192.1.0.123 &lt;span style=&quot;font-family: times roman,times,sans&quot;&gt;&lt;em&gt;&lt;span style=&quot;font-weight: bold; text-decoration: underline&quot;&gt;someserver1.host-name.net&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;.&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Then start the server again…&lt;/p&gt;&lt;p&gt;#&amp;gt; apachectl start&lt;br /&gt;Performing sanity check on apache22 configuration:&lt;br /&gt;Syntax OK&lt;br /&gt;Starting apache22.&lt;br /&gt;#&amp;gt; …&lt;/p&gt;&lt;p&gt;All set!!!&lt;/p&gt;</description>
		<guid>http://www.517sou.net/Article/httpd-apr_sockaddr_info_get-failed-for-hostname.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/740/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/httpd-apr_sockaddr_info_get-failed-for-hostname.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/740/Feeds.ashx</wfw:commentRss>
	</item>
	<item>
		<link>http://www.517sou.net/Article/howto-linux-unix-setup-nginx-ssl-proxy.aspx</link>
		<title>nginx: Setup SSL Reverse Proxy (Load Balanced SSL Proxy)</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Fri, 09 Dec 2011 01:38:39 GMT</pubDate>
		<description>&lt;p&gt;&lt;span class=&quot;drop_cap&quot;&gt;A&lt;/span&gt; reverse proxy is a proxy server that is installed in a server network. Typically, reverse proxies are used in front of Web servers such as Apache, IIS, and Lighttpd. How do I setup nginx web server as SSL reverse proxy?&lt;br /&gt;&lt;br /&gt;When you&apos;ve multiple backend web servers, encryption / SSL acceleration can be done by a reverse proxy. Nginx can act as SSL acceleration software. It provided the following benefits:&lt;/p&gt;&lt;p&gt;&lt;li&gt;&lt;strong&gt;Easy of use&lt;/strong&gt; : Nginx is easy to setup and upgrade.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Security&lt;/strong&gt; : Nginx provide an additional layer of defense as Apache is behind the proxy. It can protect against common web-based attacks too.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Load Distribution&lt;/strong&gt; : nginx use very little memory and can distribute the load to several Apache servers. It can even rewrite urls on fly.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Caching&lt;/strong&gt; : Nginx act as a reverse proxy which offload the Web servers by caching static content, such as images, css, js, static html pages and much more.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Compression&lt;/strong&gt; : Nginx can optimize and compress the content to speed up the load time.&lt;h2&gt;Our Sample Setup&lt;/h2&gt;&lt;pre&gt;
Internet--
         |
    =============                               |---- apache1 (192.168.1.15)
    | ISP Router|                               |
    =============                               |---- apache2 (192.168.1.16)
         |                                      |
         |                                      |---- db1 (192.168.1.17)
         |      |eth0 -&amp;gt; 192.168.1.11 ----------/
         |-lb0==|                          /
         |      |eth1 -&amp;gt; 202.54.1.1:443---/
         |
         |      |eth0 -&amp;gt; 192.168.1.10 ----------\
         |-lb1==|                          /    |---- apache1 (192.168.1.15)
                |eth1 -&amp;gt; 202.54.1.1:443---/     |
                                                |---- apache2 (192.168.1.16)
                                                |
                                                |---- db1 (192.168.1.17)
&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;lb0 - Linux box directly connected to the Internet via eth1. This is master SSL load balancer.&lt;/li&gt;&lt;li&gt;lb1 - Linux box directly connected to the Internet via eth1. This is backup SSL load balancer. This will become active if master networking failed.&lt;/li&gt;&lt;li&gt;202.54.1.1 A virtual IP address that moves between lb0 and lb1. It is managed by keepalived.&lt;/li&gt;&lt;li&gt;nginx - It is installed on lb0 and lb1.&lt;/li&gt;&lt;li&gt;SSL Certificate - You need to install ssl certificates on lb0 and lb1.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;For demonstration purpose I&apos;m going to use Self-signed SSL certificate, but you can use real SSL certificate signed by CAs.&lt;/p&gt;&lt;pre&gt;
+------+	+-------------+	       +-------------------+
|Client|  &amp;lt;---&amp;gt; |SSL-Nginx:443|	&amp;lt;----&amp;gt; |Apache-HTTP_mode:80|
+------+        +-------------+        +-------------------+
&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;You&apos;ve the SSL connection between client and Nginx.&lt;/li&gt;&lt;li&gt;Then Nginx act as proxy server and makes unencrypted connection to Apache at port 80.&lt;/li&gt;&lt;li&gt;Nginx can cache all static file and other files.&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Generating Self-signed Certificate&lt;/h2&gt;&lt;p&gt;First, create required directories:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;&lt;font size=&quot;+0&quot;&gt;&lt;font size=&quot;+0&quot;&gt;# cd /usr/local/nginx/conf&lt;br /&gt;&lt;/font&gt;# mkdir ssl&lt;br /&gt;&lt;/font&gt;# cd ssl&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;To create a private key, enter:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# openssl genrsa -des3 -out nixcraft.in.key 1024&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Sample outputs:&lt;br /&gt;&lt;/p&gt;&lt;div style=&quot;width: 574px&quot; id=&quot;attachment_6967&quot; class=&quot;wp-caption alignnone&quot;&gt;&lt;a href=&quot;http://www.cyberciti.biz/faq/howto-linux-unix-setup-nginx-ssl-proxy/openssl-private-key/&quot; rel=&quot;attachment wp-att-6967&quot; target=&quot;_blank&quot;&gt;&lt;img title=&quot;Linux / UNIX OpenSSL - Create a Private Key Command&quot; alt=&quot;Fig.01: OpenSSL - Create a Private Key&quot; width=&quot;574&quot; height=&quot;162&quot; class=&quot;Image&quot; src=&quot;http://www.517sou.net/Attach/month_1201/aoluvh_openssl-private-key.png&quot; /&gt;&lt;/a&gt;&lt;p class=&quot;wp-caption-text&quot;&gt;Fig.01: OpenSSL - Create a Private Key&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;To create a CSR (Certificate Signing Request):&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# openssl req -new -key nixcraft.in.key -out nixcraft.in.csr&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Sample outputs:&lt;br /&gt;&lt;div style=&quot;width: 599px&quot; id=&quot;attachment_6970&quot; class=&quot;wp-caption alignnone&quot;&gt;&lt;a href=&quot;http://www.cyberciti.biz/faq/howto-linux-unix-setup-nginx-ssl-proxy/openssl-create-csr/&quot; rel=&quot;attachment wp-att-6970&quot; target=&quot;_blank&quot;&gt;&lt;img title=&quot;Linux / UNIX OpenSSL Create a CSR (Certificate Signing Request) Command&quot; alt=&quot;Fig.02: OpenSSL - Create a CSR (Certificate Signing Request)&quot; width=&quot;599&quot; height=&quot;310&quot; class=&quot;Image&quot; src=&quot;http://www.517sou.net/Attach/month_1201/4eudvo_openssl-create-csr.png&quot; /&gt;&lt;/a&gt;&lt;p class=&quot;wp-caption-text&quot;&gt;Fig.02: OpenSSL - Create a CSR (Certificate Signing Request)&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;Please enter your domain name that you want to associate with the certificate. For example, for the Command Name I entered nixcraft.in as I&apos;m going to use https://nixcraft.in/. &lt;h3&gt;How Do I Remove The Passphrase? (Optional)&lt;/h3&gt;&lt;p&gt;You can remove the passphrase so nginx can start on boot without entering the passphrase. Type the following commands&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;&lt;font size=&quot;+0&quot;&gt;# cp nixcraft.in.key nixcraft.in.key.bak&lt;br /&gt;&lt;/font&gt;# openssl rsa -in nixcraft.in.key.bak -out nixcraft.in.key&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Finally, you should see three files as follows (note I&apos;ve created all files as vivek user and than moved lb0 and lb1 server /usr/local/ngnix/conf/ssl/ directory):&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# ls -l&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Sample outputs:&lt;br /&gt;&lt;/p&gt;&lt;div style=&quot;width: 599px&quot; id=&quot;attachment_6973&quot; class=&quot;wp-caption alignnone&quot;&gt;&lt;a href=&quot;http://www.cyberciti.biz/faq/howto-linux-unix-setup-nginx-ssl-proxy/remove-ssl-passphrase/&quot; rel=&quot;attachment wp-att-6973&quot; target=&quot;_blank&quot;&gt;&lt;img title=&quot;Nginx All the files in ssl directory&quot; alt=&quot;Fig.03: All the files in ssl directory&quot; width=&quot;599&quot; height=&quot;94&quot; class=&quot;Image&quot; src=&quot;http://www.517sou.net/Attach/month_1201/0zmw2a_remove-ssl-passphrase.png&quot; /&gt;&lt;/a&gt;&lt;p class=&quot;wp-caption-text&quot;&gt;Fig.03: All the files in ssl directory&lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# openssl x509 -req -days 365 -in nixcraft.in.csr -signkey nixcraft.in.key -out nixcraft.in.crt&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Sample outputs:&lt;br /&gt;&lt;div style=&quot;width: 599px&quot; id=&quot;attachment_6980&quot; class=&quot;wp-caption alignnone&quot;&gt;&lt;a href=&quot;http://www.cyberciti.biz/faq/howto-linux-unix-setup-nginx-ssl-proxy/openssl-create-crt-file/&quot; rel=&quot;attachment wp-att-6980&quot; target=&quot;_blank&quot;&gt;&lt;img title=&quot;Linux / UNIX Nginx Generating  Actual Self-signed  SSL Certificate&quot; alt=&quot;Fig.04: Generating The Actual Self-signed  SSL Certificate&quot; width=&quot;599&quot; height=&quot;79&quot; class=&quot;Image&quot; src=&quot;http://www.517sou.net/Attach/month_1201/yvohjh_openssl-create-crt-file.png&quot; /&gt;&lt;/a&gt;&lt;p class=&quot;wp-caption-text&quot;&gt;Fig.04: Generating The Actual Self-signed SSL Certificate&lt;/p&gt;&lt;/div&gt;&lt;h3&gt;How Do I Copy SSL Certificates Files To lb1?&lt;/h3&gt;&lt;p&gt;You need to copy those files to lb1, enter:&lt;br /&gt;&lt;code&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;font size=&quot;+0&quot;&gt;# ssh root@lb1 mkdir /usr/local/ngnix/conf/ssl&lt;br /&gt;&lt;/font&gt;# rsync -av /usr/local/ngnix/conf/ssl/* root@lb1:/usr/local/ngnix/conf/ssl/&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;h2&gt;Configure Nginx As SSL Reverse Proxy (&lt;span style=&quot;color: rgb(153,51,153)&quot;&gt;lb0 and lb1&lt;/span&gt;)&lt;/h2&gt;&lt;p&gt;Edit nginx.conf, enter (you need to edit files on both lb0 and lb1):&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# vi /usr/local/ngnix/conf/nginx.conf&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Edit / append as follows:&lt;/p&gt;&lt;pre class=&quot;ini&quot;&gt;
 
server &lt;span&gt;{&lt;/span&gt;
	### server port and name ###
        listen          &lt;span&gt;443&lt;/span&gt; ssl&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        server_name     nixcraft.in&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
	### SSL log files ###
        access_log      logs/ssl-access.log&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        error_log       logs/ssl-error.log&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
	### SSL cert files ###
        ssl_certificate      ssl/nixcraft.in.crt&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        ssl_certificate_key  ssl/nixcraft.in.key&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
	### Add SSL specific settings here ###
        keepalive_timeout    &lt;span&gt;60&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
	###  Limiting Ciphers ########################
        # Uncomment as per your setup
	#ssl_ciphers HIGH:!ADH&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        #ssl_perfer_server_ciphers on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        #ssl_protocols SSLv3&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        ##############################################
	### We want full access to SSL via backend ###
     	location / &lt;span&gt;{&lt;/span&gt;
	        proxy_pass  http://nixcraft&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
		### force timeouts if one of backend is died ##
        	proxy_next_upstream error timeout invalid_header http_500 http_502 http_503&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
		### Set headers ####
        	proxy_set_header Host $host&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        	proxy_set_header X-Real-IP $remote_addr&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
	        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
		### Most PHP, Python, Rails, Java App can use this header ###
        	proxy_set_header X-Forwarded-Proto https&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
		### By default we don&apos;t want to redirect it ####
	        proxy_redirect     off&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Save and close the file. Reload nginx:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;&lt;font size=&quot;+0&quot;&gt;# /usr/local/nginx/sbin/nginx -t&lt;br /&gt;&lt;/font&gt;# /usr/local/nginx/sbin/nginx -s reload&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Verify port is opened:&lt;br /&gt;&lt;code&gt;&lt;font style=&quot;background-color: #eeeeee&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;# netstat -tulpn | grep :443&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;h2&gt;How Do I Test And Debug SSL Certificates From The Shell Prompt?&lt;/h2&gt;&lt;p&gt;Use the &lt;a title=&quot;How to: Debug SSL certificate problems from the shell prompt&quot; href=&quot;http://www.cyberciti.biz/tips/debugging-ssl-communications-from-unix-shell-prompt.html&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#2361a1&quot;&gt;openssl command as&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; follows:&lt;br /&gt;&lt;code&gt;&lt;font style=&quot;background-color: #eeeeee&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;$ openssl s_client -connect nixcraft.in:443&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;See &amp;quot;&lt;a href=&quot;http://www.cyberciti.biz/faq/test-ssl-certificates-diagnosis-ssl-certificate/&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#2361a1&quot;&gt;How To Verify SSL Certificate From&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; A Shell Prompt&amp;quot; for more details.&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;How Do I Cache Common Files?&lt;/h2&gt;&lt;p&gt;Edit nginx.conf and add as follows to cache common files:&lt;/p&gt;&lt;pre&gt;
location ~* \.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
        proxy_buffering           on;
        proxy_cache_valid 200 120m;
        expires 864000;
}&lt;/pre&gt;&lt;p&gt;Save and close the file. Reload nginx:&lt;br /&gt;&lt;code&gt;&lt;font style=&quot;background-color: #eeeeee&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;# nginx -s reload&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;&lt;/p&gt;</description>
		<guid>http://www.517sou.net/Article/howto-linux-unix-setup-nginx-ssl-proxy.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/739/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/howto-linux-unix-setup-nginx-ssl-proxy.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/739/Feeds.ashx</wfw:commentRss>
	</item>
	<item>
		<link>http://www.517sou.net/Article/rhel-centos-fedora-keepalived-lvs-cluster-configuration.aspx</link>
		<title>CentOS / Redhat Linux: Install Keepalived To Provide IP Failover For Web Cluster</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Fri, 09 Dec 2011 01:36:24 GMT</pubDate>
		<description>&lt;p&gt;&lt;span class=&quot;drop_cap&quot;&gt;K&lt;/span&gt;eepalived provides a strong and robust health checking for LVS clusters. It implements a framework of health checking on multiple layers for server failover, and VRRPv2 stack to handle director failover. How do I install and configure Keepalived for reverse proxy server such as nginx or lighttpd?&lt;br /&gt;&lt;br /&gt;If your are using a LVS director to loadbalance a server pool in a production environment, you may want to have a robust solution for healthcheck &amp;amp; failover. This will also work with reverse proxy server such as nginx.&lt;/p&gt;&lt;h2&gt;Our Sample Setup&lt;/h2&gt;&lt;pre&gt;
Internet--
         |
    =============
    | ISP Router|
    =============
         |
         |
         |      |eth0 -&amp;gt; 192.168.1.11 (connected to lan)
         |-lb0==|
         |      |eth1 -&amp;gt; 202.54.1.1 (vip master)
         |
         |      |eth0 -&amp;gt; 192.168.1.10 (connected to lan)
         |-lb1==|
                |eth1 -&amp;gt; 202.54.1.1 (vip backup)
&lt;/pre&gt;&lt;p&gt;Where,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;lb0 - Linux box directly connected to the Internet via eth1. This is master load balancer.&lt;/li&gt;&lt;li&gt;lb1 - Linux box directly connected to the Internet via eth1. This is backup load balancer. This will become active if master networking failed.&lt;/li&gt;&lt;li&gt;202.54.1.1 - This ip moves between lb0 and lb1 server. It is called virtual IP address and it is managed by keepalived.&lt;/li&gt;&lt;li&gt;eth0 is connected to LAN and all other backend software such as Apache, MySQL and so on.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;You need to install the following softwares on both lb0 and lb1:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;keepalived for IP failover.&lt;/li&gt;&lt;li&gt;iptables to filter traffic&lt;/li&gt;&lt;li&gt;nginx or lighttpd revers proxy server.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;DNS settings should be as follows:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;nixcraft.in - Our sample domain name.&lt;/li&gt;&lt;li&gt;lb0.nixcraft.in - 202.54.1.11 (real ip assigned to eth1)&lt;/li&gt;&lt;li&gt;lb1.nixcraft.in - 202.54.1.12 (real ip assigned to eth1)&lt;/li&gt;&lt;li&gt;www.nixcraft.in - 202.54.1.1 (VIP for web server) do not assign this IP to any interface.&lt;/li&gt;&lt;/ol&gt;&lt;h2&gt;Install Keepalived&lt;/h2&gt;&lt;p&gt;Visit &lt;a href=&quot;http://www.keepalived.org/&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#2361a1&quot;&gt;keepalived.org&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; to grab latest source code. You can use the &lt;a href=&quot;http://www.cyberciti.biz/tips/linux-wget-your-ultimate-command-line-downloader.html&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#2361a1&quot;&gt;wget command&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; to download the same (you need to install keepalived on both lb0 and lb1):&lt;br /&gt;&lt;code&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;# cd /opt&lt;br /&gt;&lt;/font&gt;# wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz&lt;br /&gt;&lt;/font&gt;# tar -zxvf keepalived-1.1.19.tar.gz&lt;br /&gt;&lt;/font&gt;# cd keepalived-1.1.19&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;h3&gt;Install Kernel Headers&lt;/h3&gt;&lt;p&gt;You need to install the following packages:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;strong&gt;Kernel-headers&lt;/strong&gt; - includes the C header files that specify the interface between the Linux kernel and userspace libraries and programs. The header files define structures and constants that are needed for building most standard programs and are also needed for rebuilding the glibc package.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;kernel-devel&lt;/strong&gt; - this package provides kernel headers and makefiles sufficient to build modules against the kernel package.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Make sure kernel-headers and kernel-devel packages are installed. If not type the following install the same:&lt;br /&gt;&lt;code&gt;&lt;font style=&quot;background-color: #eeeeee&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;# yum -y install kernel-headers kernel-devel &lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;h3&gt;Compile keepalived&lt;/h3&gt;&lt;p&gt;Type the following command:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# ./configure --with-kernel-dir=/lib/modules/$(uname -r)/build&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Sample outputs:&lt;/p&gt;&lt;pre&gt;
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
...
.....
..
config.status: creating keepalived/check/Makefile
config.status: creating keepalived/libipvs-2.6/Makefile
Keepalived configuration
------------------------
Keepalived version       : 1.1.19
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
Use VRRP Framework       : Yes
Use Debug flags          : No&lt;/pre&gt;&lt;p&gt;Compile and install the same:&lt;br /&gt;&lt;code&gt;&lt;font style=&quot;background-color: #eeeeee&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;# make &amp;amp;&amp;amp; make install&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;h3&gt;Create Required Softlinks&lt;/h3&gt;&lt;p&gt;Type the following commands to create service and run it at RHEL / CentOS run level #3 :&lt;br /&gt;&lt;code&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;# cd /etc/sysconfig&lt;br /&gt;&lt;/font&gt;# ln -s /usr/local/etc/sysconfig/keepalived .&lt;br /&gt;&lt;/font&gt;# cd /etc/rc3.d/&lt;br /&gt;&lt;/font&gt;# ln -s /usr/local/etc/rc.d/init.d/keepalived S100keepalived&lt;br /&gt;&lt;/font&gt;# cd /etc/init.d/&lt;br /&gt;&lt;/font&gt;# ln -s /usr/local/etc/rc.d/init.d/keepalived .&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;h3&gt;Configuration&lt;/h3&gt;&lt;p&gt;Your main configuration directory is located at /usr/local/etc/keepalived and configuration file name is keepalived.conf. First, make backup of existing configuration:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;&lt;font style=&quot;&quot;&gt;# cd /usr/local/etc/keepalived&lt;br /&gt;&lt;/font&gt;# cp keepalived.conf keepalived.conf.bak&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Edit keepalived.conf as follows on &lt;strong&gt;&lt;span style=&quot;color: rgb(102,102,204)&quot;&gt;lb0&lt;/span&gt;&lt;/strong&gt;:&lt;/p&gt;&lt;pre&gt;
vrrp_instance VI_1 {
        interface eth0
        state MASTER
        virtual_router_id 51
        &lt;span style=&quot;color: rgb(153,102,51)&quot;&gt;priority 101&lt;/span&gt;
        authentication {
            auth_type PASS
            auth_pass Add-Your-Password-Here
        }
        virtual_ipaddress {
                202.54.1.1/29 dev eth1
        }
}&lt;/pre&gt;&lt;p&gt;Edit keepalived.conf as follows on &lt;strong&gt;&lt;span style=&quot;color: rgb(102,102,204)&quot;&gt;lb1&lt;/span&gt;&lt;/strong&gt; (note priority set to 100 i.e. backup load balancer):&lt;/p&gt;&lt;pre&gt;
vrrp_instance VI_1 {
        interface eth0
        state MASTER
        virtual_router_id 51
        &lt;span style=&quot;color: rgb(255,0,0)&quot;&gt;priority 100&lt;/span&gt;
        authentication {
            auth_type PASS
            auth_pass Add-Your-Password-Here
        }
        virtual_ipaddress {
                202.54.1.1/29 dev eth1
        }
}&lt;/pre&gt;&lt;p&gt;Save and close the file. Finally start keepalived on &lt;span style=&quot;color: rgb(102,102,204)&quot;&gt;&lt;strong&gt;both lb0 and lb1&lt;/strong&gt;&lt;/span&gt; as follows:&lt;br /&gt;&lt;code&gt;&lt;font style=&quot;background-color: #eeeeee&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;# /etc/init.d/keepalived start&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;h3&gt;Verify: Keepalived Working Or Not&lt;/h3&gt;&lt;p&gt;/var/log/messages will keep track of VIP:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# tail -f /var/log/messages&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Sample outputs:&lt;/p&gt;&lt;pre&gt;
Feb 21 04:06:15 lb0 Keepalived_vrrp: Netlink reflector reports IP 202.54.1.1 added
Feb 21 04:06:20 lb0 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 202.54.1.1&lt;/pre&gt;&lt;p&gt;Verify that VIP assigned to eth1:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# ip addr show eth1&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Sample outputs:&lt;/p&gt;&lt;pre&gt;
3: eth1:  mtu 1500 qdisc pfifo_fast qlen 10000
    link/ether 00:30:48:30:30:a3 brd ff:ff:ff:ff:ff:ff
    inet 202.54.1.11/29 brd 202.54.1.254 scope global eth1
    inet &lt;span style=&quot;color: rgb(0,153,0)&quot;&gt;202.54.1.1/29&lt;/span&gt; scope global secondary eth1&lt;/pre&gt;&lt;h4&gt;ping failover test&lt;/h4&gt;&lt;p&gt;Open UNIX / Linux / OS X desktop terminal and type the following command to ping to VIP:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# ping 202.54.1.1&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Login to lb0 and halt the server or take down networking:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# halt&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Within seconds VIP should move from lb0 to lb1 and you should not see any drops in ping. On lb1 you should get the following in /var/log/messages:&lt;/p&gt;&lt;pre&gt;
Feb 21 04:10:07 lb1 Keepalived_vrrp: VRRP_Instance(VI_1) forcing a new MASTER election
Feb 21 04:10:08 lb1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb 21 04:10:09 lb1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Feb 21 04:10:09 lb1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Feb 21 04:10:09 lb1 Keepalived_healthcheckers: Netlink reflector reports IP 202.54.1.1 added
Feb 21 04:10:09 lb1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 202.54.1.1&lt;/pre&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;&lt;p&gt;Your server is now configured with IP failover. However, you need to install and configure the following software in order to configure webserver and security:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;nginx or lighttpd&lt;/li&gt;&lt;li&gt;iptables&lt;/li&gt;&lt;/ol&gt;</description>
		<guid>http://www.517sou.net/Article/rhel-centos-fedora-keepalived-lvs-cluster-configuration.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/738/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/rhel-centos-fedora-keepalived-lvs-cluster-configuration.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/738/Feeds.ashx</wfw:commentRss>
	</item>
	<item>
		<link>http://www.517sou.net/Article/handling-nginx-failover-with-keepalived.aspx</link>
		<title>Handling nginx Failover With KeepAlived</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Fri, 09 Dec 2011 01:34:42 GMT</pubDate>
		<description>&lt;p&gt;&lt;span class=&quot;drop_cap&quot;&gt;H&lt;/span&gt;ow do configure to release and obtain VIP (virtual IP) when nginx is dead, down or system is rebooted for the kernel upgrades?&lt;br /&gt;&lt;br /&gt;Edit /usr/local/etc/keepalived/keepalived.conf and add the following section to check whether nginx is alive or dead:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# vi /usr/local/etc/keepalived/keepalived.conf&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Updated file on both &lt;span style=&quot;color: rgb(255,0,0)&quot;&gt;&lt;strong&gt;lb0&lt;/strong&gt;&lt;/span&gt; and &lt;span style=&quot;color: rgb(153,102,51)&quot;&gt;&lt;strong&gt;lb1&lt;/strong&gt;&lt;/span&gt;:&lt;/p&gt;&lt;pre class=&quot;ini&quot;&gt;
vrrp_script chk_http_port &lt;span&gt;{&lt;/span&gt;
        script &lt;span style=&quot;color: #933&quot;&gt;&amp;quot;/usr/bin/killall -0 nginx&amp;quot;&lt;/span&gt;
        interval &lt;span&gt;2&lt;/span&gt;
        weight &lt;span&gt;2&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;
vrrp_instance VI_1 &lt;span&gt;{&lt;/span&gt;
        interface eth0
        state MASTER
        virtual_router_id &lt;span&gt;51&lt;/span&gt;
        priority &lt;span&gt;101&lt;/span&gt;
        authentication &lt;span&gt;{&lt;/span&gt;
            auth_type PASS
            auth_pass Add-Your-Password-Here
        &lt;span&gt;}&lt;/span&gt;
        track_script &lt;span&gt;{&lt;/span&gt;
            chk_http_port
        &lt;span&gt;}&lt;/span&gt;
        virtual_ipaddress &lt;span&gt;{&lt;/span&gt;&lt;span&gt;202.54&lt;/span&gt;&lt;span&gt;.1&lt;/span&gt;&lt;span&gt;.1&lt;/span&gt;/&lt;span&gt;29&lt;/span&gt; dev eth1
        &lt;span&gt;}&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Save and close the file. Reload keealived:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# /etc/init.d/keepalived restart&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;If nginx died due to any issues keepalived will release master VIP and backup server will become active. When master nginx LB0 comes backs online, the backup LB1 will go down in backup state.&lt;/p&gt;</description>
		<guid>http://www.517sou.net/Article/handling-nginx-failover-with-keepalived.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/737/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/handling-nginx-failover-with-keepalived.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/737/Feeds.ashx</wfw:commentRss>
	</item>
	<item>
		<link>http://www.517sou.net/Article/rhel-linux-install-nginx-as-reverse-proxy-load-balancer.aspx</link>
		<title>CentOS / Redhat: Install nginx As Reverse Proxy Load Balancer</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Fri, 09 Dec 2011 01:29:16 GMT</pubDate>
		<description>&lt;p&gt;&lt;span class=&quot;drop_cap&quot;&gt;H&lt;/span&gt;ow do I configure nginx as failover reverse proxy load balancer in front of two Apache web servers under CentOS / RHEL 5.x?&lt;br /&gt;&lt;br /&gt;nginx is a Web and Reverse proxy server. Nginx used in front of Apache Web servers. All connections coming from the Internet addressed to one of the Web servers are routed through the nginx proxy server, which may either deal with the request itself or pass the request wholly or partially to the main web servers.&lt;/p&gt;&lt;h2&gt;Our Sample Setup&lt;/h2&gt;&lt;pre&gt;
Internet--
         |
    =============                               |---- apache1 (192.168.1.15)
    | ISP Router|                               |
    =============                               |---- apache2 (192.168.1.16)
         |                                      |
         |                                      |---- db1 (192.168.1.17)
         |      |eth0 -&amp;gt; 192.168.1.11 ----------/
         |-lb0==|                        /
         |      |eth1 -&amp;gt; 202.54.1.1 ----/
         |
         |      |eth0 -&amp;gt; 192.168.1.10 ----------\
         |-lb1==|                        /      |---- apache1 (192.168.1.15)
                |eth1 -&amp;gt; 202.54.1.1 ----/       |
                                                |---- apache2 (192.168.1.16)
                                                |
                                                |---- db1 (192.168.1.17)
&lt;/pre&gt;&lt;p&gt;Where,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;lb0 - Linux box directly connected to the Internet via eth1. This is master load balancer.&lt;/li&gt;&lt;li&gt;lb1 - Linux box directly connected to the Internet via eth1. This is backup load balancer. This will become active if master networking failed.&lt;/li&gt;&lt;li&gt;202.54.1.1 - This ip moves between lb0 and lb1 server. It is called virtual IP address and it is managed by keepalived.&lt;/li&gt;&lt;li&gt;eth0 is connected to LAN and all other backend software servers are connected via eth0.&lt;/li&gt;&lt;li&gt;nginx is installed on both lb0 and lb1. It will listen on 202.54.1.1. You need to configure nginx as reverse proxy server. It will connects to Apache1 and Apache2.&lt;/li&gt;&lt;li&gt;Install httpd server on Apache#1 and Apache#2 server. Configure them to listen on 192.168.1.15:80 and 192.168.1.16:80. Do not assign public IP to this box. Only activate eth0 via LAN.&lt;/li&gt;&lt;li&gt;Install MySQL / Oracle / PgSQL server on Db#1. Configure db server to listen on 192.168.1.17:$db_server_port. Do not assign public IP to this box. Only activate eth0 via LAN.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;In short you need the following hardware:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;2 load balancer reverse proxy servers (250GB SATA, 2GB RAM, Single Intel P-D930 or AMD 170s with RHEL 64 bit+keepalived+nginx)&lt;/li&gt;&lt;li&gt;2 Apache web servers (Software RAID-1, SCSI-73GBx2 15k disk, 6GB RAM, Dual Intel Xeon or AMD 64 bit CPU with RHEL 64 bit+Apache 2)&lt;/li&gt;&lt;li&gt;1 backup Apache web servers (Software RAID-1, SCSI-73GBx2 15k disk, 6GB RAM, Dual Intel Xeon or AMD 64 bit CPU with RHEL 64 bit+Apache 2)&lt;/li&gt;&lt;li&gt;1 database server (RAID-10, SCSI-73GBx4 15k disk, 16GB RAM, Dual Intel Xeon or AMD 64 bit CPU with RHEL 64 bit+MySQL 5)&lt;/li&gt;&lt;li&gt;1 Caching server (RAID-1, SCSI-73GBx2 15k disk, 8GB RAM, Dual Intel Xeon or AMD 64 bit CPU with RHEL 64 bit)&lt;/li&gt;&lt;li&gt;1 offsite backup server (RAID-6, 1TB SATAx4, 4GB RAM, Single Intel/AMD CPU with RHEL 64bit)&lt;/li&gt;&lt;li&gt;Slave database, storage, pop3 and SMTP server as per requirements.&lt;/li&gt;&lt;li&gt;Internet uplink 100Mbps+ or as per requirements.&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Remove Unwanted Software From &lt;span style=&quot;color: rgb(255,0,0)&quot;&gt;lb0&lt;/span&gt; and &lt;span style=&quot;color: rgb(153,51,153)&quot;&gt;lb1&lt;/span&gt;&lt;/h2&gt;&lt;p&gt;Type the following commands:&lt;br /&gt;&lt;code&gt;&lt;font style=&quot;&quot;&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;# yum -y groupremove &amp;quot;X Window System&amp;quot;&lt;br /&gt;&lt;/font&gt;# x=$(yum list installed | egrep -i &apos;php|httpd|mysql|bind|dhclient|tftp|inetd|xinetd|ypserv|telnet-server|rsh-server|vsftpd|tcsh&apos; | awk &apos;{ print $1}&apos;)&lt;br /&gt;&lt;/font&gt;# yum -y remove $x&lt;br /&gt;&lt;/font&gt;# yum -y install bind-utils sysstat openssl-devel.x86_64 pcre-devel.x86_64 openssl097a.x86_64&lt;br /&gt;&lt;/font&gt;# &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;a title=&quot;Linux Upgrade Password Hashing Algorithm to SHA-512&quot; href=&quot;http://www.cyberciti.biz/faq/rhel-centos-fedora-linux-upgrading-password-hashing/&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font style=&quot;background-color: #eeeeee&quot; color=&quot;#2361a1&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;/usr/sbin/authconfig --passalgo=sha512 --update&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;br /&gt;&lt;font style=&quot;background-color: #eeeeee&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;# passwd root&lt;/font&gt;&lt;/code&gt;&lt;br /&gt;The above will remove X windows and other unwanted software from both lb0 and lb1.&lt;/p&gt;&lt;h2&gt;Install Nginx On Both &lt;span style=&quot;color: rgb(255,0,0)&quot;&gt;lb0&lt;/span&gt; and &lt;span style=&quot;color: rgb(153,51,153)&quot;&gt;lb1&lt;/span&gt;&lt;/h2&gt;&lt;p&gt;Type the following commands to download nginx, enter:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;&lt;font style=&quot;&quot;&gt;# cd /opt&lt;br /&gt;&lt;/font&gt;# wget http://sysoev.ru/nginx/nginx-0.8.33.tar.gz&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Untar nginx, enter:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;&lt;font style=&quot;&quot;&gt;# tar -zxvf nginx-0.8.33.tar.gz&lt;br /&gt;&lt;/font&gt;# cd nginx-0.8.33&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Configure nginx for 64 bit RHEL / CentOS Linux:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# ./configure --without-http_autoindex_module --without-http_ssi_module --without-http_userid_module --without-http_auth_basic_module --without-http_geo_module --without-http_fastcgi_module --without-http_empty_gif_module --with-openssl=/lib64&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Sample outputs:&lt;/p&gt;&lt;pre&gt;
....
  nginx path prefix: &amp;quot;/usr/local/nginx&amp;quot;
  nginx binary file: &amp;quot;/usr/local/nginx/sbin/nginx&amp;quot;
  nginx configuration prefix: &amp;quot;/usr/local/nginx/conf&amp;quot;
  nginx configuration file: &amp;quot;/usr/local/nginx/conf/nginx.conf&amp;quot;
  nginx pid file: &amp;quot;/usr/local/nginx/logs/nginx.pid&amp;quot;
  nginx error log file: &amp;quot;/usr/local/nginx/logs/error.log&amp;quot;
  nginx http access log file: &amp;quot;/usr/local/nginx/logs/access.log&amp;quot;
  nginx http client request body temporary files: &amp;quot;client_body_temp&amp;quot;
  nginx http proxy temporary files: &amp;quot;proxy_temp&amp;quot;
  nginx http fastcgi temporary files: &amp;quot;fastcgi_temp&amp;quot;
...
&lt;/pre&gt;&lt;p&gt;Install the same:&lt;br /&gt;&lt;code&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;font style=&quot;&quot;&gt;# make&lt;br /&gt;&lt;/font&gt;# make install&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;&lt;h3&gt;Create nginx User Account&lt;/h3&gt;&lt;p&gt;Type the following commands to create a user account:&lt;br /&gt;&lt;code&gt;&lt;font style=&quot;background-color: #eeeeee&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;# &lt;/font&gt;&lt;a title=&quot;Linux Create User Account&quot; href=&quot;http://www.cyberciti.biz/faq/howto-add-new-linux-user-account/&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font style=&quot;background-color: #eeeeee&quot; color=&quot;#2361a1&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;useradd -s /sbin/nologin -d /usr/local/nginx/html -M nginx&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;br /&gt;&lt;font style=&quot;background-color: #eeeeee&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;# &lt;/font&gt;&lt;a title=&quot;Linux locking an account&quot; href=&quot;http://www.cyberciti.biz/faq/linux-locking-an-account/&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font style=&quot;background-color: #eeeeee&quot; color=&quot;#2361a1&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;passwd -l nginx&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/code&gt;&lt;/p&gt;&lt;h2&gt;Configure nginx As Reverse Proxy Load Balancer On Both &lt;span style=&quot;color: rgb(255,0,0)&quot;&gt;lb0&lt;/span&gt; and &lt;span style=&quot;color: rgb(153,51,153)&quot;&gt;lb1&lt;/span&gt;&lt;/h2&gt;&lt;p&gt;Edit /usr/local/nginx/conf/nginx.conf, enter:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# vi /usr/local/nginx/conf/nginx.conf&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Update it as follows:&lt;/p&gt;&lt;pre class=&quot;ini&quot;&gt;
 
pid               logs/nginx.pid&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
user              nginx nginx&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
worker_processes  &lt;span&gt;10&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
events &lt;span&gt;{&lt;/span&gt;
    worker_connections  &lt;span&gt;1024&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;
 
http &lt;span&gt;{&lt;/span&gt;
  default_type       application/octet-stream&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
 ## Common options ##
 include options.conf&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
 ## Proxy settings ##
 include proxy.conf&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
 ## lb domains ##
 include nixcraft.in.conf&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Edit /usr/local/nginx/conf/options.conf, enter:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;# vi /usr/local/nginx/conf/options.conf&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Update it as follows:&lt;/p&gt;&lt;pre class=&quot;ini&quot;&gt;
 
 ## Size Limits
  client_body_buffer_size     128K&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  client_header_buffer_size   1M&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  client_max_body_size          1M&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  large_client_header_buffers &lt;span&gt;8&lt;/span&gt; 8k&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
 ## Timeouts
  client_body_timeout   &lt;span&gt;60&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  client_header_timeout &lt;span&gt;60&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  expires               24h&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  keepalive_timeout     &lt;span&gt;60&lt;/span&gt;&lt;span&gt;60&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  send_timeout          &lt;span&gt;60&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
 ## General Options
  ignore_invalid_headers   on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  keepalive_requests      &lt;span&gt;100&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  limit_zone gulag $binary_remote_addr 5m&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  recursive_error_pages    on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  sendfile                 on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  server_name_in_redirect off&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  server_tokens           off&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
 ## TCP options
  tcp_nodelay on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  tcp_nopush  on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
 ## Compression
  gzip              on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  gzip_buffers      &lt;span&gt;16&lt;/span&gt; 8k&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  gzip_comp_level   &lt;span&gt;6&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  gzip_http_version &lt;span&gt;1.0&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  gzip_min_length   &lt;span&gt;0&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  gzip_types        text/plain text/css image/x-icon application/x-perl application/x-httpd-cgi&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  gzip_vary         on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
 ## Log Format
  log_format  main  &apos;$remote_addr $host $remote_user &lt;span style=&quot;color: #000066; font-weight: bold&quot;&gt;&lt;span&gt;[&lt;/span&gt;$time_local&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #933&quot;&gt;&amp;quot;$request&amp;quot;&lt;/span&gt; &apos;
                    &apos;$status $body_bytes_sent &lt;span style=&quot;color: #933&quot;&gt;&amp;quot;$http_referer&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #933&quot;&gt;&amp;quot;$http_user_agent&amp;quot;&lt;/span&gt; &apos;
                    &apos;&lt;span style=&quot;color: #933&quot;&gt;&amp;quot;$gzip_ratio&amp;quot;&lt;/span&gt;&apos;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Edit /usr/local/nginx/conf/proxy.conf, enter:&lt;/p&gt;&lt;pre class=&quot;ini&quot;&gt;
 
 ## Proxy caching options
  proxy_buffering           on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_cache_min_uses       &lt;span&gt;3&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_cache_path          /usr/local/nginx/proxy_temp/ &lt;span style=&quot;color: #000099&quot;&gt;levels&lt;/span&gt;=&lt;span style=&quot;color: #660066&quot;&gt;&lt;span&gt;1&lt;/span&gt;:&lt;span&gt;2&lt;/span&gt; keys_zone=cache:10m inactive=10m max_size=1000M&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_cache_valid         any 10m&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_ignore_client_abort off&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_intercept_errors    on&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_next_upstream       error timeout invalid_header&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_redirect            off&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_set_header          X-Forwarded-For $remote_addr&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_connect_timeout     &lt;span&gt;60&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_send_timeout        &lt;span&gt;60&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
  proxy_read_timeout        &lt;span&gt;60&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Edit /usr/local/nginx/conf/nixcraft.in.conf, enter:&lt;/p&gt;&lt;pre class=&quot;ini&quot;&gt;
 
## Connect to backend servers via LAN ##
## Reverse Proxy Load Balancer Logic ##
upstream nixcraft  &lt;span&gt;{&lt;/span&gt;
      server &lt;span&gt;192.168&lt;/span&gt;&lt;span&gt;.1&lt;/span&gt;&lt;span&gt;.15&lt;/span&gt;&lt;span style=&quot;color: #000099&quot;&gt;weight&lt;/span&gt;=&lt;span style=&quot;color: #660066&quot;&gt;&lt;span&gt;10&lt;/span&gt; max_fails=&lt;span&gt;3&lt;/span&gt; fail_timeout=30s&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
      server &lt;span&gt;192.168&lt;/span&gt;&lt;span&gt;.1&lt;/span&gt;&lt;span&gt;.16&lt;/span&gt;&lt;span style=&quot;color: #000099&quot;&gt;weight&lt;/span&gt;=&lt;span style=&quot;color: #660066&quot;&gt;&lt;span&gt;10&lt;/span&gt; max_fails=&lt;span&gt;3&lt;/span&gt; fail_timeout=30s&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
      # only comes alive when above two fails
      server &lt;span&gt;192.168&lt;/span&gt;&lt;span&gt;.1&lt;/span&gt;&lt;span&gt;.23&lt;/span&gt;&lt;span style=&quot;color: #000099&quot;&gt;weight&lt;/span&gt;=&lt;span style=&quot;color: #660066&quot;&gt;&lt;span&gt;1&lt;/span&gt; backup&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;
 
server &lt;span&gt;{&lt;/span&gt;
      access_log  logs/access.log main&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
      error_log   logs/error.log&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
      index       index.html&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
      root        /usr/local/nginx/html&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
      server_name nixcraft.in www.nixcraft.in subdomain.nixcraft.in&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
     ## Only requests to our Host are allowed
      if &lt;span&gt;(&lt;/span&gt;$host !~ ^&lt;span&gt;(&lt;/span&gt;nixcraft.in|www.nixcraft.in|subdomain.nixcraft.in&lt;span&gt;)&lt;/span&gt;$ &lt;span&gt;)&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;
         return &lt;span&gt;444&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;
 
     ## redirect www to nowww
     # if &lt;span&gt;(&lt;/span&gt;$&lt;span style=&quot;color: #000099&quot;&gt;host &lt;/span&gt;=&lt;span style=&quot;color: #660066&quot;&gt; &apos;www.nixcraft.in&apos; &lt;span&gt;)&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
     #    rewrite  ^/&lt;span&gt;(&lt;/span&gt;.*&lt;span&gt;)&lt;/span&gt;$  http://nixcraft.in/$&lt;span&gt;1&lt;/span&gt;  permanent&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
     # &lt;span&gt;}&lt;/span&gt;
 
     ## Only allow these request methods
     if &lt;span&gt;(&lt;/span&gt;$request_method !~ ^&lt;span&gt;(&lt;/span&gt;GET|HEAD|POST&lt;span&gt;)&lt;/span&gt;$ &lt;span&gt;)&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;
         return &lt;span&gt;444&lt;/span&gt;&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;
 
     ## PROXY - Web
      location / &lt;span&gt;{&lt;/span&gt;
        proxy_pass  http://nixcraft&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        proxy_cache            cache&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        proxy_cache_valid      &lt;span&gt;200&lt;/span&gt; 24h&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        proxy_cache_use_stale  error timeout invalid_header updating http_500 http_502 http_503 http_504&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        proxy_ignore_headers   Expires Cache-Control&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
 
        proxy_set_header        Host            $host&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        proxy_set_header        X-Real-IP       $remote_addr&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;
 
     # redirect server error pages to the static page /50x.html
        error_page   &lt;span&gt;500&lt;/span&gt;&lt;span&gt;502&lt;/span&gt;&lt;span&gt;503&lt;/span&gt;&lt;span&gt;504&lt;/span&gt;  /50x.html&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #000099&quot;&gt;location &lt;/span&gt;=&lt;span style=&quot;color: #660066&quot;&gt; /50x.html &lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
            root   html&lt;span style=&quot;font-style: italic; color: #666666&quot;&gt;;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Start nginx web server:&lt;br /&gt;&lt;font size=&quot;2&quot;&gt;&lt;font style=&quot;background-color: #eeeeee&quot;&gt;&lt;font face=&quot;Consolas&quot;&gt;&lt;code&gt;&lt;font style=&quot;&quot;&gt;&lt;font style=&quot;&quot;&gt;# /usr/local/nginx/sbin/nginx&lt;br /&gt;&lt;/font&gt;# netstat -tulpn | grep :80&lt;br /&gt;&lt;/font&gt;# echo &apos; /usr/local/nginx/sbin/nginx&apos; &amp;gt;&amp;gt; /etc/rc.local&lt;/code&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;Fire a webbrowser and type domain name such as nixcraft.in:&lt;br /&gt;&lt;code&gt;&lt;a href=&quot;http://nixcraft.in/&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font style=&quot;background-color: #eeeeee&quot; color=&quot;#2361a1&quot; size=&quot;2&quot; face=&quot;Consolas&quot;&gt;http://nixcraft.in&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/code&gt;&lt;/p&gt;&lt;h4&gt;References:&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://nginx.org/&quot; target=&quot;_blank&quot;&gt;&lt;u&gt;&lt;font color=&quot;#2361a1&quot;&gt;nginx&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; wiki&lt;/li&gt;&lt;/ul&gt;</description>
		<guid>http://www.517sou.net/Article/rhel-linux-install-nginx-as-reverse-proxy-load-balancer.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/736/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/rhel-linux-install-nginx-as-reverse-proxy-load-balancer.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/736/Feeds.ashx</wfw:commentRss>
	</item>
	<item>
		<link>http://www.517sou.net/Article/using-fastcgi-to-host-php-applications-on-iis-7-and-above.aspx</link>
		<title>Using FastCGI to Host PHP Applications on IIS 7</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Mon, 17 Oct 2011 15:04:54 GMT</pubDate>
		<description>&lt;p&gt;This article describes how to configure the FastCGI module and PHP to host PHP applications on IIS 7.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;img alt=&quot;&quot; src=&quot;http://learn.iis.net/file.axd?i=922&quot; /&gt; IMPORTANT&lt;/strong&gt;: This article provides instructions on how to install and use the FastCGI component on Windows Server 2008 and Windows Vista &lt;strong&gt;SP1&lt;/strong&gt;. SP1 is &lt;strong&gt;required&lt;/strong&gt; on Windows Vista.&lt;/p&gt;&lt;h2&gt;Table of Contents&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Overview&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Overview&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#EnableFastCGI&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Enabling FastCGI support in IIS 7&lt;/font&gt;&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Windows_Server_2008:_&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Windows Server 2008&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Windows_Vista_SP1&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Windows Vista SP1&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Update_for_FastCGI_module&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Update for the FastCGI module&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Install_Administration_Pack_for_IIS_7.0&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Administration Pack for IIS 7&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#InstallPHP&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Install and Configure PHP&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Configure_handler_mapping&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Configure IIS to handle PHP requests&lt;/font&gt;&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Using_IIS_Manager_&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Using IIS Manager&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Using_command_line&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Using command line&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#BestPractices&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Best practices for configuring FastCGI and PHP&lt;/font&gt;&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Security_Isolation&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Security Isolation&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#PHP_Recycling_Behavior&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Process recycling&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#PHP_Versioning&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;PHP versioning&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#PHP_Security_Recommendations_&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Security recommendations&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Per-site_PHP_configuration&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Per-site PHP configuration&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Rewrite&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;URL rewriting for PHP applications&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Related_resources_&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Related Resources&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Overview&lt;/h2&gt;&lt;p&gt;The FastCGI module in IIS enables popular application frameworks that support the FastCGI protocol to be hosted on the IIS Web server in a high performance and reliable way. FastCGI provides a high-performance alternative to the Common Gateway Interface (CGI), which is a standard way of interfacing external applications with Web servers that has been a part of the supported IIS feature set since the first release.&lt;/p&gt;&lt;p&gt;CGI programs are executable files that are launched by the Web server for each request to process the request and generate dynamic responses that are then sent back to the client. Because many of these frameworks do not support multi-threaded execution, CGI enables them to execute reliably on IIS by executing exactly one request per process. Unfortunately, it provides poor performance due to the high cost of starting and shutting down a process for each request.&lt;/p&gt;&lt;p&gt;FastCGI addresses the performance issues that are inherent in CGI by providing a mechanism to reuse a single process over and over again for many requests. Additionally, FastCGI maintains compatibility with non-thread-safe libraries by providing a pool of reusable processes and ensuring that each process handles only one request at a time.&lt;/p&gt;&lt;h2&gt;Enable FastCGI Support in IIS 7&lt;/h2&gt;&lt;h3&gt;Windows Server 2008&lt;/h3&gt;&lt;p&gt;Go to &lt;strong&gt;Server Manager&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Roles&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Add Role Services&lt;/strong&gt;. On the &lt;strong&gt;Select Role Services&lt;/strong&gt; page, select the &lt;strong&gt;CGI&lt;/strong&gt; check box. This enables both the CGI and FastCGI services.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=925&quot; target=&quot;_blank&quot;&gt;&lt;img style=&quot;border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; width=&quot;780&quot; height=&quot;587&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/ll8qep_IIS-EnableFastCGIRole.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;h3&gt;Windows Vista SP1&lt;/h3&gt;&lt;p&gt;Go to &lt;strong&gt;Control Panel&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Programs and Features&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Turn Windows features on or off&lt;/strong&gt;. In the &lt;strong&gt;Windows Features&lt;/strong&gt; dialog box, select the &lt;strong&gt;CGI&lt;/strong&gt; check box. This enables both the CGI and FastCGI services.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=926&quot; mce_href=&quot;/file.axd?i=926&quot; target=&quot;_blank&quot;&gt;&lt;img style=&quot;border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; width=&quot;429&quot; height=&quot;375&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/m4b0ly_CGIinVista.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;h3&gt;&lt;img style=&quot;border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; alt=&quot;&quot; src=&quot;http://learn.iis.net/file.axd?i=922&quot; /&gt; IMPORTANT: Install the Update for the FastCGI Module&lt;/h3&gt;&lt;p&gt;The update for the IIS 7 FastCGI module fixes several known compatibility issues with popular PHP applications. Install the update from one of the following locations:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a title=&quot;Update for Windows Server 2008&quot; href=&quot;http://www.microsoft.com/downloads/info.aspx?na=22&amp;amp;p=1&amp;amp;SrcDisplayLang=en&amp;amp;SrcCategoryId=&amp;amp;SrcFamilyId=&amp;amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3dd0343911-1775-4aef-8c99-5f13862ac386%26DisplayLang%3den&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Update for Windows Server 2008&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;Update for Windows Server 2008 x64 Edition&quot; href=&quot;http://www.microsoft.com/downloads/info.aspx?na=22&amp;amp;p=3&amp;amp;SrcDisplayLang=en&amp;amp;SrcCategoryId=&amp;amp;SrcFamilyId=&amp;amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3d70278393-3291-4aa1-870b-0e9b0907bddf%26DisplayLang%3den&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Update for Windows Server 2008 x64 Edition&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;Update for Windows Server 2008 for Itanium-based Systems&quot; href=&quot;http://www.microsoft.com/downloads/info.aspx?na=22&amp;amp;p=5&amp;amp;SrcDisplayLang=en&amp;amp;SrcCategoryId=&amp;amp;SrcFamilyId=&amp;amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3d98e06637-0f00-45d5-83c5-ed1b41fd6a7b%26DisplayLang%3den&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Update for Windows Server 2008 for Itanium-based Systems&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;Update for Windows Vista&quot; href=&quot;http://www.microsoft.com/downloads/info.aspx?na=22&amp;amp;p=2&amp;amp;SrcDisplayLang=en&amp;amp;SrcCategoryId=&amp;amp;SrcFamilyId=&amp;amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3d19600729-8470-4956-a276-200450d814bd%26DisplayLang%3den&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Update for Windows Vista SP1&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a title=&quot;Update for Windows Vista for x64 based Systems&quot; href=&quot;http://www.microsoft.com/downloads/info.aspx?na=22&amp;amp;p=4&amp;amp;SrcDisplayLang=en&amp;amp;SrcCategoryId=&amp;amp;SrcFamilyId=&amp;amp;u=%2fdownloads%2fdetails.aspx%3fFamilyID%3dc7066c3b-dcf7-4441-87bc-f7dcb51067d0%26DisplayLang%3den&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Update for Windows Vista SP1 for x64 based Systems&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Install the Administration Pack for IIS 7&lt;/h3&gt;&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; This step is optional.&lt;/p&gt;&lt;p&gt;Among other useful features, the Administration Pack for IIS 7 has a convenient user interface for configuring FastCGI settings. The Administration Pack can be installed from the following locations:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://www.iis.net/downloads/default.aspx?tabid=34&amp;amp;i=1682&amp;amp;g=6&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Administration Pack for IIS 7 - x86&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://www.iis.net/downloads/default.aspx?tabid=34&amp;amp;i=1683&amp;amp;g=6&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Administration Pack for IIS 7 - x64&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Install and Configure PHP&lt;/h2&gt;&lt;p&gt;It is recommended that you use a non-thread safe build of PHP with IIS 7 FastCGI. A non-thread safe build of PHP provides significant performance gains over the standard build by not doing any thread-safety checks, which are not necessary, since FastCGI ensures a single threaded execution environment.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;To install PHP:&lt;/strong&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Download the latest non-thread safe zip package with binaries of PHP: &lt;a href=&quot;http://www.php.net/downloads.php&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;http://www.php.net/downloads.php&lt;/font&gt;&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Unpack the files to the directory of your choice (e.g. C:\PHP). Rename the php.ini-recommended file to php.ini.&lt;/li&gt;&lt;li&gt;Open the php.ini file. Uncomment and modify the settings as follows: &lt;ul&gt;&lt;li&gt;Set &lt;strong&gt;fastcgi.impersonate = 1&lt;/strong&gt;. FastCGI under IIS supports the ability to impersonate security tokens of the calling client. This allows IIS to define the security context that the request runs under.&lt;/li&gt;&lt;li&gt;Set &lt;strong&gt;cgi.fix_pathinfo=1&lt;/strong&gt;. cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. Previously, PHP behavior was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not define PATH_INFO. For more information about PATH_INFO, see the cgi specifications. Setting this value to 1 will cause PHP CGI to fix its paths to conform to the specifications.&lt;/li&gt;&lt;li&gt;Set &lt;strong&gt;cgi.force_redirect&lt;/strong&gt; = 0.&lt;/li&gt;&lt;li&gt;Set &lt;strong&gt;open_basedir&lt;/strong&gt; to point to the folder or network path where the content of the Web site(s) is located.&lt;/li&gt;&lt;li&gt;Set &lt;strong&gt;extension_dir&lt;/strong&gt; to point to the location where the PHP extensions are located. Typically, for PHP 5.2.X the value would be set as &lt;strong&gt;extension_dir = &amp;quot;./ext&amp;quot;&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;Enable the required PHP extension by un-commenting the corresponding lines, for example: &lt;br /&gt;&lt;br /&gt;extension=php_mssql.dll&lt;br /&gt;extension=php_mysql.dll&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Open a command prompt, and run the following command to verify that PHP installed successfully:&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;samp&gt;C:\PHP&amp;gt;php –info&lt;/samp&gt;&lt;/p&gt;&lt;p&gt;If PHP installed correctly and all its dependencies are available on the machine, this command will output the current PHP configuration information.&lt;/p&gt;&lt;h2&gt;Configure IIS 7 to Handle PHP Requests&lt;/h2&gt;&lt;p&gt;For IIS 7 to host PHP applications, you must add a handler mapping that tells IIS to pass all PHP-specific requests to the PHP application framework by using the FastCGI protocol.&lt;/p&gt;&lt;h3&gt;Configure IIS 7 to handle PHP requests by using IIS Manager&lt;/h3&gt;&lt;p&gt;1. Open IIS Manager. At the server level, double-click &lt;strong&gt;Handler Mappings&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=2854&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/ppaydx_handlermappings.jpg&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;2. In the &lt;strong&gt;Actions&lt;/strong&gt; pane, click &lt;strong&gt;Add Module Mapping...&lt;/strong&gt;. In the &lt;strong&gt;Add Module Mapping&lt;/strong&gt; dialog box, specify the configuration settings as follows:&lt;/p&gt;&lt;p&gt;&lt;li&gt;Request path: &lt;strong&gt;*.php&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;Module: &lt;strong&gt;FastCgiModule&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;Executable: &lt;strong&gt;&amp;quot;C:\[Path to your PHP installation]\php-cgi.exe&amp;quot;&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;Name: &lt;strong&gt;PHP via FastCGI&lt;/strong&gt;&lt;p&gt;3. Click &lt;strong&gt;OK&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=2855&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; width=&quot;871&quot; height=&quot;636&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/4wtj7y_addmodulemapping.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;4. In the &lt;strong&gt;Add Module Mapping&lt;/strong&gt; confirmation dialog box that asks if you want to create a FastCGI application for this executable, click &lt;strong&gt;Yes&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=164&quot; target=&quot;_blank&quot;&gt;&lt;img style=&quot;border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; width=&quot;411&quot; height=&quot;163&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/nhicmj_file3.jpg&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;5. Test that the handler mapping works correctly by creating a phpinfo.php file in the C:\inetpub\wwwroot folder that contains the following code:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;&amp;lt;?php phpinfo(); ?&amp;gt;&lt;/samp&gt;&lt;/p&gt;&lt;p&gt;6. Open a browser and navigate to &lt;span class=&quot;hyperlinktext&quot;&gt;http://localhost/phpinfo.php&lt;/span&gt;. If everything was setup correctly, you will see the standard PHP information page.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=1291&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; width=&quot;659&quot; height=&quot;498&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/0c2lz6_phpinfo.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: If you do not see &lt;strong&gt;FastCgiModule&lt;/strong&gt; in the &lt;strong&gt;Modules:&lt;/strong&gt; list, the module is either not registered or not enabled. To check if the FastCGI module is registered, open the IIS configuration file that is located at %windir%\windows\system32\config\applicationHost.config and check that the following line is present in the &amp;lt;globalModules&amp;gt; section:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;&amp;lt;add name=&amp;quot;FastCgiModule&amp;quot; image=&amp;quot;%windir%\System32\inetsrv\iisfcgi.dll&amp;quot; /&amp;gt;&lt;/samp&gt;&lt;/p&gt;&lt;p&gt;In the same file, also check that the FastCGI module is added to the &amp;lt;modules&amp;gt; section:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;&amp;lt;add name=&amp;quot;FastCgiModule&amp;quot; /&amp;gt;&lt;/samp&gt;&lt;/p&gt;&lt;h3&gt;Configure IIS 7 to handle PHP requests by using the command line&lt;/h3&gt;&lt;p&gt;Alternatively, you can complete the steps above by using the command line tool A&lt;strong&gt;ppCmd&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;1. Create the FastCGI application process pool by running the following command:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI /+[fullPath=&apos;c:\{php_folder}\php-cgi.exe&apos;]&lt;/samp&gt;&lt;/p&gt;&lt;p&gt;2. Create the handler mapping by running the following command:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers /+[name=&apos;PHP_via_FastCGI&apos;,path=&apos;*.php&apos;,verb=&apos;*&apos;,modules=&apos;FastCgiModule&apos;,scriptProcessor=&apos;c:\{php_folder}\php-cgi.exe&apos;,resourceType=&apos;Unspecified&apos;]&lt;/samp&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Note: &lt;/strong&gt;If you are using PHP version 4.X, you can use php.exe instead of php-cgi.exe.&lt;/p&gt;&lt;h2&gt;Best Practices for Configuring FastCGI and PHP&lt;/h2&gt;&lt;p&gt;This &lt;a title=&quot;Hosting PHP&quot; href=&quot;http://download.microsoft.com/download/A/2/F/A2F199C0-672E-44E6-BF1D-878E233C3F08/hostingPHPonIIS7.zip&quot; mce_href=&quot;http://download.microsoft.com/download/A/2/F/A2F199C0-672E-44E6-BF1D-878E233C3F08/hostingPHPonIIS7.zip&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;download&lt;/font&gt;&lt;/a&gt; contains a summary presentation on Best Practices for hosting PHP in a shared hosting environment.&lt;/p&gt;&lt;h3&gt;Security Isolation for PHP Web Sites&lt;/h3&gt;&lt;p&gt;The recommendation for isolating PHP Web sites in a shared hosting environment is consistent with all general security isolation recommendations for IIS 7. In particular, it is recommended to:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Use one application pool per Web site&lt;/li&gt;&lt;li&gt;Use a dedicated user account as an identity for the application pool&lt;/li&gt;&lt;li&gt;Configure an anonymous user identity to use the application pool identity&lt;/li&gt;&lt;li&gt;Ensure that FastCGI impersonation is enabled in the php.ini file (fastcgi.impersonate=1)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;For more details about security isolation in a shared hosting environment, see &lt;a title=&quot;Ensure Security Isolation for Web Sites&quot; href=&quot;http://learn.iis.net/page.aspx/764/ensure-security-isolation-for-web-sites/&quot; mce_href=&quot;http://learn.iis.net/page.aspx/764/ensure-security-isolation-for-web-sites/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Ensure Security Isolation for Web Sites&lt;/font&gt;&lt;/a&gt;.&lt;/p&gt;&lt;h3&gt;PHP Process Recycling Behavior&lt;/h3&gt;&lt;p&gt;Ensure that FastCGI always recycles the php-cgi.exe processes before the native PHP recycling kicks in. The FastCGI process recycling behavior is controlled by the configuration property &lt;strong&gt;instanceMaxRequests&lt;/strong&gt;. This property specifies how many requests the FastCGI process will process before recycling. PHP also has a similar process recycling functionality that is controlled by the environment variable &lt;strong&gt;PHP_FCGI_MAX_REQUESTS&lt;/strong&gt;. By setting &lt;strong&gt;instanceMaxRequests&lt;/strong&gt; to be less than or equal to &lt;strong&gt;PHP_FCGI_MAX_REQUESTS&lt;/strong&gt;, you can ensure that the native PHP process recycling logic will never kick in.&lt;/p&gt;&lt;p&gt;The FastCGI settings can be configured either by using IIS Manager or by using the command line tool A&lt;strong&gt;ppCmd&lt;/strong&gt;.&lt;/p&gt;&lt;h4&gt;Configure FastCGI recycling settings by using IIS Manager&lt;/h4&gt;&lt;p&gt;1. Ensure that the &lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#Install_Administration_Pack_for_IIS_7.0&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Administration Pack for IIS 7&lt;/font&gt;&lt;/a&gt; is installed on your server. Open IIS Manager. On the server level, double-click &lt;strong&gt;FastCGI Settings&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=2856&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; width=&quot;881&quot; height=&quot;480&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/p8e52o_fastcgi.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;2. Select the FastCGI application that you want to configure. In the &lt;strong&gt;Actions&lt;/strong&gt; pane, click &lt;strong&gt;Edit...&lt;/strong&gt;.&lt;/p&gt;&lt;p mce_keep=&quot;true&quot;&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=2857&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; width=&quot;714&quot; height=&quot;556&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/14130i_fastcgieditbutton.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;3. In the &lt;strong&gt;Edit FastCGI Application&lt;/strong&gt; dialog box, set the &lt;strong&gt;InstanceMaxRequests&lt;/strong&gt; to &lt;strong&gt;10000&lt;/strong&gt;. Next to the &lt;strong&gt;EnvironmentVariables&lt;/strong&gt; setting, click the Browse (&lt;strong&gt;...&lt;/strong&gt;) button.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=1294&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; width=&quot;440&quot; height=&quot;430&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/xqkox1_FastCGIAppSettings.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;4. In the &lt;strong&gt;EnvironmentVariables Collection Editor&lt;/strong&gt; dialog box, add the &lt;strong&gt;PHP_FCGI_MAX_REQUESTS&lt;/strong&gt; environment variable and set its value to &lt;strong&gt;10000&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://learn.iis.net/file.axd?i=1296&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; width=&quot;499&quot; height=&quot;361&quot; class=&quot;Image&quot; alt=&quot;&quot; src=&quot;http://www.517sou.net/Attach/month_1110/5djyt8_SetEnvVariable.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Note: &lt;/strong&gt;If you do not configure these settings, the following default settings will be used: &lt;strong&gt;instanceMaxRequests&lt;/strong&gt; = 200, &lt;strong&gt;PHP_FCGI_MAX_REQUESTS&lt;/strong&gt; = 500 (on most PHP builds).&lt;/p&gt;&lt;h4&gt;Configure FastCGI recycling settings by using the command line&lt;/h4&gt;&lt;p&gt;Configure the recycling behavior of FastCGI and PHP by using &lt;strong&gt;AppCmd&lt;/strong&gt; by running the following commands:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd set config -section:system.webServer/fastCgi /[fullPath=&apos;c:\{php_folder}\php-cgi.exe&apos;].instanceMaxRequests:10000&lt;br /&gt;&lt;br /&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+&amp;quot;[fullPath=&apos;C:\{php_folder}\php-cgi.exe&apos;].environmentVariables.[name=&apos;PHP_FCGI_MAX_REQUESTS&apos;,value=&apos;10000&apos;]&amp;quot;&lt;/samp&gt;&lt;/p&gt;&lt;h3&gt;PHP Versioning&lt;/h3&gt;&lt;p&gt;Many PHP applications rely on functions or features that are available only in certain versions of PHP. If these types of applications are to be hosted on the same server, different PHP versions must be enabled and running side-by-side. The IIS 7 FastCGI handler fully supports running multiple versions of PHP on the same Web server.&lt;/p&gt;&lt;p&gt;For example, assume that on your Web server you plan to support PHP 4.4.8, PHP 5.2.1, and PHP 5.2.5 non-thread safe. To enable that configuration, you must place corresponding PHP binaries in separate folders on the file system (e.g. C:\php448\, C:\php521\ and C:\php525nts) and then create FastCGI application process pools for each version:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI /+[fullPath=&apos;c:\php448\php.exe&apos;]&lt;br /&gt;&lt;br /&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI /+[fullPath=&apos;c:\php521\php-cgi.exe&apos;]&lt;br /&gt;&lt;br /&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI /+[fullPath=&apos;c:\php525nts\php-cgi.exe&apos;]&lt;/samp&gt;&lt;/p&gt;&lt;p&gt;If you have three Web sites (site1, site2, site3) and each site must use a different PHP version, you can now define handler mappings on each of those sites to reference a corresponding FastCGI application process pool.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Each FastCGI process pool is uniquely identified by a combination of fullPath and arguments properties.&lt;/p&gt;&lt;p&gt;&lt;samp&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd set config site1 –section:system.webServer/handlers /+”..[name=’PHP448_via_FastCGI’,path=’*.php’,verb=’*’,modules=’FastCgiModule’,scriptProcessor=’c:\php448\php.exe’,resourceType=’Either’]&lt;br /&gt;&lt;br /&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd set config site2 –section:system.webServer/handlers /+”..[name=’PHP521_via_FastCGI’,path=’*.php’,verb=’*’,modules=’FastCgiModule’,scriptProcessor=’c:\php521\php-cgi.exe’,resourceType=’Either’]&lt;br /&gt;&lt;br /&gt;C:\&amp;gt;%windir%\system32\inetsrv\appcmd set config site3 –section:system.webServer/handlers /+”..[name=’PHP525nts_via_FastCGI’,path=’*.php’,verb=’*’,modules=’FastCgiModule’,scriptProcessor=’c:\php525nts\php-cgi.exe’,resourceType=’Either’]&lt;br /&gt;&lt;/samp&gt;&lt;/p&gt;&lt;h3&gt;PHP Security Recommendations&lt;/h3&gt;&lt;p&gt;The following settings can be used to tighten the security of a PHP installation. To make the recommended changes, locate and open the php.ini file and edit the configuration settings as described below:&lt;/p&gt;&lt;p&gt;&lt;style type=&quot;text/css&quot;&gt;TH {
	PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #222222; PADDING-LEFT: 4px; PADDING-RIGHT: 4px; COLOR: white; PADDING-TOP: 4px
}
TR {
	BACKGROUND-COLOR: #dddddd
}
TD {
	PADDING-BOTTOM: 4px; PADDING-LEFT: 4px; PADDING-RIGHT: 4px; PADDING-TOP: 4px
}&lt;/style&gt;&lt;table style=&quot;width: 100%&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Setting&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;allow_url_fopen=Off&lt;br /&gt;allow_url_include=Off&lt;/td&gt;&lt;td&gt;Disable remote URLs for file handling functions, which may cause code injection vulnerabilities.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;register_globals=Off&lt;/td&gt;&lt;td&gt;Disable register_globals.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;open_basedir=&amp;quot;c:\inetpub\&amp;quot;&lt;/td&gt;&lt;td&gt;Restrict where PHP processes can read and write on a file system.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;safe_mode=Off&lt;br /&gt;safe_mode_gid=Off&lt;/td&gt;&lt;td&gt;Disable safe mode.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;max_execution_time=30&lt;br /&gt;max_input_time=60&lt;/td&gt;&lt;td&gt;Limit script execution time.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;memory_limit=16M&lt;br /&gt;upload_max_filesize=2M&lt;br /&gt;post_max_size=8M&lt;br /&gt;max_input_nesting_levels=64&lt;/td&gt;&lt;td&gt;Limit memory usage and file sizes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;display_errors=Off&lt;br /&gt;log_errors=On&lt;br /&gt;error_log=&amp;quot;C:\path\of\your\choice&amp;quot;&lt;/td&gt;&lt;td&gt;Configure error messages and logging.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;fastcgi.logging=0&lt;/td&gt;&lt;td&gt;The IIS FastCGI module will fail the request when PHP sends any data on stderr by using the FastCGI protocol. Disable FastCGI logging to prevent PHP from sending error information over stderr and generating 500 response codes for the client.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;expose_php=Off&lt;/td&gt;&lt;td&gt;Hide the presence of PHP.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;h2&gt;Enabling per-site PHP configuration&lt;/h2&gt;&lt;p&gt;This section describes the recommended way of enabling per-site PHP configuration. This recommendation was discovered and validated by Radney Jasmin with hosting provider &lt;a href=&quot;http://www.godaddy.com/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;GoDaddy.com&lt;/font&gt;&lt;/a&gt; who now offers PHP hosting on Windows Server 2008 by using FastCGI.&lt;/p&gt;&lt;h3&gt;Per-site PHP Process Pools&lt;/h3&gt;&lt;p&gt;When each Web site has its own application pool, which is a recommended practice on IIS 7, it is possible to associate a dedicated FastCGI process pool with each Web site. A FastCGI process pool is uniquely identified by the combination of &lt;strong&gt;fullPath&lt;/strong&gt; and &lt;strong&gt;arguments&lt;/strong&gt; attributes. If you need to create several FastCGI process pools for the same process executable, such as php-cgi.exe, you can use the &lt;strong&gt;arguments&lt;/strong&gt; attribute to distinguish the process pool definitions. With php-cgi.exe processes, you can also use the command line switch &amp;quot;-d&amp;quot; to define an INI entry for a PHP process. You can use this switch to set a PHP setting that makes the arguments string unique.&lt;/p&gt;&lt;p&gt;For example, if there are two Web sites &amp;quot;website1&amp;quot; and &amp;quot;website2&amp;quot; that must have their own set of PHP settings, the FastCGI process pools can be defined as follows:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;&amp;lt;fastCgi&amp;gt;&lt;br /&gt;&amp;lt;application fullPath=&amp;quot;&lt;strong&gt;C:\PHP\php-cgi.exe&lt;/strong&gt;&amp;quot; arguments=&amp;quot;&lt;strong&gt;-d open_basedir=C:\Websites\Website1&lt;/strong&gt;&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;application fullPath=&amp;quot;&lt;strong&gt;C:\PHP\php-cgi.exe&lt;/strong&gt;&amp;quot; arguments=&amp;quot;&lt;strong&gt;-d open_basedir=C:\Websites\Website2&lt;/strong&gt;&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/fastCgi&amp;gt;&lt;/samp&gt;&lt;/p&gt;&lt;p&gt;In this example the PHP setting &lt;strong&gt;open_basedir&lt;/strong&gt; is used to distinguish between the process pool definitions. The setting also enforces that the PHP executable for each process pool can perform file operations only within the root folder of the corresponding Web site.&lt;/p&gt;&lt;p&gt;Then website1 can have the PHP handler mapping as follows:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;&amp;lt;system.webServer&amp;gt;&lt;br /&gt;&amp;lt;handlers accessPolicy=&amp;quot;Read, Script&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;add name=&amp;quot;PHP via FastCGI&amp;quot; path=&amp;quot;*.php&amp;quot; verb=&amp;quot;*&amp;quot; modules=&amp;quot;FastCgiModule&amp;quot; scriptProcessor=&amp;quot;&lt;strong&gt;C:\PHP\php-cgi.exe|-d &lt;/strong&gt;&lt;strong&gt;open_basedir=C:\Websites\Website1&lt;/strong&gt;&amp;quot; resourceType=&amp;quot;Unspecified&amp;quot; requireAccess=&amp;quot;Script&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/handlers&amp;gt;&lt;br /&gt;&amp;lt;/system.webServer&amp;gt;&lt;/samp&gt;&lt;/p&gt;&lt;p&gt;and website2 can have the PHP handler mapping as follows:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;&amp;lt;system.webServer&amp;gt;&lt;br /&gt;&amp;lt;handlers accessPolicy=&amp;quot;Read, Script&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;add name=&amp;quot;PHP via FastCGI&amp;quot; path=&amp;quot;*.php&amp;quot; verb=&amp;quot;*&amp;quot; modules=&amp;quot;FastCgiModule&amp;quot; scriptProcessor=&amp;quot;&lt;strong&gt;C:\PHP\php-cgi.exe|-d &lt;/strong&gt;&lt;strong&gt;open_basedir=C:\Websites\Website2&lt;/strong&gt;&amp;quot; resourceType=&amp;quot;Unspecified&amp;quot; requireAccess=&amp;quot;Script&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/handlers&amp;gt;&lt;br /&gt;&amp;lt;/system.webServer&amp;gt;&lt;/samp&gt;&lt;/p&gt;&lt;h3&gt;Specifying php.ini location&lt;/h3&gt;&lt;p&gt;When the PHP process starts, it determines the location of the configuration php.ini file by using various settings. &lt;a href=&quot;http://www.php.net/manual/en/configuration.php&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;The PHP documentation&lt;/font&gt;&lt;/a&gt; provides a detailed description of the PHP startup process. One of the places where the PHP process searches for the php.ini location is the PHPRC environment variable. If the PHP process finds a php.ini file in the path that is specified in this environment variable, it will use it; otherwise, the PHP process will revert to using the default location of the php.ini file. This environment variable can be used to allow hosting customers to use their own versions of php.ini files.&lt;/p&gt;&lt;p&gt;For example if there are two Web sites &amp;quot;website1&amp;quot; and &amp;quot;website2&amp;quot; that are located at the following file paths: C:\WebSites\website1 and C:\WebSites\website2, you can configure the php-cgi.exe process pools in the &amp;lt;fastCgi&amp;gt; section of the applicationHost.config file as follows:&lt;/p&gt;&lt;p&gt;&lt;samp&gt;&amp;lt;fastCgi&amp;gt;&lt;br /&gt;&amp;lt;application fullPath=&amp;quot;C:\PHP\php-cgi.exe&amp;quot; arguments=&amp;quot;-d open_basedir=C:\Websites\Website1&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;environmentVariables&amp;gt;&lt;br /&gt;&amp;lt;environmentVariable name=&amp;quot;PHPRC&amp;quot; value=&amp;quot;C:\WebSites\website1&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/environmentVariables&amp;gt;&lt;br /&gt;&amp;lt;/application&amp;gt;&lt;br /&gt;&amp;lt;application fullPath=&amp;quot;C:\PHP\php-cgi.exe&amp;quot; arguments=&amp;quot;-d open_basedir=C:\WebSites\Website2&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;environmentVariables&amp;gt;&lt;br /&gt;&amp;lt;environmentVariable name=&amp;quot;PHPRC&amp;quot; value=&amp;quot;C:\WebSites\website2&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/environmentVariables&amp;gt;&lt;br /&gt;&amp;lt;/application&amp;gt;&lt;br /&gt;&amp;lt;/fastCgi&amp;gt;&lt;/samp&gt;&lt;/p&gt;&lt;p&gt;This way website1 can have its own version of the php.ini file that is located in the C:\WebSites\website1, while website2 can have its own version of the php.ini file that is located in C:\WebSites\website2. This configuration also ensures that if a php.ini file cannot be found in the location that is specified by the PHPRC environment variable, then PHP will use the default php.ini file that is located in the same folder where the php-cgi.exe is located.&lt;/p&gt;&lt;h2&gt;Provide URL Rewriting Functionality for PHP Applications&lt;/h2&gt;&lt;p&gt;The majority of popular PHP applications rely on the URL rewriting functionality in Web servers to enable user-friendly and search engine-friendly URLs. IIS 7 provides URL rewriting capabilities by using the &lt;a title=&quot;URL rewrite module&quot; href=&quot;http://learn.iis.net/page.aspx/460/using-url-rewrite-module/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;URL rewrite module&lt;/font&gt;&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;For more information about how to use the URL Rewrite module, see the following articles:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://go.microsoft.com/fwlink/?linkid=120200&amp;amp;clcid=0x409&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Microsoft URL Rewrite Module Walkthroughs.&lt;/font&gt;&lt;/a&gt; Describes how to use the URL Rewrite module.&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://go.microsoft.com/fwlink/?linkid=120201&amp;amp;clcid=0x409&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Microsoft URL Rewrite Module configuration reference.&lt;/font&gt;&lt;/a&gt; Explains the functionality of the module and provides descriptions of all the configuration options.&lt;/li&gt;&lt;li&gt;Configuring popular PHP applications to work with the URL Rewrite module: &lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/280/wordpress-on-iis/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;WordPress&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/299/mediawiki-on-iis/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;MediaWiki&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/274/b2evolution-on-iis/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;b2Evolution&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/279/mambo-on-iis/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Mambo&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/505/drupal-on-iis/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Drupal&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Related resources&lt;/h2&gt;&lt;p&gt;For more information regarding hosting PHP applications on IIS refer to the following resources:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/271/php-applications-on-iis/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Popular PHP applications on IIS&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/248/configuring-fastcgi-extension-for-iis60/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Configuring FastCGI extension for IIS 6.0&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/247/using-fastcgi-to-host-php-applications-on-iis-60/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Using FastCGI extension to host PHP on IIS 6.0&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/335/installing-fast-cgi-support-on-server-core/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Installing FastCGI support on Windows Server 2008 Core&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Related Content&lt;/h2&gt;&lt;ul class=&quot;related-list&quot;&gt;&lt;li class=&quot;bold first&quot;&gt;Articles&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/375/set-up-fastcgi-for-php/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Set Up FastCGI for PHP&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/272/installing-php-on-windows-vista-with-fastcgi/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Installing PHP on Windows Vista with FastCGI&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/697/configure-php-process-recycling-behavior/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Configure PHP Process Recycling Behavior&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://learn.iis.net/page.aspx/247/using-fastcgi-to-host-php-applications-on-iis-60/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#118bd8&quot;&gt;Using FastCGI to Host PHP Applications on IIS 6.0&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;出处：&lt;a href=&quot;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-7-and-above/&quot; target=&quot;_blank&quot;&gt;http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-7-and-above/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;&lt;/p&gt;</description>
		<guid>http://www.517sou.net/Article/using-fastcgi-to-host-php-applications-on-iis-7-and-above.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/702/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/using-fastcgi-to-host-php-applications-on-iis-7-and-above.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/702/Feeds.ashx</wfw:commentRss>
	</item>
	<item>
		<link>http://www.517sou.net/Article/how-to-check-if-your-linux-webserver-is-under-a-dos-attack.aspx</link>
		<title>How to check if your Linux WebServer is under a DoS attack</title>
		<author>shanyiwan@live.com()</author>
		<category>WEB服务器</category>
		<pubDate>Tue, 13 Sep 2011 02:49:46 GMT</pubDate>
		<description>&lt;p&gt;There are few commands I usually use to track if my server is possibly under &lt;b&gt;a Denial of Service attack &lt;/b&gt;or under &lt;b&gt;Distributed Denial of Service &lt;/b&gt;&lt;/p&gt;&lt;p&gt;Sys Admins who still have not experienced the terrible times of being under a DoS attack are happy people for sure …&lt;/p&gt;&lt;p&gt;&lt;b&gt;1. How to Detect a TCP/IP Denial of Service Attack &lt;/b&gt;&lt;/p&gt;&lt;p&gt;This are the commands I use to find out if a loaded Linux server is under a heavy DoS attack, one of the most essential one is of course &lt;b&gt;netstat&lt;/b&gt;.&lt;br /&gt;To check if a server is under a &lt;b&gt;DoS&lt;/b&gt; attack with netstat, it’s common to use:&lt;/p&gt;&lt;p&gt;&lt;code&gt;linux:~# netstat -ntu | awk &apos;{print $5}&apos; | cut -d: -f1 | sort | uniq -c | sort -n|wc -l&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;If the output of below command returns a result like &lt;b&gt;2000 or 3000 connections!&lt;/b&gt;, then obviously it’s very likely the server is under a DoS attack.&lt;/p&gt;&lt;p&gt;To check all the IPS currently connected to the Apache Webserver and get a very brief statistics on the number of times each of the IPs connected to my server, I use the cmd:&lt;/p&gt;&lt;p&gt;&lt;code&gt;linux:~# netstat -ntu | awk &apos;{print $5}&apos; | cut -d: -f1 | sort | uniq -c | sort -n&lt;br /&gt;221 80.143.207.107 233 145.53.103.70 540 82.176.164.36&lt;/code&gt;&lt;/p&gt;&lt;p&gt;As you could see from the above command output the IP &lt;i&gt;80.143.207.107 &lt;/i&gt;is either connected 221 times to the server or is in state of connecting or disconnecting to the node.&lt;/p&gt;&lt;p&gt;Another possible way to check, if a Linux or BSD server is under a Distributed DoS is with the list open files command &lt;b&gt;lsof &lt;/b&gt;&lt;br /&gt;Here is how &lt;i&gt;lsof&lt;/i&gt; can be used to list the approximate number of &lt;b&gt;ESTABLISHED connections &lt;/b&gt;to port 80.&lt;/p&gt;&lt;p&gt;&lt;code&gt;linux:~# lsof -i TCP:80&lt;br /&gt;litespeed 241931 nobody 17u IPv4 18372655 TCP server.&lt;a title=&quot;pc-freak.net&quot; href=&quot;http://www.pc-freak.net/blog/pc-freak.net/&quot; target=&quot;_blank&quot;&gt;&lt;font color=&quot;#0066cc&quot;&gt;pc-freak.net&lt;/font&gt;&lt;/a&gt;:http (LISTEN)&lt;br /&gt;litespeed 241931 nobody 25u IPv4 18372659 TCP 85.17.159.89:http (LISTEN)&lt;br /&gt;litespeed 241931 nobody 30u IPv4 29149647 TCP server.pc-freak.net:http-&amp;gt;83.101.6.41:54565 (ESTABLISHED)&lt;br /&gt;litespeed 241931 nobody 33u IPv4 18372647 TCP 85.17.159.93:http (LISTEN)&lt;br /&gt;litespeed 241931 nobody 34u IPv4 29137514 TCP server.pc-freak.net:http-&amp;gt;83.101.6.41:50885 (ESTABLISHED)&lt;br /&gt;litespeed 241931 nobody 35u IPv4 29137831 TCP server.pc-freak.net:http-&amp;gt;83.101.6.41:52312 (ESTABLISHED)&lt;br /&gt;litespeed 241931 nobody 37w IPv4 29132085 TCP server.pc-freak.net:http-&amp;gt;83.101.6.41:50000 (ESTABLISHED)&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Another way to get an approximate number of established connections to let’s say Apache or LiteSpeed webserver with lsof can be achieved like so:&lt;/p&gt;&lt;p&gt;&lt;code&gt;linux:~# lsof -i TCP:80 |wc -l&lt;br /&gt;2100&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;I find it handy to keep track of above &lt;b&gt;lsof&lt;/b&gt; command output every few secs with &lt;i&gt;gnu watch &lt;/i&gt;, like so:&lt;/p&gt;&lt;p&gt;&lt;code&gt;linux:~# watch &amp;quot;lsof -i TCP:80&amp;quot;&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;2. How to Detect if a Linux server is under an ICMP SMURF attack&lt;/b&gt;&lt;/p&gt;&lt;p&gt;ICMP attack is still heavily used, even though it’s already old fashioned and there are plenty of other Denial of Service attack types, one of the quickest way to find out if a server is under an ICMP attack is through the command:&lt;/p&gt;&lt;p&gt;&lt;code&gt;server:~# while :; do netstat -s| grep -i icmp | egrep &apos;received|sent&apos; ; sleep 1; done&lt;br /&gt;120026 ICMP messages received&lt;br /&gt;1769507 ICMP messages sent&lt;br /&gt;120026 ICMP messages received&lt;br /&gt;1769507 ICMP messages sent&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;As you can see the above one liner in a loop would check for sent and recieved ICMP packets every few seconds, if there are big difference between in the output returned every few secs by above command, then obviously the server is under an ICMP attack and needs to hardened.&lt;/p&gt;&lt;p&gt;&lt;b&gt;3. How to detect a SYN flood with netstat &lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;linux:~# netstat -nap | grep SYN | wc -l&lt;br /&gt;1032&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;1032 SYNs per second is quite a high number and except if the server is not serving let’s say 5000 user requests per second, therefore as the above output reveals it’s very likely the server is under attack, if however I get results like 100/200 SYNs, then obviously there is no SYN flood targetting the machine &lt;img class=&quot;wp-smiley&quot; alt=&quot;;)&quot; src=&quot;http://www.pc-freak.net/blog/wp-includes/images/smilies/icon_wink.gif&quot; /&gt;&lt;/p&gt;&lt;p&gt;Another two netstat command application, which helps determining if a server is under a Denial of Service attacks are:&lt;/p&gt;&lt;p&gt;&lt;code&gt;server:~# netstat -tuna |wc -l&lt;br /&gt;10012&lt;/code&gt;&lt;/p&gt;&lt;p&gt;and&lt;/p&gt;&lt;p&gt;&lt;code&gt;server:~# netstat -tun |wc -l&lt;br /&gt;9606&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Of course there also some other ways to check the count the IPs who sent SYN to the webserver, for example:&lt;/p&gt;&lt;p&gt;&lt;code&gt;server:~# netstat -n | grep :80 | grep SYN |wc -l&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;In many cases of course the &lt;b&gt;top &lt;/b&gt;or &lt;b&gt;htop &lt;/b&gt;can be useful to find, if many processes of a certain type are hanging around.&lt;/p&gt;&lt;p&gt;&lt;b&gt;4. Checking if UDP Denial of Service is targetting the server&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;server:~# netstat -nap | grep &apos;udp&apos; | awk &apos;{print $5}&apos; | cut -d: -f1 | sort |uniq -c |sort -n&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;The above command will list information concerning possible UDP DoS.&lt;/p&gt;&lt;p&gt;The command can easily be accustomed also to check for both possible TCP and UDP denial of service, like so:&lt;/p&gt;&lt;p&gt;&lt;code&gt;server:~# netstat -nap | grep &apos;udp|udp&apos; | awk &apos;{print $5}&apos; | cut -d: -f1 | sort |uniq -c |sort -n&lt;br /&gt;104 109.161.198.86&lt;br /&gt;115 112.197.147.216&lt;br /&gt;129 212.10.160.148&lt;br /&gt;227 201.13.27.137&lt;br /&gt;3148 91.121.85.220&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;If after getting an IP that has too many connections to the server and is almost certainly a DoS host you would like to filter this IP.&lt;/p&gt;&lt;p&gt;You can use the &lt;b&gt;/sbin/route &lt;/b&gt;command to filter it out, using route will probably be a better choice instead of iptables, as iptables would load up the CPU more than simply cutting the route to the server.&lt;/p&gt;&lt;p&gt;Here is how I remove hosts to not be able to route packets to my server:&lt;/p&gt;&lt;p&gt;&lt;code&gt;route add 110.92.0.55 reject&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;The above command would null route the access of IP 110.92.0.55 to my server.&lt;/p&gt;&lt;p&gt;Later on to look up for a null routed IP to my host, I use:&lt;/p&gt;&lt;p&gt;&lt;code&gt;route -n |grep -i 110.92.0.55&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Well hopefully this should be enough to give a brief overview on how, one can dig in his server and find if he is under a Distributed Denial of Service, hope it’s helpful to somebody out there.&lt;/p&gt;</description>
		<guid>http://www.517sou.net/Article/how-to-check-if-your-linux-webserver-is-under-a-dos-attack.aspx</guid>
		<trackback:ping>http://www.517sou.net/Article/677/Trackback.ashx</trackback:ping>
		<comments>http://www.517sou.net/Article/how-to-check-if-your-linux-webserver-is-under-a-dos-attack.aspx#CommentPostAnchor</comments>
		<wfw:commentRss>http://www.517sou.net/Article/677/Feeds.ashx</wfw:commentRss>
	</item>
</channel>
</rss>
