Limit the Number of Apache Processes and Children
Most operating systems’ default Apache configurations are not well suited for smaller servers– 25 child processes or more is common. If each of your Apache child processes uses 120MB of RAM, then your VPS would need 3GB just for Apache.
One visitor’s web browser may request 4 items from the website at once, so with only 7 or 8 people trying to load a page at the same time your cloud server can become overloaded. This causes the web page to hang in a constantly loading state for what seems like an eternity.
It is often the case that the server will keep these dead Apache processes active, attempting to serve content long after the user gave up, which reduces the number of processes available to serve users and reduces the amount of system RAM available. This causes what is commonly known as a downward spiral that ends in a bad experience for both you and your site’s visitors.
What you should do is figure out how much RAM your application needs, and then figure out how much is left, and allocate most of that to Apache.
For example, if you have three php-fpm processes handling dynamic content, and each can use up to 70MB of RAM, and your MySQL server may use up to 120MB of RAM, that combines for a total of 330MB used by the application. This allows you to allocate about 150MB to Apache.
While Apache is running open the top command on the server. I’ll paste a little bit of what you’d see, trimming out most of the lines that aren’t pertinent:
top -bn 1
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15015 www-data 20 0 232m 9644 1900 S 0.0 1.6 0:00.02 apache2
15016 www-data 20 0 232m 9644 1900 S 0.0 1.6 0:00.01 apache2
15017 www-data 20 0 232m 9644 1900 S 0.0 1.6 0:00.02 apache2
Notice the RES column for an Apache child process and make note of its RES value. For example, on my virtual server which has been well optimized, the value is 9,644, which means it’s using not quite 10MB of RAM. If I limit Apache to a maximum of 15 child processes, then it should max out at about 150MB of RAM.
Edit your cloud server’s apache config file, which on Ubuntu and Debian is /etc/apache2/apache2.conf and locate the section for the mpmpreforkmodule configuration. Look for the MaxClients line and set it to 15, then save and restart Apache.
Here is an example of what you’ll look for in Ubuntu: