Trang chủ / Linux / Tối ưu Apache Optimization

Tối ưu Apache Optimization

apache-optimization-guide

Trước hết bạn cài:

yum -y install ntsysv

sau đó run:

ntsysv

Để xem các service nào start mỗi khi vps khởi động mà ko cần thiết thì tắt.

===

The default Apache settings that cPanel sets upon install are definitely something that can be improved on. With a few small tweaks, the efficiency with which Apache runs with can be greatly improved.

To start, open the Apache configuration file and finding the directives section. On a cPanel server, it will be located in /usr/local/apache/conf/. On a Plesk server, it will be in /etc/httpd/conf/. If you are using vi or vim: once you open the file, you can find the directives by scrolling through the file, or by typing forward-slash ‘/’ and typing the exact string that you are looking for (search is case specific).

[root@host /] vim /usr/local/apache/conf/httpd.conf
or

[root@host /] vim /etc/httpd/conf/httpd.conf

This list is a composite of the settings we will be reviewing from fresh install on a cPanel server:

Timeout 300
 KeepAlive On
 MaxKeepAliveRequests 100
 KeepAliveTimeout 15
 MinSpareServers 5
 MaxSpareServers 10
 StartServers 5
 MaxClients 150
 MaxRequestsPerChild 0

Please note, the settings that we will review in this article are by no means a complete list of tweakable options in the Apache configuration file. The settings we will be focusing on are the ones that control how Apache handles webpage requests.

Timeout

Timeout 300

Usually this value doesn’t require editing and a default of 300 is sufficient. Lowering the ‘Timeout’ value will cause a long running script to terminate earlier than expected.

On virtualized servers like VPS servers, lowering this value to 100 can help improve performance.

KeepAlive

KeepAlive On

This setting should be “On” unless the server is getting requests from hundreds of IPs at once.

High volume and/or load balanced servers should have this setting disabled (Off) to increase connection throughput.

MaxKeepAliveRequests

MaxKeepAliveRequests 100

This setting limits the number of requests allowed per persistent connection when KeepAlive is on. If it is set to 0, unlimited requests will be allowed.

It is recommended to keep this value at 100 for virtualized accounts like VPS accounts. On dedicated servers it is recommended that this value be modified to 150.

KeepAliveTimeout

KeepAliveTimeout 15

The number of seconds Apache will wait for another request before closing the connection. Setting this to a high value may cause performance problems in heavily loaded servers. The higher the timeout, the more server processes will be kept occupied waiting on connections with idle clients.

It is recommended that this value be lowered to 5 on all servers.

MinSpareServers

MinSpareServers 5

This directive sets the desired minimum number of idle child server processes. An idle process is one which is not handling a request. If there are fewer spareservers idle then specified by this value, then the parent process creates new children at a maximum rate of 1 per second. Setting this parameter to a large number is almost always a bad idea.

Liquidweb recommends adjusting the value for this setting to the following:

Virtualized server, ie VPS 5

Dedicated server with 1-2GB RAM 10

Dedicated server with 2-4GB RAM 20

Dedicated server with 4+ GB RAM 25

MaxSpareServers

MaxSpareServers 10

The MaxSpareServers directive sets the desired maximum number of idle child server processes. An idle process is one which is not handling a request. If there are more than MaxSpareServers idle, then the parent process will kill off the excess processes.

The MaxSpareServers value should be set as double the value that is set in MinSpareServers.

StartServers

StartServers 5

This directivesets the number of child server processes created on startup. This value should mirror what is set in MinSpareServers.

MaxClients

MaxClients 150

This directive sets the limit on the number of simultaneous requests that will be served. Any connection attempts over the specified limit will be queued. Once a process is freed at the end of a different request, the queued connection will then be served.

Total number of concurrent connections.
Locate it in the configuration file. This should be set to a reasonable value. I suggest using this formula to determine the right value for your server.

MaxClients = 150 x RAM (GB)

So for example if you have 2 GB or RAM set this value to 300.
There is no reason for you to set it any higher unless you have a specific problem with this value. A high value can lead to a complete server hang in case of a DOS attack. A value too low can create timeout problems for your clients if the limit is reached.

For virtualized servers such as VPS accounts, it is recommended to keep this value at 150. For all dedicated servers the recommended value for this setting is 250.

ServerLimit

This value should be same as MaxClients

ServerLimit = 150 x RAM (GB)

MaxRequestsPerChild

MaxRequestsPerChild 0

