小赖子的英国生活和资讯

VPS低配置服务器优化的一些方法

阅读 桌面完整版

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 的模块可以动态的通过 a2enmoda2dismod 命令来开户或关闭.static 模块是编译在 apache2 里的, 需要重新编译代码才可以去掉.

还有一个就是要把 /etc/apache2/apache2.conf 里的记录LOG级别改高一些,默认是 LogLevel Warn 我改成了 LogLevel Error 这样记录的次数就减少了,能省不少CPU和I/O资源.

从LOG访问记录来看,网络爬虫抓取 steakovercooked.com 要比 justyy.com 高三倍.也一并把 PHP 文件静态化成缓存了.

重启 sudo service apache2 restart 之后要好许多,之前是卡死,现在是慢,但至少不需要不停的重启.还有许多优化的空间.

强烈推荐

微信公众号: 小赖子的英国生活和资讯 JustYYUK

阅读 桌面完整版
Exit mobile version