Install Redis cache on WordPress blog

Source: Internet
Author: User
Tags cloudflare wordpress blog redis cluster install redis redis server

Install Redis cache on WordPress blog

Redis is an open-source, network-based, and memory-based key-value storage system, similar to memcached. It features extremely high performance and supports read/write frequencies over 100 K/second, some large websites such as ITeye (JavaEye) and CSDN now use Redis.

Compared with memcached, Redis provides persistent storage. After the server is restarted, memcached needs to re-create the cache, while Redis relies on snapshots for persistence. Even if the server is just started, the load will not increase sharply. Redis cache is suitable for high-traffic Wordpress.

When your WordPress posts reach tens of thousands, the pressure on Wordpress servers increases as traffic increases, and Wordpress posts and background operations become slow, in this case, it is obviously not cost-effective to increase Wordpress performance by investing in hardware alone.

Using Redis to directly cache WordPress pages in the server's memory, this avoids repeated PHP operations, while the memory response speed can maximize the access speed of Wordpress pages, according to the tribe's actual test, the page execution time can be reduced to seconds, which is several times or even more than 10 times higher than that without Redis cache.

Environment Description: CentOS6.6 LNMP Environment
Redis official website download source code: http://redis.io/download
[Root @ localhost src] # wget http://download.redis.io/releases/redis-3.0.2.tar.gz
[Root @ localhost src] # tar zxvf redis-3.0.2.tar.gz
[Root @ localhost redis-3.0.2] # cd redis-3.0.2
[Root @ localhost redis-3.0.2] # make

# The installation of redis is very simple. There is a ready-made Makefile file and you can directly run the make command.

After the installation is complete, several executable files will be generated in the src directory: redis-benchmark, redis-check-aof, redis-check-dump, redis-cli, redis-sentinel, redis-server. These files, coupled with a redis. conf, constitute the final available package of the entire redis.

Below you can put these executable files and redis. copy the conf file to the desired location, such as/usr/local/redis/bin and/usr/local/redis/etc. The command is as follows:
[Root @ localhost src] # cd redis-3.0.2
[Root @ localhost redis-3.0.2] # mkdir-p/usr/local/redis/{bin, var, etc}
[Root @ localhost redis-3.0.2] # cd src/
[Root @ localhost src] # cp redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server/usr/local/redis/bin/
[Root @ localhost redis-3.0.2] # cp/usr/local/src/redis-3.0.2/redis. conf/usr/local/redis/etc
[Root @ localhost redis-3.0.2] # ln-s/usr/local/redis/bin/*/usr/bin/

Modify the redis. conf configuration file:
[Root @ localhost redis-3.0.2] # sed-I's # pidfile. * $ # pidfile/var/run/redis. pid # '/usr/local/redis/etc/redis. conf
[Root @ localhost redis-3.0.2] # sed-I's # logfile. * $ # logfile/usr/local/redis/var/redis. log # '/usr/local/redis/etc/redis. conf
[Root @ localhost redis-3.0.2] # sed-I's # ^ dir. * $ # dir/usr/local/redis/var # '/usr/local/redis/etc/redis. conf
[Root @ localhost redis-3.0.2] # sed-I's # daemonize no # daemonize yes # '/usr/local/redis/etc/redis. conf

Note: by default, the daemonize parameter of the previously copied redis. conf file is no, so redis will not run in the background. to test it, we need to re-open a terminal. If it is changed to yes, redis is run in the background. In addition, the pid file, log file, and data file address are specified in the configuration file. If you need to modify the address first, the default log information is directed to the standard output.

[Root @ localhost redis-3.0.2] # echo 'vm. overcommit_memory = 1'>/etc/sysctl. conf
[Root @ localhost redis-3.0.2] # sysctl-p

Configure startup redis-server
[Root @ localhost src] # wget https://raw.githubusercontent.com/lj2007331/lnmp/master/init/Redis-server-init-CentOS
[Root @ localhost src] # mv Redis-server-init-CentOS/etc/init. d/redis-server
[Root @ localhost src] # chmod + x/etc/init. d/redis-server
[Root @ localhost src] # chkconfig -- add redis-server
[Root @ localhost src] # chkconfig redis-server on

Start redis
[Root @ localhost src] # service redis-server start

Test:
[Root @ localhost src] #/usr/local/redis/bin/redis-cli
127.0.0.1: 6379> set 123 baby
OK
127.0.0.1: 6379> get 123
"Baby"
127.0.0.1: 6379> exit

Disable redis
[Root @ localhost src] # service redis-server stop

Install the redis php Client
[Root @ localhost src] # wget http://pecl.php.net/get/redis-2.2.3.tgz
[Root @ localhost src] # tar zxf redis-2.2.3.tgz
[Root @ localhost src] # cd redis-2.2.3

Run the phpize command to generate the configure executable file.

[Root @ localhost redis-2.2.3] #/usr/local/php-fpm/bin/phpize
[Root @ localhost redis-2.2.3] #./configure -- with-php-config =/usr/local/php-fpm/bin/php-config
[Root @ localhost redis-2.2.3] # make & make install

Php. ini configuration file, add extension
12 [root @ localhost ~] # Sed-I '/; extension_dir = "ext"/a \ extension = "redis. so"'/usr/local/php-fpm/etc/php. ini
[Root @ localhost ~] # Service php-fpm restart

Make wordpress support redis
You need a client Development Kit so that PHP can connect to the redis server. Here we recommend that you add predis to the WordPress root directory and execute the following
[Root @ localhost src] # wget http://uploads.staticjw.com/ji/jim/predis.php
[Root @ localhost src] # chown php-fpm: php-fpm predis. php

Add the PHP script cached on the front end to the WordPress root directory and execute the following:

[Root @ localhost src] # wget https://gist.githubusercontent.com/JimWestergren/3053250/raw/d9e279e31cbee4a1520f59108a4418ae396b2dde/index-with-redis.php
[Root @ localhost src] # chown php-fpm: php-fpm index-with-redis.php
[Root @ localhost src] # mv predis. php index-with-redis.php/data/www/blog

Modify the index-with-redis.php as needed as follows:
$ Cf = 0; // set to 1 if you are using cloudflare
$ Debug = 1; // set to 1 if you want to see execution time and cache actions
$ Display_powered_by_redis = 0; // set to 1 if you want to display a powered by redis message with execution time, see below

If you are using cloudflare, set cf = 1 ;,
If you want to see the script execution time and cache loading time on the page, set $ debug = 1. this is cache is displayed at the bottom of the browser:
Display_powered_by_redis = 1 indicates that the powered_by information is displayed. For example, the icon in the lower right corner:

Replace index. php
[Root @ localhost blog] # mv index. php index. php. bak
[Root @ localhost blog] # Music index-with-redis.php index. php

Cache Problems
Annotation in index-with-redis.php
-Appending? C = y to a url deletes the entire cache of the domain, only works when you are logged in
-Appending? R = y to a url deletes the cache of that url
-Submitting a comment deletes the cache of that page
-Refreshing (f5) a page deletes the cache of that page

After logging on to the background website url, add? C = y to refresh the entire website
Can I add it at the end of the website page? R = y to refresh manually
The page is automatically refreshed when a comment is submitted.
You can also refresh the page by refreshing the (f5) page.
Refresh the web page to view the cache effect and the source code
360 the browser page displays a result similar to: this is a cache: 0.04534
When F5 refreshes the page, the cache time changes.


Notes
1. Note that the PHP version cached by Wordpress Redis is later than 5.3.
2. The acceleration effect of Wordpress Redis cache is undoubtedly obvious. When using Wordpress Redis cache acceleration, Please disable all other cache plug-ins when accessing large website blogs on special pages, to avoid unnecessary conflicts.

Install and test Redis in Ubuntu 14.04

Redis cluster details

Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis

Redis series-installation, deployment, and maintenance

Install Redis in CentOS 6.3

Learning notes on Redis installation and deployment

Redis. conf

Redis details: click here
Redis: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.