Home >php教程 >PHP开发 >Apache performance optimization (6)

Apache performance optimization (6)

黄舟
黄舟Original
2016-12-21 11:28:371505browse

Example of estimated parameters (for reference, not necessarily suitable for our situation):
The default maximum number of users of Apache is 256: This configuration is a very good default setting for the era when the server memory is still about 256M, but With the sharp decline in memory costs, the memory configuration of servers at large sites is now generally more than an order of magnitude higher than it was then. Therefore, the hard limit of 256 processes is too wasteful for a machine with 1G memory, and Apache's soft upper limit max_client is limited by HARD_SERVER_LIMIT. Therefore, if the WEB server memory is greater than 256M, Apache's HARD_SERVER_LIMIT should be increased. According to personal experience: 2560 can already meet the capacity planning of most servers with less than 2G of memory

An apache + mod_php server: an apache process generally requires 4M of memory
So on a machine with 1G of memory: apache_max_process_with_good_perfermance < ( 1g / 4m) * 2 = 500
apache_max_process = 500 * 1.5 = 750
So plan your application so that the service runs below 500 processes as much as possible to maintain relatively high efficiency, and set the soft upper limit of Apache at 800.
Example 2:
An apache + mod_resin server: An apache process generally requires 2M memory
On a machine with 2G memory:
apache_max_process_with_good_perfermance < (2g / 2m) * 2 = 2000
apache_max_process = 2000 * 1.5 = 3000
--prefix=/another_driver/apache/: It is recommended to install the apache service on another drive device. The purpose is that the hard disk is often the device with the lowest service life of the system. Therefore: completely separating the service data and the operating system can not only improve Data access speed, and more importantly, greatly facilitates system upgrades, application backup and recovery processes.
--shared-module=max: Using this dynamic loading method to load submodules will cause a 5% performance drop. But the benefits to standardized installation and maintenance are far greater than this. Our application is relatively fixed and it is recommended to use static.
grep -v "#" httpd.conf.default >httpd.conf
#Maximum number of service processes: Set according to service capacity prediction
MaxClients 256 => 800
Other apache performance optimization configuration:
HostnameLookups off
On will bring to delay because a DNS query is required for each request.
Options FollowSymLinks
In order to get the best performance and give up the protection of symbolic links, you can set FollowSymLinks wherever needed and give up using SymLinksIfOwnerMatch.
AllowOverride None
If the URL space allows override (usually using .htaccess file), Then Apache will try to open .htaccess for each file name component.
DirectoryIndex index.cgi index.pl index.shtml index.html
The most commonly used ones should be placed first. Do not use wildcards and use the complete list.
EnableMMAP off
When Apache 2.0 needs to search the contents of a sent file, such as when processing server-side includes, if the OS supports some form of mmap(2), memory mapping will be performed on the file.
On some platforms, memory mapping can improve performance, but in some cases, memory mapping will reduce performance and even affect the stability of httpd. For our application, it is more appropriate to turn off memory mapping for sending files.
mod_status and ExtendedStatus Off
If Apache includes mod_status when compiling and ExtendedStatus On is set at runtime, Apache will call gettimeofday(2) twice for each request (or times( depending on the operating system) 2)), and (pre-1.3) several additional time(2) calls to make status records time-stamped. In order to get the best performance, you can set ExtendedStatus off (this is also the default).
Adjust the TCP connection cutoff parameters
Add to /etc/sysctl.conf:
net.inet.tcp.msl=2000
net.inet.tcp .always_keepalive=0

The above settings will prevent TCP connections from being established in Keep Alive mode by default, and avoid a large number of idle connections consuming OS
resources; in addition, the waiting time of TCP TIME_WAIT is also reduced from 60 seconds to 4 seconds.
Adjust the memory usage allowed by UFS dirhash
vfs.ufs.dirhash_maxmem=67108864 (This setting is automatically included in 5.3p14)

This setting increases the OS default value by 32 times.
Disable KeepAlive in the Apache configuration:
Edit /usr/local/etc/apache2/httpd.conf and change
KeepAlive On
to
KeepAlive Off
On servers with heavy loads, the above settings will connect to httpd at the same time Connections dropped from around 2200 to below 400
. And effectively improve response time. The above settings have been applied to some machines before (about 3 weeks). So far, it can be basically confirmed that these settings will not cause any adverse effects that can be perceived by users. At the same time, they will improve the response time of the service. Therefore, it is recommended that all online The above settings are applied to the system.
Specific technical details
All currently online FreeBSD 5.3 versions (regardless of patchlevel) are integrated into the SINAMAIL kernel
ACCF_HTTP, its role is to send the HTTP request from the client to the application after it is completed, thereby reducing the resource consumption caused by httpd starting to wait for connections prematurely due to the slow client network.
After disabling KeepAlive, the client will have a greater chance of making concurrent requests when requesting multiple resources. Therefore, the impact of delays caused by problems such as packet loss on the link on downloading other resources will be weakened. After shortening the
TCP fragmentation timeout, the time waiting for the connection to be closed will be shortened and the established connection will be more stable.
The default maximum memory of dirhash is 2MB. Actual usage currently obtained on production systems is usually around 16-20MB. Expanding it will improve access performance to files in the same directory.

Simple performance test: (It is recommended to test various configuration parameters separately to find the best optimization parameters)
Use apache’s own ab to test. If you need to test it yourself, just test it.
[yangbin1@vHost-RH9 apache]$ ./bin/ab -n 10000 -c 100 http://10.210.128.189:7981/
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.121.2.8 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache. org/
Benchmarking 10.210.128.189 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Finished 10000 requests

Server Software:             Apache/2.0.49
Server Hostname:       10.210.128.189

Server Port:         7981

Document Path:                              456 bytes
Concurrency Level: 100
Time taken for tests: 20.756253 seconds
Complete requests : 10000
Failed requests: 0
Write errors: 0
Total transferred: 19192048 bytes
HTML transferred: 14653184 bytes
Requests per second: 481.78 [#/sec ] (mean)
Time per request: 207.563 [ms] (mean)
Time per request: 2.076 [ms] (mean, across all concurrent requests)
Transfer rate: 902.96 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 78 60.7 99 554
Processing: 20 125 57.8 114 1149
Waiting: 5 111 53.2 104 869
Total: 47 204 84.4 214 1303
Percentage of the requests served within a certain time (ms)
50% 214
66% 218
75 % 220
80% 223
90% 264
95% 302
98% 447
99% 530
100% 1303 (longest request)
Test result analysis: None yet.

The above is the content of Apache performance optimization (6). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn