Nginx + php-fpm + apc= wonderful combination

Source: Internet
Author: User
Tags apc apc module diff php tutorial

The method described in this article should be the quickest way to get PHP (PHP tutorial) Now: Nginx + php-fpm + APC. We will be from the installation of Nginx HTTP server, PHP and PHP-FPM patches, as well as APC, detailed explanation of this method of the specific configuration and application, the final effect? I believe it will surprise you.

Background

Over the past two years, our website has been running the apache+mod_php module, most of the time this combination to cope with, but with the increase in traffic, we notice that Apache has become very difficult, it began to eat crazy memory, the CPU is all preempted, We need to find a faster way to solve the problem.

Nginx is a good choice, many indicators are more than Apache, such as I/O, CPU, memory and the number of requests/sec, etc., if necessary, at any time can search for relevant information from Google. From my personal test results, the difference between Nginx and Apache is very obvious, sorry, because it is an informal test, so the relevant figures I will not publish, this is not the focus of this article. What makes me more confident is that I just need a few steps to get the performance far beyond Apache.

PHP-FPM?

Many people may choose to configure Nginx from the LIGHTTPD Project spawn-fcgi parsing PHP, but the use of spawn-fcgi some problems, so I intend to abandon it to find another way. PHP-FPM is the abbreviation for PHP FastCGI process Manager, the PHP FastCGI processes manager, which is actually a PHP patch designed to integrate FastCGI process management into a PHP package.

Note: Even if you persist in using Apache, there are many reasons to skip mod_php and parse PHP directly through fastcgi. With mod_php, Apache handles every request to load PHP into all libraries, which is a huge, fearless overhead. If using fastcgi,php behaves more like an application server, PHP-FPM and spawn-fcgi load and kill PHP instances as needed, there are many benefits, and it is important to reduce memory overhead.

Let's get started.

We are using the newly installed Ubuntu 8.10 Intrepid, the first thing is to install all the dependent packages.

    1. sudo apt-get install make Bison flex gcc patch autoconf Subversion Locate
    2. sudo apt-get install Libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 LIBCU Rl4-openssl-dev Libpq-dev libpq5 Libsyck0-dev

Once you're ready to rely on the package, we can start.

Compiling PHP

We downloaded the PHP 5.2.8 source code package, as well as the corresponding PHP-FPM patch, we first patched, and then compiled.

  1. cd/usr/local/src/
  2. sudo wget http://us.php.net/get/php-5.2.8.tar.gz/from/this/mirror
  3. sudo tar zvxf php-5.2.8.tar.gz
  4. sudo wget http://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz
  5. sudo gzip-cd php-5.2.8-fpm-0.5.10.diff.gz | sudo patch-d php-5.2.8-p1
  6. CD php-5.2.8
  7. sudo./configure--enable-fastcgi--enable-fpm--with-mcrypt--with-zlib--enable-mbstring--disable-pdo--with-pgsql- -with-curl--disable-debug--enable-pic--disable-rpath--enable-inline-optimization--with-bz2--with-xml-- With-zlib--enable-sockets--enable-sysvsem--enable-sysvshm--enable-pcntl--enable-mbregex--with-mhash-- Enable-xslt--enable-memcache--enable-zip--with-pcre-regex
  8. sudo make all install
  9. sudo strip/usr/local/bin/php-cgi

If you encounter an error while running the above command, it is most likely due to a missing dependency package. Also, make sure that you have correctly enabled and disabled specific PHP configuration options. Then we install some modules that will be used through the PECL:

    1. sudo pecl install Memcache
    2. sudo pecl Install APC
    3. sudo pecl install Syck-beta

When installing the APC module, be sure to turn off the Apache option, which will also alert you if you do not close it. Next copy our usual php.ini:

    1. sudo cp/usr/local/src/php-5.2.8/php.ini-recommended/usr/local/lib/php.ini

Finally, we establish symbolic Links:

    1. sudo mkdir/etc/php/
    2. sudo ln-s/usr/local/lib/php.ini/etc/php/php.ini
    3. sudo ln-s/usr/local/etc/php-fpm.conf/etc/php/php-fpm.conf

PHP compiles to this end, the rest of the thing is to modify the php-fpm.conf settings, with a text editor to open/etc/php/ Php-fpm.conf, the process is the main user to Www-data, this file is larger, so it is best to use the search function, I would like to modify the values in 51, 52, 63 and 66 rows.

  1. <value  name = > www-data Value>  
  2. <value  name = "group" > www-data Value>  
  3. <value Name="user">www-datavalue>
  4. <value   Name="group">www-datavalue>

Nginx

is as simple as compiling PHP:

    1. Cd..
    2. sudo wget http://sysoev.ru/nginx/nginx-0.6.35.tar.gz
    3. sudo tar zxvf nginx-0.6.35.tar.gz
    4. sudo rm-f nginx-0.6.35.tar.gz
    5. CD nginx-0.6.35
    6. sudo./configure --sbin-path=/usr/local/sbin--with-http_ssl_module--without-mail_pop3_module-- Without-mail_imap_module--without-mail_smtp_module--with-http_stub_status_module
    7. sudo make && sudo make install

Then create a link:

    1. sudo ln-s/usr/local/nginx/conf/etc/nginx

