Several PHP server architectures are strongly recommended!

Source: Internet
Author: User
Tags php mysql
Several PHP server architectures are strongly recommended !!! I found a good article and introduced in detail several architectures of the current PHP Server. I specially reprinted it. I strongly recommend it !!? Nginx + PHP + PHP-FPM (FastCGI) on Ubuntu installation and configuration reprint please indicate the original link: blog. csdn. netomohearchive20090737933 PHP server of several architectures, strongly recommended !!!

I found a good article and introduced in detail several architectures of the current PHP Server. I specially reprinted it and strongly recommended it !!


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

Reprinted please indicate the original link:

Version: v1.0? Author: OMO last Modification time: 2009.07.10

Knowledge review before 0:?

1) There are currently three types of Server HTTP Server support for PHP:?

A. It is implemented through the built-in modules of HTTPServer,

For example, Apache's mod_php5, similar to Apache's built-in mod_perl, can support perl;

B. through CGI, this is like the previous perl CGI. the disadvantage of this method is poor performance, because every time the server encounters these scripts, it needs to restart the script parser to execute the script and return the result to the server. on the other hand, it is not safe. this is rarely used.

C. The latest one is FastCGI. FastCGI is an improvement on CGI. It generally uses a C/S structure. Generally, the script processor starts one or more daemon processes. each time the HTTPServer encounters a script, it is directly delivered to the FastCGI process for execution, then return the result (usually html) to the browser.

> A small problem with this method is that the daemon process of the script processor may overload the daemon process in case of frequent requests with large traffic volumes, resulting in slow performance or even memory leakage;

> However, the advantage of comparing the built-in modules of Apache is that the Server and the script parser are completely independent of each other, so the Server is no longer bloated, you can concentrate on static file response or return the results of the dynamic script parser to the user client. Therefore, compared with the built-in modules of Apache, the performance sometimes needs to be improved a lot. Some tests may reach 5 ~ of Apache + mod_php ~ 10 times.

2) FastCGI is commonly used in two types of stack: ligthttpd + spawn-fcgi ;? Another is nginx + PHP-FPM (can also use spawn-fcgi )?.?

A. As mentioned above, both of these structures use FastCGI to support PHP. Therefore, HTTPServer is completely freed up to better respond to and concurrently process requests. Therefore, both lighttpd and nginx have the reputation of small, but powerful and efficient.

B. the two can also be considered as good or bad. since spawn-fcgi is part of lighttpd, lighttpd is installed and generally supports php using spawn-fcgi, however, some users say that when the spwan-fcgi of ligttpd is accessed in high concurrency, the memory leakage mentioned above may even automatically restart fastcgi. That is, if the PHP script processor is on the machine and the user accesses it at this time, there may be white pages (that is, PHP cannot be parsed or error ).

Another: nginx does not have lighttpd including fastcgi (spawn-fcgi). Therefore, it is completely lightweight and can be parsed using a third-party FastCGI processor, as a result, nginx is very flexible. it can be connected to any third-party processor for parsing to achieve PHP parsing (in nginx. conf ).

Nginx can use spwan-fcgi (lighttpd needs to be installed together, but the Port needs to be avoided for nginx. some earlier blogs have Installation Tutorials in this regard ), but since spawn-fcgi has the defects gradually discovered by users described above, it is now slowly reducing the use of nginx + spawn-fcgi combinations.

C. due to spawn-fcgi defects, a new third-party PHP FastCGI processor (currently, I heard it is working hard to join PHP core in the near future) has emerged, is called PHP-FPM (can google ). Compared with spawn-fcgi, it has the following advantages:

Because it is developed as a PHP patch, it needs to be compiled together with the php source code during installation, that is, compiled into the php core, so it should be better in terms of performance;
At the same time, it is superior to spawn-fcgi in terms of processing high concurrency, at least it will not automatically restart the fastcgi processor. Google can understand the specific algorithms and designs used.

Therefore, as mentioned above, due to the lightweight and flexible nature of nginx, the current performance is superior, and more people are gradually using this combination:Nginx + PHP/PHP-FPM?.

3) summary:?
Currently, three types of stacks are quite popular in HTTPServer:?

> Apache + mod_php5
> Lighttp + spawn-fcgi
> Nginx + PHP-FPM

The performance may be slightly better between the two, but Apache is still the boss because of its rich modules and functions. Some people test nginx + PHP-FPM in high concurrency may reach Apache + mod_php5 5 ~ 10 times, now more and more people use nginx + PHP-FPM.

The following describes the stack:
Install and configure Apache + mod_php5 and nginx + PHP-FPM. For lighttpd + spawn-fcgi, since I have never used it myself, I am not going to introduce it as follows. if you are interested, please refer to the relevant materials.

1. Apache + mod_php mode :?

We have been using the classic Apache + mod_php for a long time:?

Apache supports PHP through the Apache module. If you want Apache to support php after source code compilation and installation, you must specify-- With-apxs2 =/usr/local/apache2/bin/apxs? Indicates that the compiler uses mod_php5/apxs of Apache to parse PHP5;
In the last step of make install, we will see that the dynamic link library (Apache module) is copied to the modules Directory of the installation directory of apache2, and it also needs to be in httpd. add the LoadModule statement to the conf configuration file to dynamically change The module is loaded to implement Apache's support for php.

1) because this mode is too classic, it is relatively simple to describe the installation part here.

2) the reason for listing Apache + mod_php5 is as follows:
After reading the previous article, we know that nginx generally has two purposes: HTTPServer and Reverse Proxy Server (Reverse Proxy Server ).

We introduced how to deploy nginx on the front end as the reverse proxy server and deploy multiple Apache backend servers to implement the cluster architecture of the cluster system.
Therefore,In actual production, we can still retain the classic App Server of Apache + mod_php5, instead of using nginx as the front-end reverse proxy server to implement modern processing and load balancing.? Therefore, it is recommended that the 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 for PHP processing, all static files and server load balancer are completed by nginx at the front end,Due to the superiority of nginx and PHP-FPM, the performance of nginx + PHP-FPM combination has exceeded Apache + mod_php.?
Therefore, many people gradually give up the combination of Apache + mod_php, and use nginx + PHP-FPM to achieve PHP processing.
Therefore, the new term"LEMP (Linux + EngineX (nginx) + MySQL + PHP) will gradually replace the typical LAMP?.

2) There is even a new server cluster:?
No Apache shadow is visible, and nginx is responsible for all of them. Nginx is lightweight, high-performance, and highly flexible.
Since the PHP-FPM is a C/S structure, we reserve nginx at the front end for load balancing; for the various backend Apache servers, we do not need to install Apache, recompile and install PHP to make it support FastCGI in PHP-FPM;
Then configure in nginx to pass the client's php requests to multiple running PHP-FPM in the background, the latter is processed and then returned to nginx, and then displayed to the user. The whole process can be completely different from Apache.

3) The following describes how to install and configure simple configurations.?
Nginx + PHP + PHP-FPM + MySQL.

3. install and configure nginx + PHP + PHP-FPM + MySQL:?

1) install MySQL :?

MySQL must be installed first because MySQL support can be directly specified when PHP is compiled and installed later.
We know that PHP supports MySQL through PHP extension.
It can be installed in the source code, but I used Ubuntu to directly use its released binary package for installation:

$ Sudo apt-get install mysql-server

During installation, you must be prompted to set the root password;
Later use
$ Netstat-tap | grep mysql
Check whether it is running normally;

2) install PHP and PHP-FPM:?
We previously introduced that PHP-FPM is a patch for PHP, so it needs to be compiled and installed together with PHP. I am usingPHP 5.2.10?.
A. download the installation package:?
From 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 should be the same as possible (errors may occur if they are different. I have not tried them myself ).

B. decompress and install patches.?
$ 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 do not know which command shell is needed in the middle, you can use apt-get to install it or google to find the answer.

C. configure the compiling environment:?
Install several dependent packages before installation:
Sudo apt-get install libxml2-dev
Sudo apt-get install libmysqlclient15-dev
You can do this without installing it. /if configure fails, you can search and install the dependency package based on the error information. the important thing is to write down the key steps because everyone's system is not installed with anything.

$ Php-5.2.10 cd
$./Configure -- prefix =/usr/local/php -- enable-fastcgi -- enable-fpm -- with-mysql -- with-mysqli -- with-openssl

Here we configure php to install to/usr/local/php. if it is not configured to install to/usr/local by default, I think it is not very good, in this way, make install files will be copied to different directories (scattered in the local directory). if we want to uninstall the files and cannot use make uninstall, it is not convenient. Install it in/usr/local/php. if you want to delete php, simply delete the directory.

-- Enable-fastcgi and -- enable-fpm respectively set options that support fastcgi and PHP-FPM;
-- With-mysql and -- with-mysqli are equivalent to compiling php MySQL and extending it to the php kernel. in this way, we can use mysql and mysqli library functions in php to access mysql;

Note: One of the issues to be noted here is that don't set -- with-apxs2 =/usr/local/apache2/bin/apxs, we know that it tells PHP to compile it into a module to support Apache. If this option is set, Apache cannot be started after compilation and installation, and an error message is returned:?
/Usr/lib/apache2/modules/ undefined symbol:-fpm-event-base-free

So here it means that we compile PHP to PHP-FPM to support FastCGI, basically can not be used together with Apache, that is to say, we decided to use nginx + PHP + PHP-FPM, here, PHP cannot be used with Apache.

If you do not want to use it, you can compile and install a PHP file. /configure when setting -- with-apxs2 =/usr/local/apache2/bin/apxs, and do not apply PHP-FPM patches.

In addition, if an error occurs during this step, the dependency package is usually not available. follow the error message to install the dependency package.

D. Compile:?
$ Make all?
Make all, 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 above position;

If the installation is successfulPHP that supports FastCGI in PHP-FPM is installed in the/usr/local/php directory.?

3) configure PHP and PHP-FPM:

Run php-v in the/usr/local/php/bin directory to check whether PHP works.

A. configure php. ini:?
Generally, there is no strict configuration. you can configure it as required.

B. configure the PHP-FPM for this PHP parser:?
We have said that the PHP-FPM parser is a C/S structure and its configuration file is located in the/usr/local/php/etc/php-fpm.conf.
$ Cd/usr/local/php/etc
$ Sudo vi php-fpm.conf
This file is an xml file and only needs to be modified:
??? Unix user of processes
??? Www-data
??? Unix group of processes
??? Www-data
Remove the comments on both sides. Otherwise, php-fpm cannot start later;

C. After the configuration, you can start the PHP-FPM:?
$/Usr/local/php/sbin/php-fpm start

We introduced above FastCGI mode is different from CGI mode, it needs a daemon process has been running in the background to parse php requests, the PHP-FPM here is the daemon process, in the profile php-fpm.conf, you can set the IP and port it listens to, which defaults to 9000. That is, it listens to data requests on port 9000 and then parses the requests and returns them to the request end.

This is consistent with the FastCGI idea we introduced earlier. The HTTPServer server and the FastCGI mode PHP parser are separated (here is the PHP-FPM), when the HTTPServer encounters a PHP request, it will be passed to the PHP-FPM, the latter parses and returns. The full separation of the HTTPServer and PHP parser reduces the burden on the Server. the Server has more resources to process concurrent requests. In fact, this is one reason why nginx is better than apache.

D. check whether php-fpm is running properly:?
$ Ps ax | grep fpm

4) install and configure nginx:?

The previous article introduced how to install nginx and configure server load balancer using nginx as the reverser server. For more information, see.

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

$ Tar xzvf nginx-0.7.61.tar.gz
$ Nginx-0.7.61 cd
The default installation path is/usr/local/nginx. if you are not sure, you can use -- prefix =/usr/local/nginx to configure it.
$ Make
$ Sudo make install

B. thoughts:?

Our previous article introduced the flexible use of nginx. some people think of it as the Swiss Army knife in the server field. In fact, it is indeed: good performance and many usage methods.
Various usage methods are implemented through the configuration file, soIn addition to understanding the ideas of various architectures, you also need to know how to configure nginx. conf.?

Here we focus on nginx. conf configuration to implement php processing through the fastcgi of php-fpm.In fact, nginx itself does not parse PHP, which should be different from Apache? (Apache implements PHP parsing through the built-in module). nginx actually submits requests to php pages to the backend at 9000? The listening php-fpm, which has the ability to parse php.

Therefore, if you regard php-fpm as an app server, nginx serves as a reverse proxy server. The idea is almost the same as that of using location configuration to send the php request proxypass to the Apache server listening in the background.?

C. Configure 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 top to bottom:

??? 1. user? Www-data, which must be consistent with the user defined in php-fpm;

