我的WordPress性能和缓存堆栈
对于中低范围的单服务器或VPS,这是一个很好的WordPress性能堆栈。我将中端范围归类为具有约1G内存和相当快的驱动器的单核。
在您的盒子上,每小时可以提供超过10K的页面浏览量
服务器堆栈
- Linux-Debian Lenny或Ubuntu
- Nginx-配置为反向代理静态文件缓存
- Apache-Apache将在备用端口上处理Nginx卸载的PHP
- MySql-WP要求,确保您运行的是最新的稳定版本
- PHP-5.2或5.3分支的最新稳定版本
PHP缓存
- APC-配置mmap内存,且shm大小至少为128M
WordPress性能插件堆栈
借助W3 Total Cache,我们将磁盘用于页面缓存并缩小规模,因为Nginx将非常快速地提供我们的静态文件。
如何配置Nginx以提供静态文件并将PHP传递给Apache
单独使用Apache的问题在于,它会打开连接并在每个请求(甚至是静态文件)上都击中php。这会浪费连接,因为Apache将使它们保持打开状态,并且当您的通信量很大时,即使不使用它们,连接也会被阻塞。
默认情况下,Apache在端口80(默认的Web端口)上侦听请求。首先,我们将对我们的Apache conf和虚拟主机文件进行更改,以监听端口8080。
Apache配置
httpd.conf
将KeepAlive设置为关闭
ports.conf
NameVirtualHost *:8080
Listen 8080
每站点虚拟主机
<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>
您还应该安装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虚拟主机。我喜欢将启用站点的方法与每个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
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 conf
对于您自己托管的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/