You are on page 1of 21

5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server


Now!
Blog
Your
Start
×
Join Over 300K+ Linux Users

257,757 11,967 39,682


APACHE  20 I  TecMint :     
Are you subscribed?
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 1/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

5 Tips to Boost the Performance of  BEGINNER'S GUIDE FOR LINUX 

Your Apache Web Server Start learning Linux in minutes 

by Gabriel Cánepa | Published: November 23, 2015 | Last Updated: March 4, 2016
 Vi/Vim Editor BEGINNER'S GUIDE 
Learn vi/vim as a Full Text Editor 
 Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators | 4 Free
Shell Scripting eBooks
 Linux Foundation CertiLjcation 
According to a recent report by Netcraft (a well-known Internet Exam Study Guide to LFCS and LFCE
company that provides among other services web browser
usage statistics), Apache continues to be the most widely
used web server among sites and Internet-facing computers.  

How to Add Linux Host to Nagios Monitoring


Server Using NRPE Plugin

Nagios 4.2.0 Released – Install on RHEL/CentOS


7.x/6.x/5.x and Fedora 24-19

Install Cacti (Network Monitoring) on ×


Join Over 300K+ Linux Users
RHEL/CentOS 7.x/6.x/5.x and Fedora 24-12

Google Chrome 58 Released – Install on


5 Tips to Boost Your Apache Web Server Performance RHEL/CentOS 7/6 and Fedora 25-20

257,757 11,967 39,682


Additionally, Apache keeps experiencing the largest growth How to Install Ubuntu 16.10/16.04 Alongside With
Windows 10 or Are you subscribed?
8 in Dual-Boot
among the top web servers, followed by Nginx and IIS. Thus, if
4 Free of
you are a system administrator in charge Shell ScriptingApache
managing eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 2/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

installations, you need to know how to make sure your web Tecmint’s Guide to Red Hat RHCSA / RHCE
CertiLjcation Preparation Study Guide
server performs at the best of its capacity according to your
(or you client’s) needs.

In this article we will discuss a few tips that will help you
ensure that Apache will run smoothly and be able to handle the
number of requests you are expecting from remote clients.

However, please keep in mind that Apache was not designed


with the objective of setting benchmark records – but, even so,
it is still capable of providing high performance in almost any
usage case you can possibly think of.

TIP #1: Always keep Apache updated


to its latest version

It goes without saying that having the latest version of Apache


installed is probably one of the Ljrst things you need to
consider. As of November 19, 2015, the latest version of ×
Linux System Administrator Bundle
Join Over 300K+ Linux Users
Apache available in the CentOS 7 repositories is 2.4.6, whereas
with 7-Courses (96% off)
in Debian’s is 2.4.10.

However, there may be a recent improvement or a bug Ljx that Add to Cart - $69
has been added to a newly-released stable version, which is 257,757 11,967 39,682
 Ending In: 3 days
then made available to download and install from source. Are you subscribed?
Compilation and installation instructions
4 Free are
Shellalso provided
Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 3/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

here – just remember that if you choose this update method, Computer Hacker Professional
you may want to back up your current conLjguration Ljles / sites CertiLjcation Course (96% Off)
/ virtual hosts as a precaution.
Add to Cart - $59
In any event, you can check your currently installed version as
 Ending In: 4 days
follows:

# httpd ­v               [On RedHat/CentOS based systems] DOWNLOAD FREE LINUX EBOOKS


# apache2 –v             [On Debian/Ubuntu based systems] 
Complete Linux Command Line Cheat Sheet
The GNU/Linux Advanced Administration Guide
Securing & Optimizing Linux Servers
Linux Patch Management: Keeping Linux Up To
Date
Introduction to Linux – A Hands on Guide
Check Apache Version
Understanding the Linux® Virtual Memory
Manager
As a rule of thumb, stick with the update method provided by Getting Started with Ubuntu 16.04
the package manager of your chosen distribution A Newbie’s Getting Started Guide to Linux
( yum update httpd or Linux from Scratch – Create Your Own Linux OS
Linux Shell Scripting Cookbook, Second Edition
aptitude safe­upgrade apache2 , for CentOS or Debian,
Securing & Optimizing Linux: The Hacking ×
respectively) unless there is no other way. You can read the Join Over 300K+ Linux Users
Solution
latest release notes in the Apache Documentation section in User Mode Linux – Understanding and
the Apache HTTP server Project website. Administration

TIP #2: If you are using a Kernel older 257,757 11,967 39,682
    
than 2.4, consider upgrading now Are you subscribed?
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 4/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

