Answers:
“什么插件”的基本答案可能是W3 Total Cache。它是目前功能最强大且开发最活跃的插件之一。但是,完整的性能链更长,仅WordPress插件可以处理。
一个好的开始就是使用基于操作码内存的缓存(如APC)的静态缓存插件(如W3)。
但是从那里可以做更多(而且更复杂)的事情,例如内容分发网络,备用Web服务器等。
对于中低范围的单服务器或VPS,这是最佳的WordPress性能堆栈。我将中端范围归类为具有约1G内存和相当快的驱动器的单核。
借助W3 Total Cache,我们将磁盘用于页面缓存并缩小规模,因为Nginx将非常快速地提供我们的静态文件。
单独使用Apache的问题在于,它会打开连接并在每个请求(甚至是静态文件)上都击中php。这会浪费连接,因为Apache将使它们保持打开状态,并且当您的通信量很大时,即使不使用它们,连接也会被阻塞。
默认情况下,Apache在端口80(默认的Web端口)上侦听请求。首先,我们将对我们的Apache conf和虚拟主机文件进行更改,以监听端口8080。
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地址。
在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
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链接到sites-available目录中的文件使用支持sites的方法。
$ 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/
对多站点使用至少64MB Ram的Webspace,并在Apache上使用APC和Memcached,缓存不是静态的,并且可以毫无问题地使用所有WP函数。您通过PageSpeed扫描还阅读了其他选项,主题中已进行了编码。缓存可以完成出色的工作,但不能修复错误的主题或插件。另一种解决方案是在WordPress中将不带Cookie的子域用作CDN。将其添加到wp-config.php中,仅在域而不是在子域上用于Cookies。
define( 'COOKIE_DOMAIN', 'example.com' );
现在,在主题的functions.php中设置新功能,或编写一个插件来替换静态内容到您的子域(即自定义CDN)的路径。
// replace for CDN on bloginfo
if ( !function_exists('fb_add_static_wpurl') ) {
function fb_add_static_wpurl($info, $show) {
if ( is_admin() )
return $info;
$keys = array(
'url',
'wpurl',
'stylesheet_url',
'stylesheet_directory',
'template_url',
'template_directory',
);
if ( in_array( $show, $keys ) ) {
$wpurl = get_bloginfo('wpurl');
$search = array(
$wpurl . '/wp-content/images/',
$wpurl . '/wp-content/download/',
$wpurl . '/wp-content/themes/',
$wpurl . '/wp-content/plugins/',
);
$replace = array(
'http://cdn1.example.com/',
'http://cdn2.example.com/',
'http://cdn3.example.com/',
'http://cdn4.example.com/',
);
return str_replace( $search, $replace, $info );
} else {
return $info;
}
}
add_filter( 'bloginfo_url', 'fb_add_static_wpurl', 9999, 2 );
}
现在,模板和样式表路径的功能
function fb_add_static_stylesheet_uri($uri) {
if ( is_admin() )
return $uri;
$wpurl = get_bloginfo('wpurl');
$search = array(
$wpurl . '/wp-content/images/',
$wpurl . '/wp-content/download/',
$wpurl . '/wp-content/themes/',
$wpurl . '/wp-content/plugins/',
);
$replace = array(
'http://cdn1.example.com/',
'http://cdn2.example.com/',
'http://cdn3.example.com/',
'http://cdn4.example.com/',
);
return str_replace( $search, $replace, $uri );
}
add_filter ( 'template_directory_uri', 'fb_add_static_stylesheet_uri' );
add_filter ( 'stylesheet_uri', 'fb_add_static_stylesheet_uri' );
add_filter ( 'stylesheet_directory_uri', 'fb_add_static_stylesheet_uri' );
现在,在没有cookie的前端静态CDN URL上阅读Page Speed。
还将跟随源添加到.htaccess中以获取块重复内容:
##
# Explicitly send a 404 header if a file on cdn[0-9].example.org is not
# found. This will prevent the start page (empty URL) from being loaded.
##
RewriteCond %{HTTP_HOST} ^cdn[0-9]\.example\.org [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* - [R=404,L]
##
# Do not dispatch dynamic resources via cdn[0-9].example.org.
##
RewriteCond %{HTTP_HOST} ^cdn[0-9]\.example\.org [NC]
RewriteCond %{REQUEST_FILENAME} \.(php|html)$
RewriteRule .* - [R=404,L]
请使用该功能,它也是示例,您可以根据我的想法编写解决方案。
基本上将所有操作都保存在内存中!
我所有的知识都是开源的https://github.com/szepeviktor/debian-server-tools