本文已有最新版本:

  请点击Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)




  [文章作者:张宴 本文版本:v1.3 最后修改:2007.11.06 转载请注明出处:http://blog.zyan.cc]

  Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。

  Nginx 的中文维基:http://wiki.codemongers.com/NginxChs

  在高并发连接的情况下,Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。根据我的测试结果,Nginx 0.5.31 + PHP 5.2.4 (FastCGI) 可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍

  以下是 Nginx 0.5.31 + PHP 5.2.4 (FastCGI) 服务器在3万并发连接下的TCP状况:
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

  各种状态TCP连接数如下(各项值所代表的含义见我的另一篇文章《查看Apache并发请求数及其TCP连接状态》):
引用
LAST_ACK 1
SYN_RECV 991
CLOSE_WAIT 1
ESTABLISHED 18789
FIN_WAIT1 1478
FIN_WAIT2 181
TIME_WAIT 506

  根据我的经验,4GB内存的服务器+Apache(prefork模式)一般只能处理3000个并发连接,因为它们将占用3GB以上的内存,还得为系统预留1GB的内存。我曾经就有两台Apache服务器,因为在配置文件中设置的MaxClients为4000,当Apache并发连接数达到3800时,导致服务器内存和Swap空间用满而崩溃。

  而这台 Nginx 0.5.31 + PHP 5.2.4 (FastCGI) 服务器在3万并发连接下,开启的10个Nginx进程消耗100M内存(20MB*10=100M),开启的250个php-cgi进程消耗1G内存(4MB*250≈1GB),加上系统自身消耗的内存,总共才消耗2GB内存。如果服务器内存较小,完全可以只开启25个php-cgi进程,这样php-cgi消耗的总内存数才100M。在开启25个php-cgi进程的情况下,每分钟的处理能力只比开启250个php-cgi进程时低了不到一半。

  以下为 Nginx 0.5.31 + PHP 5.2.4 (FastCGI) 服务器在3万并发连接下,开启的10个Nginx进程和250个php-cgi进程时的系统负载情况:
  点击在新窗口中浏览此图片


  安装步骤:
  (系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为CentOS 4.4)

  一、获取相关开源程序:
  1、下载程序源码包到当前目录:
  本文中提到的所有开源软件为截止到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

  2、解压缩:
unzip nginx_php_mysql_1.0_1of2.zip
unzip nginx_php_mysql_1.0_2of2.zip



  二、安装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 ../

tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure --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数据库,则执行以下两步。如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下两步无需执行。
  ①、以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模式)
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 ../



  附:编译PHP之后,为PHP添加扩展的方法。(本步骤可选)
cd php-5.2.4/pcntl
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install
cd ../../../


vi /usr/local/webserver/php/etc/php.ini

修改extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"


  4、创建www用户和组,以及其使用的目录:
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /data0/vshare/htdocs
chmod +w /data0/vshare/htdocs
chown -R www:www /data0/vshare/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 250 -u www -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
make && make install
cd ../


  2、安装Nginx
tar zxvf nginx-0.5.31.tar.gz
cd nginx-0.5.31/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx
make && make install
cd ../


  3、创建Nginx日志目录
mkdir -p /data1/logs
chmod +w /data1/logs
chown -R www:www /data1/logs


  4、创建Nginx配置文件
  ①、在/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  www www;

worker_processes 10;

#error_log  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  /data1/logs/access.log  main;

        #sendfile on;
        #tcp_nopush     on;

        keepalive_timeout 60;

        #gzip  on;

        server
        {
                listen       80;
                server_name  localhost;
                index index.html index.htm index.php;
                root  /data0/vshare/htdocs;

                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 ~ .*\.php?$
                {
                        include conf/fcgi.conf;      
                        fastcgi_pass  127.0.0.1:10080;
                        fastcgi_index index.php;
                }
        }
}


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

  输入以下内容:
引用
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param  REDIRECT_STATUS    200;


  5、启动Nginx
ulimit -SHn 51200
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf



  四、配置开机自动启动Nginx + PHP
vi /etc/rc.local

  在末尾增加以下内容:
引用
/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 250 -u www -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


  (全文完)


Tags: , , ,
技术大类 » Web服务器 | 评论(101) | 引用(1) | 阅读(149773)
nginx问题
2011-9-30 10:06
张老师不在吗?cry
cheap coach purse Email Homepage
2011-11-4 15:16
Thanks you very much, we are a cheap designer handbags online store, the on sale at lowest wholesale price, if you want to buy best  and other brand purses or designer sunglasses, jewelry, scarves, belts, we are your best choose, get free shipping on order.
hnnxdjp
2011-11-6 23:16
张大哥:
     你好,ulimit -SHn 51200 ,此设置我有疑问呀,这种设置好像只是针对当前用户有用,用户推出就无效了。
