varnish+nginx+php (FastCGI) +mysql5+mencache+mencachedb

Source: Internet
Author: User
Tags openssl nginx server varnish

Schema varnish+nginx+php (FastCGI) +mysql5+mencache+mencachedb

Description
When I was designing the system architecture, I made a bold attempt to use only 6 Web servers to withstand 40 million PV (page traffic) performance:

Abandoned Apache because it can withstand relatively low concurrent connections;
Abandon Squid, because it in memory utilization, access speed, concurrent connection, clear cache and so on aspect inferior Varnish;
Abandoned the PHP4, because PHP5 processing object-oriented code faster than PHP4, in addition, PHP4 has no longer continue to develop;
Abandoned the F5 big-IP load balancer switch, F5 Although is a good thing, but because of expensive, multiple departments of multiple products are running on top of it, traffic, high load, resulting in a large performance discount;

By using Varnish cache, the database query was reduced by 90%, and the MySQL database bottleneck was solved.
The memory cache hits of Varnish cache speed up the access speed of the webpage;
Using Nginx + PHP5 (FastCGI) over Apache 10 times times the high concurrency performance, with the fewest number of servers to solve the PHP dynamic program Access problems;
Using Memcached to process real-time data reading and writing;
Use HAProxy to do interface server health check;

After the stress test, each Web server can handle 30,000 concurrent connections, withstanding 40 million PV completely no problem.

Guaranteed 40 million PV concurrent connections: (40000000pv/86400 sec * 10 Derived connections * 5 seconds in response * 5 times times Peak)/6 Web server = 19290 connections

Experimental results show that:

For a simple example, run apache+php on the server 192.168.0.2 running nginx+php,192.168.0.3, You install the Stress test tool on the 192.168.0.4 webbench, request a PHP file on the Nginx and Apache servers for 60 seconds with 300,000 concurrent connections. During this time, you use your browser to access the Apache server PHP files, you will find either "the page can not be displayed", or wait a few seconds to open, and Nginx server php files, still do not affect the slightest, the access speed is still fast.

Webbench-c 300000-t http://192.168.0.2/index.php

Webbench-c 300000-t http://192.168.0.3/index.php

The following is the system load of Nginx 0.5.33 + PHP 5.2.5 (FastCGI) server with 10 Nginx processes and 250 php-cgi processes open under 30,000 concurrent connections:

-------------------------------------------------------------------------

Installation steps:
(System requirements: Linux 2.6+ kernel, Linux operating system in this article is AS4.3)

First, access to relevant open source programs:
1, download the program source code package to the current directory:
All open source software mentioned in this article is the latest stable version until September 21, 2007. I've punched them in two compressed packs.

First compressed package: Nginx_php_mysql_1.0_1of2.zip:
: http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2289607

Second compressed package: Nginx_php_mysql_1.0_2of2.zip:
: http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2289595

2. Decompression:

Unzip Nginx_php_mysql_1.0_1of2.zip
Unzip Nginx_php_mysql_1.0_2of2.zip


-------------------------------------------------------------------------
I.) Installation of Nginx
1.) Installation
Nginx ("Engine X") is a high-performance HTTP and reverse proxy server, also a IMAP/POP3/SMTP proxy server. Nginx was developed by Igor Sysoev, the second-most visited rambler.ru site in Russia, which has already run more than 2.5 of the site. Igor release the source code in the form of a BSD-like license. Although still beta, Nginx has been known for its stability, rich feature set, sample configuration files, and low system resource consumption. Welcome to the Nginx Wiki, http://wiki.codemongers.com/NginxChs.

2.) Install the Pcre library required for Nginx:

[[email protected]] #tar ZXVF pcre-7.2.tar.gz
[[email protected]] #cd pcre-7.2/
[[Email Protected]]#./configure
[[email protected]] #make && make install
[[email protected]] #cd. /


3.) The compiler parameters of Nginx are as follows:
[Email protected]]#./configure--user=www--group=www--prefix=/usr/local/nginx--with-openssl=/usr/include/ OpenSSL--with-pcre=/usr/local/lib--with-http_stub_status_module--without-http_memcached_module--without-http_ Fastcgi_module--without-http_rewrite_module--without-http_map_module--without-http_geo_module--without-http_ Autoindex_module

Here, need to explain, because Nginx configuration file I want to use the regular, so need Pcre module support. I have installed pcre and pcre-devel rpm packages in the above installation steps, but Ngxin does not correctly find the. h/.so/.a/.la file, so I am slightly flexible:

[[email protected]] #mkdir/usr/include/pcre/.libs/
[[email protected]] #cp/usr/local/lib/libpcre.a/usr/include/pcre/.libs/libpcre.a
[[email protected]] #cp/usr/local/lib/libpcre.a/usr/include/pcre/.libs/libpcre.la
[[email protected]] #cp/usr/local/lib/libpcre.a/usr/include/pcre/libpcre.a
[[email protected]] #cp/usr/local/lib/libpcre.a/usr/include/pcre/libpcre.la
Then, modify the Objs/makefile about 908 lines, commenting out the following:

