Apache is one of the most popular and powerful web servers. And of course, it is open source. It is also known as httpd server. Apache web servers are designed to provide a balance of portability, flexibility, and high performance. Optimizing Apache plays a crucial role in determining the overall performance of the system. Apache is easy to configure and provides simple performance tuning options, which we will discuss in detail today.

PrerequisiteS

  • A live CentOS 7 server
  • Apache web server installed and running

First, let us walk you through the process of installing Apache, incase you haven’t done it already. To install Apache, use the command – sudo yum install httpd as shown below-

installation

The installation will take some time. After the installation, you will be able to see a message on the console as shown below-

installation complete

The main configuration file (httpd.conf) can be seen in location /etc/httpd/conf.

The httpd.conf file contains some default parameters. You can also add your own parameters based on your requirements. Let’s now start with some performance optimizations.

KEEP ALive TIMEOUT-

KeepAliveTimeout is a parameter that defines the number of seconds before closing a connection. This is the maximum seconds Apache will wait for a new request before closing the connection. By default, this parameter is disabled in CentOS 7. However, it is generally good practice to have a KeepAliveTimeout value.

This parameter can be set to a very low value, between 1 and 5. To make this change you can edit the httpd.conf file at location- /etc/httpd/conf

You can add the following line:
KeepAliveTimeout 5

Max Keep-Alive Requests-

MaxKeepAliveRequests is the maximum number of requests which can be served on a TCP connection. This will allow you to limit the number of allowed connections and will help in effective utilization of network bandwidth, without causing traffic congestion. This is a numeric value which when set to 0, will allow unlimited requests.

The recommended value here is 500. To add this parameter you will have to edit the httpd.conf file

You can add the following line-
MaxKeepAliveRequests 500

 

Keep-Alive-

KeepAlive is a parameter which determines whether more than one request per connection is allowed or not. This helps in ensuring one client does not consume all of the server’s resources. By default, this parameter is disabled in CentOS 7. If you expect Apache to be getting multiple requests from different IPs at once, then this should be turned ON. This can be edited in the httpd.conf file.

In order to turn on this parameter, you can add the following line-
KeepAlive On

 

Configure Multi-Processing Module-

One of the reasons for slow performance in Apache can be its inability to deal with load. In such cases, a multi-processing module will help.

mpm_prefork_module is part of CentOS 7 and is enabled by default. To confirm this is running, you can use the command- sudo apachectl -t -D DUMP_MODULES |grep mpm

multiprocessing module

If mod_deflate is enabled it will display mpm_prefork_module (shared)

For better performance, you can also use Apache MPM prefork module. This can be set in the httpd.conf file.

You can add the following lines in the file-
<IfModule prefork.c>
    StartServers 5
    MinSpareServers 5
    MaxSpareServers 10
    MaxClients 150
    MaxRequestsPerChild 3000
</IfModule>

Once the file is saved, you can restart Apache using command- sudo apachectl restart.

Here is a brief explanation of the mentioned parameters-

  • StartServers – This sets the number of child server processes created at startup. Initially, this can be kept as a small number and can be gradually increased on a high payload server. This helps in ensuring the right utilization of server resources.
  • MinSpareServers – This sets the minimum number of idle child server processes and can be tuned for high payload servers.
  • MaxSpareServers – This sets the maximum number of idle child server processes. In case the number of idle child server processes exceeds this value, then the idle processes are killed.
  • MaxClients – This is the maximum number of simultaneous requests Apache can handle. Once this limit is reached, the connection will be queued.
  • MaxRequestsPerChild – This indicates how many requests a child process will handle before terminating. Once this limit has been reached, the child process will die. If this value is set to 0, then the process will never die.

Allow Over ride-

The parameter AllowOverride can be set to ‘All’. However, if this is included within a simple directory tag then it will open an .htaccess file for every directory it visits. So if you have the following configuration-
DocumentRoot /var/www/html/example
<Directory />
     AllowOverride All
</Directory>

What will happen is that if you make a request to URL /index.html, then Apache will open an .htaccess file for /, /var/, /var/www/, /var/www/html/, /var/www/html/example

To avoid this waste, you can configure AllowOverride for a specific directory, such as:

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

DNS LOOKUPS

Another factor that can slow Apache down is the time required to perform DNS lookups. Apache records the full hostname of each incoming request in the access.log file. Resolving each of these can be time consuming.

This is configured with the parameter- HostnameLookups in the httpd.conf file. This enables DNS lookups so that hostnames can be logged instead of IP address. In Apache, by default, this parameter is turned off. This property is defined in the httpd.conf file as-

HostnameLookups Off

Once you’re done making all of the above changes, restart Apache using the command- sudo apachectl restart

And that’s it! Your Apache web server should now be way more efficient than it was before.

Facebooktwittergoogle_plusredditpinterestlinkedinmail