This directive sets the limit on the number of requests that an individual child server process will handle. After the number of requests reaches the value specified, the child process will die. When this value is set at 0, then the process will never expire.

Liquidweb recommends adjusting the value for this setting to the following:

Virtualized server, ie VPS 300

Dedicated server with 1-4GB RAM 500

Dedicated server with 4+GB RAM 1000

Other More….

All the important configuration options are stored by Apache in a config file called httpd.conf that is located at /usr/local/apache/conf/httpd.conf. We will start by opening this file in your favorite text editor.

For example:

vi /usr/local/apache/conf/httpd.conf

MaxClients

Total number of concurrent connections.
Locate it in the configuration file. This should be set to a reasonable value. I suggest using this formula to determine the right value for your server.

MaxClients = 150 x RAM (GB)

So for example if you have 2 GB or RAM set this value to 300.
There is no reason for you to set it any higher unless you have a specific problem with this value. A high value can lead to a complete server hang in case of a DOS attack. A value too low can create timeout problems for your clients if the limit is reached.

ServerLimit

This value should be same as MaxClients

ServerLimit = 150 x RAM (GB)

MinSpareServers and MaxSpareServers

MaxSpareServers and MinSpareServers control how many spare (unused) child-processes Apache will keep alive while waiting for more requests to put them to use. Each child-process consumes resources, so having MaxSpareServers set too high can cause resource problems. On the other hand, if the number of unused servers drops below MinSpareServers, Apache will fork (an expensive operation) new child-processes until MinSpareServers is satisfied.

Leave those values to:

MinSpareServers 5
MaxSpareServers 10

If you have more them 2 GB of RAM and you run a resource intensive website consider increasing MaxSpareServers.

MaxRequestsPerChild

Controls the number of request the a child serves before the child is killed. This should not be set too low as it will put an unnecessary load on the apache server to recreate the child. I suggest setting it to:

MaxRequestsPerChild 1000 for 1 GB RAM

10,000 for 2 GB and 0 for more than 2 GB RAM

KeepAlive and MaxKeepAliveRequests

KeepAlive provides long-lived HTTP sessions which allow multiple requests to be sent over the same TCP connection. In some cases this has been shown to result in an almost 50% speedup in latency times for HTML documents with many images, but having keepalive on is also a resource intensive setting.

Here comes the big question: To KeepAlive or not to KeepAlive?
Well the opinions are mixed here, some say to KeepAlive some say not to.

KeepAlive off

If you want to hear my option I would say NOT to KeepAlive if you are running a shared hosting business or if you want to get the most out of your hardware. You should KeepAlive only if the loading time of your pages is the most important factor in your business and you have the money to invest in a more powerful hardware. If you decide to KeepAlive I suggest you set MaxKeepAliveRequest low to something like 2 seconds.

StartServers

Sets the number of child server processes created on startup. This setting depends greatly on the type of webserver you run. If you run low traffic websites on that server set it low to something like 5. If you have resource intensive websites on that server you should set it close to MaxClients.

StartServers 5

Timeout

The amount of time Apache will wait for three things: the total amount of time it takes to receive a GET request, The amount of time between receipt of TCP packets on a POST or PUT request, the amount of time between ACKs on transmissions of TCP packets in responses.

The default value is 300. You should set time to something a bit lower. A setting of 150 is probably ok. This will also help in case of small DOS attacks like to ones targeting some phpBB forums. Do NOT set it any lower then 90 as your users will start having timeout problems.

Timeout 150

After you have done all the necessary changes you can go ahead and restart Apache.
There is an extra step that you have to do so that the changes that you done to httpd.conf aren’t lost when a recompile is done.
To also save the changes in the database you will have to run:

/usr/local/cpanel/bin/apache_conf_distiller –update

You can check to see if the changes were accepted and will not be discarded at the next apache recompile by running

/usr/local/cpanel/bin/build_apache_conf

Sample values:

MinSpareServers 5
MaxSpareServers 10
KeepAlive off
ServerLimit 1400
MaxClients 1400
MaxRequestsPerChild 0
Timeout 150

Giới thiệu về Văn Phương

Văn Phương
Là một người bình thường, thích làm những việc bình thường cho mình và mọi người

Bài viết khác...

Tìm thông tin mật khẩu thiết lập ban đầu của Direct Admin(DA)

Sau khi cài xong thông tin cấu hình chi tiết được lưu ở đây: ‘/usr/local/directadmin/scripts/setup.txt’. ...

Bình luận