Apache is very easy to install and initialize, but it comes with numerous modules pre-installed and as a result you may have performance problems when running on a low RAM VPS.
So, there is not always need for a bigger server, but you can simply configure Apache settings the right way and have the best possible performance. Here are a few things you should do to accomplish that.
This tutorial is for Ubuntu 14.04 LTS but the principles are similar to previous versions as well.
1. Disable Apache modules
You can see a list of all the modules installed with the command
ls /etc/apache2/mods-available/ and all modules that are currently active with
ls /etc/apache2/mods-enabled/ .
On the initial Apache installation there are 15 or more modules enabled by default. These are too many and most of them aren’t needed.
First of all, you have to make a list of all the currently active modules and save it for future reference in case you disable something that is needed and you have to re-enable it. Then simply disable modules one-by-one with the command (using moduleName as an example):
sudo a2dismod moduleName
… and restart Apache after each change to see if any error occurs.
Some modules that you should disable if you don’t need them are:
After you disable a module and reload the Apache configuration, you can check for errors by opening the Apache error log with a text editor like nano.
sudo nano /var/log/apache2/error.log
If you get an error just re-enable the corresponding module with:
sudo a2enmod moduleName
Then, restart Apache again until you get the minimum list sorted out!
2. Set up mpm_prefork
With the default Apache configuration (which isn’t well balanced for small servers), the memory can be depleted quickly and your cloud server can become overloaded. This will cause the web page to hang in a constantly loading state. It is likely that the server will keep these dead Apache processes active, attempting to serve content unnecessarily, reducing the number of processes available to serve.
To resolve this issue, first you have to find out how much RAM your server needs apart from Apache and the average amount of memory that Apache processes are using.
While Apache is running, open the top command on the server.
top -bn 1
On the result table:
- add the numbers on the RES column up for every process, except apache2 and then remove it from the total amount of RAM. Let’s say you get 600 MB.
- get the average from all apache2 processes. Let’s say get 20 MB.
- then, just do the division 600/20 = 30 maximum Apache processes
Next step, edit the mpm_prefork module config file:
sudo nano /etc/apache2/mods-enabled/mpm_prefork.conf
It may look like this:
<IfModule mpm_prefork_module> StartServers 2 MinSpareServers 2 MaxSpareServers 5 MaxRequestWorkers 30 MaxConnectionsPerChild 0 </IfModule>
- StartServers: number of server processes to start.
- MinSpareServers: minimum number of server processes which are kept spare.
- MaxSpareServers: maximum number of server processes which are kept spare.
- MaxRequestWorkers: maximum number of server processes allowed to start.
- MaxConnectionsPerChild: maximum number of requests a server process serves.
In the MaxRequestWorkers setting, you have to change it to whatever you found in the division above.
Now, if your VPS gets overloaded and reaches the maximum number of clients it can serve at once, it will serve those and other users will simply get a quick failure. They can then reload the page to try again. It’s much better to have these connections close quickly but leave the server in a healthy state rather than hanging open forever.
3. Reduce your logs
To maximize performance, you can definitely log less information. In the default Apache configuration the LogLevel setting is set to warn. You can change that to error to keep only the error messages.
Open the Apache configuration file:
sudo nano /etc/apache2/apache2.conf
Find the line:
… and change it to:
Then, save the file and restart Apache with:
sudo service apache2 restart
The default file that Apache keeps the logs is /var/log/apache2/error.log.
4. Consider replacing mod_php
A great alternative is php-fpm module, which is a separate process that uses the fastcgi protocol. With php-fpm, the memory for Apache processes can drop to 10 MB for static content and 60 MB for dynamic.
Although it may be a little difficult to get things to work with this change, it’s worth the effort.
5. Consider replacing mpm_prefork
Most Apache configurations are using the mpm_prefork module which is thread safe and uses multiple child processes with one thread each and each process handles one connection at a time. If you don’t need external modules such as PHP or Rails then you can replace it with the apache2-mpm-worker module, which often is faster. Worker MPM uses multiple child processes with many threads each and each thread handles one connection at a time.
In order to enable the worker module, you have to install it.
sudo apt-get install apache2-mpm-worker
This command will uninstall mpm_prefork, mos_php and other incompatible add-on modules.
Want to contribute?
You could earn up to $300 by adding new articles
Suggest an update
Request an article