Question

How to fix apache high CPU usage on Ubuntu

Posted May 14, 2020 27.8k views
Apache

In the past, I used to managed a WordPress site on DigitalOcean droplet (One-click app) with 2 vCore, 4GB of memory, Ubuntu 16.04, Apache 2.4.18, and mysql community version 5.7.29.

Then the website was transferred to a droplet with 2 vCore, 4GB of memory, Ubuntu 18.04, Apache 2.4.29, and mysql community version 8.0.20.

Instantly after went live on the new droplet apache (“apache2 -k start” on htop) started to spike CPU to 30 and 40% and down to 0 to 3% and then after a few moments up again, and down again.

This is how the average graphs looks like (more or less): ||__||___|
the old server, it used to look like this —————–.

Both were one-click WordPress servers from DigitalOcean with default settings. Also, in the old server, the CPU usage with the same and sometimes even more load was always a flat line of 3 to 6%.

  1. Why is this problem happening?
  2. How to troubleshoot the problem?
  3. How to fix the problem?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
4 answers

Hello, @mhweb

What you can do is to monitor the droplet’s performance and check from where the CPU usage is coming from. Apache might be consuming more resources if the traffic to your sites is bigger as well.

The thing is the issue might not be related with your website itself and you might experiencing the speed issues because your server is running low on memory. That is why it’s important to make sure that everything is operating normally.

You can also examine the logs for any killed processes as this will help you to see if the server is killing processes in order to remain stable. You can check this by looking at the /var/log/messages and search for kill, e.g

grep -i kill /var/log/messages

You can also check for out of memory (The OOM Killer) references in the log to see if the system is critically low on memory. This usually indicates that you need to upgrade if there are a lot of references.

grep -i oom /var/log/messages

You can exacute top or htop via ssh to see what process is taking the most CPU or memory. Both tools are really handy when it comes to monitor the server’s performance. I would like to mention that top comes installed by default and you might need to install htop.

For Ubuntu:

`sudo apt-get install htop

Top provides a simple, real-time table of your processes, with the largest consumers on top. Running htop, we can see that it has a similar output, but is colorized, and is more interactive:

There is a really useful tutorial you can check on how to use top, htop, netstat, du and other tools to Monitor Server Resources:

https://www.digitalocean.com/community/tutorials/how-to-use-top-netstat-du-other-tools-to-monitor-server-resources

You can also use sar to check what’s going on your droplet. With sar, you can monitor performance of various Linux subsystems (CPU, Memory, I/O..) in real time.

You can use it like this:

To check the memory usage (Free and Used ):

sar -r

To check the cpu usage:

sar -u

You can also check this tutorial for monitoring the CPU usage on your droplet:

https://www.digitalocean.com/community/tutorials/how-to-monitor-cpu-use-on-digitalocean-droplets

You can also track the performance with the Droplet Graphs which are available in your control panel. You can find more information here:

https://www.digitalocean.com/docs/droplets/how-to/graphs/

Another thing that you could try using is this script here which summarizes your access logs. It would summarize your access log and give you the following information:

  • Top 20 POST requests
  • Top 20 GET requests
  • Top 20 IP logs and their geo-location

Another thing that could be causing the high CPU load is a cron job or any other scheduled task. I would recommend checking your crontab and reviewing your cron jobs. To do that you can run this command here:

  • crontab -l

Hope that this helps!

Regards,
Alex

by Justin Ellingwood
Monitoring a server's resources is an important task for every administrator. There are many command line tools made for this purpose. This guide will cover some of the more popular utilities for monitoring your VPS.
  • Thanks, I will check this out, but are you certain of the information? For example, the log/messages is a deprecated option.

    Oh, and I know about the problem because I used htop and syslog file.

    • Hey, @mhweb

      The /var/log/messages file doesn’t exist anymore on some distributions, that is correct most notably Ubuntu. Instead, the /var/log/syslog file is used. However you can still check if there are references in the log.

      Also you know that apache is causing the CPU spikes, but you still need to check if this is caused from general website traffic or the Apache configuration needs a tweak. For example the Apache workers can be exceeded and this can cause a CPU and memory spikes. You need to check the apache error_log and see if there are any logged errorss about this.

      You can also consider changing the Apache MPM in case you’re using prefork. You can check this using this command:

      apachectl -V | grep MPM
      

      Using MPM Event or Worker is considered a better option. I will recommend using Event.

      Worker

      Unlike Prefork, each child process under Worker can have multiple threads. As such, Worker can handle more requests with fewer resources than Prefork. Worker generally is recommended for high-traffic servers running Apache versions prior to 2.4. However, Worker is incompatible with non-thread safe libraries. If you need to run something that isn’t thread safe, you will need to stick with Prefork.

      Event

      Each process under Event also can contain multiple threads but, unlike Worker, each is capable of more than one task. Apache has the lowest resource requirements when used with the Event MPM. Event, though, is supported only on servers running Apache 2.4. Under Apache 2.2, Event is considered experimental and is incompatible with some modules on older versions of Apache. Nevertheless, on high-traffic Apache 2.2 servers where Apache has experienced issues with memory, upgrading Apache to take advantage of the Event MPM can yield significant results.

      Hope this helps!

      Regards,
      Alex

Hello, all

If anyone is looking for a way to configure their Apache server with MPM Event and PHP-FPM on Ubuntu droplet they can all follow this article:

https://www.digitalocean.com/community/tutorials/how-to-configure-apache-http-with-mpm-event-and-php-fpm-on-ubuntu-18-04

As already mentioned aach process under Event also can contain multiple threads but, unlike Worker, each is capable of more than one task. Apache has the lowest resource requirements when used with the Event MPM. Event, though, is supported only on servers running Apache 2.4. Under Apache 2.2, Event is considered experimental and is incompatible with some modules on older versions of Apache. Nevertheless, on high-traffic Apache 2.2 servers where Apache has experienced issues with memory, upgrading Apache to take advantage of the Event MPM can yield significant results.

Hope that this helps!
Regards,
Alex

by Albert Valbuena
In this tutorial you will improve the performance of the LAMP stack by changing the default multi-processing module from pre-fork to event and by using the PHP-FPM process manager to handle PHP code instead of the classic mod_php in Apache HTTP.

Hi, @sldownload

I’m glad that this is working for you! :)

Regards,
Alex

Hello @dittoasnar

Could you please elaborate more on the current issue that you’re experiencing?

Regards,
Alex

  • I’m not sure how to describe the detail.

    My website is custom coded PHP, not a CMS (Wordpress etc)

    After I finished installing PHP-FPM, and restarted Apache, my website wasn’t accessible.

    The configuration check on step 3 was okay (grep mpm & proxy etc), except for the last part. I can’t open the info.php, in the last step to check whether the PHP-FPM was even installed or not.

    That’s when I realized, all page of my website was not accessible. I tried restarting apache several times, but no success.

    I tried rebooting the server. It was okay for a few minute, but then later my website can’t be accessed again.

    I finally give up and reinstall the whole thing prior to my latest backup. But still haven’t solved my apache high CPU usage

    Was I missing something when I installed FPM ?

    • Hello there,

      My assumption is that you were most probably hitting either the PHP-FPM pool limits or the Apache workers limit and hence the issue was re-occurring again shortly after a reboot.

      I will recommend you to check our tutorial on how to monitor the CPU usage on a droplet:

      https://www.digitalocean.com/community/tutorials/how-to-monitor-cpu-use-on-digitalocean-droplets

      Hope that this helps!
      Regards,
      Alex

      by Melissa Anderson
      The amount of memory, the size of the cache, the speed of the input and output, and the speed and availability of processing power are key elements that affect the performance of your infrastructure. In this article, we'll focus on understanding and monitoring CPU usage.We'll describe how to use two common utilities in order to learn about your CPU usage, how to set up DigitalOcean monitoring to watch performance over time, and how to set set Alert Policies to notify you about significant chang