Nginx ("engine X") is a high-performance HTTP and reverse proxy server and an IMAP/POP3/SMTP proxy server. Nginx
It was developed by the rambler.ru site, where Igor Sysoev is the second highest traffic in Russia. Nginx build It has been running on this site for more than two and a half years. Igor
Publish the source code in the form of a class BSD license.
Nginx Chinese wiki: http://wiki.codemongers.com/NginxChs
In the case of high-concurrency connections, nginx is a good alternative to Apache servers. Nginx can also be used as a layer-7 Server Load balancer. Based on my test results, nginx 0.5.33 + PHP
5.2.5 (FastCGI) can accommodate more than 30 thousand concurrent connections, equivalent to 10 times of Apache in the same environment
How to build nginx
In my experience, servers with 4 GB of memory + Apache (prefork mode) can only process 3000 concurrent connections, because they will occupy more than 3 GB of memory and have to be tied
1 GB of memory is reserved. I once had two Apache servers, because the maxclients set in the configuration file is 4000, when the number of concurrent Apache connections reaches 3800
The server memory and swap space are full and thus crash.
This nginx 0.5.33 + PhP 5.2.5 (FastCGI)
The server consumes 30 thousand MB of memory (15 MB * 10 = 150 MB) for the 10 nginx processes enabled under concurrent connections, and the 64 PHP-CGI processes enabled consume MB of memory.
(20 m * 64 = 1280 m), and the memory consumed by the system itself, total consumption of less than 2 GB memory. If the server memory is small, only 25 PHP-CGI processes can be started.
Php-CGI consumes only 500 mb of memory.
Access nginx 0.5.33 + PhP 5.2.5 (FastCGI) with 30 thousand concurrent connections)
The server's PHP program is still fast. Is the nginx status monitoring page, and the number of active connections is displayed as 28457 (for the nginx monitoring page configuration, we will provide
In the nginx configuration file ):
The following figure shows the system load of the nginx 0.5.33 + PhP 5.2.5 (FastCGI) server when 10 nginx processes and 64 PHP-CGI processes are enabled under 30 thousand concurrent connections:
Installation steps:
(System requirements: Linux
2.6 + kernel, Linux operating system in this article
Centos 4.4)
1. Obtain relevant open-source programs:
1. Download the program source code package to the current directory:
.
2. decompress:
- Mkdir-P/data0/software
- CD/data0/software
- Unzip nginx-php-1.zip
- Unzip nginx-php-2.zip
- CD/data0/software/nginx-PHP
Copy code
Ii. install PHP 5.2.5 (FastCGI Mode)
1. Compile and install the supported libraries required 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 restart src.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
- 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 ../
Copy code
2. Compile and install MySQL
5.0.45
- /Usr/sbin/groupadd MySQL
- /Usr/sbin/useradd-G 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-generator er -- 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 ../
Copy code
Appendix: The following is an additional step. If you want to run MySQL on this server
Database
, Perform the following two steps. If you only want PHP to support MySQL extension libraries and connect to MySQL Databases on other servers, you do not need to perform the following two steps.
① Create a data table as a MySQL user account:
- /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. PID -- skip-locking
-Port = 3306 -- socket =/tmp/MySQL. Sock
Copy code
② Start MySQL (the last & indicates running in the background)
- /Bin/sh/usr/local/webserver/MySQL/bin/mysqld_safe -- defaults-file =/usr/local/webserver/MySQL/My. CNF &
Copy code
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-safe-mode -- 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 ../
Copy code
4. Compile and install the PhP5 extension 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 ../../../
Copy code
5. Modify the php. ini file
Manually modify: Find extension_dir = "./" in/usr/local/webserver/PHP/etc/PHP. ini "./"
Change to extension_dir = "/usr/local/webserver/PHP/lib/PHP/extensions/no-debug-non-zts-20060613 /"
Add the following lines after this line, and then save:
- Extension = "memcache. So"
- Extension = "GD. So"
Copy code
Automatic Modification: IF Manual modification is troublesome, You can execute the following shell command to automatically modify 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/PHP. ini
Copy code
6. Create WWW users and groups and their directories:
- /Usr/sbin/groupadd www-G 48
- /Usr/sbin/useradd-u 48-G WWW
- Mkdir-P/data0/vshare/htdocs
- Chmod + w/data0/vshare/htdocs
- Chown-r www: www/data0/vshare/htdocs
Copy code
7. Install the spawn-fcgi included in Lighttpd to start PHP-CGI.
Note: The spawn-fcgi program in the compressed package is compiled into a binary version.
- CP spawn-fcgi/usr/local/webserver/PHP/bin
- Chmod + x/usr/local/webserver/PHP/bin/spawn-fcgi
Copy code
8. Start the PHP-CGI process and listen to port 10080 of 127.0.0.1. The number of processes is 64 (if the server memory is smaller than 3 GB, only 25 processes can be started). 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
Copy code
3. Install nginx 0.5.33
1. Install the PCRE Library required by nginx:
- Tar zxvf pcre-7.2.tar.gz
- CD pcre-7.2/
- ./Configure
- Make & make install
- CD ../
Copy code
2. Install 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 ../
Copy code
3. Create an nginx log directory
- Mkdir-P/data1/logs
- Chmod + w/data1/logs
- Chown-r www: www/data1/logs
Copy code
4. Create an nginx configuration file
① Create the nginx. conf file 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
Copy code
Enter the following content:
- User WWW;
- Worker_processes 10;
- Error_log/data1/logs/nginx_error.log crit;
- # 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;
-
- 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 1 K;
- Gzip_buffers 4 8 K;
- 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;
- }
- }
- }
Copy code
② Create the fcgi. conf file in the/usr/local/webserver/nginx/CONF/directory:
- VI/usr/local/webserver/nginx/CONF/fcgi. conf
Copy code
Enter the following content:
- 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;
Copy code
5. Start nginx
- Ulimit-shn 51200
- /Usr/local/webserver/nginx/sbin/nginx-C/usr/local/webserver/nginx/CONF/nginx. conf
Copy code
4. Configure to automatically start nginx + PhP upon startup
- VI/etc/rc. Local
Copy code
Add the following content at the end:
- 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
Copy code
5. Optimize Linux Kernel Parameters
- VI/etc/sysctl. conf
Copy code
Add the following content at the end:
- 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
Copy code
Make the configuration take effect immediately:
- /Sbin/sysctl-P
Copy code
6. smoothly change the nginx configuration without stopping the nginx Service
(1) After modifying the/usr/local/webserver/nginx/CONF/nginx. conf configuration file, run the following command to check whether the configuration file is correct:
- /Usr/local/webserver/nginx/sbin/nginx-T
Copy code
If the following two lines of information are displayed, 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) enter the following command to view the nginx master process Number:
- PS-Ef | grep "nginx: Master process" | grep-V "grep" | awk-F ''' {print $2 }'
Copy code
The screen displays the nginx main process number, for example:
6302
Then, run the following command to make the modified nginx configuration file take effect:
- Kill-HUP 6302
Copy code