The difference between Nginx and Apache parsing php files

Source: Internet
Author: User
Tags fpm

First, how does Apache parse PHP files

We often say that the lamp architecture is Linux, Apache, MySQL, PHP, we know that any architecture or Web site without the support of the database, then how does PHP and Apache work together?


PHP is an Apache plug-in program, you must rely on the Web server to run. When the client browser triggers an event---The >php program submits to the Apache server----The PHP program is determined by the >apache server, and the application is extracted from memory or hard disk to access the required applications and submitted to the PHP engine program--- The >php engine program parses and reads the database to generate the corresponding page---the >php engine returns the HTML page to the Web server, and the Web server returns the page to the client.


Roughly this is the process of Apache and PHP interaction. Apache alone cannot handle PHP code, PHP (PHP application Server) can not exist alone, need to rely on Apache. It can also be said that PHP is an extension of the Apache function module, PHP as an Apache plug-in for parsing PHP code. The parsed Dynamic Web page does not exist at this time <?php?>. PHP was launched while the Apache service was started. This means that PHP must be installed on the same server as Apache and cannot be split. It is important to note that MySQL is not related to Apache, but is only accessed when it responds to SQL operations in PHP code. It does not need to be installed on the same server as Apache and can exist separately, just by connecting PHP remotely.

PHP is called by Apache as a dynamic, shared module and is automatically loaded when Apache is started.

Second, how does nginx parse php file?

To figure out how Nginx works with PHP, first look at the CGI and fastcgi two protocols. Cgi:common Gateway Interface is a protocol for Web server to interact with the background programming language. With this protocol, developers can use any language to process requests from the Web Server and dynamically generate content. But CGI has a fatal disadvantage, that is, every request to deal with a new process fork, with the rise of the Web, high concurrency is becoming the norm, so inefficient way obviously can not meet the demand. In this way, FastCGI was born, and CGI soon withdrew from the stage of history. FastCGI, as the name implies for faster CGI, which allows processing of multiple requests within a process, rather than simply ending the process with a request being processed, has a significant performance improvement.

PHP-CGI is PHP's own fastcgi manager, but there are a number of drawbacks. FPM is also a fastcgi implementation, compared to php-cgi has a lot of advantages. By the PHP5.3.3 has been integrated php-fpm, no longer a third-party package. PHP-FPM provides a better way to manage the PHP process, effectively controlling memory and processes, and smoothing the overloading of PHP configurations with only the./configure, plus--enable-fpm.

FPM is a PHP process manager that contains both master and worker processes: The master process has only one, is responsible for listening to the port, receives requests from the Web Server, and the worker process is typically multiple (the exact number is configured according to actual needs), each Inside the process, a PHP interpreter is embedded, which is where the PHP code really executes.


The request is received from the FPM , and the process is completed with the following specific procedures:

the 1.FPM master process receives the request

The 2.master process assigns a specific worker process based on configuration to request processing, and if no process is available, returns an error, which is what we encountered with Nginx 502 reasons for more errors.

3.worker Process processing request, if timeout, return 504 Error

4. Request processing end, return result


The flow of fpm from receiving to processing requests is that, so How does Nginx send the request to fpm ? This requires an Nginx level to illustrate.

We knowNginxnot just aWebserver, is also a powerfulProxyserver, in addition tohttpThe requested agent can also make many other protocol requests for the agent, including this article with the F pmrelated tofastcgiagreement. In order to makeNginxUnderstandingfastcgiAgreement,Nginxprovides afastcgimodule to addhttpthe request maps to the correspondingfastcgirequest.

the Nginx fastcgi module provides the fastcgi_param instruction to deal with these mappings primarily, In addition, it is very important that the fastcgi_pass instruction is used to specify the address that the FPM process listens to, and Nginx will translate all PHP requests into the FASTCGI request before sending it to this address.

Location ~ \.php$ {

Include Fastcgi_params;

Fastcgi_pass Unix:/tmp/www.sock;

#fastcgi_pass 127.0.0.1:9000;

Fastcgi_index index.php;

Fastcgi_param Script_filename/data/www$fastcgi_script_name;

}

/ tmp/www.sock   Yes listener socket nginx through location will all php http requests are given to FPM processing, implemented nginx php work together.

In other words,nginx is a proxy for fastcgi, using php in a fastcgi way . Therefore, the PHP-FPM service needs to be turned on .


This article is from the "94 Buns" blog, please be sure to keep this source http://cct0513.blog.51cto.com/12331798/1894491

The difference between Nginx and Apache parsing php files

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.