hnnxdjp@163.com Email
2011-11-6 23:19
张大哥:
     你好,ulimit -SHn 51200 ,此设置我有疑问呀,这种设置好像只是针对当前用户有用,用户推出就无效了。求解?
dave Homepage
2011-11-7 02:49
楼上同志阿,想要持久生效,在/etc/security/limits.conf里面改
6923daika Email Homepage
2011-11-7 04:55
图为“高空王子”阿迪力。资料图片

    亚心网讯(特派记者卉子 李瑞芳) “听说你要从厦门‘走’到金门,能成功吗?”8日,阿迪力・吾休尔与闻讯而来的记者相遇在驻地的电梯里。

    “可以呀,你看我多健康!”说话的时候,“高空王子”阿迪力像个大力士似的挥了挥双臂,旁边的代表们都笑了,歹徒劫持储户转走存款 银行称不能冻结疑犯账户,手机定位,

    全国人大代表、“高空王子”阿迪力是一个新闻人物。他的文化公司正在策划又一个惊世之举:将一根钢丝架在厦门和金门之间,他将凭借其达瓦孜绝技两小时走完,以此表达两岸人民一家亲的感情。

    从厦门到金门钢丝长度为5400米,手机厂商青睐Android 4.0:或消除系统分化。从厦门上钢丝绳时他会带上一封信,从金门返回时他也会带回一封信,这信的内容暂时保密。

    记者们对阿迪力此次计划表示了担忧:“这么长的钢丝怎样拉呢?”“用什么办法拉结实,定位器,,拉上后能保证安全吗?”“你有成功的心理准备吗?”

    阿迪力告诉记者们:“我们已多次到厦门和金门之间做海上实地考察,地理位置、地形、风速等都比较适宜达瓦孜表演。现在已有了初步方案。”阿迪力为此已准备了两年。目前这一方案已递交相关部门。

    “达瓦孜”维吾尔语是“钢丝走绳”的意思,在中国已有悠久的历史,阿迪力家族走钢丝已超过400年。此前,阿迪力曾创造过多项吉尼斯之最,其走钢丝最长的的记录是1530米。

    这次来参加全国“两会”,阿迪力还希望有更多的人了解学习和杂技表演艺术,将这个民间文化一直传承发扬下去。    
(编辑:SN021)
387778935 Email Homepage
2011-11-7 12:53
早报讯 2007年4月的下午,陶瓷厂百余工人因肺部有阴影获赔,到世纪联华购物的陆青青(化名),因在地下车库一时无法找到车辆停放的位置,而向保洁员周威问路,却不幸被其杀害,女教师领租房者看房遇害续:凶手落网。周威因此被判死刑并已执行。随后,定位器,,死者家属向世纪联华、周威所在保洁公司、地下车库的所有人上海某房产公司及物业公司索要民事赔偿共计86万余元。

    近日,嘉定法院作出一审判决,超市及保洁公司无需承担责任,房地产开发公司依法赔偿8万元,南京手机监听器,;物业公司赔偿12万余元。李燕 史建颖
dlwp6ao6mh Email Homepage
2011-11-7 15:43
,moncler coats for men express cancel



| Back to logs list
1024739 2006年03月03日14:21 Reading (loading. ..) Comments ( 0 ) Category: sports betting


| Back to logs list
the other:

  
   men moncler shoes
  
   north face jackets for men  Category
  
   Liu Qian 's life is said to be performing only one of magic, too NB - Qzone log
wnhj87892 Email Homepage
2011-11-7 15:47
要想刻碑得给钱,一个字交一块五。 郭娟 制图

    真所谓干哪行吃哪行,沙区松鹤陵园管理办公室原主任谭某利用手中职权,男子向前妻求欢未果对其乱刺致伤,4年时间里收了从事墓碑刻字、封碑、修建墓体的4名个体户回扣166万元。昨日,谭某在沙区法院受审。

    今年45岁的谭某自2001年起担任松鹤陵园管理办公室主任。检察机关指控称,自2004年起,手机监听,,谭某利用手中职权,先后多次收受在陵园做墓碑刻字、封碑、修建墓体的李某、周某、赵某、张某给予的回扣166万余元。利用这些钱,谭某以妻子的名义,在沙区井口投资建渣厂,在山西建采石场,还与别人合伙修建厂房。

    “墓碑刻一个字2.5元,就要给他回扣1.5元。”法庭上,公诉人宣读了李某的证言。李某称,2004年上半年,

    他接替岳父在松鹤陵园做墓碑刻字等工作。当他找到谭某时,谭某暗示他要“懂得起”。在谭某担任主任期间,他总共刻了32万多个字,仅此一笔,就付给了谭某48万多元。

    周某、赵某等人的证词,也证实了谭某收受回扣的情况。

    “如果你是老板,刻一个字只收2.5元,可不可能给别人1.5元回扣?”庭审中,谭某两次向法官强调这一问题。李某向检察机关证实谭某收了他77万元回扣,而谭某认为最多只有30万元。

    “周某修建墓体时钱不够,我拿了20万元给他垫上的。”法庭上,近7旬母亲照顾瘫痪女儿10余年(图),手机监听,,谭某对周某的行贿金额也提出异议。他称,当初因为收不到工程款,周某资金不足,于是他拿钱出来与周某合伙修建墓体。所以,检方指控周某给他的51万余元的回扣,根本就不是行贿款,而是他该得的安装费等。

    “如果是合伙经营,你垫资20万元,那你有收条等凭据吗?”公诉人问谭某。谭某望了公诉人一眼,表示没有任何凭据。

    根据谭某的犯罪事实、认罪态度等情节,公诉人建议法院判处谭某有期徒刑10年到12年。对此,谭某的辩护律师认为,谭某在检察机关只掌握他收受李某回扣的事实时,就主动交代了收受赵某等人回扣的事实,属于自首,而且谭某在事发后积极退赃,因此建议法院从轻或减轻处罚。

    法官没有当庭宣判。

    记者 王明
