Linux-Installs the MOD_PHP5 module, implements the Apache to the PHP support, this thing how to understand??

Source: Internet
Author: User
Tags apache php php server sapi unix domain socket
Doesn't PHP have its own parser?
Apache simply corresponds the URL request to the local server's disk file and finds that the file is. PHP, which will be given to the PHP parser (this process is not related to Apache for the time being)
The Apache User (Www-data) needs to have Read permission on the file and then the PHP parser executes the script.
Hand over the finished result (DOM document) to Apache,
The Apche then returns the DOM document as-is (possibly with some response headers, and so on) back to the client browser
The client browser receives the DOM document, loading the JS engine line-by-row parsing, encountering img These special tags will send the request to the server again ...

I understand that Apache is just responsible for passing the assignment? Please point out that I understand the wrong place

Reply content:

Doesn't PHP have its own parser?
Apache simply corresponds the URL request to the local server's disk file and finds that the file is. PHP, which will be given to the PHP parser (this process is not related to Apache for the time being)
The Apache User (Www-data) needs to have Read permission on the file and then the PHP parser executes the script.
Hand over the finished result (DOM document) to Apache,
The Apche then returns the DOM document as-is (possibly with some response headers, and so on) back to the client browser
The client browser receives the DOM document, loading the JS engine line-by-row parsing, encountering img These special tags will send the request to the server again ...

I understand that Apache is just responsible for passing the assignment? Please point out that I understand the wrong place

From a macro point of view, the implementation of the PHP kernel is like most of the world's programs, receive input data, do the corresponding processing and then output (return) results. The code we write is the input data that PHP receives, and the PHP kernel interprets and evaluates the code we write, and finally returns the result of the operation.

This problem is understood in terms of "PHP internal life cycle" and "Sapi Interface":
Take a look at the picture first:

As you can see from the diagram, PHP is a 4-tier system from bottom to top:

Zend Engine: Zend as a whole with pure C implementation, is the core part of PHP, it will be PHP code translation (lexical, parsing and so on a series of compilation process) for the execution of opcode processing and implementation of the corresponding processing methods, the implementation of basic data structures (such as Hashtable, OO), Memory allocation and management, providing the corresponding API method for external invocation, is the core of all, all the peripheral functions are around Zend implementation.

Extensions: Around the Zend Engine, Extensions provides a variety of basic services through a component-based approach, and our common set of built-in functions (such as the array series), standard libraries, etc. are implemented through extension. Users can also implement their own extension to achieve functional expansion, performance optimization and other purposes (such as paste in the PHP middle layer, Rich text parsing is the typical application of extension).

SAPI:SAPI full name is the server application programming Interface, that is, the service-side application programming interface, SAPI through a series of hook functions, so that PHP can interact with the peripheral data, which is a very elegant and successful PHP design, By SAPI successfully decoupling PHP itself from the upper-layer application, PHP can no longer consider how to implement compatibility for different applications, and the application itself can be handled differently for its own characteristics.

Upper application: This is the PHP program we usually write, through different sapi ways to get a variety of application patterns, such as the implementation of Web applications through webserver, the command line to run scripts, and so on.

SAPI through a series of interfaces, so that external applications can exchange data with PHP and can be based on different application characteristics to achieve specific processing methods, some of our common SAPI are:

    • Apache2handler: This is the use of Apache as a webserver, using the mod_php mode of operation of the process, is now the most widely used.

    • CGI: This is another direct interaction between Webserver and PHP, known as the FASTCGI protocol, which has been fastcgi+php more and more recently this year, and is the only way to support asynchronous webserver.

    • CLI: Application Mode for command line invocation

Apache is an open source Web server of the Apache Software Foundation, which can run on most computer operating systems, and is one of the most popular Web server-side software because of its widespread use across platforms and security. Apache supports many features, most of which are implemented through module extensions. Common modules include Mod_auth (permission validation), MOD_SSL (SSL and TLS support) mod_rewrite (URL rewriting), and so on. Some common languages also support integration with Apache in the Apache module. such as PERL,PYTHON,TCL, and PHP and so on.

When PHP needs to be run under the Apache server, in general, it can be integrated in the form of MOD_PHP5 modules, at which point the MOD_PHP5 module is to receive PHP file requests from Apache, process the requests, and then return the processed results to Apache. If we configure the PHP module (MOD_PHP5) in its configuration file before Apache starts, the PHP module registers the apache2 ap_hook_post_config hook and starts the module to accept PHP file requests when Apache starts.

First look at some of the Apache PHP configuration httpd.conf:

LoadModule php5_module modules/libphp5.soAddHandler application/x-httpd-php .php

Libphp5.so/libphp7.so as a module of Apache module,php interpreter is working within the Apache process (running user is the same), Apache will put the. php suffix file to the in-process PHP module processing, and return the results to the browser. In other words, Apache loaded PHP module is a PHP runtime container, they are a whole, is coupled, so you must compile Apache, in order to compile PHP with parameters to --with-apxs2=/png/httpd/2.4/bin/apxs specify the Apache module build script, Tell Apache I want to build a PHP module.

For Nginx and php-fpm this mode, the landlord needs to take into account the different permissions of the user to run different issues, because Nginx and PHP-FPM is separate, the two are different processes, through TCP or UNIX Domain socket communication, However, the general Nginx and PHP-FPM are set to the same running user, so that both Nginx and PHP-FPM can have permission to operate the same file. Also php-fpm can be seen as a PHP container, because the PHP-FPM process is also built into the PHP interpreter, Because it does not rely on the command line of PHP, and does not rely on php-cgi, the difference with Apache is that PHP-FPM is a fastcgi service, and Apache is an HTTP service, Apache can directly communicate with the browser, PHP-FPM, however, needs to interact with the browser via Nginx.

Off Topic:
PHP has built an HTTP server from 5.4, and official targeting is for development and testing because the HTTP server is not a common standard HTTP server, such as it does not support HTTPS, does not support directory listings, and a single-process architecture cannot take advantage of multicore. In addition to development, I also found an interesting application scenario, since PHP has built-in HTTP server and SQLite database, it can be used as a home IoT server, so I cross-compiled on Ubuntu for Android and Raspberry Pi (Raspbian) PHP, Then write an app in Java, invoke the command line to start PHP HTTP server, and then open webview access to the local PHP server, local PHP is responsible for operating the file system and network, WebView for human-computer interaction, although not to call the Java API on Android, But I can do a lot of things. The app is already running on my Xiaomi 4.

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