「备份」Wordpress 优化

起因

在用17ce测速时,CPU占用达到了100%,php-fpm进程占用大量资源。

方法

环境:lnmp.org 一键包

需求:openresty,php,mysql,memcached,redis

插件:wpjam-basic,Nginx-helper

一个针对Wordpress效率极高的缓存方案,Openresty+Redis,这个方案将会绕过php,通过Nginx SRcache 拓展直达Redis。

  • 首先需要将普通的Nginx替换为Openresty,我使用的是军哥的lnmp一键包安装的环境,若替换后还要能够执行相关指令,就必须要同时替换lnmp脚本命令中的Nginx

编译Openresty

 

wget https://openresty.org/download/openresty-1.17.8.2.tar.gz
tar -zxvf openresty-1.17.8.2.tar.gz
cd openresty-1.17.8.2/
./configure
make
make install

编译完成后需要替换nginx的开机启动和lnmp中的相关路径

vim /etc/init.d/nginx
;NGINX_BIN='/usr/local/nginx/sbin/nginx'
NGINX_BIN='/usr/local/openresty/nginx/sbin/nginx'
;CONFIG='/usr/local/nginx/conf/nginx.conf'
CONFIG='/usr/local/openresty/nginx/conf/nginx.conf'
#修改/bin/lnmp里的,保证lnmp vhost可用
vim /bin/lnmp
#替换内部的 /usr/local/nginx 为 /usr/local/openresty/nginx
#再将原先nginx的配置文件复制过来
cp /usr/local/nginx/conf/* /usr/local/openresty/nginx/conf

 

重启 openresty

lnmp nginx restart

到这里,OpenResty替换Nginx的工作就已经完成了。

vhost 参考配置文件

upstream redis {
server 127.0.0.1:6379;
keepalive 512;
}
server
{
listen 80;
#listen [::]:80;
server_name sep.cc;
rewrite ^(.*)$ https://$host$1 permanent;
}
server
{
listen 443 ssl http2;
#listen [::]:443 ssl http2;
server_name sep.cc;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/inwao.com;
set $skip_cache 0;
#POST请求直接调用后端
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
#不要缓存以下部分
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
#不缓存登陆用户和最近评论的用户
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
location /redis-fetch {
internal ;
set $redis_key $args;
redis_pass redis;
}
location /redis-store {
internal ;
set_unescape_uri $key $arg_key ;
redis2_query set $key $echo_request_body;
redis2_query expire $key 14400;
redis2_pass redis;
}
ssl_certificate /home/wwwroot/ssl/1.crt;
ssl_certificate_key /home/wwwroot/ssl/1.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/openresty/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/openresty/nginx/conf/ssl/dhparam.pem;
include rewrite/wordpress.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
# include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
location ~ [^/]\.php(/|$) {
set $key "nginx-cache:$scheme$request_method$host$request_uri";
try_files $uri =404;
srcache_fetch_skip $skip_cache;
srcache_store_skip $skip_cache;
srcache_response_cache_control off;
set_escape_uri $escaped_key $key;
srcache_fetch GET /redis-fetch $key;
srcache_store PUT /redis-store key=$escaped_key;
more_set_headers 'X-Cache $srcache_fetch_status';
more_set_headers 'X-Store $srcache_store_status';
#PHP版本号根据实际修改
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/:/proc/";
}
}

 

修改好后重启openresty,去wordpress后台安装Nginx-Helper插件,开启redis缓存,其他全部默认即可。

这个插件的作用是能够及时的刷新缓存

命中情况可以在服务器上执行

telnet 127.0.0.1 6379
info

keyspace_hits为命中次数,keyspace_misses为未命中次数

配置Memcached缓存

 

在wordpress后台安装wpjam-basic插件,然后点击扩展管理,微信扫码登录

然后将/wp-content/plugins/wpjam-basic/template/object-cache.php复制到/wp-content

即可开启Memcached缓存,点击WPJAM-系统信息可以查看详情。

教程结束。

oneinstack 一键包已集成openresty,开箱即用。

请站长喝杯可乐吧

『 本站部分内容具有时效性,如遇失效请留言反馈 』

转载声明:本站所有内容采用 CC BY-NC-SA 国际许可协议,转载请注明来源:Sep blog,谢谢!

版权声明:如文章内容涉及侵权,请联系 [email protected], 待查证属实之后会立刻删除侵权内容。