3

I have CentOs 6.5 running on a 32bit VPS host. It is running only php-fpm, nginx, and mysql.

I have a site that gets on average 20 visitors per day, but I am recently getting this message from my host provider.

This is an automated notification to inform you that your VPS named 'name.here' has been severely slowed down the past hour because of swapping.

Swapping is the result of a lack of memory. If the memory of a VPS is completely filled, parts of memory are written to hard disk to prevent the server from crashing. Because hard disks are many times slower than memory your server performance is severely reduced.

I am trying to make a sense of that. How can a site with small content getting 20 visits consumes that much memory?

I am not sure if this is from php, centos ... or other. But I have some outputs here that someone may determine core of the problem.

[[email protected]~]# free -m
             total       used       free     shared    buffers     cached
Mem:           498        489          9          0          1         19
-/+ buffers/cache:        469         29
Swap:         1022        180        842

top - 18:33:36 up 65 days, 1 min,  1 user,  load average: 0.00, 0.02, 0.00
Tasks: 109 total,   1 running, 108 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.0%us,  0.3%sy,  0.0%ni, 98.3%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    510868k total,   503092k used,     7776k free,     1904k buffers
Swap:  1047544k total,   185096k used,   862448k free,    20956k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 7435 mysql     20   0  352m 7752 2852 S  1.0  1.5  56:23.52 mysqld
17806 nginx     20   0 50088  12m 2640 S  0.7  2.5  18:13.65 php-fpm
  984 root      20   0  8940  372  272 S  0.3  0.1   0:29.68 sshd
 3148 root      20   0  2692 1128  880 R  0.3  0.2   0:00.15 top
    1 root      20   0  2900  416  292 S  0.0  0.1   0:07.29 init

What could be the problem here? Is there any fix to it ?


UPDATE

@Michael Hampton Output for top > Shift M

top - 14:34:29 up 66 days, 20:02,  2 users,  load average: 0.00, 0.00, 0.00
Tasks: 110 total,   1 running, 109 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    510868k total,   504528k used,     6340k free,     3084k buffers
Swap:  1047544k total,   179784k used,   867760k free,    19532k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
19041 nginx     20   0 58672  22m 2660 S  0.0  4.5  24:00.18 php-fpm
17805 nginx     20   0 58672  22m 2688 S  0.0  4.5  16:46.40 php-fpm
19040 nginx     20   0 58776  17m 2684 S  0.0  3.6  24:27.53 php-fpm
19046 nginx     20   0 53412  17m 2692 S  0.0  3.5  22:51.54 php-fpm
17796 nginx     20   0 53604  16m 2648 S  0.0  3.3  15:14.62 php-fpm
19042 nginx     20   0 52144  15m 2660 S  0.0  3.0  22:16.40 php-fpm
17186 nginx     20   0 52208  15m 2656 S  0.0  3.0  25:22.13 php-fpm
24580 nginx     20   0 50600  14m 2660 S  0.0  3.0  13:23.23 php-fpm
19014 nginx     20   0 52084  14m 2692 S  0.0  3.0  22:16.57 php-fpm
19007 nginx     20   0 58724  14m 2724 S  0.0  3.0  25:09.20 php-fpm
19032 nginx     20   0 50156  14m 2692 S  0.0  3.0  24:25.53 php-fpm
17830 nginx     20   0 50088  14m 2696 S  0.0  2.9  16:37.53 php-fpm
17816 nginx     20   0 49996  14m 2768 S  0.0  2.9  18:57.37 php-fpm
19043 nginx     20   0 50140  14m 2648 S  0.0  2.9  23:30.66 php-fpm
17806 nginx     20   0 50088  14m 2656 S  0.0  2.9  19:38.99 php-fpm
17188 nginx     20   0 50100  14m 2656 S  0.0  2.9  22:46.46 php-fpm
17817 nginx     20   0 49836  14m 2660 S  0.0  2.9  18:29.45 php-fpm
17820 nginx     20   0 48892  13m 2972 S  0.0  2.8  18:01.07 php-fpm
19030 nginx     20   0 58824  13m 2652 S  0.0  2.8  23:40.90 php-fpm
17798 nginx     20   0 48780  13m 2696 S  0.0  2.7  17:09.82 php-fpm
17187 nginx     20   0 58672  13m 2716 S  0.0  2.7  23:04.46 php-fpm
17807 nginx     20   0 52076  13m 2660 S  0.0  2.6  16:45.52 php-fpm
19029 nginx     20   0 58924  12m 2688 S  0.0  2.6  24:00.66 php-fpm
17808 nginx     20   0 58676  12m 2688 S  0.0  2.6  17:31.33 php-fpm
17799 nginx     20   0 50088  12m 2660 S  0.0  2.6  17:41.13 php-fpm
19015 nginx     20   0 58680  12m 2656 S  0.0  2.6  21:59.83 php-fpm
18295 nginx     20   0 48512  12m 2692 S  0.0  2.6  16:57.08 php-fpm
19027 nginx     20   0 48368  12m 2656 S  0.0  2.6  24:40.65 php-fpm
17819 nginx     20   0 48208  12m 2656 S  0.0  2.5  16:43.87 php-fpm
19031 nginx     20   0 49960  12m 2648 S  0.0  2.5  23:28.34 php-fpm
17832 nginx     20   0 47884  12m 2660 S  0.0  2.5  16:04.87 php-fpm
17818 nginx     20   0 47884  12m 2660 S  0.0  2.5  16:03.53 php-fpm
17831 nginx     20   0 47864  12m 2696 S  0.0  2.5  16:43.48 php-fpm
18249 nginx     20   0 47884  12m 2664 S  0.0  2.5  16:27.29 php-fpm
 7435 mysql     20   0  352m 7780 2872 S  0.3  1.5  62:00.00 mysqld
19033 nginx     20   0 42540 7368 2676 S  0.0  1.4  24:04.93 php-fpm
31015 root      20   0 12028 3708 2940 S  0.0  0.7   0:00.17 sshd
30826 postfix   20   0 13628 2260 1640 S  0.0  0.4   0:00.00 pickup
30995 root      20   0  5252 1704 1412 S  0.0  0.3   0:00.03 bash
31020 root      20   0  5256 1704 1416 S  0.0  0.3   0:00.03 bash
19028 nginx     20   0 58668 1648 1528 S  0.0  0.3   9:11.84 php-fpm
4
  • Can you give it more RAM? 500MB is a little low for mysql with apache and addons. Also does your hoster run the backups? They could be slowing the system down. Try writing a shell script to monitor resource usage, or use sar, you can run sar on past data with the -f option on the files under /var/log/sa/ . Use iostat to check disk throughput, iostat -xmd /dev/sd* 5 .
    – user232238
    Jul 10, 2014 at 17:07
  • 1
    First, sort top by memory usage by pressing Shift-M. Then post its display. Jul 10, 2014 at 17:08
  • Sorry guys. I didn't have internet so I couldn't respond. @nickl Yes I am on a 512 RAM. I will try your recommendations. Although I am new at this. Jul 12, 2014 at 12:38
  • @MichaelHampton I have updated the answer. Check out the output. Hope it helps Jul 12, 2014 at 12:39

2 Answers 2

4

We can see from your updated output that you have 34 PHP workers running (not counting the master process), all of which are taking up memory and completely idle. This means you have tuned php-fpm to run on a much larger capacity machine with far more than the traffic you are actually getting. Or, perhaps, have not tuned it at all.

At the moment, those processes are taking up over 3/4 of your VM's RAM and giving you absolutely no benefit.

So, check the tuning in the php-fpm pool configuration, e.g. the default /etc/php-fpm.d/www.conf. For a small VPS like that I would probably start somewhere around:

pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 3
pm.max_spare_servers = 5
3
  • Ok. Done. Almost all of my settings were twice as much as yours :) I have one question though: How do I know now, if my server is getting exhausted or not? There are so many settings I am not familiar with, so instead of waiting for warning from my host provider, is there a quick way to find out, if server has enough memory to work with ? Jul 12, 2014 at 13:13
  • That takes experience and I cannot answer it in a 500 character comment! :) Jul 12, 2014 at 13:18
  • No need. I just checked free -m line by line with my previous one, and there is a major difference. I guess your answer really worked :) thanks Jul 12, 2014 at 13:21
2

From the looks of, you need to tune MYSQL for a small VPS setup. By default, it'll gobble up as much RAM as possible.

Copied from this site so it'll remain available, my.cnf:

[mysqld]
     port            = 3306
     socket          = /var/lib/mysql/mysql.sock
     skip-locking
     key_buffer = 16K
     max_allowed_packet = 1M
     table_cache = 4
     sort_buffer_size = 64K
     read_buffer_size = 256K
     read_rnd_buffer_size = 256K
     net_buffer_length = 2K
     thread_stack = 64K

     # For low memory, InnoDB should not be used so keep skip-innodb uncommented unless required
     skip-innodb

     # Uncomment the following if you are using InnoDB tables
     #innodb_data_home_dir = /var/lib/mysql/
     #innodb_data_file_path = ibdata1:10M:autoextend
     #innodb_log_group_home_dir = /var/lib/mysql/
     #innodb_log_arch_dir = /var/lib/mysql/
     # You can set .._buffer_pool_size up to 50 - 80 %
     # of RAM but beware of setting memory usage too high
     #innodb_buffer_pool_size = 16M
     #innodb_additional_mem_pool_size = 2M
     # Set .._log_file_size to 25 % of buffer pool size
     #innodb_log_file_size = 5M
     #innodb_log_buffer_size = 8M
     #innodb_flush_log_at_trx_commit = 1
     #innodb_lock_wait_timeout = 50

     [mysqldump]
     quick
     max_allowed_packet = 16M

     [mysql]
     no-auto-rehash
     # Remove the next comment character if you are not familiar with SQL
     #safe-updates

     [isamchk]
     key_buffer = 8M
     sort_buffer_size = 8M

     [myisamchk]
     key_buffer = 8M
     sort_buffer_size = 8M

     [mysqlhotcopy]
     interactive-timeout

Adjust to suit, but that should work well on a 512MB VPS. If you use InnoDB, you'll need to enable it.

3
  • I don't think 8MB resident is excessive for MySQL. Jul 10, 2014 at 17:08
  • @Nathan C Thanks. In addition to Michael's answer. I am also trying to tweak mysql as you suggested. Thanks Jul 12, 2014 at 13:15
  • @CONtext You should only make one change at a time. Otherwise you will be unable to evaluate the effects of a change. Jul 12, 2014 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .