Introduction

Apache is a powerful and capable open-source web server, designed to provide a balance of flexibility, portability, and performance. Apache optimization is an important task for every system administrator.

You can improve Apache's performance by customizing the Apache configuration without needing to add additional hardware like RAM, CPU etc.

This post describes various Apache configuration options that can be used to improve Apache performance without adding additional hardware resources to your system.

Requirements

  • A server running CentOS v.7
  • Apache installed and running

MaxKeepAliveRequests

MaxKeepAliveRequests is the maximum number of requests to serve on a TCP connection. It limits the number of requests allowed per connection. If it is set to 0, unlimited requests will be allowed. You can set it to any value you desire.

Keep this setting to a high value for maximum server performance. The recommended value of MaxKeepAliveRequests is 500.

To change this setting, edit the Apache configuration file:

sudo nano /etc/httpd/conf/httpd.conf

Add the following line:

MaxKeepAliveRequests 500

Save and close the file when you are finished.

KeepAliveTimeout

KeepAliveTimeout defines the number of seconds Apache will wait for the new request from connected clients before closing the connection. (Once the server receives a request, the Timeout directive applies instead.)

By default Keepalive is disabled in CentOS 7. If Keepalive is set to on, it is a good idea to set the KeepAliveTimeout value low.

The recommended KeepAliveTimeout can be between 1 to 5.

You can do this by editing Apache configuration file:

sudo nano /etc/httpd/conf/httpd.conf

Add the following line:

KeepAliveTimeout 5

Save and close the file when you are finished.

KeepAlive

KeepAlive sets whether the server allows more than one request per connection. It can be used to prevent any one client from consuming too much of the server's resources.

By default KeepAlive is disabled in CentOS 7. Once the Apache server is getting requests from hundreds and thousands of IPs at once, this setting should be On.

You can enable this setting by editing Apache configuration file:

sudo nano /etc/httpd/conf/httpd.conf

Add the following line:

KeepAlive On

Save and close the file when you are finished.

Configure MPM Prefork

One reason for poor Apache performance is that Apache is having trouble coping with the load. The Apache MPM (Multi-Processing Module) can help.

mpm_prefork_module is included and enabled in the default Apache installation on CentOS 7. To confirm this run the following command:

sudo apachectl -t -D DUMP_MODULES |grep mpm

You should see mpm_prefork_module (shared) if mod_deflate is installed and enabled.

You can make Apache performance better using the Apache MPM prefork module. To do this, set the following parameters in your Apache configuration file:

sudo nano /etc/httpd/conf/httpd.conf

Add the following lines:

KeepAlive Off
<IfModule prefork.c>
   StartServers        5
   MinSpareServers     5
   MaxSpareServers     10
   MaxClients          150
   MaxRequestsPerChild 3000
</IfModule>

Save and close the file, then restart Apache to reflect these changes.

sudo apachectl restart

Explanations

StartServers: This directive sets the number of child server processes created on startup. It is a good idea to increase this number on a high-load server, so the server is ready to handle a lot of connections.

MinSpareServers: This directive sets the minimum number of idle child server processes. This value will need to be tuned for high-load servers.

MaxSpareServers: This directive sets the maximum number of idle child server processes. When there are more idle child server processes than defined by MaxSpareServers the idle process will be killed.

MaxClients: This directive sets the maximum number of simultaneous requests that Apache will handle. When this limit has been reached, any other connection attempts will be queued.

Number of MaxClients = (Total RAM memory – RAM memory used for other process except Apache process) / (Memory used by a single Apache process)

MaxRequestsPerChild: This directive sets how many requests a child process will handle before terminating. Once the limit has been reached, the child process will die.

Note: If this value is 0, then the process will never die.

AllowOverride

If AllowOverride is set to 'All', then Apache will attempt to open a .htaccess file in each directory that it visits.

For example:

 DocumentRoot /vaw/www/html/website1
 <Directory /> 
 AllowOverride All
 </Directory>

If a request is made for URI /index.html, then Apache will attempt to open .htaccess file in /, /var/, /var/www/, /var/www/html/ and /var/www/html/website1/.

Therefore, it is good idea to add AllowOverride all for a specific directory only:

 DocumentRoot /vaw/www/html/example
 <Directory /var/wwww/html/example/admin> 
   AllowOverride All
 </Directory>

DNS Lookups

The biggest reason for Apache web server slowdowns is the time required to perform DNS lookups. Apache will record the full host name of each incoming client connection in its access.log file. Resolving each one eats up a significant chunk of time.

The HostnameLookups option enables DNS lookup so that hostnames can be logged instead of the IP address. By default HostnameLookups is Off in Apache.

You can verify that this is the case by editing the Apache config file:

sudo nano /etc/httpd/conf/httpd.conf

Be sure the HostnameLookups line reads:

HostnameLookups Off

Save and close the file when you are finished, then restart Apache to reflect changes.

sudo apachectl restart

Conclusion

Configuring Apache for maximum performance is very easy. You can experiment with various available options and measure the web server performance with tools like ab and httperf.

هل كانت المقالة مفيدة ؟ 3 أعضاء وجدوا هذه المقالة مفيدة (4 التصويتات)