PHP server of several architectures, highly recommended!

Source: Internet
Author: User
Tags php server php source code
PHP server of several architectures, highly recommended!!!


Found a good article, very detailed introduction of the current PHP server several architectures, specially reproduced, highly recommended!!

?

NGINX+PHP+PHP-FPM (FastCGI) installation and configuration on Ubuntu

Reprint please specify the original link: http://blog.csdn.net/omohe/archive/2009/07/10/4336731.aspx

Version: v1.0? Author: OMO Last modified: 2009.07.10

0 Prior Knowledge review:?

1) There are three types of PHP support for various server HTTP server:?

A. Implemented via Httpserver built-in modules,

For example Apache MOD_PHP5, similar to Apache built-in Mod_perl can support Perl;

B. Through CGI, this is like the previous Perl CGI, the disadvantage of this approach is poor performance, because each time the server encounters these scripts need to restart the script parser to execute the script and then return the results to the server, on the other hand is not very safe, which is rarely used.

C. A recent emergence of a kind called fastcgi. The so-called fastcgi is the improvement of CGI. It generally uses the C/s structure, the general script processor will start one or more daemon processes, each time Httpserver encountered a script, the direct delivery to the FASTCGI process to execute, and then the resulting results (usually HTML) returned to the browser.

> The problem with this approach is that when you encounter frequent requests for large traffic, the daemon process of the script processor can become overloaded and slow, or even memory leaks occur;

> But the advantage of comparing Apache's built-in modules is that the server and the script parser are completely separate from the accountability, so the servers are no longer bloated, can focus on static file responses, or return the results of the dynamic script parser to the user client. So compared to the Apache built-in module, sometimes performance to improve a lot. Some tests may reach apache+mod_php 5~10 times.


2) Using the FastCGI method is now common in two types of stacks:ligthttpd+spawn-fcgi; The other is nginx+php-fpm (can also use spawn-fcgi)? ?

A. As mentioned above, both structures are supported by fastcgi for PHP, so Httpserver is completely free and can be better responsive and concurrent processing. So lighttpd and Nginx have small, but powerful and efficient reputation.

B. The two can also be divided into a good or bad, spawn-fcgi because it is part of the LIGHTTPD, so installed LIGHTTPD general will use spawn-fcgi to PHP support, But now there are users said LIGTTPD spwan-fcgi in high concurrent access, the above mentioned memory leak or even automatic restart fastcgi. That is: PHP script processor When the computer, this time if the user access, may appear white page (that is, PHP can not be parsed or error).

Another: First nginx is not like lighttpd itself with fastcgi (spawn-fcgi), so it is completely lightweight, must rely on a third-party fastcgi processor to be able to parse PHP, so in fact, it seems that nginx is very flexible, It can be connected with any third-party provider to parse the processor to enable parsing of PHP (easy to set up in nginx.conf).

Nginx can use spwan-fcgi (need to install lighttpd together, but need to avoid the port for Nginx, some earlier blog has this aspect of the installation of the tutorial), but because spawn-fcgi with the above mentioned the user gradually discovered defects, Now slowly reduce the use of nginx+spawn-fcgi combination.

C. Due to SPAWN-FCGI's shortcomings, a new third party (currently known as the one that is trying to join PHP core in the near future) is now appearing as a fastcgi processor called PHP-FPM (specific to Google). Compared with spawn-fcgi, it has the following advantages:
Since it was developed as a patch patch for PHP, the installation needs to be compiled with the PHP source code, that is, compiled into the PHP core, so in terms of performance should be excellent;
It also outperforms spawn-fcgi in handling high concurrency, at least not automatically restarting the FASTCGI processor. The specific algorithm and design can be understood by Google.

Therefore, as mentioned above because of the light weight and flexibility of nginx, so the current performance is superior, more and more people gradually use this combination:NGINX+PHP/PHP-FPM?。

3) So summarize:?
At present, there are three kinds of stacks that can be seen in Httpserver:?

>apache+mod_php5
>lighttp+spawn-fcgi
>nginx+php-fpm