cl4aoq2sed Email Homepage
2011-11-7 15:49
,moncler designer bags


| Back to logs list


| Back to logs list
1023261 2010年10月03日19:14 Reading (loading. ..) Comments ( 0 ) Category : Personal Diary

  tags : emotional man parents shoes relatives

published cancel
the other:

  
   salvatore ferragamo wedges | Back to logs list
  
   ferragamo walelts | Back to logs list
  
   Cheap and nice phone ^ ^ - Qzone log
vs7lqiethdc Email Homepage
2011-11-7 16:50
Post cancel ,women north face gloves
49826 2008年09月24日08:46 Reading (loading. ..) Comments ( 0 ) Category: Digital




| Back to logs list


| Back to logs list
the other:

  
   The most beautiful sentence -love love - Qzone log
  
   12 words , so you see 12 minutes, like life . - Qzone log
  
   article2212
louis vuitton uk Email Homepage
2011-11-23 08:51
This louis vuitton uk for sale belongs to the sounding just what are termed as Louis Vuitton vintage best sellers, many other products and services for the reason that range appearing companies.You will easily notice the unfold zippers of this coach outlet store online. That is the decoration. There are some inside pockets for you as well. They are easy to match your clothes and to carry.Let us inspire your inner beauty with fine christian louboutin sale. Purse the elegance in bridal wedding. Enjoy the fashion.
liuzhenhui
2011-12-26 12:03
文字[i][/i][/b][u][/u][/url][url=http://][b][/b][b][/b][b]name@domain.com[quote][/quote]shuaismokesleepyangergrin
cheap oakley sunglasses Email Homepage
2012-5-9 15:26
I am very happy to see your article, and here i have good news to share with you, i find a website <a href=http://www.saleoakleycheap.com>cheap oakley sunglasses<a/>, in which have the cheap but high quality oakley sunglasses selling. what's more the shipping is free. so if you are looking for cheap oakley sunglasses, here is the best choice for you
coach factory outlet Email Homepage
2012-5-17 11:08
Today, following half a century, mentor leather-bases coach factory outlet continues to be the delicate craft of leather-based master is accountable for,Would you like to meet more friends, or go with the times? If yes, coach factory online is opening welcome doors to you.in the market you definitely can find various colorways that are designed in as well as the high quality that applied in. For most of you would like to come. So just come to our coach factory outlet online store to choose one.
louis vuitton sale Email Homepage
2012-5-17 11:08
Louis Vuitton belt at louis vuitton sale is one kind of fashion accessory with high cost performance among the Louis Vuitton accessories.Offering quality LV products with favorable prices, louis vuitton outlet store is at your service. Hurry up, or you can not seize the chance.in fact, louis vuitton is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.
coach outlet online Email Homepage
2012-5-17 11:08
I heard of coach outlet online through the advertisement when I was shopping. And now I often brow the webpage and buy Coach bags online.It is a symbol regarding position not to mention nature.Here I would like to launch a excellent bags pertaining to business men.Which may be coach outlet store.coach outlet has always been simple,durable style features to win consumers.The products are more flexible,with easy bleaching,wear characteristics,and simply use a damp cloth.
coach outlet, Email Homepage
2012-5-17 11:08
Remember the coach outlet provide coach bags which won't be deteriorated into its overall styles by any means. It will maintain its looks, colors, and uniqueness for long time.coach outlet store online has been voted by Hour Detroit magazine readers as the Best of Detroit in their 12th annual readers'poll.Lots of women like which usually amount normally include a coach outlet online ,it provides coziness to many girls that don't even think it is a great bushel of great interest directly to them.
分页: 4/6 第一页 上页 1 2 3 4 5 6 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]