./configure--disable-shared

Next, you will be able to perform the make and makes install normally.

4.) Modify the configuration file/usr/local/server/nginx/conf/nginx.conf
The following is my nginx.conf content, for reference only:

#运行用户
User nobody nobody;

#启动进程
Worker_processes 2;

#全局错误日志及PID文件
Error_log Logs/error.log Notice;
PID Logs/nginx.pid;

#工作模式及连接数上限
Events {
Use Epoll;
Worker_connections 1024;
}

#设定http服务器, using its reverse proxy function to provide load balancing support
HTTP {
#设定mime类型
Include Conf/mime.types;
Default_type Application/octet-stream;

#设定日志格式
Log_format Main ' $remote _addr-$remote _user [$time _local] '
"$request" $status $bytes _sent '
' "$http _referer" "$http _user_agent" '
' $gzip _ratio ';

Log_format Download ' $remote _addr-$remote _user [$time _local] '
"$request" $status $bytes _sent '
' "$http _referer" "$http _user_agent" '
' "$http _range" "$sent _http_content_range";

#设定请求缓冲
Client_header_buffer_size 1k;
Large_client_header_buffers 4 4k;

#开启gzip模块
gzip on;
Gzip_min_length 1100;
Gzip_buffers 4 8k;
Gzip_types Text/plain;

Output_buffers 1 32k;
Postpone_output 1460;

#设定access Log
Access_log Logs/access.log Main;

Client_header_timeout 3m;
Client_body_timeout 3m;
Send_timeout 3m;

Sendfile on;
Tcp_nopush on;
Tcp_nodelay on;

Keepalive_timeout 65;

         #设定负载均衡的服务器列表
         upstream Mysvr {
                  #weigth参数表示权值, the higher the weight, the greater the chance of being allocated
                  #本机上的Squid开启3128端口
                 server 192.168.8.1:3128 weight=5;
                server 192.168.8.2:80   weight=1;
                server 192.168.8.3:80   weight=6;
        }

#设定虚拟主机
server {
Listen 80;
server_name 192.168.0.1 www.test.com;

CharSet gb2312;

#设定本虚拟主机的访问日志
#access_log Logs/access.log Main;

                 #如果访问/img/* ,/js/*,/css/* resources, then directly fetch local files, not through squid
                  #如果这些文件较多, this is not recommended because the cache effect through squid is better
                 location ~ ^/(img|js|css)/  {
                          root    /home/web;
                         expires 24h;
                }

#对 "/" Enable load balancing
Location/{
Proxy_pass Http://mysvr;

Proxy_redirect off;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Client_max_body_size 10m;
Client_body_buffer_size 128k;
Proxy_connect_timeout 90;
Proxy_send_timeout 90;
Proxy_read_timeout 90;
Proxy_buffer_size 4k;
Proxy_buffers 4 32k;
Proxy_busy_buffers_size 64k;
Proxy_temp_file_write_size 64k;
}

#设定查看Nginx状态的地址
Location/nginxstatus {
Stub_status on;
Access_log on;
Auth_basic "Nginxstatus";
Auth_basic_user_file conf/htpasswd;
}
}
}

Run the following command to detect if the configuration file is correct:

If there is no error, then you can start to run Nginx, execute the following command:
Note: The contents of the conf/htpasswd file can be generated using the HTPASSWD tools provided by Apache, as follows:

5.) View Nginx running status

Enter the address http://192.168.0.1/NginxStatus/, enter the verification account password, you can see similar to the following:

Active connections:328
Server accepts handled requests
9309 8982 28890
Reading:1 Writing:3 waiting:324

Here I am using a virtual machine to do the test, so the number of links is less.
The first line indicates the number of connections currently active
The third number in the third row indicates the total number of requests that Nginx has received at the current time, and if the upper limit is reached, the upper limit will need to be increased.
Top-b-NL
To view Nginx process number:
Netstat-n | awk '/^tcp/{++s[$NF]} END {for (a in S) print A, s[a]} '

6.) Configure Support fcgi files
Vi/usr/local/webserver/nginx/conf/fcgi.conf

Enter the following:


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 is built with--enable-force-cgi-redirect
#fastcgi_param Redirect_status 200;

7.) Start Nginx

Ulimit-shn 51200
/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf

-------------------------------------------------------------------------

8.) Configure boot automatically start Nginx + PHP

Vi/etc/rc.local

Add the following at the end:

/usr/local/php/bin/spawn-fcgi-a 127.0.0.1-p 10080-c 250-u www-f/usr/local/php/bin/php-cgi
Ulimit-shn 51200
/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf

-------------------------------------------------------------------------

varnish+nginx+php (FastCGI) +mysql5+mencache+mencachedb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.