??? 2. worker_processes 2; you can set more options and worker_connections? 1024;
??? ??? Define the maximum number of concurrent connections for each process. Therefore, the number of concurrent requests can reach 2*1024;

??? 3. on server {
??????? Listen ?????? 8080;
??? ??? If you have installed Apache and used port 80, change it to another 8080;

??? 4. as described above, we can actually set nginx to forward PHP requests to the backend php-fpm server. The latter has the php parsing function.
??? In fact, it still acts as a reverse proxy;
??? ??? # Pass the PHP scripts to FastCGI server listening on? Wagner. 0.0.1: 9000?
??????? #
??????? Location ~ \. Php $ {
??????????? Root ?????????? Html;
??????????? Fastcgi_pass ??? Wagner. 0.0.1: 9000 ?;
??????????? Fastcgi_index? Index. php;
??????????? Fastcgi_param? SCRIPT_FILENAME? Html/$ fastcgi_script_name;
??????????? Include ??????? Fastcgi_params;
??????? }
????Note fastcgi_param? SCRIPT_FILENAME? Html/$ fastcgi_script_name;

??? You need to set itPlace the php script?, Here weFor example, you can create a phpinfo. php file in the/usr/local/nginx/html directory.?
??? Contains code

$ Sudo vi fastcgi. params

Configure fastcgi parameter file, specific can refer to the
You can use the default file without modifying it.

5) run nginx:

$ Sudo/usr/local/nginx/sbin/nginx

Then, check http: // localhost in the browser?

> By default, the index.html page under the/usr/local/nginx/htmldirectory is displayed: Welcome to Nginx!

> Then check http: // localhost/phpinfo. php ?, Access the phpinfo. php page in the html directory,

If it is normal, the phpinfo page is displayed. The Server API contains CGI/FastCGI, which indicates that the Server runs in FastCGI mode.

If an error occurs in the preceding steps, it is usually because the nginx. conf configuration is incorrect. you can search for a solution by google. Generally, you can find the solution ). Then modify the nginx. conf file again.

Restart nginx and run the following command:

$ Sudo kill 'cat/usr/local/nginx/logs/nginx. Pi' indicates disabling nginx
$ Sudo/usr/local/nginx/sbin/nginx restart nginx

6) set automatic startup:

In Ubuntu, if you want to add it to/etc/init. d. to restart the instance, search for the init script of nginx and php-fpm on Google (php-fpm itself is the init script that does not need to be searched) and copy it to/etc/init. d directory.

In a simple way, set rc. local:

$ Sudo vi/etc/rc. local

Add the following before exit 0:
/Usr/local/php/sbin/php-fpm start

In this way, nginx and php-fpm are automatically started at startup.

7) use nginx and php-fpm to implement server? Cluster:?

Similar to load balancing for multiple app server proxies in nginx, we can achieve load balancing for multiple php-fpm instances in nginx:

? O configure Nginx to load balance multiple FastCgi servers use this type of configuration:

??? Upstream fastcgiServers {
??? ??? Server? Wagner. 0.0.1: 9000 ?;
??? ??? Server? Wagner. 0.0.1: 9001 ?;
??? ??? Server? 9000 ?;
??? ??? Server? 9000 ?;
??? ??? Server? 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. conclusion:?

Three common modes:?
Apache + mod_php5 ;?
Lightppd + spawn-fcgi ;?
Nginx + PHP-FPM

We can use it in the production environment:?

0) if it is not a server cluster:
You can use either of the preceding methods, but various tests have shown thatNginx + PHP-FPM has superior performance, but Apache + mod_php5 has many classic modules, such as support for. htaccess.?

If you build a server cluster:
1) nginx acts as a reverse proxy server, with multiple Apache + mod_php5 backend servers.?
Nginx processes static files and balances the load of multiple app servers in the background for concurrent php requests;

2) nginx as a reverse proxy, multiple PHP-FPM at the background?
Nginx processes static files and sends concurrent php requests to the backend php-fpm for parsing;

In addition, for how to better use nginx, a lightweight and high-performance Swiss Army knife, how to configure nginx. conf, refer:
In addition, the various caches supported by PHP are not installed here. if you are interested, you can install them separately.

More references:
It is possible that the PHP-FPM will be directly added to the PHP kernel for release later
Will there be a PHP-FPM is supported in the official PHP?


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