Many use apache2 as a web server. However, few people think about optimizing its performance, which is directly proportional to 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 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. Disable unused modules
The first method is to disable modules that you do not use:
')
The list of currently used modules can be viewed with the command:
apache2ctl -M
To disable a 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.
For example, if you saw the
proxy_module in the output of the
apache2ctl -M command , then to disable it you must use the command -
a2dismod proxyThe 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 any module, I recommend using the command -
apache2ctl configtest , which will check the configuration of the used sites and if any of the disabled modules were necessary for them, will generate 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 also improves stability and security.
But to optimize performance, I recommend using MPM Worker, which allows you to use several threads at once on one connection.
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 encounter 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 for testing 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 using php-fpm will also reduce the amount of RAM used by the apache2 process and speed up the development of php scripts slightly.
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 most of them are already enabled by default), changing the parameters (configuration files) of MPM, disabling HostnameLookups, etc., but in this article I tried to reflect exactly the points that helped me the most, and I hope that they will help others.