优化 apache,在 2GB RAM E6500 CPU 上实现每天 10K+ wordpress 浏览量

优化 apache,在 2GB RAM E6500 CPU 上实现每天 10K+ wordpress 浏览量

我在 ubuntu 上有一个安装有 apache/php 的专用服务器,用于为我的 Wordpress 博客提供服务,每天的页面浏览量约为 10K+。我已使用 APC 安装了 W3TC 插件。

但时不时地,服务器会停止响应或变得非常慢,我必须重新启动 apache 才能恢复。

这是我的配置我做错了什么?

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/

答案1

我的 WordPress 性能和缓存堆栈

对于中低端单服务器或 VPS 来说,这是一个很棒的 WordPress 性能堆栈。我将中端分类为单核、内存约 1G 且驱动器速度相当快。

在您的盒子上,这将能够提供每小时超过 10K 的页面浏览量

服务器堆栈

  • Linux - Debian Lenny 或 Ubuntu
  • Nginx-配置为反向代理静态文件缓存
  • Apache - Apache 将在备用端口上处理由 Nginx 卸载的 PHP
  • MySql - WP 所需,请确保运行的是最新稳定版本
  • PHP - 5.2 或 5.3 分支的最新稳定版本

PHP 缓存

  • APC - 配置至少 128M 的 mmap 内存和 shm 大小

WordPress 性能插件堆栈

  • Nginx 代理缓存集成器
  • W3 总缓存- 将页面缓存增强到磁盘,最小化到磁盘,并将对象和数据库设置为 APC。
  • 自托管 CDN - 创建 4 个 cname 别名,指向服务器上的域,仅用于提供静态文件

使用 W3 Total Cache,我们使用磁盘进行页面缓存和最小化,因为 Nginx 将非常快速地提供我们的静态文件。

如何配置 Nginx 来提供静态文件并将 PHP 传递给 Apache

单独使用 Apache 的问题在于,它会打开一个连接,并且每次请求都会触发 php,即使是静态文件也是如此。这会浪费连接,因为 Apache 会保持它们打开,并且当您有大量流量时,即使没有使用连接,您的连接也会陷入困境。

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

Apache 配置

httpd配置文件

将 KeepAlive 设置为关闭

端口配置文件

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 地址。

Nginx 配置

在 Debian 上,您可以使用存储库进行安装,但它们仅包含版本 0.6.33。要安装更高版本,您必须添加 lenny backports 包

$ 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 导入密钥来验证软件包并更新系统的软件包数据库:

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

现在使用 apt-get 安装

apt-get install nginx

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

Nginx conf 和服务器文件配置

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 虚拟主机。我喜欢使用 sites-enabled 方法,将每个 v host sym 链接到 sites-available 目录中的文件。

$ 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

默认配置文件

笔记:

只有启用 Nginx 代理缓存集成器插件后,以下文件中的静态缓存设置才会起作用。

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 网站配置(对于多站点,您只需要一个虚拟主机)

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;
        }

        }

自托管 CDN 会议

对于您自己托管的 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

基准测试结果

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

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

替代文本

答案2

它看起来像一个标准的 Apache 配置,尽管由于它看起来像 HTML,其中的一些内容似乎已被删除。

您需要调查服务器响应缓慢时发生了什么。您没有说明服务器的规格,但您提到它是专用的,每天 10k 应该很容易处理。

  • top 显示什么?
  • 瓶颈在哪里?CPU、内存、I/O 等等?
  • 有多少个 Apache 进程正在运行?
  • netstat 中显示了多少个连接?

我猜,CPU 可能是 PHP 造成的瓶颈。使用 APC 和 WP 缓存插件是缓解此问题的好方法,您已经这样做了。您还可以尝试 Apache 的“MPM”进程模型,而不是“Prefork”。确保您为 APC 分配了足够的内存,以便它可以缓存您的 PHP 脚本而不会“丢失”。

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

如果启用了 mod_deflate,请考虑将其关闭 - 它确实可以缩短加载时间,但会增加 CPU 负载。可能值得一试。

使用“siege”或“ab”之类的工具对您的服务器进行基准测试,并找出服务器变慢的点。

以下是我从 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/

但我最初的建议仍然是一样的 - 首先找出瓶颈!否则,您只是盲目地试图提高性能(当然,提高性能总是好的),却不知道应该关注哪个领域。

答案3

还可以启用服务器状态模块并访问该模块来了解发生了什么。

您可能正在交换。发生这种情况时,您是否检查过 vmstat?80 MaxClients 的 2GB RAM 仅为每个 25 MB(假设该框没有执行任何其他操作)。您的 MaxClients 可能太高了。解决方案很明显:添加更多 RAM 或降低 MaxClients。如果重新启动 apache 时命令行响应缓慢,则表明存在这种情况。

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

答案4

以下是一些建议,特别是当您托管大量媒体文件时:

  • 将媒体移至专用的 Apache(或更好的:nginx)实例。没有 PHP,没有模块,只有一个可以尽快传输媒体的裸 http 服务器。
  • 缓存,缓存,缓存!使用 wordpress 上的超级缓存插件。它很有帮助。
  • 检查 Apache 的标头配置。验证图像和其他“稳定”媒体的到期时间是否设置为较远的日期,以及 Apache 在客户端请求时是否返回 HTTP 304 代码
  • 启用 mod_deflate。它可能会降低客户端的性能,但服务速度会更快。

相关内容