After all, the performance may be slightly better, but Apache is still the boss because of its rich modules and functions. Some people test that NGINX+PHP-FPM may reach apache+mod_php5 5~10 times in high-concurrency situations, and now nginx+php-fpm use more and more.

The following highlights the stack:
Installation and configuration of APACHE+MOD_PHP5 and NGINX+PHP-FPM. For lighttpd+spawn-fcgi, because I personally did not how to use, so the following is not ready to introduce, interested in the information can be consulted.


1.apache+mod_php mode:?

we used the classic apache+mod_php for a long time:?

The Apache support for PHP is supported by the Apache module. If Zengyuan code compiles and installs PHP, if you want Apache to support PHP, you need to specify the./configure step.--with-apxs2=/usr/local/apache2/bin/apxsMod_php5/apxs tells the compiler to provide parsing of PHP5 through Apache.
And in the final step of make install we will see the dynamic link library libphp5.so (Apache module) copied to the modules directory of the APACHE2 installation directory, and also need toAdd the LoadModule statement to the httpd.conf configuration file to dynamically libphp5.soThe module is loaded in to enable Apache support for PHP.

1) Because this mode is too classic, so here about the installation section is not ready to detail, relatively simple.

2) The reason why this is still listed here is APACHE+MOD_PHP5 to discuss because:
Read the previous article, we know that Nginx generally includes two purposes httpserver and reverse proxy server (reverse proxy).

We introduce how to deploy Nginx as reverse proxy server on the front end, and the backend to decorate multiple Apache to implement the cluster system server cluster architecture.
Soin actual production, we can still retain APACHE+MOD_PHP5 's classic app server, and only use Nginx as the front-end reverse proxy server for proxy and load balancing. ? Therefore, it is recommended that nginx (one or more) + multiple Apache architectures continue to be used.

2. NGINX+PHP-FPM:?

1) through the above analysis, although we can still retain apache+mod_php to deal with PHP, all static files and load balancing by the top of the front-end nginx to complete, butdue to the advantages of Nginx and PHP-FPM, the performance of NGINX+PHP-FPM combination has surpassed apache+mod_php. ?
As a result, many people are gradually abandoning the apache+mod_php combination, and fully using NGINX+PHP-FPM to implement PHP processing.
So now there's a new noun calledLEMP (Linux+enginex (nginx) +mysql+php), slowly replace the classic for many years lamp?。

2) There is even a new server cluster:?
It does not see the shadow of Apache, all by Nginx to fix. Nginx lightweight, high-performance, high-flexibility makes it fully capable of handling.
Because the PHP-FPM is the C/s structure, so we keep nginx to do load balancing, for the previous back-end of the various Apache servers, we do not need to install Apache, the PHP installation to PHP-FPM to support the fastcgi mode;
Then in Nginx configure the client's PHP request to pass to the background of the multiple running PHP-FPM, the latter processing and then returned to Nginx, and then displayed to the user. The whole process can not be completely apache.

3) Below we specifically describe how to install and simply configure?
Nginx+php+php-fpm+mysql.

3. Install and configure Nginx+php+php-fpm+mysql:?

1) Install MySQL:?

The first reason to install MySQL here is because you can specify MySQL support directly after compiling and installing PHP.
We know that PHP support for MySQL is implemented via PHP extensions.
The source code can be installed, but I used Ubuntu, which was installed directly using its released binary package:

$sudo Apt-get Install Mysql-server

Install the need to prompt to set the root password;
After use
$netstat-tap |grep MySQL
See if it works properly;

