ginx + PHP搭建可承受3万以上并发连接数,胜过Apache 10倍的Web服务器

ginx 0.5.31 + PHP 5.2.6(FastCGI)搭建可承受3万以上并发连接数,胜过Apache 10倍的Web服务器

叶军,软件互联平台架构师,研发中心

 

99年开始就在搭服务器,装很多服务,当时望江楼团队有BODY,CARP这样的超级高手在,所以服务搭建的事我只是个学徒。

 

2001年,当这些高手开始离校的时候,WJL团队青黄不接,我被迫操持起这些家务。WJL有十几台服务器,有PC机,有小型机,有联想万全的老服务器,有IBM POWER机型,有一般的DELL服务器,一大堆。这些装FACEFEED.CN的机器也是非常的兴奋。好久没有摸机器了。

这是一台独立主机,运营 www.FACEFEED.cn。
参考张宴的配置经验http://blog.s135.com/read.php/297.htm,我把www.facefeed.cn的服务器重新搭建了。 由于张宴的配置中有几个过程不能正常执行(可能是环境问题,可能是张宴的笔误),所以在我的文章里特别加上了这些内容。
系统环境:
[code]
[root@localhost proc]# more /proc/version
Linux version 2.6.9-42.ELsmp ([url=mailto:[email protected]][email protected][/url]) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-2)) #1 SMP Wed Jul
12 23:32:02 EDT 2006
[root@localhost proc]# more /etc/issue
Red Hat Enterprise Linux AS release 4 (Nahant Update 4)
 [/code]

==============yejun.cn======================安装步骤=============yejun.cn==================
  一、获取相关开源程序:
  1、下载程序源码包到目录:~root/server;
  本文中提到的所有开源软件为截止到2007年9月21日的最新稳定版。张宴将它们打了两个压缩包。

  第一个压缩包:nginx_php_mysql_1.0_1of2.zip:
  下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2289607

  第二个压缩包:nginx_php_mysql_1.0_2of2.zip:
  下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2289595

