我在 32 位 VPS 主机上运行 CentOs 6.5。它仅运行 php-fpm、nginx 和 mysql。
我有一个网站,平均每天有 20 位访问者,但我最近从主机提供商那里收到了此消息。
这是一个自动通知,告知您名为“name.here”的 VPS 在过去一小时内由于交换而严重减慢了速度。
交换是由于内存不足而导致的。如果 VPS 的内存已满,则部分内存将写入硬盘以防止服务器崩溃。由于硬盘比内存慢很多倍,因此您的服务器性能会严重降低。
我试图理解这一点。一个内容很少的网站,只有 20 次访问,怎么会消耗那么多内存呢?
我不确定这是来自 php、centos 还是其他。但我这里有一些输出,有人可能会确定问题的核心。
[[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
这里可能存在什么问题?有解决办法吗?
更新
@Michael Hampton
输出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
答案1
从更新后的输出中我们可以看到,您有 34 个 PHP 工作进程正在运行(不计算主进程),它们全都在占用内存并且完全处于空闲状态。这意味着您已将 php-fpm 调整为在容量大得多的机器上运行,其流量远超实际流量。或者,也许您根本没有调整它。
目前,这些进程占用了超过 3/4 的虚拟机 RAM,但对您没有任何好处。
因此,请检查 php-fpm 池配置中的调整,例如默认配置/etc/php-fpm.d/www.conf
。对于这样的小型 VPS,我可能会从以下某个地方开始:
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 3
pm.max_spare_servers = 5
答案2
从表面上看,您需要针对小型 VPS 设置调整 MYSQL。默认情况下,它会占用尽可能多的 RAM。
复制自这网站,以便它保持可用,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
进行调整以适应,但这在 512MB VPS 上应该可以很好地工作。如果您使用 InnoDB,则需要启用它。