nginx0.5.33+php5.2.5 (FastCGI) builds a Web server that beats Apache10 times _nginx

Source: Internet
Author: User
Tags chmod install php mcrypt mkdir php and zts server memory vshare
Fixed the PHP iconv and the GD library conflict Bug, added PHP mcrypt, memcache extension, modified PHP and Nginx compile parameters, optimized Nginx profile, added some features.

Nginx
("Engine X") is a high-performance HTTP and reverse proxy server, as well as a IMAPPOP3SMTP proxy server. Nginx is the second Ambe by Igo Sysoev for Russian visits. Site developed, it has been running over 2.5 in the site. Igo releases the source code in the form of a BSD-like license.
Nginx's Chinese wiki:
Http:wiki.codemonges.comNginxChs
In the case of high concurrent connections, Nginx is a good substitute for Apache servers. Nginx can also be used as a 7-tier load-balancing server. According to my test results, Nginx 0.5.33 + PHP 5.2.5 (FastCGI) can withstand more than 30,000 concurrent connections, equivalent to Apache 10 times times the same environment.
In my experience, the server +apache (Pefok mode) of 4GB memory generally can only handle 3,000 concurrent connections, because they will consume more than 3GB of memory and have to reserve 1GB of memory for the system. I used to have two Apache servers, because the maxcients set in the configuration file is 4000, and when the number of Apache concurrent connections reaches 3800, the server memory and swap space are full and crash.
And this Nginx 0.5.33 + PHP 5.2.5 (FastCGI) server in the 30,000 concurrent connection, the 10 open Nginx process consumes 150M of memory (15m*10=150m), the open 64 php-cgi process consumes 1280M memory (20m*64 =1280m), combined with the memory consumed by the system itself, consumes less than 2GB of memory. If the server memory is small, you can simply turn on 25 php-cgi processes so that the total amount of memory consumed by php-cgi is 500M.
Under 30,000 concurrent connections, access to Nginx 0.5.33 + PHP 5.2.5 (FastCGI) server's PHP program is still fast. The following image shows the status monitoring page for Nginx, with an active connection number of 28457 (for the Nginx Monitor page configuration, as described in the Nginx configuration file given in the following article):

The following are the system load conditions for the 10 Nginx processes and 250 php-cgi processes that were opened by the Nginx 0.5.33 + PHP 5.2.5 (FastCGI) server under 30,000 concurrent connections:
  

Installation steps:
(System requirements: Linux 2.6+ kernel, the Linux operating system in this article is CentOS 4.4)
First, obtain the relevant open source program:
1, download the source package to the current directory:
All of the open source software mentioned in this article is the latest stable version as of November 25, 2007. I put them in two compression packs.
First Compressed package: Nginx-php-1.zip
Download Address:
http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2746375
Second compression package: Nginx-php-2.zip
Download Address:
http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2746370
Attached: Each open source software separately downloads the address (if downloaded the above two compressed packages, then does not need to download the following software):
  
