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.
- sudo apt-get install make Bison flex gcc patch autoconf Subversion Locate
- 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.
- cd/usr/local/src/
- sudo wget http://us.php.net/get/php-5.2.8.tar.gz/from/this/mirror
- sudo tar zvxf php-5.2.8.tar.gz
- sudo wget http://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz
- sudo gzip-cd php-5.2.8-fpm-0.5.10.diff.gz | sudo patch-d php-5.2.8-p1
- CD php-5.2.8
- 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
- sudo make all install
- 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:
- sudo pecl install Memcache
- sudo pecl Install APC
- 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:
- sudo cp/usr/local/src/php-5.2.8/php.ini-recommended/usr/local/lib/php.ini
Finally, we establish symbolic Links:
- sudo mkdir/etc/php/
- sudo ln-s/usr/local/lib/php.ini/etc/php/php.ini
- 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.
- <value name = > www-data Value>
- <value name = "group" > www-data Value>
- <value Name="user">www-datavalue>
- <value Name="group">www-datavalue>
Nginx
is as simple as compiling PHP:
- Cd..
- sudo wget http://sysoev.ru/nginx/nginx-0.6.35.tar.gz
- sudo tar zxvf nginx-0.6.35.tar.gz
- sudo rm-f nginx-0.6.35.tar.gz
- CD nginx-0.6.35
- 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
- sudo make && sudo make install
Then create a link:
- 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:
- User Www-data;
- Worker_processes 6;
- Events {
- Worker_connections 1024;
- }
- HTTP {
- Include Mime.types;
- Default_type Application/octet-stream;
- Sendfile on;
- Keepalive_timeout 10 10;
- gzip on;
- Gzip_comp_level 1;
- Gzip_proxied any;
- Gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/ JavaScript
- Log_format Main ' $remote _addr-$remote _user [$time _local] '
- "$request" $status $body _bytes_sent "$http _referer"
- ' "$http _user_agent" "$http _x_forwarded_for";
- Access_log/var/log/nginx_access.log main;
- Error_log/var/log/nginx_error.log Debug;
- include/usr/local/nginx/sites-enabled/*;
- }
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:
- Fastcgi_connect_timeout 60;
- Fastcgi_send_timeout 180;
- Fastcgi_read_timeout 180;
- Fastcgi_buffer_size 128k;
- Fastcgi_buffers 4 256k;
- Fastcgi_busy_buffers_size 256k;
- Fastcgi_temp_file_write_size 256k;
- Fastcgi_intercept_errors on;
Finally, we create a SYSTEMV-style startup script that is saved as/etc/init.d/nginx.
- #! /bin/sh
- # # # BEGIN INIT INFO
- # Provides:nginx
- # Required-start: $all
- # Required-stop: $all
- # Default-start:2 3 4 5
- # default-stop:0 1 6
- # short-description:starts the Nginx Web server
- # description:starts Nginx using Start-stop-daemon
- # # # END INIT INFO
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DAEMON=/usr/local/sbin/nginx
- NAME=nginx
- DESC=nginx
- Test-x $DAEMON | | Exit 0
- # Include Nginx Defaults if available
- if [-f/etc/default/nginx]; Then
- . /etc/default/nginx
- Fi
- Set-e
- Case "$" in
- Start
- Echo-n "Starting $DESC:"
- Start-stop-daemon--start--quiet--pidfile/usr/local/nginx/logs/$NAME. PID \
- --exec $DAEMON--$DAEMON _opts
- echo "$NAME."
- ;;
- Stop
- Echo-n "Stopping $DESC:"
- Start-stop-daemon--stop--quiet--pidfile/usr/local/nginx/logs/$NAME. PID \
- --exec $DAEMON
- echo "$NAME."
- ;;
- Restart|force-reload)
- Echo-n "Restarting $DESC:"
- Start-stop-daemon--stop--quiet--pidfile \
- /usr/local/nginx/logs/$NAME. PID--exec $DAEMON
- Sleep 1
- Start-stop-daemon--start--quiet--pidfile \
- /usr/local/nginx/logs/$NAME. PID--exec $DAEMON--$DAEMON _opts
- echo "$NAME."
- ;;
- Reload
- Echo-n "Reloading $DESC configuration:"
- Start-stop-daemon--stop--signal HUP--quiet--pidfile/usr/local/nginx/logs/$NAME. PID \
- --exec $DAEMON
- echo "$NAME."
- ;;
- *)
- N=/etc/init.d/$NAME
- echo "Usage: $N {start|stop|restart|force-reload}" >&2
- Exit 1
- ;;
- Esac
- 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:
- sudo mkdir/usr/local/nginx/sites-enabled
- sudo ln-s/usr/local/nginx/sites-enabled/etc/sites
Next, add a conf file/etc/sites/default.conf for our website, as follows:
- server {
- Listen *:80;
- Location/{
- Root/var/www/default/pub;
- Index index.php;
- # If file exists return it right away
- if (-f $request _filename) {
- Break
- }
- # otherwise rewrite the fucker
- if (!-e $request _filename) {
- Rewrite ^ (. +) $/index.php$1 last;
- Break
- }
- }
- # If the request starts with our Frontcontroller, pass it in to fastcgi
- Location ~ ^/index.php
- {
- Fastcgi_pass 127.0.0.1:9000;
- Fastcgi_param Script_filename/var/www/default/pub$fastcgi_script_name;
- Fastcgi_param path_info $fastcgi _script_name;
- Include/usr/local/nginx/conf/fastcgi_params;
- }
- }
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.
- sudo php-fm start
- 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