欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Drupal 性能优化之 Redis 篇

shiping1 的头像

Drupal 性能优化之 Redis 篇

Drupal 7 的性能确实是个问题,使用 APC、Memcache 等可以提升不少,但如果想获得更好的性能,可以试试 redis 模块,这个模块可以使用 Redis 数据库作为Drupal的缓存,效果更好。下面的步骤是将 Redis 数据库服务安装在一台远程服务器上,当然与Drupal 安装在同一服务器上也可,具体方法如下:

一、安装 Redis

code
1
sudo apt-get install redis-server
喜欢编译安装的同学可以自己编译,方法如下:
code
1
2
3
4
5
6
7
8
sudo apt-get install build-essential
sudo apt-get install tcl8.5
wget <a href="http://download.redis.io/releases/redis-2.8.11.tar.gz" title="http://download.redis.io/releases/redis-2.8.11.tar.gz">http://download.redis.io/releases/redis-2.8.11.tar.gz</a>
tar -zxvf redis-2.8.11
cd redis-2.8.11
make
sudo make test   #安装前测试一下
sudo make install
另外,Redis还提供一个脚本来安装为 Ubuntu 服务,如下:
code
1
sudo ./utils/install_server.sh
然后按提示进行安装。

二、配置 Redis

主要是配置 Redis 可以远程访问,并添加个密码。
code
1
vi /etc/redis/redis.conf
  • 取消绑定:将 bind 127.0.0.1 一行注释掉
  • 设置密码:将# requirepass foobared 前的 # 号去掉,将 foobared 修改为你要设置的密码
然后重新启动 redis

三、下载 Redis 模块

将 Redis 模块 放到 sites/all/module/contrib 目录下吧,不用安装。

四、配置 PHP 扩展

PHP访问 Redis 数据库的扩展库有两个,一个是 Predis ,这是一个PHP库,要求 PHP 5.3,直接下载放置到 libraries 目录下即可;另一个是PhpRedis,需要自己编译。两者性能上差别也就毫秒,PhpRedis 性能更好一点。下面说说 PhpRedis 编译方法。
code
1
pecl install redis
安装完成,显示如下信息: phpredis.png将 extension=redis.so 添加到 php.ini vi /etc/php5/fpm/php.ini 然后重新载入 php 配置 /etc/init.d/php5-fpm reload 打开 admin/reports/status/php 检查 phpRedis 是否载入: redis.png

五、配置 settings.php

下面参考了老赵的配置,而且使用了 entitycache 模板
code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Required configurations.
 $conf['lock_inc'] = 'sites/all/modules/contrib/redis/redis.lock.inc';
 $conf['cache_backends'][] = 'sites/all/modules/contrib/redis/redis.autoload.inc';
 $conf['redis_client_interface'] = 'PhpRedis';
 $conf['redis_client_base'] = 2;
 $conf['redis_client_host'] = '192.168.1.109'; //换成你的IP
 $conf['redis_client_port'] = '6379';
 $conf['redis_client_password'] = 'yourpassword';
 // $conf['cache_prefix'] = 'mysite_';
 
 // Optional not redis specific.
 // $conf['cache_lifetime'] = 0;
 // $conf['page_cache_max_age'] = 0;
 // $conf['page_cache_maximum_age'] = 0;
 $conf['page_cache_invoke_hooks'] = TRUE;
 $conf['page_cache_without_database'] = FALSE;
 
 // Cache bins.
 $conf['cache_default_class'] = 'Redis_Cache';
 $conf['cache_bootstrap'] = 'Redis_Cache';
 $conf['cache_class_cache'] = 'Redis_Cache';
 $conf['cache_class_cache_menu'] = 'Redis_Cache';
 $conf['cache_class_cache_block'] = 'Redis_Cache';
 $conf['cache_class_cache_views'] = 'Redis_Cache';
 $conf['cache_class_cache_views_data'] = 'Redis_Cache';
 $conf['cache_field'] = 'Redis_Cache';
 $conf['cache_filter'] = 'Redis_Cache';
 $conf['cache_image'] = 'Redis_Cache';
 $conf['cache_libraries'] = 'Redis_Cache';
 $conf['cache_metatag'] = 'Redis_Cache';
 //$conf['cache_search_api_solr'] = 'Redis_Cache';
 
 // Always Database Cache.
 $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
 
 // Entity Cache.
 if (file_exists('sites/all/modules/contrib/entitycache/entitycache.info')) {
   $conf['cache_entity_node'] = 'Redis_Cache';
   $conf['cache_entity_fieldable_panels_pane'] = 'Redis_Cache';
   $conf['cache_entity_file'] = 'Redis_Cache';
   $conf['cache_entity_taxonomy_term'] = 'Redis_Cache';
   $conf['cache_entity_taxonomy_vocabulary'] = 'Redis_Cache';
 }
妥了,跑跑看如何?
来自 http://www.cuitu.net/content/drupal-xing-neng-you-hua-zhi-redis-pian
普通分类: