Nginx + PhP (FastCGI) build a web server 10 times better than Apache

Source: Internet
Author: User
Tags mcrypt zts vshare

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:

  1. Mkdir-P/data0/software
  2. CD/data0/software
  3. Unzip nginx-php-1.zip
  4. Unzip nginx-php-2.zip
  5. 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:

  1. Tar zxvf libiconv-1.11.tar.gz
  2. CD libiconv-1.11/
  3. ./Configure -- prefix =/usr/local
  4. Make
  5. Make install
  6. CD ../
  7. Tar zxvf freetype-2.3.5.tar.gz
  8. CD freetype-2.3.5/
  9. ./Configure
  10. Make
  11. Make install
  12. CD ../
  13. Tar zxvf libpng-1.2.20.tar.gz
  14. CD libpng-1.2.20/
  15. ./Configure
  16. Make
  17. Make install
  18. CD ../
  19. Tar zxvf restart src.v6b.tar.gz
  20. CD jpeg-6b/
  21. ./Configure -- enable-static -- enable-shared
  22. Make
  23. Make install
  24. Make install-lib
  25. CD ../
  26. Tar zxvf libxml2-2.6.30.tar.gz
  27. CD libxml2-2.6.30/
  28. ./Configure
  29. Make
  30. Make install
  31. CD ../
  32. Tar zxvf libmcrypt-2.5.8.tar.gz
  33. CD libmcrypt-2.5.8/
  34. ./Configure
  35. Make
  36. Make install
  37. /Sbin/ldconfig
  38. CD libltdl/
  39. ./Configure -- enable-ltdl-install
  40. Make
  41. Make install
  42. CD ../../
  43. Tar zxvf mhash-0.9.9.tar.gz
  44. CD mhash-0.9.9/
  45. ./Configure
  46. Make
  47. Make install
  48. CD ../
  49. CP/usr/local/lib/libmcrypt. */usr/lib
  50. Ln-S/usr/local/lib/libmhash. so.2/usr/lib/libmhash. so.2
  51. Tar zxvf mcrypt-2.6.6.tar.gz
  52. CD mcrypt-2.6.6/
  53. ./Configure
  54. Make
  55. Make install
  56. CD ../

Copy code

2. Compile and install MySQL

5.0.45

  1. /Usr/sbin/groupadd MySQL
  2. /Usr/sbin/useradd-G MySQL
  3. Tar zxvf mysql-5.0.45.tar.gz
  4. CD mysql-5.0.45
  5. ./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
  6. Make & make install
  7. Chmod + w/usr/local/webserver/MySQL
  8. Chown-r MYSQL: MySQL/usr/local/webserver/MySQL
  9. CP support-files/my-medium.cnf/usr/local/webserver/MySQL/My. CNF
  10. 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:

  1. /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)

  1. /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)

  1. Tar zxvf php-5.2.5.tar.gz
  2. CD php-5.2.5/
  3. ./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
  4. Sed-I's #-LZ-lm-lxml2-LZ-lm-lxml2-LZ-lm-lcrypt # &-liconv # 'makefile
  5. Make
  6. Make install
  7. Cp php. ini-Dist/usr/local/webserver/PHP/etc/PHP. ini
  8. CD ../

Copy code

4. Compile and install the PhP5 extension module

  1. Tar zxvf memcache-2.2.1.tgz
  2. CD memcache-2.2.1/
  3. /Usr/local/webserver/PHP/bin/phpize
  4. ./Configure -- With-PHP-Config =/usr/local/webserver/PHP/bin/PHP-config
  5. Make
  6. Make install
  7. CD ../
  8. CD php-5.2.5/EXT/GD/
  9. /Usr/local/webserver/PHP/bin/phpize
  10. ./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
  11. Make
  12. Make install
  13. 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:

  1. Extension = "memcache. So"
  2. 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:

  1. 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:

  1. /Usr/sbin/groupadd www-G 48
  2. /Usr/sbin/useradd-u 48-G WWW
  3. Mkdir-P/data0/vshare/htdocs
  4. Chmod + w/data0/vshare/htdocs
  5. 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.

  1. CP spawn-fcgi/usr/local/webserver/PHP/bin
  2. 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:

  1. /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:

  1. Tar zxvf pcre-7.2.tar.gz
  2. CD pcre-7.2/
  3. ./Configure
  4. Make & make install
  5. CD ../

Copy code

2. Install nginx

  1. Tar zxvf nginx-0.5.33.tar.gz
  2. CD nginx-0.5.33/
  3. ./Configure -- user = WWW -- group = WWW -- prefix =/usr/local/webserver/nginx -- with-http_stub_status_module
  4. Make & make install
  5. CD ../

Copy code

3. Create an nginx log directory

  1. Mkdir-P/data1/logs
  2. Chmod + w/data1/logs
  3. 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:

  1. Rm-F/usr/local/webserver/nginx/CONF/nginx. conf
  2. VI/usr/local/webserver/nginx/CONF/nginx. conf

Copy code

Enter the following content:

  1. User WWW;
  2. Worker_processes 10;
  3. Error_log/data1/logs/nginx_error.log crit;
  4. # PID logs/nginx. PID;
  5. # Specifies the value for maximum file descriptors that can be opened by this process.
  6. Worker_rlimit_nofile 51200;
  7. Events
  8. {
  9. Use epoll;
  10. Worker_connections 51200;
  11. }
  12. HTTP
  13. {
  14. Include CONF/mime. types;
  15. Default_type application/octet-stream;
  16. Charset gb2312;
  17. Server_names_hash_bucket_size 128;
  18. # Sendfile on;
  19. # Tcp_nopush on;
  20. Keepalive_timeout 60;
  21. Tcp_nodelay on;
  22. Gzip on;
  23. Gzip_min_length 1 K;
  24. Gzip_buffers 4 8 K;
  25. Gzip_http_version 1.1;
  26. Gzip_types text/plain application/X-JavaScript text/CSS text/HTML Application/XML;
  27. Server
  28. {
  29. Listen 80;
  30. SERVER_NAME blog.s135.com;
  31. Index index.html index.htm index. php;
  32. Root/data0/vshare/htdocs;
  33. If (-d $ request_filename)
  34. {
  35. Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
  36. }
  37. Location ~ . */. Php? $
  38. {
  39. Include CONF/fcgi. conf;
  40. Fastcgi_pass 127.0.0.1: 10080;
  41. Fastcgi_index index. php;
  42. }
  43. Log_format access' $ remote_addr-$ remote_user [$ time_local] "$ request "'
  44. '$ Status $ body_bytes_sent "$ http_referer "'
  45. '"$ Http_user_agent" $ http_x_forwarded_for ';
  46. Access_log/data1/logs/access. Log Access;
  47. }
  48. Server
  49. {
  50. Listen 80;
  51. SERVER_NAME status.blog.s135.com;
  52. Location /{
  53. Stub_status on;
  54. Access_log off;
  55. }
  56. }
  57. }

Copy code

② Create the fcgi. conf file in the/usr/local/webserver/nginx/CONF/directory:

  1. VI/usr/local/webserver/nginx/CONF/fcgi. conf

Copy code

Enter the following content:

  1. Fastcgi_param gateway_interface cgi/1.1;
  2. Fastcgi_param server_software nginx;
  3. Fastcgi_param QUERY_STRING $ QUERY_STRING;
  4. Fastcgi_param request_method $ request_method;
  5. Fastcgi_param content_type $ content_type;
  6. Fastcgi_param content_length $ content_length;
  7. Fastcgi_param script_filename $ document_root $ fastcgi_script_name;
  8. Fastcgi_param script_name $ fastcgi_script_name;
  9. Fastcgi_param request_uri $ request_uri;
  10. Fastcgi_param document_uri $ document_uri;
  11. Fastcgi_param document_root $ document_root;
  12. Fastcgi_param server_protocol $ server_protocol;
  13. Fastcgi_param remote_addr $ remote_addr;
  14. Fastcgi_param remote_port $ remote_port;
  15. Fastcgi_param server_addr $ server_addr;
  16. Fastcgi_param server_port $ server_port;
  17. Fastcgi_param SERVER_NAME $ SERVER_NAME;
  18. # PHP only, required if PHP was built with -- enable-force-CGI-Redirect
  19. # Fastcgi_param redirect_status 200;

Copy code

5. Start nginx

  1. Ulimit-shn 51200
  2. /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

  1. VI/etc/rc. Local

Copy code

Add the following content at the end:

  1. Ulimit-shn 51200
  2. /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
  3. /Usr/local/webserver/nginx/sbin/nginx-C/usr/local/webserver/nginx/CONF/nginx. conf

Copy code

5. Optimize Linux Kernel Parameters

  1. VI/etc/sysctl. conf

Copy code

Add the following content at the end:

  1. Net. ipv4.tcp _ fin_timeout = 30
  2. Net. ipv4.tcp _ keepalive_time = 300
  3. Net. ipv4.tcp _ syncookies = 1
  4. Net. ipv4.tcp _ tw_reuse = 1
  5. Net. ipv4.tcp _ tw_recycle = 1
  6. Net. ipv4.ip _ local_port_range = 5000 65000

Copy code

Make the configuration take effect immediately:

  1. /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:

  1. /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:

  1. 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:

  1. Kill-HUP 6302

Copy code

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.