2) Install PHP and PHP-FPM:?
We described earlier that PHP-FPM is a patch to PHP, so we need to compile the installation with PHP. I use it here.PHP 5.2.10?。
A. Download the installation package:?
Fromphp.net? Download: php-5.2.10.tar.gz
Download from PHP-FPM official website: php-5.2.10-fpm-0.5.13.diff.gz
Note that the two versions are the same as possible (different may be wrong, I haven't tried them myself).

B. Decompression patching?
$tar XZVF php-5.2.10.tar.gz
$gzip-CD php-5.2.10-fpm-0.5.13.diff.gz | Patch-d PHP-5.2.10-P1
If you need any command in the middle that the shell doesn't know, you can use Apt-get to install it, or Google to find the answer.

c. Configuring the compilation Environment:?
You may need to install several dependent packages before installing:
sudo apt-get install Libxml2-dev
sudo apt-get install Libmysqlclient15-dev
Not install can also, after./configure failure, according to the error message, and then slowly search the installation of the dependency package can also, it is important to write down the key steps, because everyone's system is not installed anything is not necessarily.

$CD php-5.2.10
$./configure--prefix=/usr/local/php--enable-fastcgi--enable-fpm--with-mysql--with-mysqli--with-openssl

Here we configure the PHP installation to/usr/local/php, if not configured by default installation to/usr/local, so I feel not very good, so that make install each file will be copied to separate (scattered in the local directory), If we later want to uninstall clean and can not use make uninstall, it is not convenient. Install to/usr/local/php, if we want to delete PHP, delete the directory directly.

--enable-fastcgi and--ENABLE-FPM respectively set options to support fastcgi and PHP-FPM;
--with-mysql and--with-mysqli are equivalent to compiling PHP's MySQL extension into the PHP kernel so that we can access MySQL in PHP using the functions of MySQL and mysqli libraries;

Note: One of the issues to note here is that do not set--WITH-APXS2=/USR/LOCAL/APACHE2/BIN/APXS, we know that it is telling PHP to compile into a modular way for Apache to support. If this option is set, Apache will not start after compiling the installation, error message:?
/usr/lib/apache2/modules/libphp5.so:undefined symbol:-fpm-event-base-free

So it also means that we compiled PHP to support fastcgi in a php-fpm way, basically not with Apache, which means we decided to use NGINX+PHP+PHP-FPM. PHP cannot be used with Apache.

If you do not want to use it, you can compile and install a PHP, compile the time in the./configure setting--WITH-APXS2=/USR/LOCAL/APACHE2/BIN/APXS, and do not hit PHP-FPM patch.

Also, if there is an error in the step, usually a lack of dependent packages, follow the error message to install the dependent package.

d. Compiling:?
$make all?
Be careful to use make all as much as possible, not just make

E. Installation:?
$make Install

F. Copy the php.ini file:?
$sudo CP Php.ini-dist/usr/local/php/lib/php.ini
Copy the php.ini file to the top position;

If the installation is successful, ourPHP that supports fastcgi in PHP-FPM mode is installed in the/usr/local/php directory. ?


3) Configure PHP and PHP-FPM:
?

First, you can use the/usr/local/php/bin directory to perform a php-v to see if PHP work.

A. Configure php.ini:?
Located under/usr/local/php/lib
There is generally no strict need to configure what, can be configured according to their own requirements.

B. Configure PHP-FPM This PHP parser:?
As we said above, the PHP-FPM parser is the C/s structure, and its configuration file is located in/usr/local/php/etc/php-fpm.conf.
$CD/usr/local/php/etc
$sudo VI php-fpm.conf
The file is an XML file that needs to be modified only:
??? Unix User of processes
??? Www-data
??? Unix Group of processes
??? Www-data
Pay attention to removing the annotations on both sides , or php-fpm can not be started after;

c. After the configuration is complete, you can start php-fpm:?
$/USR/LOCAL/PHP/SBIN/PHP-FPM start

We described above the fastcgi mode is different from the CGI mode, it needs a daemon process has been running in the background to the PHP request to parse, here php-fpm is the daemon process, In profile php-fpm.conf, you can set the IP and port it listens on, and the default is 127.0.0.1:9000. That is, it listens on a data request for Port 9000, and then parses it and returns it to the requester side.

