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

Bro*_*ist 10 scalability php5 wordpress apache-2.2

我在 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/
Run Code Online (Sandbox Code Playgroud)

小智 23

我的 WordPress 性能和缓存堆栈

这是一个很好的 WordPress 性能堆栈,适用于中低端单服务器或 VPS。我将中档归类为具有大约 1G 内存和相当快的驱动器的单核。

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

服务器堆栈

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

PHP缓存

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

WordPress 性能插件栈

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

使用 W3 Total Cache,我们将磁盘用于页面缓存和缩小,因为 Nginx 将非常快地为我们的静态文件提供服务。

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

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

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

阿帕奇配置

配置文件

将 KeepAlive 设置为关闭

端口配置文件

NameVirtualHost *:8080
Listen 8080
Run Code Online (Sandbox Code Playgroud)

每站点虚拟主机

<VirtualHost 127.0.0.1:8080>
     ServerAdmin info@yoursite.com
     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>
Run Code Online (Sandbox Code Playgroud)

您还应该安装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
Run Code Online (Sandbox Code Playgroud)

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

$ wget -O - http://backports.org/debian/archive.key | apt-key add -
$ apt-get update
Run Code Online (Sandbox Code Playgroud)

现在使用 apt-get 安装

apt-get install nginx

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

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/*;
}
Run Code Online (Sandbox Code Playgroud)

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

$ 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
Run Code Online (Sandbox Code Playgroud)

默认配置文件

笔记:

以下文件中的静态缓存设置仅在启用 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;
}
Run Code Online (Sandbox Code Playgroud)

每个 WordPress 站点配置(对于多站点,您只需要一个 vhost)

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

        }
Run Code Online (Sandbox Code Playgroud)

自托管 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;
        }

    }
Run Code Online (Sandbox Code Playgroud)

现在启动服务器

$ /etc/init.d/apache2 restart  
$/etc/init.d/nginx start
Run Code Online (Sandbox Code Playgroud)

基准测试结果

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

$ ab -n 1000 -c 20 http://yoursite.com/
Run Code Online (Sandbox Code Playgroud)

替代文字


Raf*_*iar 0

这看起来像一个标准的 Apache 配置,尽管其中一些配置由于看起来像 HTML 而被删除了。

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

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

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

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

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

使用“siege”或“ab”等工具对您的服务器进行基准测试并找出服务器速度变慢的时间点。

以下是我在网络服务器性能调整中的一些书签: 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/

但我最初的建议仍然是一样的——首先找出瓶颈是什么!否则,您会盲目地尝试提高绩效(当然,提高绩效总是好的),但不知道应该将注意力集中在哪个领域。