The next step is dispensable, but I have been using it so far, so to speak. Open the/etc/nginx/nginx.conf, and the final result is as follows:

  1. User Www-data;
  2. Worker_processes 6;
  3. Events {
  4. Worker_connections 1024;
  5. }
  6. HTTP {
  7. Include Mime.types;
  8. Default_type Application/octet-stream;
  9. Sendfile on;
  10. Keepalive_timeout 10 10;
  11. gzip on;
  12. Gzip_comp_level 1;
  13. Gzip_proxied any;
  14. Gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/ JavaScript
  15. Log_format Main ' $remote _addr-$remote _user [$time _local] '
  16. "$request" $status $body _bytes_sent "$http _referer"
  17. ' "$http _user_agent" "$http _x_forwarded_for";
  18. Access_log/var/log/nginx_access.log main;
  19. Error_log/var/log/nginx_error.log Debug;
  20. include/usr/local/nginx/sites-enabled/*;
  21. }

We also set some fastcgi parameters, so that PHP does not choke, you can avoid the Nginx 503 error, open/etc/nginx/fastcgi_params, add the following parameters:

    1. Fastcgi_connect_timeout 60;
    2. Fastcgi_send_timeout 180;
    3. Fastcgi_read_timeout 180;
    4. Fastcgi_buffer_size 128k;
    5. Fastcgi_buffers 4 256k;
    6. Fastcgi_busy_buffers_size 256k;
    7. Fastcgi_temp_file_write_size 256k;
    8. Fastcgi_intercept_errors on;

Finally, we create a SYSTEMV-style startup script that is saved as/etc/init.d/nginx.

  1. #! /bin/sh
  2. # # # BEGIN INIT INFO
  3. # Provides:nginx
  4. # Required-start: $all
  5. # Required-stop: $all
  6. # Default-start:2 3 4 5
  7. # default-stop:0 1 6
  8. # short-description:starts the Nginx Web server
  9. # description:starts Nginx using Start-stop-daemon
  10. # # # END INIT INFO
  11. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  12. DAEMON=/usr/local/sbin/nginx
  13. NAME=nginx
  14. DESC=nginx
  15. Test-x $DAEMON | | Exit 0
  16. # Include Nginx Defaults if available
  17. if [-f/etc/default/nginx]; Then
  18. . /etc/default/nginx
  19. Fi
  20. Set-e
  21. Case "$" in
  22. Start
  23. Echo-n "Starting $DESC:"
  24. Start-stop-daemon--start--quiet--pidfile/usr/local/nginx/logs/$NAME. PID \
  25. --exec $DAEMON--$DAEMON _opts
  26. echo "$NAME."
  27. ;;
  28. Stop
  29. Echo-n "Stopping $DESC:"
  30. Start-stop-daemon--stop--quiet--pidfile/usr/local/nginx/logs/$NAME. PID \
  31. --exec $DAEMON
  32. echo "$NAME."
  33. ;;
  34. Restart|force-reload)
  35. Echo-n "Restarting $DESC:"
  36. Start-stop-daemon--stop--quiet--pidfile \
  37. /usr/local/nginx/logs/$NAME. PID--exec $DAEMON
  38. Sleep 1
  39. Start-stop-daemon--start--quiet--pidfile \
  40. /usr/local/nginx/logs/$NAME. PID--exec $DAEMON--$DAEMON _opts
  41. echo "$NAME."
  42. ;;
  43. Reload
  44. Echo-n "Reloading $DESC configuration:"
  45. Start-stop-daemon--stop--signal HUP--quiet--pidfile/usr/local/nginx/logs/$NAME. PID \
  46. --exec $DAEMON
  47. echo "$NAME."
  48. ;;
  49. *)
  50. N=/etc/init.d/$NAME
  51. echo "Usage: $N {start|stop|restart|force-reload}" >&2
  52. Exit 1
  53. ;;
  54. Esac
  55. Exit 0

Do not forget to set the executable permissions.

Set up your site

This phase of work is done mainly in your own custom, and here is a rough introduction. First we create a directory to store our website configuration files:

    1. sudo mkdir/usr/local/nginx/sites-enabled
    2. sudo ln-s/usr/local/nginx/sites-enabled/etc/sites

Next, add a conf file/etc/sites/default.conf for our website, as follows:

  1. server {
  2. Listen *:80;
  3. Location/{
  4. Root/var/www/default/pub;
  5. Index index.php;
  6. # If file exists return it right away
  7. if (-f $request _filename) {
  8. Break
  9. }
  10. # otherwise rewrite the fucker
  11. if (!-e $request _filename) {
  12. Rewrite ^ (. +) $/index.php$1 last;
  13. Break
  14. }
  15. }
  16. # If the request starts with our Frontcontroller, pass it in to fastcgi
  17. Location ~ ^/index.php
  18. {
  19. Fastcgi_pass 127.0.0.1:9000;
  20. Fastcgi_param Script_filename/var/www/default/pub$fastcgi_script_name;
  21. Fastcgi_param path_info $fastcgi _script_name;
  22. Include/usr/local/nginx/conf/fastcgi_params;
  23. }
  24. }

Above this conf file control the front-end style of the website, including Wordpress,cake, note that static content is not resolved by fastcgi. In addition, you may need to modify the/var/www/default file to set the default placement location for Web site files.

Start

Our work is over, start looking at the effect.

    1. sudo php-fm start
    2. Sudo/etc/init.d/nginx start

Now you can test your website to see how it works and if you have any questions, please feel free to ask.

Nginx + php-fpm + apc= wonderful combination

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.