Project

General

Profile

Support #496

Updated by Daniel Curtis over 9 years ago

{{>toc}} 

 h2. Installing 
 
 * Install redis FreeBSD port collection: 
 <pre> 
 portsnap fetch extract  
 cd /usr/ports/databases/redis 
 make install clean 
 </pre> 
 * Or with portmaster 
 <pre> 
 portmaster databases/redis 
 </pre> 
 * Or pkg2ng 
 <pre> 
 pkg install redis 
 </pre> 

 h3. Redis configuration file file** 

 * Editing the redis config file: 
 <pre> 
 vi /usr/local/etc/redis.conf 
 </pre> 
 
 h3. Auto-start Redis at Boot 
 
 * To run redis from startup: 
 <pre> 
 echo 'redis_enable="YES"' >> /etc/rc.conf 
 </pre> 

 Bind Redis to IP** 
 
 * To bind Redis to a single interface, add the following line to @/usr/local/etc/redis.conf@: 
 <pre> 
 #bind 127.0.0.1 
 bind 192.168.0.1 
 </pre> 
 
 h2. Commands 
 
 * Stop/Start Redis  
 <pre> 
 service redis stop 
 service redis start 
 </pre> 
   
 * Accessing Redis 
 <pre> 
 telnet localhost 6397 
 </pre> 
 
 * Or the Redis CLI client 
 <pre> 
 redis-cli 
 </pre> 

 h2. PHP5-Extension for Redis 
 
 * Install php5-redis extension from the ports tree 
 <pre> 
 cd /usr/ports/databases/php5-redis/ && make install clean 
 </pre> 
 * Or portmaster 
 <pre> 
 portmaster databases/php5-redis 
 </pre> 
 * Or pkg2ng 
 <pre> 
 pkg install php5-redis 
 </pre> 

 h3. PHP Session handler 

 php5-redis can be used to store PHP sessions. To do this, configure @session.save_handler@ and @session.save_path@ in php.ini to tell php5-redis where to store the sessions: 
 <pre> 
 session.save_handler = redis 
 session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2" 
 </pre> 

 @session.save_path@ can have a simple host:port format too, but you need to provide the tcp:// scheme if you want to use the parameters.  

 The following parameters are available: 
 # *weight* (_integer_): the weight of a host is used in comparison with the others in order to customize the session distribution on several hosts. If host A has twice the weight of host B, it will get twice the amount of sessions. In the example, host1 stores 20% of all the sessions (1/(1+2+2)) while host2 and host3 each store 40% (2/1+2+2). The target host is determined once and for all at the start of the session, and doesn't change. The default weight is 1. 
 # *timeout* (_float_): the connection timeout to a redis host, expressed in seconds. If the host is unreachable in that amount of time, the session storage will be unavailable for the client. The default timeout is very high (86400 seconds). 
 # *persistent* (_integer_, should be 1 or 0): defines if a persistent connection should be used. *(experimental setting)* 
 # *prefix* (_string_, defaults to "PHPREDIS_SESSION:"): used as a prefix to the Redis key in which the session is stored. The key is composed of the prefix followed by the session ID. 
 # *auth* (_string_, empty by default): used to authenticate with the server prior to sending commands. 
 # *database* (_integer_): selects a different database. 

 Sessions have a lifetime expressed in seconds and stored in the INI variable "session.gc_maxlifetime". You can change it with ini_set(). The session handler requires a version of Redis with the SETEX command (at least 2.0). php5-redis can also connect to a unix domain socket: @session.save_path = "unix:///var/run/redis/redis.sock?persistent=1&weight=1&database=0@. 

 h3. Ruby & Rails Session Handler 

 * Install the redis-rails gem: 
 <pre> 
 pkg install rubygem-redis-rails 
 </pre> 

 * Gemfile Installation 
 <pre> 
 gem 'redis-rails' 
 </pre> 

 * Usage in @config/application.rb@: 
 <pre> 
 config.cache_store = :redis_store, 'redis://localhost:6379/0/cache', { expires_in: 90.minutes } 
 </pre> 

 * Configuration values at the end are optional. If you want to use Redis as a backend for sessions, you will also need to set in @config/initializers/session_store.rb@ 
 <pre> 
 MyApplication::Application.config.session_store :redis_store 
 </pre> 

 * And if you would like to use Redis as a rack-cache backend for HTTP caching, @config/environments/production.rb@ 
 <pre> 
 config.action_dispatch.rack_cache = { 
   metastore:     'redis://localhost:6379/1/metastore', 
   entitystore: 'redis://localhost:6379/1/entitystore' 
 } 
 </pre> 

 Then you’re good to go. Your cache store should now be using Redis. 

 h2. Resources 

 * http://jonlabelle.com/snippets/view/markdown/redis-and-freebsd 
 * https://github.com/nicolasff/phpredis 
 * http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html

Back