Optimize the performance of the Apache server

Source: Internet
Author: User
Tags dedicated server website performance
Article Title: optimizing the performance of Apache servers. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

By cutting out the potential of the Apache server, you can make a big difference to your website, especially for some dynamic websites. This article focuses on how to optimize the installation and configuration of the Apache server, which is also the most controllable part.

Test and improve performance

The Apache server has been designed as fast as possible. Even if you use a machine with low configuration and do not need to make too complex settings, the response content of the server is sufficient to fill with various narrowband connections. However, with the increasing complexity of website content and bandwidth, it is increasingly important to optimize Apache to achieve better performance.

If the optimization result is only a small performance improvement, it is a waste of time. Imagine that it took you several hours or even days to adjust Apache parameters, but the result was only a few percent of performance improvement? Therefore, before optimization, the first step should be to test the performance level of your current server so as to determine how to optimize your server and measure the optimization effect.

We have not mentioned the test of Apache for the first time (see the article rush out of the water), as we mentioned earlier, it is often difficult to determine which part of the bottleneck is caused by the configuration of Apache or the dynamic website program environment you are using? Whether the query is caused by the website program is beyond the scope of this article, here we will mainly discuss some common practices for improving the speed of the Apache server and how it interacts with other components that make up the WEB site.

Host hardware

The machine and operating system environment where Apache is located are the most influential factors on performance. Obviously, a 368 PC won't have the same performance as a P4 or dual-processor machine. However, if hardware changes are not involved, the biggest thing we can do is to first check whether Apache runs on a dedicated server, sharing a server with other applications will definitely affect the server performance.

In most cases, bid farewell is a key factor affecting the performance of static websites, because it determines how much content Apache can cache. The more content it caches, the fewer chances it will have to read from the hard disk. It is time-consuming to access a specific file on the hard disk. If your website mainly contains static content, you 'd better use the mod_cache option. If your memory is large enough, you can use the mod_mem_cache option.

The first option caches the information on the disk. It works well when used with the mod_include option. This option stores the final version of the page in the cache when creating a page. Use mod_mem_cache to save the cached content in a memory heap that can be shared by all Apache processes.

Using a faster disk or RAID can increase Apache's access to disk files. Note that if you have made all of the following optimizations, a hardware (not software) upgrade solution is a better choice. The last question about hardware is the CPU capability, which has a great impact on dynamic content websites. The more dynamic content, the higher the requirement.

Host environment

Regardless of the operating system used, the following principles apply:

● Minimize unnecessary number of programs running in the background. If you are really concerned about server performance, you can even close some background services that are generally considered necessary. For example, in UNIX, you can disable the NFS service, any print service, or even the mail sending service (if you do not use these services). In Windows, you can use the control panel to optimize the system and cache, of course, necessary services (such as MySQL) still need to be run.

● Avoid using the system. If you use it to perform other work (such as compiling software, editing files, or configuring machines) when the server is running, you will surely reduce the performance provided by the server. If you have to install some components on it, you can compile them on other machines and copy them to the server.

● Keep your system components up to date. Although this is mainly from the security perspective, software patches often provide improved network and I/O performance.

Apache program

Next, we will talk about Apache itself.

First, check whether Apache only contains the components required by your website during compilation. This means that you can ignore unnecessary components. This causes it to occupy less memory, and it also means that you do not need to activate some options accidentally to reduce server performance.

Static or dynamic

Flexibility is often the main factor for Apache administrators to consider, but flexibility also has a cost. It is very convenient to use Dynamic Loading modules, but it will lead to performance degradation, because the module is loaded only when necessary. However, the dynamic module also helps reduce the server's memory requirements.

To install Apache in static mode, use the setting script to define the modules you need, but do not define them as shared (for example: use -- enable-rewrite instead of -- enable-rewrite = shared, and do not use the share option -- enable-so ).

Module Settings

If you use the static mode of Apacher, you must be careful with the option to load the module. This is an overhead of the static mode. The more modules you load, the larger the memory usage, this effect is even more obvious for programs that use a multi-threaded model with forks.

Note that some projects are automatically included. Therefore, you must explicitly specify the modules to include or disable. Remember that if you need to include third-party modules such as authentication, PHP, or mod_perl, you need WEB services ), use the configure-help command to view the list of available options.

Server Settings

If your environment has been set up with Apache and has been optimized, you can view your configuration file to perform the next optimization. A good way is to simplify the setting file and reduce the directive in the file to several hundred rows. First, delete the comments and then delete any unnecessary content.

Simplify setting files

The first step is to simplify the setting file, which does not bring any direct performance improvement, but it will make the setting file easy to use to reduce your chances of errors.

Before optimization, it is a good idea to start with a default setting file. These files are often stored in the Apache setting directory named httpd. conf. orig or httpd-std.conf, don't use a file like a performance-std.conf name, in the long run, if you're going to add a lot of additional setup information in it, it's never a good start. On the other hand, if your goal is to build a very fast static WEB server, this may be the easiest way to run the server.

If you are familiar with the configuration instructions of Apache or are willing to browse the Help file, you can delete all comments from the configuration file because they often make the actual instructions difficult to find, you can also delete a reference to a multi-processor mode (MPM) that is not available on the current platform.

Disable a module

Now we have a fresh setting file. Now we can start to delete elements that are not available to our system, especially:

● HostnameLookups (host name search), which increases the overhead for processing each request. First, the server performs a reverse query on the DNS system to find the host name of the customer system, then, perform a forward query to check whether the obtained host name actually points to the customer's IP address. In most cases, you can simply disable this function. If you often process server logs, this can be done later. You can disable this function by adding HostnameLookups off to the setting file.

● Symbolic connection. When this option is enabled, Apache checks whether each request contains a reference to the symbolic connection, which calls the lstat () system call for each path contained in the request. Use Options-FollowSymLinks to disable it unless you are about to use Symbolic connections.

● Server status information. Although this is useful for testing and monitoring servers, it also brings additional overhead to servers. You can disable it by looking for any indication similar to SetHandler server-status. If possible, you can remove this module when installing Apache.

● Do not use flexible options such as wildcards when you can be more precise. For example, for DirectoryIndex instructions, you can explicitly specify the file list, which is most commonly used at the beginning.

● Unless you have a good reason, you can execute CGI. Put the CGI file in a specific directory and set the correct permissions for it, this avoids Apache from determining whether a static file or a dynamic file is required for each request.

Disable logs

Writing log information is a very time-consuming task. Although Apache keeps the log file open to save time for opening the file, it still takes a lot of time. If you do not need to store log information, you can disable this option to save more processing time. You only need to comment out the log line in the setting file to disable it.

If logs must be retained, you can disable the HostnameLookups option (see the previous section) and copy the log file to another machine for further analysis.

Simplify directory-level settings

The htaccess file can greatly expand the configuration parameters of Apache. You do not need to edit the Apache main configuration file every time you change the design. However, the use of this file also reduces the server performance.

If you use this file, Apache must first check whether the file exists in the current directory. If so, parse the file and apply the settings in the file in the current directory. Worse, Apache not only needs to check the current directory, but also whether all the upper-level directories of the current directory contain the htaccess file to determine the settings based on all these files.

If you want to optimize the server performance, you should disable the use of the htaccess file. Any basic directory settings can be performed in the master setting file, the main setting file is parsed only once when the server is started. To disable the htaccess file, add the directive AllowOverride None in any section.

Multi-processing mode settings

Multi-Processing Module/MPM allows a specific platform to process multiple concurrent connections. The MPM module is platform-related. There are different solutions for Unix, Windows, BeOS, and NetWare. There are more than one solutions available for some platforms. For most users, the default settings for a specific platform have worked well, and precise adjustment of these parameters is a time-consuming task. However, if you want to maximize the potential of Apache, you must adjust these settings.

For most platforms, there is only one MPM option, but in UNIX there are two options: prefork and worker, prefork mode to generate multiple identical Apache processes, in worker mode, multiple threads are created. Generally, for systems with only one or two processors, the prefork mode works better, while for systems with more processors, the thread model works more efficiently.

In either case, MaxClients indicates that it is the most effective way to improve server performance. It controls the maximum number of concurrent connections that Apache can process.

Optimize static content

If your WEB server uses a large amount of static content or you use two WEB servers to process dynamic and static content, your main goal now is to shorten the response time for the server to send the requested content, the simplest method is to use the mod_cache cache module. You can use mod_disk_cache and mod_mem_cache to provide disk-based Cache and memory-based Cache respectively.

You can view the description file about mod_cache for further information.

Optimize Dynamic Content

Dynamic content may be the most consumed time on all WEB servers, especially when CGI is used, a simple program may increase the response time by several seconds.

One of the major advantages of using a script-based solution is that they mount the interpreter in Apache, which reduces the time to load the interpreter during execution, some schemes also cache the parsed scripts, so that the next request can be directly executed without re-parsing.

The adjustment and optimization of specific systems are complex and time-consuming, and you need to adjust specific scripts to take advantage of the optimization results.

However, the optimization of dynamic content is very obvious. Simply converting the Execution Mode of perl scripts from CGI to mod_perl can reduce the execution time by up to 70%, you can also use persistent connections to the database or cache information between multiple requests, which is very useful for e-commerce websites and reduces the overhead of repeatedly loading information between different requests.

Summary

Although Apache is a highly customizable powerful and complex server software, we are very interested to note that even standard installation of Apache can achieve good performance. Adjusting Apache parameters is a way to easily significantly improve server performance. Unfortunately, we often have the most uncontrollable content in Apache-such as dynamic content scripts and CGI of websites are the most important aspects that affect website performance, if you manage a typical Apache server, you will find that the time it takes for Apache to respond to an incoming connection and finally send the content to the customer is in milliseconds, wait for several seconds for the required data resources.

Of course, this does not mean that our optimization work is meaningless, and the effect of optimization cannot be underestimated over time. In addition, it is important that by simplifying your settings file, you can greatly reduce the server management burden.

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.