Why? Kernel versions 2.4 and above have the sendLjle kernel Never Miss Any Linux Tutorials,
system call enabled by default. That, in turn, facilitates high Guides, Tips and Free eBooks
performance network Ljle transfers (which are desired in the Join Our Community Of 150,000+ Linux Lovers
context of web server-client communications) and enables and get a weekly newsletter in your inbox

Apache to deliver static content faster and with lower CPU


YES! SIGN ME UP
utilization by performing simultaneous read and send
operations.

You can view your currently installed kernel with:

# uname ­r

and compare it to the latest stable kernel in www.kernel.org


(4.3 at the time of this writing).

Check Linux Kernel Version

Although it is a process not intended for beginners, upgrading ×


Join Over 300K+ Linux Users
your kernel is an interesting exercise to learn more about the
internals of Linux.

TIP #3: Choose the Multi-Processing 257,757 11,967 39,682

Module (MPM) that works best for Are you subscribed?


your case 4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 5/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

In practice, MPMs extend the modular functionality of Apache


by allowing you to decide how to conLjgure the web server to
bind to network ports on the machine, accept requests from
clients, and use children processes (and threads, alternatively)
to handle such requests.

Beginning with version 2.4, Apache offers three different MPMs


to choose from, depending on your needs:

The prefork MPM uses multiple child processes without


threading. Each process handles one connection at a time
without creating separate threads for each. Without going
into too much detail, we can say that you will want to use
this MPM only when debugging an application that uses,
or if your application needs to deal with, non-thread-safe
modules like mod_php.
The worker MPM uses several threads per child
processes, where each thread handles one connection at a
time. This is a good choice for high-tra‫܀‬c servers as it
×
allows more concurrent connections to be handled with Join Over 300K+ Linux Users
less RAM than in the previous case.
Finally, the event MPM is the default MPM in most
Apache installations for versions 2.4 and above. It is
similar to the worker MPM in that it also creates multiple 257,757 11,967 39,682

threads per child process but with an advantage: it causes Are you subscribed?
KeepAlive or idle connections (while they remain in that
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 6/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

state) to be handled by a single thread, thus freeing up


memory that can be allocated to other threads. This MPM
is not suitable for use with non-thread-safe modules like
mod_php, for which a replacement such a PHP-FPM must
be used instead.

To check the MPM used by your Apache installation, you can


do:

# httpd ­V

The image below show that this particular web server is using
the prefork MPM.

×
Join Over 300K+ Linux Users

Check Apache MPM

257,757 11,967 39,682


To change this, you will need to edit:
Are you subscribed?
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 7/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

# /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.

and uncomment the line that loads the desired module like so:

LoadModule mpm_event_module modules/mod_mpm_event.so

Note: To make the event MPM work in Debian, you may have to
install the libapache2-mod-fastcgi package from the non-free
repositories.

Additionally, for CentOS you will need php-fpm (along with fcgi
and mod_fcgid) whereas in Debian it’s called php5-fpm (along
with apache2-mpm-event).
×
Last, but not least, restart the web server and the newly Join Over 300K+ Linux Users
installed php-fpm (or php5-fpm) service:

On RedHat/CentOS
257,757 11,967 39,682

# systemctl restart httpd php­fpm && systemctl enable httpd php­fpm Are you subscribed?


4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 8/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

On Debian/Ubuntu
SHARE

+
# systemctl restart apache2 php5­fpm && systemctl enable apache2 php5­fpm


0
Although you can set Apache to use a speciLjc MPM, that
conLjguration can be overridden on a per-virtual host basis in 
the same fashion as indicated earlier.
0

Just drop the corresponding tags into the conLjguration Ljle for
each virtual host and you’re ready to go – but make sure you’re

using one and only one MPM per vhost. 75

Finally, please note that regardless of your chosen distribution, 


php-fpm relies on the implementation of FastCGI, which is the
reason why I recommended the additional package
installations earlier.

For more details and examples on php-fpm and how it can


along with the event MPM increase the performance of ×
Join Over 300K+ Linux Users
Apache, you should refer to the o‫܀‬cial documentation.

This is what I see after changing the default MPM from prefork
to event in the same box shown in the previous image:
257,757 11,967 39,682

Are you subscribed?


4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 9/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

Choose Apache MPM Module

In CentOS 7, you will need to make sure that the http and https
services are enabled through the Ljrewall, and that the network
interface(s) are properly added to the default zone.

For example:

×
# firewall­cmd ­­zone=internal ­­add­interface=tun6to4 
Join Over 300K+ Linux Users
# firewall­cmd ­­zone=internal ­­add­interface=tun6to4 ­­permanent 
# firewall­cmd ­­set­default­zone=internal 
# firewall­cmd ­­add­service=http 
# firewall­cmd ­­add­service=https 
# firewall­cmd ­­add­service=http ­­permanent 
# firewall­cmd ­­add­service=https ­­permanent  257,757 11,967 39,682
# firewall­cmd ­­reload
Are you subscribed?
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 10/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

The reason why I’m bringing this up is because I recently


experienced an issue where the default Ljrewalld conLjguration
settings in a cloud VPS prevented php-fpm and Apache from
processing php Ljles.

As a basic test (I am sure you can think of more complicated


or stressful ones), I will create a php Ljle that checkes the
existence of another Ljle named test.php in the same
directory of two CentOS 7 servers with the same hardware
characteristics and load but with different MPM. One of them
will use event and the other one will use prefork:

Compare Apache Event and Prefork Module

This is the php code that I’ve saved into a Ljle named
checkiffileexists.php :
×
Join Over 300K+ Linux Users
<?php
$filename = 'test.php';
if (file_exists($filename)) {
echo "The file $filename exists";
} else { 257,757 11,967 39,682
echo "The file $filename does not exist";
} Are you subscribed?
?>
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 11/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

Then we will run the Apache benchmark tool (ab) with 200
simultaneous requests until 2000 requests are completed:

# ab ­k ­c 100 ­n 2000 localhost/checkiffileexists.php

Let’s run the test and compare the results. Pay attention to the
performance statistics:

×
Join Over 300K+ Linux Users

Apache Performance Load Testing

As you can see, the performance of the server with event is


257,757 11,967 39,682
highly superior to its prefork counterpart in every aspect of this
test. Are you subscribed?
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 12/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

TIP #4: Allocate RAM wisely for


Apache
Perhaps the most critical hardware item to be taken into
account is the amount of RAM allocated for each Apache
process. While you cannot control this directly, you can restrict
the number of child processes through the
MaxRequestWorkers directive (formerly known as MaxClients
in Apache 2.2), which will put limits on the RAM usage by
Apache. Again, you can set this value on a per host or per
virtual host basis.

To do this, you should take note of the average amount of RAM


used by Apache, then multiply it by the number of
MaxRequestWorkers, and that is the amount of memory that
will be allocated for Apache processes. One thing you never
want your web server to do is to begin using swap, as that will
signiLjcantly decrease its performance. Thus, you should
always keep the usage of RAM by Apache within the limits that
×
you can afford and never rely on swap for it. Join Over 300K+ Linux Users

For example, the following block will restrict the number of


simultaneous clients to 30. If more clients hit the host, they
may experience a delay or a momentary failure that can be 257,757 11,967 39,682
easily solved by refreshing the browser. While this may be
Are you subscribed?
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 13/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

considered undesirable, it is healthier for the server and in the


long run, best for your site as well.

You can place this block 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 – I am


using event here to continue with the concept outlined in the
previous tip:

<IfModule mpm_event_module>
StartServers 3
MinSpareThreads          25
MaxSpareThreads          75
ThreadLimit                      64
ThreadsPerChild          25
MaxRequestWorkers    30
MaxConnectionsPerChild    1000
</IfModule>

×
Join Over 300K+ Linux Users
In any event, it is highly recommended that you refer to the
Apache 2.4 docs to see which directives are allowed for your
chosen MPM.

257,757 11,967 39,682


TIP #5: Know your applications
Are you subscribed?
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 14/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

As a rule of thumb, you should not load any Apache modules


that are not strictly needed for your application to work. This
will require at least an overall knowledge of the applications
running on your server, specially if you are a system
administrator and there’s another team in charge of
development.

You can list the currently loaded modules with:

# httpd ­M          [On RedHat/CentOS based systems]
# apache2ctl ­M     [On Debian/Ubuntu based systems]

To unload / disable modules in CentOS, you will need to