http://down.s135.com/linux/nginx-php/
(login username, password are blog.s135.com)
2, Decompression:
Mkdir-p/data0/software
Cd/data0/software
Unzip Nginx-php-1.zip
Unzip Nginx-php-2.zip
cd/data0/software/nginx-php
Second, the installation of PHP 5.2.5 (fastcgi mode)
1. Compile and install the required support library for PHP 5.2.5:
Tar zxvf libiconv-1.11.tar.gz
CD libiconv-1.11/
./configure--prefix=/usr/local
Make
Make install
Cd.. /
Tar zxvf freetype-2.3.5.tar.gz
CD freetype-2.3.5/
./configure
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
Make Install-lib
Cd.. /
Tar zxvf libxml2-2.6.30.tar.gz
CD libxml2-2.6.30/
./configure
Make
Install
Cd.. /
Tar zxvf libmcrypt-2.5.8.tar.gz
CD libmcrypt-2.5.8/
./configure
Make
Make install
/sbin/ldconfig
CD libltdl/
./configure--enable-ltdl-install
Make
Make install
Cd.. /.. /
Tar zxvf mhash-0.9.9.tar.gz
CD mhash-0.9.9/
./configure
Make
Make install
Cd.. /
Cp/usr/local/lib/libmcrypt.*/usr/lib
Ln-s/usr/local/lib/libmhash.so.2/usr/lib/libmhash.so.2
Tar zxvf mcrypt-2.6.6.tar.gz
CD mcrypt-2.6.6/
./configure
Make
Make install
Cd.. /
2. Compile and install 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
Cd.. /
Attached: The following are additional steps, if you want to run the MySQL database on this server, perform the following two steps. If you just want PHP to support MySQL extensions and be able to connect to MySQL databases on other servers, there are two steps that you don't need to follow.
①, the MySQL user account to establish the data table:
/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
②, start MySQL (Final & show run in background)
/bin/sh/usr/local/webserver/mysql/bin/mysqld_safe--DEFAULTS-FILE=/USR/LOCAL/WEBSERVER/MYSQL/MY.CNF &
3. Compile and install PHP (fastcgi mode)
Tar zxvf php-5.2.5.tar.gz
CD php-5.2.5/
./configure--prefix=/usr/local/webserver/php--with-config-file-path=/usr/local/webserver/php/etc--with-mysql=/ Usr/local/webserver/mysql--with-iconv-dir=/usr/local--with-freetype-dir--with-jpeg-dir--with-png-dir-- With-zlib--with-libxml-dir--enable-xml--disable-debug--disable-rpath--enable-discard-path-- Enable-bcmath--enable-shmop--enable-sysvsem--enable-inline-optimization--with-curl--with-curlwrappers-- Enable-mbregex--enable-fastcgi--enable-force-cgi-redirect--enable-mbstring--with-mcrypt
Sed-i ' s#-lz-lm-lxml2-lz-lm-lxml2-lz-lm-lcrypt#&-liconv# ' Makefile
Make
Make install
CP Php.ini-dist/usr/local/webserver/php/etc/php.ini
Cd.. /
4, compile and install PHP5 expansion module
Tar zxvf memcache-2.2.1.tgz
CD MEMCACHE-2.2.1/
/usr/local/webserver/php/bin/phpize
./configure--with-php-config=/usr/local/webserver/php/bin/php-config
Make
Make install
Cd.. /
CD php-5.2.5/ext/gd/
/usr/local/webserver/php/bin/phpize
./configure--with-jpeg-dir--with-png-dir--with-zlib-dir--with-ttf--with-freetype-dir--with-php-config=/usr/ Local/webserver/php/bin/php-config
Make
Make install
Cd.. /.. /.. /
5. Modify php.ini File
   Manual modification:Find the Extension_dir = "./" in/usr/local/webserver/php/etc/php.ini
Modified to Extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
and add the following lines after this row, and then save:
Extension = "memcache.so"
Extension = "gd.so"
   Automatic Modification:If you are not able to manually modify the trouble, you can execute the following shell command to automatically complete the modification of the php.ini file:
