Showing posts with label Apache MPM Modules. Show all posts
Showing posts with label Apache MPM Modules. Show all posts

Friday, 16 December 2016

Tuning Your Apache Server ~ AwsTechNix




Apache configuration has a major affect on your Linode’s performance. The easiest way to increase server performance is to turn off unneeded modules. This guide covers Apache modules, information on which modules to turn off, and other Apache performance tuning options.
The steps in this guide require root privileges. Be sure to run the steps below as root or with the sudo prefix. 

Tools

There are a variety of tools that can assist in determining if you need to alter resource settings, including the top command and the load-testing program Siege. Siege’s own Longview service can also help in server monitoring. At minimum, familiarize yourself with the RAM and CPU usage of your server. Discover usage statistics with these commands:

echo [PID]  [MEM]  [PATH] &&  ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 20
ps -eo pcpu,pid,user,args | sort -k 1 -r | head -20
More specific resources for resource tuning Apache includes Apache mod_status and Apache2Buddy.

Apache mod_status

Apache mod_status diplays information related to incoming server connections by generating a detailed status page. View an example of this page at Apache’s own website.
  1. Open your website’s configuration file. This file is located at /etc/apache2/sites-available/example.com.conf on Debian/Ubuntu systems or /etc/httpd/conf.d/vhost.conf on CentOS/Fedora systems.
  2. Add the following to the <virtual_hosts> block:
    /etc/apache2/sites-available/example.com.conf (Debian/Ubuntu)
    /etc/httpd/conf.d/vhost.conf (CentOS/Fedora)

    <Location /server-status>  SetHandler server-status  Order Deny,Allow  Deny from all  Allow from localhost</Location>


  3. Apache mod_status also offers an option called ExtendedStatus, which provides additional information about each request made to Apache. To enable ExtendedStatus edit your Apache configuration file:
    /etc/apache2/apache2.conf (Debian/Ubuntu)
    /etc/httpd/confd/httpd.conf (CentOS/Fedora)

    ExtendedStatus On
    Enabling ExtendedStatus consumes additional system resources.
  4. Restart Apache:
    • Debian/Ubuntu:
    • service apache2 restart
    • CentOS/Fedora:
    • /bin/systemctl reload httpd.service
  5. To view the file generated, download Lynx:
    • Debian/Ubuntu:
    • apt-get install lynx
    • Fedora/CentOS:
    • yum install lynx
  6. Open the file:
  7. lynx http://localhost/server-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 -L http://apache2buddy.pl/ | perl

Multi Processing Modules


Before making any changes to your Apache configuration, be sure to back up the configuration file:
On Debian/Ubuntu:
cp /etc/apache2/apache2.config ~/apache2.conf.backup
On CentOS/Fedora:

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



Apache offers two Multi-Processing Modules, three if on Apache 2.4, for managing your settings.
Each module creates child processes, differing primarily in how they handle threads.

Prefork

The prefork module creates a number of child processes at launch, each child handles only one thread. Since these processes deal solely with one thread at a time, making request speed suffer should there be too many concurrent requests. Should this occur, some requests essentially have to wait in line to be acted upon. To handle this, increase the number of child processes spawned, but be aware that this increases the amount of RAM being used. Prefork is the safest module and should be used when using mods that are not thread safe.

Worker

The worker module’s child processes spawn many threads per process, each thread ready to take on new requests. This allows for a greater number of concurrent requests to come in, and in turn is easier on the server’s RAM usage. Overall, the worker module offers higher performance, but is less secure than prefork and cannot be used with modules that are not thread safe.

Event

The event module is only available on Apache 2.4 and is based off the worker MPM. Like the worker, it creates multiple threads per child process, with a thread dedicated to KeepAlive connections that are handed down to child threads once the request has been made. This is good for multiple concurrent connections, especially those that are not all active at the same time but make the occasional request. The event MPM functions the same as worker in the event of SSL connections.

Module Values

Once you select your MPM, you will need to change the values inside the configuration. These settings are located in the /etc/apache2/apache2.conf file on Debian/Ubuntu, and the /etc/httpd/conf/httpd.conf file on CentOS/Fedora. The MPM looks like this:
/etc/apache2/apache2.conf (Debian/Ubuntu)
/etc/httpd/conf/httpd.conf (CentOS/Fedora)
<IfModule mpm_prefork_module>
 StartServers          4
 MinSpareServers       20
 MaxSpareServers      40
 MaxClients           200
 MaxRequestsPerChild  4500
</IfModule>

For other MPMs replace <IfModule mpm_prefork_module>  with <IfModule mpm_worker_module>  or <IfModule mpm_event_module> for worker and event, respectively.
The next step to reconfiguring your Apache server is altering the above settings. To do this, you need to be aware of what each value does, and how best to change it.
Again, the best way to make configuration changes is to make incremental changes and then monitor the effects.
After making alterations to the Apache configuration, restart the service using service apache restart on Debian/Ubuntu or /bin/systemctl reload httpd.service on CentOS/Fedora.

StartServers

The StartServers value indicates the number of child processes created at startup, and is dynamically controlled depending on load. There is often little reason to alter this number, unless your server is restarted frequently and contains a large number of requests upon reboot.

MinSpareServers

Sets the minimum number of idle child processes. If there are fewer processes than the MinSpareServer number, more processes are created at the rate of one per second on Apache 2.2 or lower. With Apache 2.4, this rate increases exponentially starting with 1 and ending with 32 children spawned per second. The benefit of this value is that when a request comes in it can take an idle thread; should a thread not be available, Apache would have to spawn a new child, taking up resources and extending the time it takes for the request to go through. Note, too many idle processes would also have an adverse effect on the server.

MaxSpareServers

Sets the maximum number of idle child processes. If there are more idle processes than this number, then they are terminated. Unless your website is extremely busy, this number should not be set too high, since even idle processes consume resources.

MaxClients

The maximum amount of requests that can be served simultaneously, with any number going past the limit being queued. If this is set too low, connections sent to queue eventually time-out; however, if set too high, it causes the memory to start swapping. If this value is increased past 256, the ServerLimit value must also be increased.
One way to calculate the best value for this is to divide the amount of RAM each Apache process uses by the amount of RAM available, leaving some room for other processes. Use ApacheBuddy to help determine these values, or the commands below.
To determine the RAM each Apache process uses, replace httpd with  apache2 on Debian or Ubuntu systems:
ps -ylC httpd --sort:rss
Divide the number by 2048 for megabytes.
To get information on memory usage:
free -m
To receive a fuller view of the resources Apache is using, use the top command.

MaxRequestsPerChild

This limits the number of requests a child server handles during its life. Once the limit has been hit, the child server dies. If set to 0, the child servers are set to never expire. The suggested value for this is a few thousand, to prevent memory leakage. Be aware that setting this too low can slow down the system, since creating new processes does take up resources.

ServerLimit

If you need to increase the MaxClients above 256, then increase your ServerLimit to match. To do this, add the ServerLimit line to your MPM code and alter the value:
ServerLimit          256
KeepAlive
The KeepAlive directive, when set to on allows for multiple requests to come from the same TCP connection. When a KeepAlive connection is used, it counts as only one request against the MaxRequestsPerChild directive. This value is kept outside of your MPM, but can tie in closely to your MPM choices.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

WHAT & HOW TO CONFIGURE APACHE MULTI-PROCESSING MODULES (MPMs) ~ AwsTechNix



Are you worried about your Apache server performance? Okay, let’s talk about Apache Multi-Processing Modules (MPMs). There is a documentation for Apache MPM but who has got time to read the documentations. Let’s talk in simple plain English about Apache Multi-Processing Modules (MPMs). All you need is 15 mins to learn Apache MPMs(happy-face).



What


The MPMs are used to change the basic functionality of the web server. It’s possible due to Apache’s modular design. The MPM, or Multi-Processing Module, you use is responsible for just about the entire HTTP session. Starting from listening on the network, taking requests in and most importantly, how to handle those requests. With the MPM you use Apache’s behavior will change. Apache offers three MPMs to choose from; PreforkWorker, and Event. You might be wondering which MPM module you should choose. The answer is right below.

How do I select which Apache MPM to use?


Above links explains about the three MPM modules and when to use them. If you don’t know about the Apache MPMs or which one to use, time to start reading.

How


1. Check what your Apache server has got
Most of the Apache server comes with Prefork module. To make sure whether your server has got Prefork, type the below command and see.

apache2ctl ­-l


See the output below


khimlal@devOps$ apache2ctl -­l 
Compiled in modules:
  core.c
  mod_so.c
  mod_watchdog.c
  http_core.c
  mod_log_config.c
  prefork.c
  mod_logio.c
  mod_version.c
  mod_unixd.c
  mod_unixd.c


If the Prefork Module is installed it should be shown under compiled in modules. prefork.c is shown on the list.

2. Let’s install Apache Worker MPM
Let’s configure Worker MPM. So time to install Worker.

apt-get install apache-mpm-worker

If you are willing to install Prefork or Event MPMs it should be as below

apt-get install apache-mpm-prefork


apt-get install apache-MPM-event


When the installation is completed type apache2ctl -l and see whether the prefork.c/worker.c shows up under “Compiled in modules”.
Note in Ubuntu you can only have one MPM module at a time. It means if you install Worker while the server has got Prefork, Prefork will be automatically removed. When you are switching MPMs it’s a good idea to backup your .conf files.

3. Let’s understand Apache MPM directives
Please read the comments below to understand about the each directive and what they do. Below configurations are extracted from apache2.conf


# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
#
#
# worker MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadLimit: ThreadsPerChild can be changed to this maximum value during a
#              graceful restart. ThreadLimit can only be changed by stopping
#              and starting Apache.
# ThreadsPerChild: constant number of worker threads in each server process
# MaxClients: maximum number of simultaneous client connections

# MaxRequestsPerChild: maximum number of requests a server process serves


If you need more information on directives checks the documentation.

4. Time to customize the Apache Worker directives as we need
ServerLimit            10
StartServers             2
MaxClients          100
MinSpareThreads  25
MaxSpareThreads 75
ThreadsPerChild   20

Before customizing the directives you need to understand how the directives work. Let me explain in plain English. The server will start 2 child processes which are determined by StartServers directive. Each process will start 20 threads which are determined by ThreadsPerChild directive so this means 2 processes can service only 40 concurrent connections/clients(i.e. 20×2=40). So what if more requests come in.
Now if more concurrent users come, then another child process will start, that can serve another 20 users. But how many child processes can be started is controlled by ServerLimit parameter, this means that in the configuration above, I can have 10 child processes in total, with each child process can handle 20 thread, in total handling 10×20=200 concurrent users.
But there is a problem, number defined in MaxClients is 100 here, this means that after 5 child processes, no extra process will start since we have defined an upper cap of MaxClients. This also means that if I set MaxClients to 500, after 10 child processes and 200 connections, no extra process will start and we cannot service more than 200 concurrent clients even if we have increased the MaxClient parameter. In this case, we need to also increase ServerLimit to 500/20 i.e. MaxClients/ThreadsPerChild=25
Okay now you know the directives and how they work, the problem is how to calculate the directives. Let’s jump into calculating directive values.

ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x/((y-1)*1024)}'


You can use above shell script to determine an average amount of memory consumed by one Apache process. In addition to that, it’ll show the total amount of memory consumed by all Apache processes. Just unzip and execute with sh command. Accurate results will be shown when server is under heavy load.
The output

Apache Memory Usage (MB): 57.586
Average Process Size    (MB): 10.2

if in average, let’s assume that one Apache process consumes 50MB RAM and server has got RAM is 2048MB, and you want to leave 512MB for the rest of the processes, then:

MaxClients = (2048MB 512MB)/10MB = 153.6 ~ 153

So that’s how you configure Apache Multi-Processing Modules. If you have any questions let me know in the comments below. Your feedback is highly appreciated(happy-face).

Understanding Apache 2 MPM (worker vs prefork) ~ AwsTechNix






From time to time I want to learn more about stability and high availability web servers. I usually get a sandbox site where I start to play with configurations.
This time all started because I also like testing service providers, and this week I have been moving my site between Site44 and NearlyFreeSpeech to test its features for static sites.
Finally, in order to test how Apache may work when serving static files (I usually use Nginx for that matter), I installed Apache 2.2 on an Ubuntu 12.10 server (Linode). After doing that I headed to blitz.io and tested my Apache 2.2 serving static files. To my surprise, it served without a sweat for one minute a page with user concurrency of 250 users per second.
The next day I tried to install a Wordpress site on the same server, as soon as I tried to install PHP, aptly told me that it was going to uninstall apache2-mpm-worker and install apache2-mpm-prefork instead.
I instructed apt to do nothing and started to read about the differences. It turned out that Apache may work in two different ways which are:
Apache MPM Worker
In this mode, Apache works more or less like Nginx.
This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server. However, it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.
A single control process (the parent) is responsible for launching child processes. Each child process creates a fixed number of server threads as specified in the ThreadsPerChild directive, as well as a listener thread which listens for connections and passes them to a server thread for processing when they arrive.
There is the reason why my tests with blitz.io and Apache2 serving static files went so well, I was using a Nginx-like configured Apache.
Apache MPM Prefork
This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.
Differences are notable, and because we usually install Apache in a LAMP stack, we install something like this
 sudo apt-get install apache2 php5 mysql-server mysql-client
That way we end up with apache2-mpm-prefork package. Because I have installed my Apache2 copy as a stand alone server Ubuntu installed the apache2-mpm-worker pre-build package instead.
Why prefork
Despite MPM worker being more efficient than MPM prefork when the server is under heavy load, it is usually not installed that way because mod_php can't work with MPM worker.
We need to know a little bit about how you can execute PHP code when working with Apache.
The most common way is with mod_php which is an Apache module capable of understanding and executing PHP code inside Apache itself. This is the most common way to run PHP under Apache.
But there is another way, which is similar to what Nginx does, and that is having PHP interpreted as CGI, which means that Apache is going to forward PHP to an external interpreter and get the result back. Using this method allows you to keep Apache configured to run in worker MPM mode, and still be able to "understand" PHP.