7.PHP kernel Discovery: Apache module Introduction

Source: Internet
Author: User

Apache Overview

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

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 Component Logic diagram

Apache is based on the modular design, the overall appearance of the code is more readable than PHP code, its core code is not many, most of the functions are scattered into the various modules, the modules are loaded on demand when the system starts. If you want to read the Apache source code, it is recommended that you read directly from the Main.c file, the system's most important processing logic is contained inside.

MPM (multi-processing Modules, multiprocessing module) is one of the core components of Apache and Apache uses the resources of the operating system to manage processes and thread pools. Apache in order to get the best performance, for different platforms (Unix/linux, Window) has been optimized for different platforms to provide different MPM, users can choose according to the actual situation, the most commonly used MPM have prefork and worker two kinds. As to how your server is running, the default compilation parameters on the X system are prefork, depending on the MPM compilation parameters specified during the installation of Apache. Because most UNIX does not support true threads, the use of pre-derived subprocess (Prefork) methods, such as Windows or Solaris-supported threading platforms, is a good choice for multi-process multithreaded mixed worker patterns. Interested students can read the relevant information, there is no more to say here. An important component of Apache is the APR (Apache Portable Runtime Library), the Apache Portable runtime, which is an abstract library of operating system calls to implement the operating system used by Apache internal components. Improve the portability of the system. Apache's parsing of PHP is done through a number of module PHP module.

The logical composition of Apache and its relationship to the operating system

PHP vs Apache

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.

In addition to this boot-time loading, Apache modules can be loaded dynamically at runtime, which means that the server can be expanded without the need to re-compile the source code or even stop the server at all. All we need to do is send a signal to the server Hup or ap_sig_graceful notify the server to reload the module. But before we load it dynamically, we need to compile the module into a dynamic-link library. Dynamic loading at this time is the loading of the dynamic link library. The processing of the dynamic link library in Apache is done through the module Mod_so, so the Mod_so module cannot be dynamically loaded, it can only be statically compiled into Apache core. This means that it was launched with Apache.

How does Apache load the module? Let's take the MOD_PHP5 module mentioned earlier as an example. First we need to add a line to the Apache configuration file httpd.conf:

1 LoadModule php5_module modules/mod_php5.so

Here we use the LoadModule command, the first parameter of the command is the name of the module, the name can be found in the module implementation of the source code. The second option is the path where the module is located. If you need to load the module while the server is running, you can send the signal hup or ap_sig_graceful to the server, and once the signal is accepted, Apache will reload the module without restarting the server.

After adding the instructions shown in the configuration file, Apache will find the module and load it according to the module name when the module is loaded, and for each module, Apache must ensure that its file name starts with "Mod_", such as PHP mod_php5.c. If the naming format is not correct, Apache will consider this module illegal. Each of the Apache modules is in the form of a module structure, the name of the module structure in the last is through the macro Standard20_module_stuff to __file__. This can be seen later in the introduction of the MOD_PHP5 module. This also determines that our file name and module name are the same. After finding the relevant dynamic link library file through the path specified in the previous instruction, Apache obtains the contents of the dynamic link library through the internal function and loads the contents of the module into the specified variable in memory.

Before actually activating the module, Apache checks whether the loaded module is a real Apache module, which is implemented by checking the Magic field in the module structure. The Magic field is represented by the macro Standard20_module_stuff, in which the value of the magic is Module_magic_cookie, and the Module_magic_cookie is defined as follows:

1 #define MODULE_MAGIC_COOKIE 0x41503232UL /* "AP22" */

Finally, Apache invokes the relevant function (Ap_add_loaded_module) to activate the module, where it is activated by placing the module in the corresponding list (ap_top_modules linked list: Ap_top_ The modules list is used to save all active modules in Apache, including the default activation module and the active third-party module. )

7.PHP kernel Discovery: Apache module Introduction

Related Article

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.