Performance optimization apache2

Many use apache2 as a web server. However, few people think about optimizing its performance, which directly affects the speed of loading pages on the site, the speed of processing scripts (in particular php), as well as the increase in CPU load and the increase in the amount of RAM used.

Thus, the following manual should help beginners (and not only) users.
All the examples below were used on Raspberry PI 3, Debian 9, Apache 2.4.38, PHP 7.3.

So, let's begin.

1. Disabling unused modules


The first method is the banal disabling of modules that you do not use: The

list of currently used modules can be viewed with the command:

apache2ctl -M

To disable the module, use the command:

a2dismod *название модуля*

Accordingly, to enable the module, use the command:

a2enmod *название модуля*

Please note that when using a2dismod , the name of the module must be written without the word module itself.

For example, if you saw proxy_module in the output of the apache2ctl -M command , then to disable it, you need to use the command - a2dismod proxy.

The most system-loading modules (from personal experience) are:

  • PHP, Ruby, Perl and other modules for various scripting languages
  • SSL
  • Rewrite
  • Cgi

So in those cases when you do not need these modules, I recommend disabling these modules.

Also, after disabling a module, I recommend using the command - apache2ctl configtest , which will check the configuration of the sites used and if any of the disabled modules was necessary for them, it will give an error.

2. Change MPM (Multi-Processing Module) and use php-fpm


By default, after installation, apache2 uses MPM Prefork (1 thread per 1 connection), which significantly reduces performance, but at the same time improves stability and security.

But to optimize performance, I recommend using MPM Worker, which allows you to use multiple threads on one connection at once.

To enable it, use the following commands:

a2dismod mpm_prefork  //Отключаем prefork
a2dismod php7.3  //Отключаем модуль php, который зависит от prefork
a2enmod mpm_worker  //Включаем worker

However, when using Worker, you may run into a problem because The php7.3 module depends on the Prefork module.

To solve this problem, install the php7.3-fpm module, which will be used to work out php scripts:

apt-get update && apt-get install php7.3-fpm  //Устанавливаем
systemctl enable php7.3-fpm && systemctl start php7.3-fpm  //Добавляем в автозагрузку и запускаем
a2enmod php7.3-fpm && a2enconf php7.3-fpm.conf  //Включаем модуль и конфиг для него

It is worth noting that the use of php-fpm will also reduce the amount of RAM used by the apache2 process and will slightly speed up the processing of php scripts.

3. Conclusion


Thus, with such simple actions, we were able to optimize performance and reduce the load on the machine (in this case, RPI3).

Of course, there are hundreds of other optimization options, such as enabling compression (which is really useful, but for the most part it is already enabled by default), changing MPM parameters (configuration files), disabling HostnameLookups, etc., but in this article I tried to reflect those are the points that have helped me the most, and I hope that they will help others.

Also popular now: