MPM optimizes your Apache

Source: Internet
Author: User
MPM optimizes your Apache-Linux Enterprise Application-Linux server application information. The following is a detailed description. Preface:
Recently, my friend's server traffic was too high, leading to slow page opening and downloading speed reduced to 30-40 KB/second. Due to funding and environmental problems, the cluster solution was not applied. Taking the above factors into consideration, I decided to optimize it by adding modules to Apache. below is my implementation process. If you are interested in it, take a look.

Body:
The performance improvement of Apache 2.0 is the most attractive. On Unix systems that support POSIX Threads, Apache can use different MPM to run in a mix of multi-process and multi-thread modes to enhance the scalability of some configurations. Compared with Apache 1.3, version 2.0 has made a lot of Optimizations to improve processing capabilities and scalability, and most of the improvements will take effect by default. However, at the time of compilation and running, 2.0 also has many options that can significantly improve the performance.

MPM (Multi-Processing Modules, Multi-channel Processing module) is the core feature that affects performance in Apache2.0.

It is no exaggeration to say that the introduction of MPM is the most important change in Apache 2.0. As you know, Apache is based on modular design, while Apache 2.0 extends the most basic functions of modular design to Web servers. The server is loaded with a multi-channel processing module, which is responsible for binding the network port of the Local Machine, accepting requests, and scheduling sub-processes to process requests. Extended modular design has two important benefits:

◆ Apache supports multiple operating systems in a more concise and effective manner;

◆ Servers can be customized according to the special needs of the site.

At the user level, MPM looks very similar to other Apache modules. The main difference is that only one MPM can be loaded to the server at any time.

The following uses Linux RedHat AS3 as a platform to demonstrate how to specify MPM in Apache 2.0.

# Wget http://archive.apache.org /...
# Tar jxvf httpd-2.0.52.tar.bz2
# Cd httpd-2.0.52
#./Configure -- help | grep mpm

Display: -- with-mpm = MPM Choose the process model for Apache to use. MPM = {beos | worker | prefork | mpmt_os2 | perchild | leader | threadpool}

The preceding operation is used to select the MPM module of the process model to be used. Beos and mpmt_os2 are the default MPM on BeOS and OS/2 respectively. perchild is designed to run different sub-processes with different user and group identities. This is especially useful when running multiple virtual hosts that require CGI, and it is better than the SuExec mechanism in version 1.3. The leader and threadpool are both worker-based variants and are still in the experimental stage. In some cases, they do not work as expected. Therefore, Apache is not officially recommended. Therefore, we mainly describe the product-level MPM with the highest performance relationship between prefork and worker.

How prefork works
If "-- with-mpm" is not used to explicitly specify an MPM, prefork is the default MPM on Unix platforms. It adopts the pre-school child process mode, which is also used in Apache 1.3. Prefork itself does not use threads. Version 2.0 uses it to maintain compatibility with version 1.3. On the other hand, prefork uses a separate sub-process to process different requests, processes are independent from each other, which makes them one of the most stable MPM.
The working principle of prefork is that after the control process initially creates a "StartServers" sub-process, it creates a process to meet the needs of the MinSpareServers settings, waits for one second, creates two more, and then waits for one second, create four more ...... In this way, the number of Created processes is increased exponentially, up to 32 processes per second until the value set by MinSpareServers is met. This is the origin of prefork. This mode eliminates the need to generate new processes when requests arrive, thus reducing system overhead and increasing performance.

Working principle of worker
Compared with prefork, worker is a brand new MPM that supports multi-thread and multi-process hybrid models in version 2.0. Because threads are used for processing, a relatively large number of requests can be processed, and the overhead of system resources is smaller than that of process-based servers. However, worker also uses multiple processes, and each process generates multiple threads to achieve stability based on the process server. This kind of MPM will be the trend of Apache 2.0.
The working principle of worker is that the main control process generates a "StartServers" subprocess. Each subprocess contains a fixed number of ThreadsPerChild threads, and each thread processes Requests independently. Similarly, in order not to generate a thread when the request arrives, MinSpareThreads and MaxSpareThreads set the minimum and maximum number of Idle threads, while MaxClients sets the total number of threads in all sub-processes. If the total number of threads in the existing sub-process cannot meet the load, the control process will derive a new sub-process.

# Compile and install in worker Mode
#. /Configure -- prefix =/usr/local/apache -- with-mpm = worker -- enable-so (enable it to support the DSO function, so that the module can be dynamically loaded later) -- enable-rewrite
# Make
# Make install
# Cd/usr/local/apache/conf
# Vi httpd. conf
StartServers 2 MaxClients 150 ServerLimit 25 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 25 ThreadsPerChild 25 MaxRequestsPerChild 0

In Worker mode, the total number of requests that can be processed simultaneously is determined by the total number of sub-processes multiplied by the value of ThreadsPerChild. The value must be greater than or equal to MaxClients. If the load is large and the number of existing sub-processes cannot meet the requirements, the control process will derive a new sub-process. The maximum number of sub-processes by default is 16. You must explicitly declare ServerLimit when increasing the number (the maximum value is 20000)

Note that if ServerLimit is explicitly declared, the value multiplied by ThreadsPerChild must be greater than or equal to MaxClients, and MaxClients must be an integer multiple of ThreadsPerChild, otherwise, Apache automatically adjusts to a value (which may be an unexpected value ). The following is the author's worker configuration section:
StartServers 3 MaxClients 2000 ServerLimit 25 MinSpareThreads 50 MaxSpareThreads 200 ThreadLimit 200 ThreadsPerChild 100 MaxRequestsPerChild 0
# Save and exit.
#/Usr/local/apache/bin/apachectl start
# You can configure Apache-related core parameters based on actual conditions to achieve maximum performance and stability.

Ii. Limit the number of concurrent Apache connections
We know that when a website provides software download via http, if each user enables multiple threads without bandwidth restrictions, the maximum number of connections over http will soon be reached or the network will be blocked, this makes many normal services of the website unavailable. Next we will add the mod_limitipconn module to control the number of concurrent http connections.

# Wget http://dominia.org/djao/li...
# Tar zxvf mod_limitipconn-0.22.tar.gz
# Cd mod_limitipconn-0.22
#/Usr/local/apache/bin/apxs-c-I-a mod_limitipconn.c
# After compilation, mod_rewrite.so is automatically copied to/usr/local/apache/modules and your httpd. conf file is modified.
# Vi/usr/local/apache/conf/httpd. conf
# Add # The restricted directory to the last line. This indicates that the root directory of the host MaxConnPerIP 2 # The number of concurrent connections of each IP address is 2
# Save and exit.
#/Usr/local/apache/bin/apachectl start
# Let's test it with ant or express. 1:

As shown in the preceding figure, the configuration is successful.

3. Prevent file leeching
We have already limited the number of IP concurrency, but if the other party leeches the link to another page, what we just did is meaningless, because he can download it through ant financial or express. So in this case, we need to reference the mod_rewrite.so module. In this way, when a file is leeched, the page is directed to an error page that we have prepared in advance through the mod_rewrite.so module, thus preventing leeching.
#/Usr/local/apache/bin/apxs-c-I-a/opt/httpd-2.0.52/modules/mappers/mod_rewrite.c
# After compilation, mod_rewrite.so is automatically copied to/usr/local/apache/modules and your httpd. conf file is modified.
# Vi/usr/local/apache/conf/httpd. conf
RewriteEngine onRewriteCond % {HTTP_REFERER }! ^ Http://www.squall.cn/.*$ [NC] RewriteCond % {HTTP_REFERER }! ^ Http://www.squall.cn $ [NC] RewriteCond % {HTTP_REFERER }! ^ Http://squall.cn/.*$ [NC] RewriteCond % {HTTP_REFERER }! ^ Http://squall.cn $ [NC] RewriteRule. *. (jpg | gif | png | bmp | tar | gz | rar | zip | exe) $ http://www.squall.cn/error... [R, NC]
# Open a browser and test it. 2:

You can learn more through my animation demonstration.

At this point, we have made a comprehensive optimization of Apache, and the performance has significantly improved compared with the original. This implementation process has ended successfully. I believe you have some experience in Apache Optimization after reading this article. I believe that you will handle emergencies during your work.
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.