This is consistent with the thought of fastcgi we introduced earlier. The Httpserver server is separated from the PHP parser in fastcgi mode (this is PHP-FPM), and Httpserver is sent to PHP-FPM when it encounters a PHP request, which parses and returns. The implementation of the Httpserver and PHP parser is completely decoupled, easing the burden on the server, and the server has more resources to handle concurrent requests. In fact, this is a reason that Nginx is superior to Apache.

d. Check if the PHP-FPM is functioning properly:?
$ps Ax|grep FPM

4) Install and configure Nginx:?

Previous articles we introduced Nginx installation and use Nginx as Reverser server load balancer configuration, interested can see.

A. Nginx installation is simple:?
Download the installation package from the official website: nginx-0.7.61.tar.gz

$tar XZVF nginx-0.7.61.tar.gz
$CD nginx-0.7.61
$./configure?
The default installation path is/usr/local/nginx, if you do not rest assured that you can use--prefix=/usr/local/nginx configuration
$make
$sudo make Install

B. Thought:?

Our previous article introduced the use of Nginx is very flexible, some people liken it to the server field of Swiss Army knife, in fact, it is: good performance, and the use of many methods.
Various usage methods are implemented through configuration files, soMastering the use of Nginx, in addition to mastering the ideas of various architectures, but also to master how to configure the nginx.conf. ?

Here we focus on the nginx.conf configuration, the implementation of PHP-FPM through the fastcgi of PHP processing.in fact, Nginx itself does not parse the PHP, this is different from Apache? (Apache through the built-in module implementation of the PHP parsing), Nginx is actually the PHP page request to the background in127.0.0.1:9000Listen for PHP-FPM, which has the ability to parse PHP.

so if the PHP-FPM as an app server, in fact, Nginx here is a reverse proxy server. And we previously described the use of the location configuration of PHP request Proxypass to the background to listen to the Apache server, almost the same mentality. ?


C. Configuring nginx.conf and Fastcgi.params in the/usr/local/nginx/conf directory
?

>nginx.conf Configuration:?
$CD/usr/local/nginx/conf
$sudo VI nginx.conf
Modify the default configuration file from the top down:

??? 1. User? Www-data; This needs to be consistent with the user defined in PHP-FPM;

??? 2. Worker_processes 2; Can you set more, this option and after the worker_connections? 1024;
??? ??? To define the corresponding maximum number of connections per process, so that 2*1024 concurrent requests can be reached;

??? 3. In Server {
??????? Listen?????? 8080;
??? ??? If you have already installed Apache and occupied 80 ports, this is changed to another 8080, is responsible for boot not;

??? 4. As mentioned above, we actually set Nginx to forward the PHP request to the backend PHP-FPM server, which has the ability to parse PHP.
??? In fact, the role of acting as a reverse proxy;
??? ??? # Pass the PHP scripts to FastCGI server listening on?127.0.0.1:9000?
??????? #
??????? Location ~ \.php$ {
??????????? Root?????????? html
??????????? Fastcgi_pass???127.0.0.1:9000?;
??????????? Fastcgi_index? index.php;
??????????? Fastcgi_param? Script_filename? html/$fastcgi _script_name;
??????????? Include??????? Fastcgi_params;
??????? }
????Notice Fastcgi_param? Script_filename? html/$fastcgi _script_name;

??? Then you need to set thewhere to place the PHP script?, here weFor example, create a phpinfo.php file under the/usr/local/nginx/html directory?
??? Contains code

$sudo VI Fastcgi.params

Configure the fastcgi parameter file to refer toHttp://wiki.nginx.org/NginxFcgiExample?
You can basically use the default file, and you don't need to modify it.

5) Run Nginx:

$sudo/usr/local/nginx/sbin/nginx

And then view it in the browserhttp://localhost?

> The index.html page in the/usr/local/nginx/html directory is displayed by default: Welcome to nginx!

> Then viewhttp://localhost/phpinfo.php?, equivalent to accessing the phpinfo.php page in the HTML directory,

If normal, the Phpinfo page is displayed. You can see that the server API contains: cgi/fastcgi, which means that the FastCGI is running.