Sed-i ' S#extension_dir = './' #extension_dir = '/usr/local/webserver/php/lib/php/extensions/ no-debug-non-zts-20060613/"\nextension =" memcache.so "\nextension =" gd.so "\n#"/usr/local/webserver/php/etc/" Ini
6. Create WWW users and groups, and the directories they use:
/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
7, the installation of LIGHTTPD included in the spawn-fcgi, used to start php-cgi
Note: The SPAWN-FCGI program in the compressed package is a binary version that has been compiled.
CP Spawn-fcgi/usr/local/webserver/php/bin
chmod +x/usr/local/webserver/php/bin/spawn-fcgi
8, start the php-cgi process, listening to 127.0.0.1 10080 ports, the number of processes 64 (if the server memory is less than 3GB, you can only open 25 processes), the user is www:
/usr/local/webserver/php/bin/spawn-fcgi-a 127.0.0.1-p 10080-c 64-u www-f/usr/local/webserver/php/bin/php-cgi
Third, the installation Nginx 0.5.33
1, the installation of Nginx required Pcre library:
Tar zxvf pcre-7.2.tar.gz
CD PCRE-7.2/
./configure
Make && make install
Cd.. /
2. Installation Nginx
Tar zxvf nginx-0.5.33.tar.gz
CD nginx-0.5.33/
./configure--user=www--group=www--prefix=/usr/local/webserver/nginx--with-http_stub_status_module
Make && make install
Cd.. /
3. Create Nginx Log directory
Mkdir-p/data1/logs
chmod +w/data1/logs
Chown-r Www:www/data1/logs
4. Create Nginx configuration file
①, creating nginx.conf files in the/usr/local/webserver/nginx/conf/directory:
Rm-f/usr/local/webserver/nginx/conf/nginx.conf
Vi/usr/local/webserver/nginx/conf/nginx.conf
Enter the following content:
Reference
User www www.
Worker_processes 10;
Error_log/data1/logs/nginx_error.log Crit;
#pid Logs/nginx.pid;
#Specifies the value for maximum file descriptors the can is opened by this process.
Worker_rlimit_nofile 51200;
Events
{
Use Epoll;
Worker_connections 51200;
}
http
{
Include Conf/mime.types;
Default_type Application/octet-stream;
CharSet gb2312;

Server_names_hash_bucket_size 128;

#sendfile on;
#tcp_nopush on;
Keepalive_timeout 60;
Tcp_nodelay on;
gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 8k;
Gzip_http_version 1.1;
Gzip_types text/plain application/x-javascript text/css text/html application/xml;
Server
{
Listen 80;
server_name blog.s135.com;
Index index.html index.htm index.php;
Root/data0/vshare/htdocs;
if (-D $request _filename)
{
Rewrite ^/(. *) ([^/]) $
http://$host/$1$2/
permanent;
}

Location ~. *\.php?$
{
Include conf/fcgi.conf;
Fastcgi_pass 127.0.0.1:10080;
Fastcgi_index index.php;
}
Log_format access ' $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 access;
}
Server
{
Listen 80;
server_name status.blog.s135.com;
Location/{
Stub_status on;
Access_log off;
}
}
}
②, creating fcgi.conf files in the/usr/local/webserver/nginx/conf/directory:
Vi/usr/local/webserver/nginx/conf/fcgi.conf
Enter the following content:
Reference
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;
5, start Nginx
Ulimit-shn 51200
/usr/local/webserver/nginx/sbin/nginx-c/usr/local/webserver/nginx/conf/nginx.conf
Four, configure boot automatically start Nginx + PHP
Vi/etc/rc.local
Add the following at the end:
Reference
Ulimit-shn 51200
/usr/local/webserver/php/bin/spawn-fcgi-a 127.0.0.1-p 10080-c 64-u www-f/usr/local/webserver/php/bin/php-cgi
/usr/local/webserver/nginx/sbin/nginx-c/usr/local/webserver/nginx/conf/nginx.conf
V. Optimizing Linux kernel Parameters
Vi/etc/sysctl.conf
Add the following at the end:
Reference
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
To have the configuration take effect immediately:
/sbin/sysctl-p
Vi. smooth change of nginx configuration without stopping Nginx service
(1), after modifying the/usr/local/webserver/nginx/conf/nginx.conf configuration file, perform the following command to check that the configuration file is correct:
/usr/local/webserver/nginx/sbin/nginx-t
If the screen displays the following two lines of information, the configuration file is correct:
The configuration file/usr/local/webserver/nginx/conf/nginx.conf syntax is OK
The configuration file/usr/local/webserver/nginx/conf/nginx.conf was tested successfully
(2), at this point, enter the following command to view the Nginx main process number:
Ps-ef | grep "Nginx:master Process" | Grep-v "grep" | Awk-f ' ' {print $} '
The screen displays the Nginx main process number, for example:
6302
At this point, you can make the modified Nginx configuration file effective by executing the following command:
Kill-hup 6302

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.