在2GB RAM E6500 CPU上,每天优化Apache 10K + wordpress视图

我有一个专用的服务器,在Ubuntu的Apache / PHP服务,我的WordPress的博客,每天约10K +的浏览量。 我已经与APC一起安装了W3TC插件。

但是,现在,然后服务器停止响应或死亡缓慢,我必须重新启动Apache来恢复。

inheritance人我的configuration我在做什么错?

ServerRoot "/etc/apache2" LockFile /var/lock/apache2/accept.lock PidFile ${APACHE_PID_FILE} TimeOut 40 KeepAlive on MaxKeepAliveRequests 200 KeepAliveTimeout 2 <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 8 ServerLimit 80 MaxClients 80 MaxRequestsPerChild 1000 </IfModule> <IfModule mpm_worker_module> StartServers 3 MinSpareServers 3 MaxSpareServers 3 ServerLimit 80 MaxClients 80 MaxRequestsPerChild 1000 </IfModule> <IfModule mpm_event_module> StartServers 3 MinSpareServers 3 MaxSpareServers 3 ServerLimit 80 MaxClients 80 MaxRequestsPerChild 1000 </IfModule> User ${APACHE_RUN_USER} Group ${APACHE_RUN_GROUP} AccessFileName .htaccess <Files ~ "^\.ht"> Order allow,deny Deny from all Satisfy all </Files> DefaultType text/plain HostnameLookups Off ErrorLog /var/log/apache2/error.log LogLevel error Include /etc/apache2/mods-enabled/*.load Include /etc/apache2/mods-enabled/*.conf Include /etc/apache2/httpd.conf Include /etc/apache2/ports.conf LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %O" common LogFormat "%{Referer}i -> %U" referer LogFormat "%{User-agent}i" agent CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined Include /etc/apache2/conf.d/ Include /etc/apache2/sites-enabled/ 

    我的WordPress性能和caching堆栈

    这是一款适用于中低档单服务器或VPS的优秀WordPress性能堆栈。 我将中端范围划分为单核,内存大约1G,速度相当快。

    在你的盒子上,这将是能够服务超过10K每小时的页面浏览量

    服务器堆栈

    • Linux – Debian Lenny或Ubuntu
    • Nginx – configuration为反向代理静态文件caching
    • Apache – Apache将在另一个端口上处理由Nginx卸载的PHP
    • MySql – WP所要求的,确保你运行最新的稳定版本
    • PHP – 5.2或5.3分支的最新稳定版本

    PHPcaching

    • APC – 使用mmap内存进行configuration,并且至less128M的shm大小

    WordPress性能插件堆栈

    • Nginx的代理caching集成商
    • W3总caching – 将页面caching设置为增强的磁盘,缩小到磁盘,将对象和数据库设置为APC。
    • 自托pipeCDN – 创build4个cname别名,指向服务器上的域,以便为静态文件提供服务

    随着W3总caching,我们正在使用磁盘的页面caching和缩小,因为Nginx将服务于我们的静态文件非常快。

    如何configurationNginx提供静态文件并将PHP传递给Apache

    单独使用Apache的问题在于,它打开了一个连接,甚至对于静态文件,每个请求都会触发php。 这会浪费连接,因为Apache会保持打开状态,当您有很多stream量时,即使连接不被使用,连接也会陷入困境。

    默认情况下,Apache在80端口(这是默认的Web端口)上侦听请求。 首先我们要修改我们的Apache conf和虚拟主机文件来监听端口8080。

    Apacheconfiguration

    httpd.conf文件

    设置KeepAliveclosures

    ports.conf

     NameVirtualHost *:8080 Listen 8080 

    每站点虚拟主机

     <VirtualHost 127.0.0.1:8080> ServerAdmin [email protected] ServerName yoursite.com ServerAlias www.yoursite.com DocumentRoot /srv/www/yoursite.com/public_html/ ErrorLog /srv/www/yoursite.com/logs/error.log CustomLog /srv/www/yoursite.com/logs/access.log combined </VirtualHost> 

    您还应该安装mod_rpaf,以便您的日志将包含访问者的真实IP地址。 如果不是你的日志将有127.0.0.1作为始发IP地址。

    Nginxconfiguration

    在Debian上,你可以使用软件仓库进行安装,但只包含0.6.33版本。 要安装更高版本,您必须添加lenny后端软件包

    $ nano /etc/apt/sources.list

    将此行添加到文件deb http://www.backports.org/debian lenny-backports main

    $ nano /etc/apt/preferences

    将以下内容添加到文件中:

     Package: nginx Pin: release a=lenny-backports Pin-Priority: 999 

    发出以下命令从backports.org导入密钥以validation软件包并更新系统的软件包数据库:

     $ wget -O - http://backports.org/debian/archive.key | apt-key add - $ apt-get update 

    现在用apt-get安装

    apt-get install nginx

    这比从源代码编译要容易得多。

    Nginx的conf和服务器文件configuration

    nginx.conf

     user www-data; worker_processes 4; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; client_body_temp_path /var/lib/nginx/body 1 2; gzip_buffers 32 8k; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; gzip on; gzip_comp_level 6; gzip_http_version 1.0; gzip_min_length 0; gzip_types text/html text/css image/x-icon application/x-javascript application/javascript text/javascript application/atom+xml application/xml ; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } 

    现在你需要设置你的Nginx虚拟主机。 我喜欢使用站点启用方法与每个v主机sym链接到站点可用目录中的文件。

     $ mkdir /etc/nginx/sites-available $ mkdir /etc/nginx/sites-enabled $ touch /etc/nginx/sites-available/yourservername.conf $ touch /etc/nginx/sites-available/default.conf $ ln -s /etc/nginx/sites-available /etc/nginx/sites-enabled $ nano /etc/nginx/sites-enabled/default.conf 

    default.conf

    注意:

    以下文件中的静态caching设置仅在启用Nginx代理caching集成器插件后才起作用。

     proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=staticfilecache:180m max_size=500m; proxy_temp_path /var/lib/nginx/proxy; proxy_connect_timeout 30; proxy_read_timeout 120; proxy_send_timeout 120; #IMPORTANT - this sets the basic cache key that's used in the static file cache. proxy_cache_key "$scheme://$host$request_uri"; upstream wordpressapache { #The upstream apache server. You can have many of these and weight them accordingly, #allowing nginx to function as a caching load balancer server 127.0.0.1:8080 weight=1 fail_timeout=120s; } 

    每个WordPress站点conf (对于多站点,你将只需要一个虚拟主机)

     server { #Only cache 200 responses, and for a default of 20 minutes. proxy_cache_valid 200 20m; #Listen to your public IP listen 80; #Probably not needed, as the proxy will pass back the host in "proxy_set_header" server_name www.yoursite.com yoursite.com; access_log /var/log/nginx/yoursite.proxied.log; # "combined" matches apache's concept of "combined". Neat. access_log /var/log/apache2/nginx-access.log combined; # Set the real IP. proxy_set_header X-Real-IP $remote_addr; # Set the hostname proxy_set_header Host $host; #Set the forwarded-for header. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { # If logged in, don't cache. if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) { set $do_not_cache 1; } proxy_cache_key "$scheme://$host$request_uri $do_not_cache"; proxy_cache staticfilecache; proxy_pass http://wordpressapache; } location ~* wp\-.*\.php|wp\-admin { # Don't static file cache admin-looking things. proxy_pass http://wordpressapache; } location ~* \.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ { # Cache static-looking files for 120 minutes, setting a 10 day expiry time in the HTTP header, # whether logged in or not (may be too heavy-handed). proxy_cache_valid 200 120m; expires 864000; proxy_pass http://wordpressapache; proxy_cache staticfilecache; } location ~* \/[^\/]+\/(feed|\.xml)\/? { # Cache RSS looking feeds for 45 minutes unless logged in. if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) { set $do_not_cache 1; } proxy_cache_key "$scheme://$host$request_uri $do_not_cache"; proxy_cache_valid 200 45m; proxy_cache staticfilecache; proxy_pass http://wordpressapache; } location = /50x.html { root /var/www/nginx-default; } # No access to .htaccess files. location ~ /\.ht { deny all; } } 

    自托pipe的CDN conf

    对于您自己托pipe的CDN conf,您只需将其设置为在没有代理密码的情况下提供静态文件即可

     server { proxy_cache_valid 200 20m; listen 80; server_name yourcdndomain.com; access_log /srv/www/yourcdndomain.com/logs/access.log; root /srv/www/yourcdndomain.com/public_html/; proxy_set_header X-Real-IP $remote_addr; location ~* \.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ { # Cache static-looking files for 120 minutes, setting a 10 day expiry time in the HTTP header, # whether logged in or not (may be too heavy-handed). proxy_cache_valid 200 120m; expires 7776000; proxy_cache staticfilecache; } location = /50x.html { root /var/www/nginx-default; } # No access to .htaccess files. location ~ /\.ht { deny all; } } 

    现在启动服务器

     $ /etc/init.d/apache2 restart $/etc/init.d/nginx start 

    基准testing结果

    在Apache Bench上,这个设置理论上可以每秒处理1833.56个请求

     $ ab -n 1000 -c 20 http://yoursite.com/ 

    替代文字

    这看起来像一个标准的Apacheconfiguration,虽然它看起来有些已经被剥离,因为它看起来像HTML。

    您需要调查服务器响应缓慢时发生的情况。 你不说你的服务器的规格,但你提到它的专用和10K /天应该很容易处理。

    • 顶部显示什么?
    • 瓶颈在哪里? CPU,内存,I / O等待?
    • 有多less个Apache进程正在运行?
    • 在netstat中显示多less个连接?

    猜测,CPU可能是PHP造成的瓶颈。 使用APC和WPcaching插件是缓解这种情况的好办法,您已经完成了。 你也可以尝试Apache的“MPM”过程模型,而不是“Prefork”。 确保你有足够的内存分配给APC,以便它可以caching你的PHP脚本,而不是“错过”。

    它也可以是MySQL – 看看是否占用CPU或磁盘。

    考虑closuresmod_deflate(如果启用它) – 它确实提供了加载时间的好处,但可能会增加CPU负载。 值得尝试。

    使用像'siege'或'ab'这样的工具来testing你的服务器,并找出你的服务器在哪一点上变慢。

    以下是一些来自web服务器性能调优的书签: http : //articles.slicehost.com/2010/5/19/configuring-the-apache-mpm-on-ubuntu

    http://www.thebuzzmedia.com/increase-wordpress-performance-on-apache-with-worker-mpm-php-and-mod_fcgid/

    http://www.devside.net/articles/apache-performance-tuning

    http://www.brandonturner.net/blog/2009/07/fastcgi_with_php_opcode_cache/

    但我原来的build议仍然是一样的 – 找出什么是瓶颈是第一! 否则,你会盲目地试图提高性能(当然,提高性能总是很好),但不知道哪个领域关注。

    同时启用服务器状态模块并访问该模块以了解发生了什么事情。

    你可能会交换。 发生这种情况时,你有没有签出vmstat? 对于80个MaxClients,2GB内存每个只有25 MB(假设这个盒子没有做其他事情)。你的MaxClients可能太高了。 解决scheme很明显:添加更多的RAM或更低的MaxClients。 如果命令行响应缓慢,当你重新启动Apache,这是这种情况的一个迹象。

    也有可能你用一些“大”的文件给一些移动客户端(或其他客户端在慢速连接上)喂食,从而占用你所有可用的apache插槽。 也许你的MaxClients太less了。 检查服务器状态会告诉你当时每个客户正在做什么。 这种情况的一个解决scheme是增加MaxClients(但也可能会变成上述情况)。更好的解决scheme是在Apache前面安装一个HTTP加速器(一个免费的选项是perlbal)。如果你的命令行是正常的当你重新启动Apache时,速度就是这种情况的一个指标。

    使用mod_status是查看多个Apache实例内部发生了什么的方法,但是请注意,这会真正影响性能。 它似乎吃掉了内存,在一个案例中,我无法诊断是否是在没​​有任何东西直接提供的反向代理服务器环境中进行单进程locking的原因。 这些当然是由用户报告“永久加载页面”。 他们甚至不理解“等待10秒钟”和“无法完成”之间的区别,因为他们通常在一些(<10秒)的时间内在浏览器中点击“停止”。

    还要检查你是否正在configuration正确的地方(使用mod_status很容易看,因为你看到了实例/进程的数量)。 至less在ubuntu下的股票configuration有每个MPM模式的ifdef'ed部分,当你运行prefork时(如传统的智慧所暗示的PHP不是线程安全的),很容易编辑worker模式。

    哦,最重要的是:以root身份运行,并注意最大限度地释放资源。 内存,光盘,CPU – 你会看到。

    还有一点:虽然你的设置不容易出现错误的内容长度信息导致浏览器等待数据“永远”给你的报告“死慢”到“没有响应”,但取消激活mod_deflate的想法可能是好的。

    BTW:每天发送10K页面,如果在一个小时内访问这台计算机上的媒体文件应该只是一个问题。

    一些build议,特别是如果你主持大量的媒体文件:

    • 将您的媒体转移到专用的Apache(或更好的:nginx)实例。 没有PHP,没有模块,只有一个光秃的HTTP服务器,它将尽可能快地提供媒体。
    • caching,caching,caching! 在wordpress上使用超级caching插件。 它帮助了很多。
    • 检查您的标头上的Apacheconfiguration。 validation图片和其他“stable”媒体的“到期时间”设置为远程date,并且您的apache在客户端请求时返回HTTP 304代码
    • 启用mod_deflate。 这可能会降低客户的performance,会更快地服务。