Many use apache2 as a web server. However, few think about optimizing its performance, which directly affects the website's page loading speed, script processing speed (especially PHP), as well as CPU load and increased RAM usage.
Thus, the following manual should help beginners (and not only) users.
All the examples provided below were used on Raspberry PI 3, Debian 9, Apache 2.4.38, PHP 7.3.
So, let's get started.
1. Disabling unused modules
The first method is simply disabling modules that you do not use:
You can view the list of currently used modules with the command:
apache2ctl -MTo disable a module, use the command:
a2dismod *module name*Accordingly, to enable a module, use the command:
a2enmod *module name*Note that when using a2dismod, the module name should be written without the word module itself.
For example, if you see apache2ctl -M in the output of the command proxy_module, then to disable it, you need to use the command — a2dismod proxy
The most system resource-consuming modules (based on personal experience) are:
- PHP, Ruby, Perl, and other modules for various scripting languages
- SSL
- Rewrite
- CGI
So, in cases where you do not need these modules, I recommend disabling them.
Also, after disabling any module, I recommend using the command — apache2ctl configtest, which will check the configuration of the websites being used and, if any of the disabled modules were necessary for them, will issue an error.
2. Changing MPM (Multi-Processing Module) and using php-fpm
By default, after installation, apache2 uses MPM Prefork (1 thread per 1 connection), which significantly reduces performance but improves stability and security.
However, for performance optimization, I recommend using MPM Worker, which allows multiple threads for a single connection.
To enable it, use the following commands:
a2dismod mpm_prefork //Disable prefork
a2dismod php7.3 //Disable the php module that depends on prefork
a2enmod mpm_worker //Enable worker
However, when using Worker, you may encounter a problem since the php7.3 module depends on the Prefork module.
To solve this problem, we will install the php7.3-fpm module, which will be used for executing php scripts:
apt-get update && apt-get install php7.3-fpm // Installing
systemctl enable php7.3-fpm && systemctl start php7.3-fpm // Enabling and starting the service
a2enmod php7.3-fpm && a2enconf php7.3-fpm.conf // Enabling the module and its configuration
It is worth noting that using php-fpm will also reduce the amount of RAM used by the apache2 process and slightly speed up the execution of php scripts.
3. Conclusion
Thus, with these simple actions, we managed 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 indeed useful but mostly already enabled by default), changing the parameters (configuration files) of MPM, disabling HostnameLookups, etc., but in this article, I aimed to highlight the points that helped me the most, and I hope they will help others.
Source: habr.com
