How to find out what is overloading your VPS in Virtualmin
Author: admin admin Reference Number: AA-00439 Views: 60441 Created: 2016-12-05 13:42 Last Updated: 2016-12-06 06:12 0 Rating/ Voters

People who get VPS servers for their websites usually do this because they have a website with many visitors that can't be hosted efficiently on shared hosting plans due to resource limits.

We install the latest the latest and greatest versions of PHP, APACHE, MYSQL on our VPS servers so our clients benefit from the latest optimized features in these recent versions.

Sometimes you may login to your Virtualmin panel and see that your server is fully busy and the resources are being used 100%:

As you see in the above picture, CPU USAGE is 100%, Real Memory is being used 75% and Virtual Memory is being used 92% ! Here in this article we want to find out what is going on the server.

CPU Usage debugging:

1- First you should see what processes are using CPU on your server, click on the CPU icon, you'll see a page like this:

In the first line you see

CPU load averages: 20.58 (1 mins) , 19.97 (5 mins) , 19.45 (15 mins)

The load average should be below 4 on a normal VPS server but as you see they are around 20 for this VPS!

In the second line you see:

CPU type: Intel(R) Xeon(R) CPU E5645 @ 2.40GHz , 3 cores

So we have 3 cores on this VPS and each core can be used upto 100% so we have  3 x 100resources to use here, let's look at the table now:

74.8%  of one core is used by /usr/sbin/mysqld  process which is the mysql server

There are around 20 processes of php-fpm that each are using around 9% which becomes 20*9% ~ 180%, it means that 2 of those 3 cores would process this each around 90%

OK so most of the process is used by PHP and the rest by MYSQL. Good we know the suspects at least! Keep in mind that PHP and MYSQL are inter-related to each other! e.g. When you have a wordpress website, the codes are running in PHP and PHP itself makes queries to get the necessary data from MYSQL database.

But in this case when you see that your server is overloaded by PHP it means that you have probably some BAD PHP codes that makes that server load! Let's look into this.

IMPORTANT Note 1: Check that you have a working CACHE plugin in the script of your website. Caching can significantly reduce PHP-MYSQL usage!

IMPORTANT Note 2: Check that you are running php 7.x which is at least 2 times faster than earlier 5.x versions

IMPORTANT Note 3: Check that you are running mysql 5.7

DEBUGGING PHP:

Fortunately the php scripts are running by PHP-FPM (This is iFastnet's default VPS Config!) rather than php-cgi, php-fcgi, mod_suphp or mod_php! This is good because among all the php handlers it's only PHP-FPM that allows us to easily find the bad code using PHP SLOW LOG!

The slowlog file is placed by default in /home/USERNAME/logs/slow_log which USERNAME is the username of your virtual server. run the following command in SSH to see the content:

cat /home/USERNAME/logs/VIRTUAL-SERVER-DOMAIN.COM_slow_log

You should now see what lines in your php files take long time to be executed.

Note: PHP-FPM is configured by default to log scripts that take longer than 5 seconds in the above slow_log file. You can change this setting by editing the fpm config file inside the your php-fpm config folder which is:

PHP v5.x on Debian OS: /etc/php5/fpm/pool.d/
PHP v7.x on Debian OS: /etc/php/7.0/fpm/pool.d/

PHP v5.x on Centos distros: /etc/php-fpm.d/
PHP v7.x on Centos distros: /etc/opt/remi/php70/php-fpm.d/

You should look for a file 12345.conf where 12345 is the userid of your virtual server's user. You can get the user by running id USERNAME command in SSH where username is the username of your virtual-server. Edit the file and set the request_slowlog_timeout variable:

request_slowlog_timeout = 3
slowlog = /home/maurice/logs/maurice-info.mu_slow_log

and finally restart php-fpm process for the changes to take effect

PHP v5.x on Debian : service php5-fpm restart
PHP v7.x on Debian : service php7.0-fpm restart

PHP v5.x on Centos : service php-fpm restart
PHP v7.x on Centos : service php70-php-fpm restart


DEBUGGING MYSQL:

You need to edit your mysql config file:

On Debian: /etc/mysql/my.cnf
On Centos: /etc/my.cnf

and add the following lines to the [mysqld] section of the file:

log_slow_queries = 1

long_query_time = 8
slow_query_log = /var/log/mysql_slow.log

Then restart mysql service:

On Debian: service mysql restart
On Centos: service mysqld restart

You can now watch the slow log file to see queries that take longer than 8 seconds to execute:

tail -f /var/log/mysql_slow.log

Quick Jump Menu