If the above steps are wrong, usually because the nginx.conf configuration is not correct, Google can find a solution, generally can be found (English). Then re-modify the nginx.conf file.

After that, you need to restart Nginx to perform:

$sudo kill ' cat/usr/local/nginx/logs/nginx.pid ' means to close nginx
$sudo/usr/local/nginx/sbin/nginx to start the Nginx again.

6) Set the boot from:
?

Under Ubuntu, if you want to add to the/ETC/INIT.D implementation restart, you can Google to find Nginx and php-fpm init script (PHP-FPM itself is the init script does not need to find), and then copied to the/etc/ The INIT.D directory.

a simple way to set rc.local:

$sudo vi/etc/rc.local

Add before exit 0:
/USR/LOCAL/PHP/SBIN/PHP-FPM start
/usr/local/nginx/sbin/nginx

This boot automatically starts Nginx and PHP-FPM.

7) using Nginx and PHP-FPM to implement server? Cluster:?

and Nginx for more than one application Server Agent load balancing similar, we can implement Nginx to multiple PHP-FPM to achieve load balancing:

T
? o Configure Nginx to load balance multiple FASTCGI servers use this type of configuration:

??? Upstream Fastcgiservers {
??? ??? Server?127.0.0.1:9000?;
??? ??? Server?127.0.0.1:9001?;
??? ??? Server?198.192.0.1:9000?;
??? ??? Server?198.192.0.2:9000?;
??? ??? Server?198.192.0.3:9000?;
??? }

??? Location ~ \.php$ {
??? Fastcgi_pass fastcgiservers;
??? Fastcgi_index Stream.app;
??? Fastcgi_param Script_filename/var/www/htdocs$fastcgi_script_name;
??? include/etc/nginx/fastcgi.conf;

??? }

????
4. Summary:?

three common modes:?
apache+mod_php5;?
lightppd+spawn-fcgi;?
NGINX+PHP-FPM
?

we can use it in a production environment:?

0) If not server cluster:
You can use either of these, but there are a variety of tests that showNGINX+PHP-FPM performance is superior, but APACHE+MOD_PHP5 is a classic module, such as the support of. htaccess. ?

If you build server cluster:
1) Nginx as a reverse proxy server, backstage more than one apache+mod_php5. ?
Nginx processing static files, and PHP concurrent requests to the background of multiple app server load balancing;

2) Nginx as a reverse proxy, backstage multiple php-fpm?
Nginx processing static files and send PHP concurrent requests to the background php-fpm to parse;


In addition: How to better use nginx this lightweight high-performance Swiss Army knife, mainly how to configure nginx.conf, more see:
Http://wiki.nginx.org/Main?
In addition, the various caches about PHP support are not installed here, interested can be installed separately.


More resources:
http://www.php.net/manual/en/install.unix.apache2.php?
http://www.softwareprojects.com/resources/programming/t-installing-nginx-web-server-w-php-and-ssl-1474.html?
Http://php-fpm.org/Main_Page?
http://www.softwareprojects.com/resources/programming/t-how-to-install-php-fpm-spawn-fcgi-replacement-1602.html?
Http://wiki.nginx.org/NginxFcgiExample?
It is possible that PHP-FPM will be added directly to the PHP kernel for publishing in the future
Would there be a PHP-FPM was included in the official PHP?
Http://php-fpm.org/FAQ#Will_there_be_a_PHP-FPM_is_included_in_the_official_PHP.3F?

http://bookmarks.honewatson.com/2008/04/24/multiple-fastcgi-php-servers-nginx-load-balancing/?
Http://www.wikivs.com/wiki/Lighttpd_vs_nginx?
Http://en.wikipedia.org/wiki/Reverse_proxy?
http://sameerparwani.com/posts/nginx-as-a-front-end-to-apache/?
http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/?
http://www.yawn.it/2008/04/30/nginx-php-php-fpm-on-debian-etch-40/?
http://howtoforge.org/installing-nginx-with-php5-and-mysql-support-on-ubuntu-8.10

  • 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.