Optimizing Apache2 for low spec servers

Recently my low spec web servers were starting to see more traffic and as such the available memory was running out.

This will be part 1 on how to optimize a low spec web server to be less hungry on the resources.

One of the most hungry processes on your web server by default will be Apache. But there are a few quick wins you can put in place to reduce its resource drain, even with web servers that are running multiple CMS (in my case both drupal and joomla)

Let's jump in and start modifying the apache2.conf:

sudo pico /etc/apache2/apache2.conf

You want to scroll through until you reach the mpm_prefork_module area. The defaults here are chewing through your servers memory - we want to make sure we are not starting too many servers or have too many spare servers remaining. Here are the settings I have used:

<IfModule mpm_prefork_module>
StartServers 1
MinSpareServers 1
MaxSpareServers 3
MaxClients 10
MaxRequestsPerChild 3000

<IfModule mpm_worker_module>
StartServers 1
MinSpareThreads 5
MaxSpareThreads 15
ThreadLimit 25
ThreadsPerChild 5
MaxClients 25
MaxRequestsPerChild 200

Also, you have an option to make some adjustments to KeepAliveTimeout. If the server is just going to be used for basic operations like static websites then something like 15 is ok. If you are using the server with a CMS like Joomla or Drupal then I would suggest keeping it to its default of 5 - this is also the case if you are using the mysql server remotely. This setting is the amount of time the server will wait for subsequent requests on a persistent connection. In reality it means that for the first connections it will wait the full time before 'processing' the login or page load.

Now to restart the server and to monitor the changes

service apache2 restart

I recommend checking out HTOP for monitoring the server, its appetite and what specific processes are doing the mastication.

Copyright 2020 OReillyIT. All rights reserved.