网站性能不佳 – 高CPU和磁盘使用率

在过去的几天里,我的网站performance一直很不好,查询花了很多时间来执行。 我的CPU使用率在本周四次达到100%左右。 这里是top的输出

 top - 00:08:03 up 3 days, 21:47, 2 users, load average: 6.06, 1.95, 0.84 Tasks: 92 total, 2 running, 90 sleeping, 0 stopped, 0 zombie %Cpu(s): 86.1 us, 12.9 sy, 0.0 ni, 0.0 id, 0.0 wa, 1.0 hi, 0.0 si, 0.0 st KiB Mem: 1017948 total, 773520 used, 244428 free, 107200 buffers KiB Swap: 0 total, 0 used, 0 free. 257228 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 28433 www-data 20 0 854660 69288 5608 S 98.7 6.8 0:47.36 apache2 28469 www-data 20 0 529692 7692 3012 S 0.7 0.8 0:00.13 apache2 28514 root 20 0 24820 1488 1064 R 0.7 0.1 0:00.08 top 25 root 20 0 0 0 0 S 0.3 0.0 1:00.70 kworker/0:1 28518 postgres 20 0 370016 6984 4276 S 0.3 0.7 0:00.01 postgres 1 root 20 0 33384 1288 0 S 0.0 0.1 0:11.70 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.13 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:09.40 ksoftirqd/0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H 7 root 20 0 0 0 0 S 0.0 0.0 0:45.06 rcu_sched 8 root 20 0 0 0 0 R 0.0 0.0 1:54.47 rcuos/0 9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0 11 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 

Apache似乎占用了很多的CPU,但我不知道为什么。 直到几天前,它的工作状况都非常好。 我已经通过删除未使用的模块来优化Apache,将其调整为只有less量的备用子项正在运行,但似乎没有任何区别。 我也安装了mod-evasivemod-qos来防止DDOS。 这是我的Apacheconfiguration

 Timeout 30 KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 5 <IfModule mpm_prefork_module> StartServers 1 MinSpareServers 1 MaxSpareServers 3 MaxClients 10 MaxRequestsPerChild 3000 </IfModule> <IfModule mpm_worker_module> StartServers 1 MinSpareThreads 5 MaxSpareThreads 15 ThreadLimit 25 ThreadsPerChild 5 MaxClients 25 MaxRequestsPerChild 200 </IfModule> <IfModule mpm_event_module> StartServers 1 MinSpareThreads 5 MaxSpareThreads 15 ThreadLimit 25 ThreadsPerChild 5 MaxClients 25 MaxRequestsPerChild 200 </IfModule> <IfModule mod_spamhaus.c> MS_METHODS POST,PUT,OPTIONS,CONNECT MS_WhiteList /etc/spamhaus.wl MS_CacheSize 256 </IfModule> 

这是我的VirtualHostconfiguration

 <VirtualHost *:80> RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com [nocase] RewriteRule ^(.*) http://www.example.com$1 [last,redirect=301] ServerName example.com ServerAlias www.example.com ServerAdmin [email protected] WSGIDaemonProcess example python-path=/home/abc/example:/home/abc/example/env/lib/python2.7/site-packages WSGIProcessGroup example WSGIApplicationGroup %{GLOBAL} WSGIScriptAlias / /home/abc/example/wsgi.py DocumentRoot /home/abc/example <Directory /> Require all granted </Directory> Alias /static/ /home/abc/example/static/ <Directory /home/abc/example/static> Order deny,allow Allow from all </Directory> Alias /media/ /home/abc/example/media/ <Directory /home/abc/example/media> Order deny,allow Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 

这是我的.htaccess文件

 <FilesMatch "\.(ico|svg|woff|eot|ttf)$"> Header set Cache-Control "max-age=31536000, public" </FilesMatch> <FilesMatch "\.(jpg|png|gif|css|js|json)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch> <IfModule mod_mime.c> AddType application/javascript js AddType application/vnd.ms-fontobject eot AddType application/x-font-ttf ttf ttc AddType font/opentype otf AddType application/x-font-woff woff AddType image/svg+xml svg svgz AddEncoding gzip svgz </Ifmodule> <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/html text/plain text/css application/json AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE text/xml application/xml text/x-component AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml AddOutputFilterByType DEFLATE image/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype </Ifmodule> 

我正在使用memcachedcaching大部分查询。 几乎没有基本查询的网页速度更快(尽pipe如此,仍然没有以前那么快),而具有复杂查询的网页却花费了大量的时间。 这些页面的服务器响应时间从0.2秒增加到4秒(使用Google PageSpeed Insights测量)。

我正在使用PostgreSQL 9.3数据库。 以下是我使用PgTune调整的postgresql.conf。

 default_statistics_target = 50 maintenance_work_mem = 60MB constraint_exclusion = on checkpoint_completion_target = 0.9 effective_cache_size = 704MB work_mem = 6MB wal_buffers = 8MB checkpoint_segments = 16 shared_buffers = 240MB max_connections = 80 

以下是过去一个月CPU,磁盘和带宽使用情况的图表

每月CPU使用率每月磁盘使用情况每月带宽使用情况

虽然过去一周左右带宽有所增加,但实际stream量并没有增加。 过去15-20天,我平均每天有1500位访问者。 增加的带宽使用可能可能是机器人活动的增加。

我的网站是一个Django应用程序托pipe在configuration上 – 1GB内存,30GB SSD磁盘,Ubuntu 14.04 x64。 我已经尝试过所有我能想到的事情,不能为了我的生活找出这里有什么问题。 我不擅长处理服务器,我现在唯一能想到的就是从Apache切换到nginx,从PostgreSQL切换到MySQL。 任何build议,可以帮助我弄清楚如何解决这个将不胜感激。

删除未使用的模块有时会产生相反的效果,因为服务器无法有效caching,但假设这不是问题,我认为另一个问题可能是您的内存不足,因此称为出现“磁盘抖动”(高IO),这在虚拟主机上是非常可能的。 同一个虚拟主机上的邻居可能已经发布了一个新的站点,这个站点在内存上占用了更大的空间,使得用户数量减less。 另外,请查看分析数据,找出stream量突然增加的原因以及来自何处。