comment out the line that begins with LoadModule (either in
the main conLjguration Ljle or in an auxiliary one inside
/etc/httpd/conf.modules.d.

On the other hand, Debian provides a tool called a2dismod to


disable modules and is used as follows: ×
Join Over 300K+ Linux Users

# a2dismod module_name

To enable it back: 257,757 11,967 39,682

Are you subscribed?


# a2enmod module_name 4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 15/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

In either case, remember to restart Apache for the changes to


take effect.

Summary
In this article we have reviewed 5 tips that will help you tune
the Apache web server and increase its performance. In
addition, you should remember that optimization and
performance without security is pointless, so you may want to
refer to the install mod_pagespeed to improve webserver
performance and Apache hardening tips article in
Tecmint.com as well.

Since we cannot adequately cover all the aspects of this topic


in this article, perhaps you will think of other ideas that you
would like to share with the rest of the community. If so, feel
free to let us know using the comment form below.

×
Join Over 300K+ Linux Users

If You Appreciate What We Do Here On


TecMint, You Should Consider:
257,757 11,967 39,682

1. Stay Connected to: Twitter | Facebook | Google Plus Are you subscribed?
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 16/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

2. Subscribe to our email updates: Sign Up Now

3. Get your own self-hosted blog with a Free Domain at

($3.95/month).

4. Become a Supporter - Make a contribution via PayPal


5. Support us by purchasing our premium books in PDF

format.

6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Tags: Apache

Gabriel Cánepa View all Posts


Gabriel Cánepa is a GNU/Linux sysadmin and web

developer from Villa Mercedes, San Luis, Argentina. He works for a ×
worldwide leading consumer product company and takes great pleasure Join Over 300K+ Linux Users
in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit


it here to become an TecMint author.
257,757 11,967 39,682

Are you subscribed?


4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 17/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

PREVIOUS STORY NEXT STORY

 5 Things I Dislike and Love About How to Setup Name-based and IP-
GNU/Linux based Virtual Hosts (Server Blocks)

with NGINX

 YOU MAY ALSO LIKE...

7  44 0

×
Join Over 300K+ Linux Users

257,757 11,967 39,682


How to Check Which Apache Virtual Hosting: How to Perform Internal
Apache Modules are IP Based and Name Redirection with Are you subscribed?
4 Free Shell Scripting eBooks forinNewbies
mod_rewrite Apache and Administrators
https://www.tecmint.com/apache­performance­tuning/ 18/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

Enabled/Loaded in Based Virtual Hosts in 25 NOV, 2016


Linux RHEL/CentOS/Fedora
15 NOV, 2016 6 JAN, 2014

20 RESPONSES

 Comments 1  Pingbacks 0

phalacee  March 22, 2017 at 8:31 am


This is a TERRIBLE guide.

You don’t need to compile from Source unless you need to do something
differently to the package maintainer!

And having your server drop a connection and force a user to refresh? WTF are
you on!?
Reply

« Older Comments

GOT SOMETHING TO SAY? JOIN THE DISCUSSION.


×
Join Over 300K+ Linux Users
Comment

257,757 11,967 39,682

Name * Email * Are you subscribed?


4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 19/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

Website

Notify me of followup comments via e-mail. You can also subscribe without
commenting.

Post Comment

LINUX MONITORING TOOLS LINUX INTERVIEW QUESTIONS OPEN SOURCE TOOLS

How to Limit the Network Bandwidth 10 Useful Interview Questions on Linux Atom – A Hackable Text and Source
Used by Applications in a Linux System Services and Daemons Code Editor for Linux
with Trickle 10 MAR, 2014 29 JUN, 2015
25 FEB, 2015
10 Core Linux Interview Questions and Install Wine 2.2 (Development Release)
Install IfTop (Bandwidth Monitoring) Answers to Run Windows Games and Apps on
Tool in RHEL / CentOS / Fedora 26 JAN, 2014 Linux ×
Join Over 300K+ Linux Users
5 OCT, 2012 31 OCT, 2015
10 Useful “Squid Proxy Server”
20 Useful Commands of ‘Sysstat’ Interview Questions and Answers in Install Joomla 3.6 Using LAMP (Linux,
Utilities (mpstat, pidstat, iostat and Linux Apache, MySQL, PHP) on RHEL,
sar) for Linux Performance Monitoring 28 JUL, 2014 CentOS257,757
& Fedora 11,967 39,682

4 SEP, 2014 30 JUL, 2015


Are you subscribed?
4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 20/21
5/7/2017 5 Tips to Boost the Performance of Your Apache Web Server

screenFetch – An Ultimate System 11 Basic Linux Interview Questions and 2013: The Golden Year for Linux – 10
Information Generator for Linux Answers Biggest Linux Achievements
7 JUL, 2014 18 NOV, 2013 31 DEC, 2013


Tecmint: Linux Howtos, Tutorials & Guides © 2017. All Rights Reserved.
This work is licensed under a (cc) BY-NC     
The material in this site cannot be republished either online or oꟌ�ine,
without our permission.

×
Join Over 300K+ Linux Users

257,757 11,967 39,682

Are you subscribed?


4 Free Shell Scripting eBooks for Newbies and Administrators
https://www.tecmint.com/apache­performance­tuning/ 21/21

You might also like