再下次其它需要的包(jdk6 u10; ant非SOURCE版,SOURCE版装起来多费时; php 5.2.6 2008年8月的最新版):
[code]
  wget [url=http://www.java.net/download/jdk6/6u10/promoted/b28/binaries/jdk-6u10-rc-bin-b28-linux-i586-21_jul_2008-rpm.bin]http://www.java.net/download/jdk6/6u10/promoted/b28/binaries/jdk-6u10-rc-bin-b28-linux-i586-21_jul_2008-rpm.bin[/url]
  wget [url=http://apache.mirror.phpchina.com/ant/binaries/apache-ant-1.7.1-bin.tar.gz]http://apache.mirror.phpchina.com/ant/binaries/apache-ant-1.7.1-bin.tar.gz[/url]
  wget [url=http://cn2.php.net/get/php-5.2.6.tar.gz/from/cn.php.net/mirror]http://cn2.php.net/get/php-5.2.6.tar.gz/from/cn.php.net/mirror[/url]
[/code]

2、解压缩:
[code]

unzip nginx_php_mysql_1.0_1of2.zip
unzip nginx_php_mysql_1.0_2of2.zip

[/code]

二、安装PHP 5.2.4(FastCGI模式)
  1、编译安装PHP 5.2.4所需的支持库:

tar zxvf libiconv-1.11.tar.gz
cd libiconv-1.11/
./configure –prefix=/usr/local/webserver/lib/libiconv
make && make install
cd ../

tar zxvf freetype-2.3.5.tar.gz
cd freetype-2.3.5/
./configure –prefix=/usr/local/webserver/lib/freetype
make && make install
cd ../

tar zxvf libpng-1.2.20.tar.gz
cd libpng-1.2.20/
./configure
make && make install
cd ../

mkdir -pv /usr/local/webserver/lib/jpeg6/{,bin,lib,include,man/{,man1},man1} 
#需要手工建目录
tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b/
vi configure
#把.libtool改成/usr/bin/libtool

./configure –prefix=/usr/local/webserver/lib/jpeg6 –enable-static –enable-shared
make && make install
cd ../

tar zxvf gd-2.0.35.tar.gz
cd gd-2.0.35/
./configure –prefix=/usr/local/webserver/lib/gd –with-freetype=/usr/local/webserver/lib/freetype –with-jpeg –with-png
make
make install
cd ../

tar zxvf libxml2-sources-2.6.30.tar.gz
cd libxml2-2.6.30/
./configure –prefix=/usr/local/webserver/lib/libxml
make && make install
cd ../

 2、编译安装MySQL 5.0.45

/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
tar zxvf mysql-5.0.45.tar.gz
cd mysql-5.0.45
./configure –prefix=/usr/local/webserver/mysql/ –without-debug –with-unix-socket-path=/tmp/mysql.sock –with-client-ldflags=-all-static –with-mysqld-ldflags=-all-static –enable-assembler –with-extra-charsets=gbk,gb2312,utf8 –with-pthread –enable-thread-safe-client
make && make install
chmod +w /usr/local/webserver/mysql
chown -R mysql:mysql /usr/local/webserver/mysql
cp support-files/my-medium.cnf /usr/local/webserver/mysql/my.cnf
 ①、以mysql用户帐号的身份建立数据表:

/usr/local/webserver/mysql/bin/mysql_install_db –defaults-file=/usr/local/webserver/mysql/my.cnf –basedir=/usr/local/webserver/mysql –datadir=/usr/local/webserver/mysql/data –user=mysql –pid-file=/usr/local/webserver/mysql/mysql.pid –skip-locking –port=3306 –socket=/tmp/mysql.sock
 ②、启动MySQL(最后的&表示在后台运行)

/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe –defaults-file=/usr/local/webserver/mysql/my.cnf &
 3、编译安装PHP(FastCGI模式)

在~root下建启动mysql脚本
vi startmysql.sh
配置php安装所需要的一些so目录
vi /etc/ld.so.conf
加入以下两行:
/usr/local/webserver/mysql/lib/
/usr/local/webserver/lib/libiconv/lib/

执行/sbin/ldconfig 重新载入这些配置

cd ..
tar zxvf php-5.2.4.tar.gz
cd php-5.2.4/
./configure –prefix=/usr/local/webserver/php –with-mysql=/usr/local/webserver/mysql –with-config-file-path=/usr/local/webserver/php/etc –with-gd=/usr/local/webserver/lib/gd –enable-gd-native-ttf –enable-gd-jis-conv –with-iconv-dir=/usr/local/webserver/lib/libiconv –with-freetype-dir=/usr/local/webserver/lib/freetype –with-jpeg-dir=/usr/local –with-png-dir=/usr/local –with-zlib –with-libxml-dir=/usr/local/webserver/lib/libxml –enable-xml  –disable-debug –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fastcgi –enable-force-cgi-redirect
make && make install
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
cd ../

4、创建www用户和组,以及其使用的目录:
系统可能已经存在APACHE用户和组
/usr/sbin/groupadd apache -g 48
/usr/sbin/useradd -u 48 -g apache apache
mkdir -p /webroot/htdocs
chmod +w /webroot/htdocs
chown -R apache:apache /webroot/htdocs
5、安装lighttpd中附带的spawn-fcgi,用来启动php-cgi
  注:压缩包中的spawn-fcgi程序为已经编译成二进制的版本。

cp spawn-fcgi /usr/local/webserver/php/bin
chmod +x /usr/local/webserver/php/bin/spawn-fcgi
  6、启动php-cgi进程,监听127.0.0.1的10080端口,进程数为250(如果服务器内存小于3GB,可以只开启25个进程),用户为www:

/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 25 -u apache -f /usr/local/webserver/php/bin/php-cgi

 三、安装Nginx 0.5.31
  1、安装Nginx所需的pcre库:
tar zxvf pcre-7.2.tar.gz
cd pcre-7.2/
./configure –enable-utf8
make && make install
cd ../

2、安装Nginx

tar zxvf nginx-0.5.31.tar.gz
cd nginx-0.5.31/
./configure –user=apache –group=apache –prefix=/usr/local/webserver/nginx
make && make install
cd ../

默认的一些目录和文件属性:
  nginx path prefix: “/usr/local/webserver/nginx”
  nginx binary file: “/usr/local/webserver/nginx/sbin/nginx”
  nginx configuration file: “/usr/local/webserver/nginx/conf/nginx.conf”
  nginx pid file: “/usr/local/webserver/nginx/logs/nginx.pid”
  nginx error log file: “/usr/local/webserver/nginx/logs/error.log”
  nginx http access log file: “/usr/local/webserver/nginx/logs/access.log”
  nginx http client request body temporary files: “/usr/local/webserver/nginx/client_body_temp”
  nginx http proxy temporary files: “/usr/local/webserver/nginx/proxy_temp”
  nginx http fastcgi temporary files: “/usr/local/webserver/nginx/fastcgi_temp”
  3、创建Nginx日志目录

[root@localhost server]# mkdir -p /webroot/logs
[root@localhost server]# chmod +w /webroot/logs
[root@localhost server]# chown apache:apache /webroot/logs
  4、创建Nginx配置文件,需要支持codeigniter (CI)
  ①、在/usr/local/webserver/nginx/conf/目录中创建nginx.conf文件:

rm -f /usr/local/webserver/nginx/conf/nginx.conf
vi /usr/local/webserver/nginx/conf/nginx.conf

输入以下内容:

引用
user  apache apache;

worker_processes 10;

error_log  /webroot/logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
       use epoll;

       #maxclient = worker_processes * worker_connections / cpu_number
       worker_connections 51200;
}

http
{
       include       conf/mime.types;
       default_type  application/octet-stream;

       log_format  main  ‘$remote_addr - $remote_user [$time_local] $request ‘
                         ‘”$status” $body_bytes_sent “$http_referer” ‘
                         ‘”$http_user_agent” “$http_x_forwarded_for”‘;

       access_log  /webroot/logs/access.log  main;

       #sendfile on;
       #tcp_nopush     on;

       keepalive_timeout 60;

       #gzip  on;

       server
       {
               listen       80;
               server_name  www.facefeed.cn new.facefeed.cn localhost *.facefeed.cn;
               index index.html index.htm index.php;
               root  /webroot/htdocs/facefeed;

 

       
               if (-f $request_filename/index.html)
               {
                       rewrite (.*) $1/index.html break;
               }
               if (-f $request_filename/index.htm)
               {
                       rewrite (.*) $1/index.htm break;
               }
               if (-f $request_filename/index.php)
               {
                       rewrite (.*) $1/index.php break;
               }

location /
  {
    index index.php;
    include conf/fcgi.conf;
    root  /webroot/htdocs/facefeed;

    if ($request_filename !~ (js|css|images|robots/.txt|index/.php.*) ) {
      rewrite ^/(.*)$ /index.php/$1 last;
    }
fastcgi_pass  127.0.0.1:10080;
fastcgi_index index.php;
  }

location ~ /index.php/
  {
    include conf/fcgi.conf;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /webroot/htdocs/facefeed/index.php;
    fastcgi_param  REQUEST_URI      $request_uri;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
    fastcgi_pass   127.0.0.1:10080;
  }

  location ~ .*/.php?$
{
  include conf/fcgi.conf;
  fastcgi_pass  127.0.0.1:10080;
  fastcgi_index index.php;
}
        #access_log /webroot/logs/facefeed/access.log combined;            
        #error_log /webroot/logs/facefeed/error.log;

 }
}
 ②、在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件:

vi /usr/local/webserver/nginx/conf/fcgi.conf
四、配置开机自动启动Nginx + PHP

vi /etc/rc.local

  在末尾增加以下内容:

引用
/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 25 -u apache -f /usr/local/webserver/php/bin/php-cgi
ulimit -SHn 51200
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf

 

  五、优化Linux内核参数

vi /etc/sysctl.conf

  在末尾增加以下内容:

引用
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000
  使配置立即生效:

/sbin/sysctl -p

你可能感兴趣的:(apache,mysql,PHP,nginx,include,web服务)