11号晚上开始,未托管的VPS就发了疯一样, 只要一启动 sudo service apache2 start CPU用量就接近 100%, 然后如果你不去管它的话, 很快服务器就停止反应,只能从控制面版里重启.
虽然有在前面放个cloudflare挡一下, 但是免费的(cache size 好像才 100M)感觉不是很好用,而且免费ssl有点坑.后来, 果断禁掉了一些IP (大多是疯狂没有节制的搜索爬虫),并且加强了安全,防止 root 帐号被暴力破解.
再一次查看最耗资源的进程,发现仍然是 apache2 请求.于是果断先备份所有数据文件, 然后尝试不同的参数配置(默认的对于我的情况不是很靠谱),以下是针对我的VPS的配置,我的VPS是双核 2.8G, 1G 内存, 20G 硬盘:
<IfModule mpm_prefork_module> StartServers 1 MinSpareServers 1 MaxSpareServers 3 MaxRequestWorkers 30 MaxConnectionsPerChild 3000 </IfModule> <IfModule mpm_worker_module> StartServers 1 MinSpareThreads 5 MaxSpareThreads 15 ThreadLimit 25 ThreadsPerChild 5 MaxRequestWorkers 25 MaxConnectionsPerChild 200 </IfModule> MaxKeepAliveRequests 0 Timeout 150 KeepAlive On KeepAliveTimeout 15
然后去掉我不确定是不是原因的WP插件,比如 iThemes Security, 然后又改了一下 mysql 的配置:
[mysqldump] quick quote-names max_allowed_packet = 16M [mysql] no-auto-rehash [isamchk] key_buffer = 32M sort_buffer_size = 16M default-storage-engine=MyISAM default-tmp-storage-engine=MyISAM skip-innodb # 需要把所有表类型转换成 MyISAM 格式. [myisamchk] key_buffer = 32M sort_buffer_size = 16M
把一些 Apache2 的模块去掉,原本是:
core_module (static) so_module (static) watchdog_module (static) http_module (static) log_config_module (static) logio_module (static) version_module (static) unixd_module (static) access_compat_module (shared) alias_module (shared) #去掉 auth_basic_module (shared) auth_mysql_module (shared) authn_core_module (shared) authn_file_module (shared) authz_core_module (shared) authz_host_module (shared) authz_user_module (shared) autoindex_module (shared)#去掉 deflate_module (shared) dir_module (shared) env_module (shared) filter_module (shared) mime_module (shared) mpm_prefork_module (shared) negotiation_module (shared) #去掉 php5_module (shared) rewrite_module (shared) setenvif_module (shared) socache_shmcb_module (shared) #去掉 ssl_module (shared) status_module (shared)#去掉
现在是 (命令 apache2ctl -M):
core_module (static) so_module (static) watchdog_module (static) http_module (static) log_config_module (static) logio_module (static) version_module (static) unixd_module (static) access_compat_module (shared) auth_basic_module (shared) auth_mysql_module (shared) authn_core_module (shared) authn_file_module (shared) authz_core_module (shared) authz_host_module (shared) authz_user_module (shared) deflate_module (shared) dir_module (shared) env_module (shared) filter_module (shared) mime_module (shared) mpm_prefork_module (shared) php5_module (shared) rewrite_module (shared) setenvif_module (shared) socache_shmcb_module (shared) ssl_module (shared)
只有 shared 的模块可以动态的通过 a2enmod 和 a2dismod 命令来开户或关闭.static 模块是编译在 apache2 里的, 需要重新编译代码才可以去掉.
还有一个就是要把 /etc/apache2/apache2.conf 里的记录LOG级别改高一些,默认是 LogLevel Warn 我改成了 LogLevel Error 这样记录的次数就减少了,能省不少CPU和I/O资源.
从LOG访问记录来看,网络爬虫抓取 steakovercooked.com 要比 justyy.com 高三倍.也一并把 PHP 文件静态化成缓存了.
重启 sudo service apache2 restart 之后要好许多,之前是卡死,现在是慢,但至少不需要不停的重启.还有许多优化的空间.
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK