Linux/Apache 效能調教

Linux/Apache 效能調教

這篇文章主要說明Linux/Apache 效能調教的一些設定值,

如何在不更改程式架構下,更改一些系統參數設定,讓效能更好的一些方法。
These changes are useful for all web servers, regardless of the architectural model.

 

Linux 設定

The following parameter increases the number of TCP SYN packets that the server can queue before SYNs are dropped:

echo 30000 > /proc/sys/net/ipv4/tcp_max_syn_backlog

Web servers typically have a large number of TCP connections in the TIME-WAIT state. The following parameter increases the number connections that are allowed in that state:

echo 2000000 > /proc/sys/net/ipv4/tcp_max_tw_buckets

The following parameter sets the length for the number of packets that can be queued in the network core (below the IP layer). This allows more memory to be used for incoming packets, which would otherwise be dropped.

echo 50000 > /proc/sys/net/core/netdev_max_backlog

Apache設定

Apache
Apache’s main configuration file is httpd.conf. Several parameters can be modified in that file to improve performance:

The following parameter sets the upper bound on the number of processes that Apache can have running concurrently:

MaxClients 150

Larger values allow larger numbers of clients to be served simultaneously. Very large values may require Apache to be recompiled. This change should be used with care, because large numbers of processes can cause excessive overhead.

The following parameter indicates the number of requests that a single Apache process will perform on a connection for a client before it closes the connection:

MaxKeepAliveRequests 100

Opening and closing connections consumes CPU cycles, so it is better for the server to provide as many responses on a single connection as possible. Therefore, larger numbers are better. In fact, setting this value to 0 indicates that the server should never close the connection if possible.

MaxRequestsPerChild 0

It determines the number of requests an individual Apache process will serve before it dies (and is reforked). 0 implies that the number is unlimited, but on some systems with memory leaks in the operating system, setting this to a nonzero value keeps the memory leak under control.

MinSpareServers

The following parameters determine the minimum and maximum number of idle processes that Apache keeps in anticipation of new requests coming in:

MinSpareServers 5
MaxSpareServers 10

The idea is that it is cheaper to keep a live process idle than to fork a new process in response to a new request arrival. For highly loaded sites, you might want to increase these values.

 

ulimit

Flash and Other Event-Driven Servers
A common problem event-driven servers have is that they use a large number of file descriptors and thus can run out of these descriptors if the maximum is not increased.

The following is an sh (shell) command that increases the number of open files a process may have:

ulimit -n 16384

The process (in this case, the web server) must also be modified to take advantage of large numbers of descriptors:

#include <bits/types.h>
#undef __FD_SETSIZE
#define __FD_SETSIZE 16384

The default FD_SETSIZE on Linux 2.4 is only 1024.

Max_Connect

The following parameter determines the number of active simultaneous connections:

echo 20000 > /proc/sys/net/tux/max_connect

The following parameter sets the maximum number of connections waiting in Tux’s accept queue:

echo 8192 > /proc/sys/net/tux/max_backlog

The following parameter disables logging of requests to disk:

echo 0 > /proc/sys/net/tux/logging

 

實際個案

以下是筆者曾經修改過一個 apache效能個案實例,供參考。

vim /etc/httpd/conf/httpd.conf
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 10240
MaxClients 10240
MaxRequestsPerChild 10240
</IfModule>

DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

KeepAlive On
KeepAliveTimeout 60
MaxKeepAliveRequests 0

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/x-icon “access plus 1 week”
ExpiresByType image/gif “access plus 1 week”
ExpiresByType image/jpg “access plus 1 week”
ExpiresByType image/jpeg “access plus 1 week”
ExpiresByType image/png “access plus 1 week”
ExpiresByType text/javascript “access plus 3 days”
ExpiresByType text/js “access plus 3 days”
ExpiresByType application/javascript “access plus 3 days”
ExpiresByType application/x-javascript “access plus 3 days”
ExpiresByType text/css “access plus 3 days”
ExpiresByType text/x-json “access plus 3 days”
</IfModule>

 

Leave a Reply

Your email address will not be published.