Boost the Performance of Your Apache
Apache continues to be the most widely used web server among sites and Internet-facing computers.
Additionally, Apache keeps experiencing the largest growth among the top web servers, followed by Nginx and IIS. Thus, if you are a system administrator in charge of managing Apache installations, you need to know how to make sure your web server performs at the best of its capacity according to your (or your client’s) needs.
Here, 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.
#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 first things you need to consider.
In any event, you can check your currently installed version as follows:
# httpd -v [On RedHat/CentOS based systems] # apache2 –v [On Debian/Ubuntu based systems]
As a rule of thumb, stick with the update method provided by the package manager of your chosen distribution (
yum update httpd or
aptitude safe-upgrade apache2, for CentOS or Debian, respectively) unless there is no other way.
You can read the latest release notes in the Apache Documentation section in the Apache HTTP server Project website.
#2: If you are using a Kernel older than 2.4, consider upgrading now
Why? Kernel versions 2.4 and above have the sendfile kernel system call enabled by default. That, in turn, facilitates high-performance network file transfers (which are desired in the context of web server-client communications) and enables Apache to deliver static content faster and with lower CPU utilization by performing simultaneous read and send operations.
You can view your currently installed kernel with:
# uname -r
Although it is a process not intended for beginners, upgrading your kernel is an interesting exercise to learn more about the internals of Linux.
#3: Choose the Multi-Processing Module (MPM) that works best for your case
MPMs extend the modular functionality of Apache by allowing you to decide how to configure 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:
preforkMPM 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.
workerMPM uses several threads per child processes, where each thread handles one connection at a time. This is a good choice for high-traffic servers as it allows more concurrent connections to be handled with less RAM than in the previous case.
- Finally, the
eventMPM 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 threads per child process but with an advantage: it causes KeepAlive or idle connections (while they remain in that 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
To change this, you will need to edit:
# /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 installed php-fpm (or php5-fpm) service:
# systemctl restart httpd php-fpm && systemctl enable httpd php-fpm
# systemctl restart apache2 php5-fpm && systemctl enable apache2 php5-fpm