LOADING
×

Optimize Web Server Performance by Tuning Apache

The article defines the procedure of tuning the Apache server to increase its performance and optimization.

Optimize Web Server Performance by Tuning Apache

Introduction

The article describes about tuning Apache and review its processing to handle a number of requests from clients. It guides on which modules of Apache to turn off and the need to alter resource settings.

Review Apache Status


Apache2Buddy


The Apache2Buddy script, similar to MySQLTuner, reviews your Apache setup, and makes suggestions based on your Apache process memory and overall RAM. Although it is a fairly basic program, focusing on the MaxClients directive, Apache2Buddy is useful, and can be run through a single command:


curl -sL https://raw.githubusercontent.com/richardforth/apache2buddy/master/apache2buddy.pl | perl


Multi-Processing Modules(MPM)


First, take a backup of your Apache configuration file before making any changes to it. Run the following command to do the same depending upon the system you are using.  


On Debian/Ubuntu:

cp /etc/apache2/apache2.config ~/apache2.conf.backup


On CentOS/Fedora:

cp /etc/httpd/conf/httpd.config ~/httpd.conf.backup


In practice, modular functionality of Apache is extended by MPMs which allow us to decide the way to modify the Apache server configurations such as web server to bind to network ports, requests from clients to be accepted, and children processes (and threads, alternatively) to handle client requests.

There are three different MPMs offered by Apache such as - Worker MPM, Event MPM, Prefork MPM. The speed and scalability of the httpd/apache2 may be affected, depending upon the choice :


  • Multiple child processes have been used in the Worker MPM with many threads each. Each thread handles one connection at a time. Worker MPM is a good choice for high-traffic servers because of its smaller memory footprint than the prefork MPM.


  • The Event MPM is also threaded like the Worker MPM, except it allows simultaneous service to more requests by passing off some processing work to supporting threads and freeing up the main threads to work on new requests.


  • Multiple child processes have been used in the Prefork MPM with one thread each. Each process handles one connection at a time. On many systems, in comparison with worker, prefork is faster in speed, but it consumes more memory. Prefork's threadless design has some advantages over worker in some situations: it can be used with non-thread-safe third-party modules, and it is easier to debug on platforms with poor thread debugging support.


Command to check the MPM used by your Apache installation:

httpd -V

To change this, we need to edit the MPM conf file. The file location is as follows:

# /etc/httpd/conf.modules.d/00-mpm.conf          [On RedHat/CentOS based systems]

# /etc/apache2/mods-available/<mpm>.load   [On Debian/Ubuntu based systems]

Where <mpm> can be mpm_event, mpm_worker, or mpm_prefork.

Open the file and uncomment the line that loads the desired module like:

LoadModule mpm_event_module modules/mod_mpm_event.so


Now, restart the web server as well as the newly installed php-fpm (or php5-fpm) service. Command as follows:


On RedHat/CentOS

# systemctl restart httpd php-fpm && systemctl enable httpd php-fpm

On Debian/Ubuntu

# systemctl restart apache2 php5-fpm && systemctl enable apache2 php5-fpm


We need to place the block as described below, inside /etc/httpd/conf/httpd.conf or /etc/apache2/apache2.conf, depending on whether you are using CentOS or Debian.

Please note that the same principle applies to all MPMs – we are using worker here.


Configuration to provide 8000 concurrent connections.

<IfModule mpm_worker_module>

    ServerLimit              250

    StartServers              10

    MinSpareThreads     75

   MaxSpareThreads      250

    ThreadLimit               64

    ThreadsPerChild         32

    MaxRequestWorkers   8000

    MaxConnectionsPerChild 10000

</IfModule>


The details about the parameters described below:


ServerLimit

Declares the maximum number of running apache processes. If you change this value, the daemon will require a restart.

StartServers

The number of initial processes to start when starting the apache daemon.


MinSpareThreads/MaxSpareThreads

This regulates the number of threads may stay idle without being killed.

ThreadsPerChild

How many threads can be created per process. Can be changed during a reload.

ThreadLimit

During runtime, ThreadsPerChild can be configured as high as this value. If you change this value, the daemon will require a restart.

MaxRequestWorkers

This declares how many concurrent connections we provide. Divided by ThreadsPerChild you get the suitable ServerLimit value. Maybe less than ServerLimit * ThreadsPerChild to reserve some resources that can be engaged during runtime with increasing MaxRequestWorkers and reloading the configuration.

MaxConnectionsPerChild

Defines the number of Connections that a process can handle during its lifetime (keep-alives are counted once). After that, it will be killed. It is used to prevent possible apache memory leaks. If set to 0 the lifetime is infinite.


The default configuration of the process-thread controls in the worker MPM could look as follows:

ServerLimit         16

StartServers         2

MaxRequestWorkers  150

MinSpareThreads     25

MaxSpareThreads     75

ThreadsPerChild     25


References:

  1. https://www.linode.com/docs/web-servers/apache-tips-and-tricks/tuning-your-apache-server/

  2. https://www.tecmint.com/apache-performance-tuning/

  3. https://httpd.apache.org/docs/2.4/

  4. https://httpd.apache.org/docs/2.4/misc/perf-tuning.html

  5. https://httpd.apache.org/docs/2.4/mod/worker.html

  6. https://oxpedia.org/wiki/index.php?title=Tune_apache2_for_more_concurrent_connections




Trendy