今天中午的时候,博客突然无法打开了。输入网址打开网站后显示了“您的站点遇到了致命错误。 ”仔细回想一下,最近博客除了遭受到了攻击外,也没有操作别的东西,下面就来分享一下出现这种问题的解决方法。
一、开启WP Debug模式当你的Wordpress遇到错误时,不要惊慌,选择打开Wordpress的Debug模式,调用PHP错误,方法参考:WordPress错误诊断模式-专治WP页面空白,服务器500错误,插件冲突
二、找到WP致命的错误打开了Wordpress的Debug模式后,此时网页就会显示PHP错误了,如下:
Fatal error: Uncaught RedisException: OOM command not allowed when used memory > ‘maxmemory’. in /xxx/wzfou.com/wp-content/plugins/w3-total-cache/Cache_Redis.php:68 Stack trace: #0 /xxxxx/wzfou.com/wp-content/plugins/w3-total-cache/Cache_Redis.php(68): Redis->setex(‘w3tc_1987146988…’, 180, ‘a:2:{s:7:”conte…’) #1 /xxxx/wzfou.com/wp-content/plugins/w3-total-cache/ObjectCache_WpObjectCache_Regular.php(293): W3TC\Cache_Redis->set(‘0optionsallopti…’, Array, 180) #2 /xxx/wzfou.com/wp-content/plugins/w3-total-cache/ObjectCache_WpObjectCache_Regular.php(388): W3TC\ObjectCache_WpObjectCache_Regular->set(‘alloptions’, Array, ‘options’, 0) #3 /xxx/wzfou.com/wp-content/plugins/w3-total-cache/ObjectCache_WpObjectCache.php(77): W3TC\ObjectCache_WpObjectCache_Regular->add(‘alloptions’, Array, ‘options’, 0) #4 /xxxx/wzfou.com/wp-content/object-cache.php(95): W3TC\ObjectCache_WpObjectCache->add(‘alloptions’, Array, ‘options’, 0) #5 /xxxx/wzfou.com/wp-includes/option.php(258): in /xxxx/wzfou.com/wp-content/plugins/w3-total-cache/Cache_Redis.php on line 68
错误提示:“Uncaught RedisException: OOM command not allowed when used memory > ‘maxmemory’. in /xxx/wzfou.com/wp-content/plugins/w3-total-cache/Cache_Redis.php”,显然是因为开启了Redis对象缓存而导致了错误。查看了Redis配置:
root@localhost:~# redis-cli 127.0.0.1:6379> info memory # Memory used_memory:250161160 used_memory_human:238.57M used_memory_rss:274608128 used_memory_rss_human:261.89M used_memory_peak:254227888 used_memory_peak_human:242.45M used_memory_peak_perc:98.40% used_memory_overhead:7712830 used_memory_startup:803088 used_memory_dataset:242448330 used_memory_dataset_perc:97.23% allocator_allocated:250366144 allocator_active:260894720 allocator_resident:268197888 total_system_memory:2111430656 total_system_memory_human:1.97G used_memory_lua:37888 used_memory_lua_human:37.00K used_memory_scripts:0 used_memory_scripts_human:0B number_of_cached_scripts:0 maxmemory:251000000 maxmemory_human:239.37M maxmemory_policy:noeviction allocator_frag_ratio:1.04 allocator_frag_bytes:10528576 allocator_rss_ratio:1.03 allocator_rss_bytes:7303168 rss_overhead_ratio:1.02 rss_overhead_bytes:6410240 mem_fragmentation_ratio:1.10 mem_fragmentation_bytes:24487992 mem_not_counted_for_evict:0 mem_replication_backlog:0 mem_clients_slaves:0 mem_clients_normal:390678 mem_aof_buffer:0 mem_allocator:jemalloc-5.1.0 active_defrag_running:0 lazyfree_pending_objects:0 127.0.0.1:6379>
从配置中可以看出:已经占用的内存used_memory_human:238.57M,系统允许Redis最大使用的内存为:maxmemory_human:239.37M,确实是占用完毕,空间已经没有了。
三、解决Redis内存不足如果你用的是Oneinstack,可以参考以下方法来修改Redis的配置,顺便你也可以修改一下Memcached配置,将默认的内存加大:
#Redis默认端口:6379 #Memcached默认端口:11211 #默认监听地址:127.0.0.1 #调整Redis最大内存大小? vi /usr/local/redis/etc/redis.conf maxmemory 1024000000#单位字节,默认1G,可调整 service redis-server restart#重启生效 #增加Memcached最大内存大小? vi/etc/init.d/memcached CACHESIZE=256 #单位M,默认256M,可调整 service memcached restart#重启生效
设置淘汰策略:maxmemory-policy volatile-lru
其它几种淘汰策略说明如下:
noeviction
默认策略
不驱逐
当没有足够的内存给Redis使用时,执行写命令时会报一个异常:redis.exceptions.ResponseError,OOM command not allowed when used memory > ‘maxmemory’volatile-lru
对存在expire的key,执行lru淘汰策略(驱逐最近最少使用的数据)
如果没有存在expire的key,则在内存不够时执行写命令时也会报一个异常:redis.exceptions.ResponseError,OOM command not allowed when used memory > ‘maxmemory’,同noeviction policyallkeys-lru
针对所有key执行lru
没办法限制些算法针对某些key去删除
如果你的应用需要持久化一些数据,请不要使用allkeys-lru策略volatile-random
对存在expire的key,随机选择驱逐某个keyallkeys-random
针对所有key,随机选择驱逐某个keyvolatile-ttl
对存在expire的key中,优先驱逐ttl值最小的四、总结WordPress优化加速是一个老生常谈的话题,为了达到最好的效果,我们经常要根据自己的服务器实际配置来合理调整优化策略。