10.PHP Kernel Quest: Apache Run with hook function

Source: Internet
Author: User

Apache is currently the most widely used web Server in the world, and it is known for its cross-platform, efficiency and stability. According to last year's official statistics, the Apache server's installed capacity accounted for more than 60% of the market share. Especially on the X (unix/linux) platform, Apache is the most common choice. Other Web server products, such as IIS, can only be run on Windows platforms and are based on Microsoft. NET architecture technology is the choice.

Apache is not without shortcomings, it is the most criticized is becoming more and more heavy, is widely regarded as the heavyweight webserver. So, in recent years, there have been a lot of lightweight alternative products, such as Lighttpd,nginx, and so on, the advantages of these webserver is the operation of high efficiency, but the shortcomings are also obvious, maturity is often lower than Apache, usually only for some specific occasions.

Apache's Running process

Apache runs into start-up and run-time phases. During the start-up phase, Apache will start with a privileged user root (*nix system) or Super Administrator Administrator (Windows System) in order to gain maximum access to system resources, and the entire process is in a single-process single-threaded environment. This phase includes configuration file parsing (such as the http.conf file), module loading (such as Mod_php,mod_perl), and system resource initialization (such as log files, shared memory segments, database connections, and so on).

The Apache startup phase performs a number of initialization operations, and many of the slower or more expensive operations are concentrated at this stage to reduce the stress behind processing the request service.

During the run phase, Apache is primarily working to process User Service requests. At this stage, Apache abandons the privileged user level and uses normal permissions, which are primarily security-based considerations to prevent security breaches due to code flaws. Apache requests for HTTP can be divided into three large stages of connection, processing, and disconnection. At the same time can be divided into 11 small stages, in order: Post-read-request,uri Translation,header parsing,access control,authentication,authorization, MIME Type Checking,fixup,response,logging,cleanup

Apache Hook mechanism

Apache hook mechanism refers to: Apache allows the module (including internal modules and external modules, such as mod_php5.so,mod_perl.so, etc.) to inject the custom function into the request processing loop. In other words, the module can join the Apache request processing process by hooking up its own processing function at any one of Apache's processing stages.

Mod_php5.so/php5apache2.dll is the inclusion of the custom functions, through the hook mechanism into Apache, at all stages of the Apache processing process is responsible for processing PHP requests.

About hook mechanisms are also often encountered in Windows system development, where Windows develops both system-level hooks and application-level hooks. Common translation software (such as PowerWord, etc.) of the screen-word function, most of which is done by installing the system-level hook function, the custom function is replaced Gdi32.dll in the screen output of the drawing function.

The most important feature of the Apache server architecture is its highly modular design. If you are in pursuit of processing efficiency, you can put these DSO modules at the time of the Apache compile static link, this will improve the processing performance around Apache 5%.

Apache Request processing Loop

What has been done in the 11 phases of the Apache request processing cycle?

  1. Post-read-request stage. In the normal request processing process, this is the first stage in which a module can insert a hook. This phase can be exploited for modules that want to enter processing requests very early.
  2. URI translation phase. Apache's primary work at this stage is to map the requested URL to the local file system. The module can insert hooks at this stage to perform its own mapping logic. Mod_alias is using this phase to work.
  3. Header parsing stage. The main work of Apache at this stage: Check the header of the request. This hook is rarely used because the module can perform the task of inspecting the request header at any point in the request processing process. Mod_setenvif is using this phase to work.
  4. Access control phase. The main work of Apache at this stage is to check whether the requested resource is allowed to be accessed based on the configuration file. The standard logic of Apache implements the Allow and deny directives. Mod_authz_host is using this phase to work.
  5. Authentication stage. Apache's main work at this stage is to authenticate the user according to the policy set by the profile and set the user name area. The module can insert hooks at this stage to implement an authentication method.
  6. Authorization stage. The main work of Apache at this stage is to check whether authenticated users are allowed to perform the requested action based on the profile. The module can insert hooks at this stage to implement a user rights management approach.
  7. The MIME Type checking stage. The main work of Apache at this stage is to determine which content handler will be used, based on the rules of the MIME type of the requested resource. The standard modules mod_negotiation and mod_mime implement this hook.
  8. Fixup stage. This is a generic phase that allows the module to run any necessary process before the content generator. Similar to Post_read_request, this is a hook that captures any information and is the most commonly used hook.
  9. Response stage. The main work of Apache at this stage is to generate the content returned to the client, which is responsible for sending an appropriate reply to the client. This phase is a core part of the entire processing process.
  10. Logging stage. Apache's primary work at this stage is to log transactions after a reply has been sent to the client. The module may modify or replace the standard log records of Apache.
  11. Cleanup stage. Apache's main work at this stage is to clean up the environment left behind by the request transaction, such as file, directory processing, or socket closure, which is the last phase of Apache request processing.

10.PHP Kernel Quest: Apache Run with hook function

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.