Sie sind auf Seite 1von 10

Django WagTail CMS

I have problem install Pillow with error relating to libjpeg

To resolve, I install the libjpeg-turbo-iX86_64 using yum.

Then ok.

Install redis server

Now use yum command to install redis server
yum install redis

Using Redis as Django's

session store and cache
JUL 14TH, 2013

Redis is an in-memory key-value store, somewhat similar to

Memcached. Because Redis keeps its dataset in memory, storage
and retrieval is very fast. Its a good idea to use this kind of
solution for storing ephemeral application data, such as contents
of the cache, or temporary information associated with active
user sessions. This unburdens your database system from
performing unnecessary read and write operations and can
considerably speed up your application. Modules for using Redis
together with Django are available and quite easy to set up.


Im going to assume you have a Django application running in a

virtual environment, under supervisord on a Debian server.
Instructions for creating such a setup can be found in a previous

Setting up Redis on Debian is simple using apt-get . On an RPMbased system you can use the equivalent yum command or similar.

Install Redis
$ sudo apt-get install redis-server
$ redis-server --version
Redis server version 2.4.14 (00000000:0)

Configure Redis to connect over a socket

You can connect to a local Redis instance over the network layer
(TCP to the loopback interface) or through a unix socket file.
In order to avoid the small overhead of TCP, we can configure
Redis to accept direct socket connections. To do this, edit
your /etc/redis/redis.conf file, comment out the bind and port directives
and uncomment the two unixsocket directives.
1# Accept connections on the specified port, default is 6379.
2# If port 0 is specified Redis will not listen on a TCP socket.
3# port 6379
5# If you want you can bind a single interface, if the bind option is not
6# specified all the interfaces will listen for incoming connections.
8# bind
1# Specify the path for the unix socket that will be used to listen for
0# incoming connections. There is no default, so Redis will not listen

2# on a unix socket when not specified.
3unixsocket /var/run/redis/redis.sock
1unixsocketperm 700

After making changes to its configuration you will need to restart

$ sudo service redis-server restart

You can now check if Redis is up and accepting connections:

$ redis-cli ping

Redis socket permissions

Default permissions on the Redis socket are very restrictive on
Debian and allow only the user redis to connect through it. You can
relax these permissions and allow any local user to connect to
Redis by changing the unixsocketperm directive in redis.conf to:
1unixsocketperm 777

Remember to restart Redis after making configuration changes

$ sudo service redis-server restart

For security reasons, it may be better to restrict access to the

socket to a chosen group of users. You can add the user which
your application will run as to the group redis :
$ sudo usermod -a -G redis django_username

Then change the permissions on the socket file by changing

the unixsocketperm directive in redis.conf to:
1unixsocketperm 770

Groups are assigned at login, so if your application is running,

youll need to restart it. If youre running an application
called hello via supervisor , you can restart it like this:
$ sudo supervisorctl restart hello

Find more information about getting started with Redis in the


Python bindings for Redis

In order to use Redis with a Python application such as Django,
youll need to install the Redis-Python interface bindings module.
You can install it in your virtualenv with pip :
$ source bin/activate
(hello_django) $ pip install redis

Redis as backend for Djangos cache

You can set up Redis to store your applications cache data. Use
the django-redis-cache module for this.
Install django-redis-cache in your virtual environment:
(hello_django) $ pip install django-redis-cache

And add the following to your



'default': {

'BACKEND': 'redis_cache.RedisCache',

'LOCATION': '/var/run/redis/redis.sock',



You can now restart your application and start using Djangos
Redis-powered cache.

Using Djangos cache in your application

Djangos cache framework is very flexible and allows you to cache
your entire site or individual views. You can control the behavior
of the cache using the @cache_page decorator. For instance to cache
the results of my_view for 15 minutes, you can use the following
1from django.views.decorators.cache import cache_page
3@cache_page(60 * 15)
4def my_view(request):


If you havent set up Djangos cacheing middleware yet, you

should do so by adding lines 2 and 6 from the snippet below





'django.middleware.cache.FetchFromCacheMiddleware', # This must be last

# This must be first on the list

More information: using Djangos cache.

Using the cache inside your functions
You can also use the cache in your functions to store arbitrary
data for quick retrieval later on.
1# Start by importing your default cache:
2from django.core.cache import cache
4# Store data under a-unique-key:
5cache.set('a-unique-key', 'this is a string which will be cached')
7# Later on you can retrieve it in another function:
8cache.get('a-unique-key') # Will return None if key is not found in cache
1# You can specify a default value:
0cache.get('another-unique-key', 'default value')
1# You can store multiple values at once:
1cache.set_many({'a': 1, 'b': 2, 'c': 3})
1# And fetch multiple values:
3cache.get_many(['a', 'b', 'c']) # returns {'a': 1, 'b': 2, 'c': 3}
4# You can store complex types in the cache:
1cache.set('a-unique-key', {



: 42,


: [1, 2, 3, 4],




: 'this is a string',

: (1, 2, 3, 4),
: {'A': 1, 'B' : 2},


Complex values will be serialized and stored under a single key.

Before you can look up a value in the structure, it has to be
retrieved from cache and unserialized. This is not as fast as
storing simple values directly in the cache under a more complex
key namespace.
More information: Djangos cache API.

Redis as backend for Django session data

If youre using django-redis-cache as described above, you can use it to
store Djangos sessions by adding the following to your :
1SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

You can also write session information to the database and only
load it from the cache by using:
1SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'

This ensures that session data is persistent and can survive a

restart of Redis.

Redis for Django session data without djangoredis-cache

Alternatively, you can use Redis exclusively as a store for
Djangos session data. The django-redis-sessions module lets you
do this.
Inside your virtual environment install django-redis-sessions :
(hello_django) $ pip install django-redis-sessions

Now, configure redis_sessions.session as the session engine in

your . Since were using a socket to connect, we also need
to provide its path:
1SESSION_ENGINE = 'redis_sessions.session'
2SESSION_REDIS_UNIX_DOMAIN_SOCKET_PATH = '/var/run/redis/redis.sock'

Thats it. After you restart your application, session data should
be stored in Redis instead of the database.
More information: working with sessions in Django.

Redis for multiple applications

Please note that the solution described above can only be used by
a single Django application. If you want to use multiple Django
applications with Redis, you could try to separate their
namespaces, by using key prefixes, or using a different Redis
numeric database for each ( 1 for one application, 2 for another,
etc). These solutions are not advised however.
If you want to run multiple applications each with a Redis cache,
the recommendation is to run a separate Redis instance for each

application. On Chris Laskeys blog you can find instructions

for setting up multiple Redis instances on the same server.

Install Elasticsearch

Download and install the public signing key:
rpm --import
Add the following in your /etc/yum.repos.d/ directory in a file with
a .repo suffix, for exampleelasticsearch.repo
name=Elasticsearch repository for 1.7.x packages

And your repository is ready for use. You can install it with:
yum install elasticsearch

Configure Elasticsearch to automatically start during bootup. If your

distribution is using SysV init, then you will need to run:

The repositories do not work with older rpm based distributions that
still use RPM v3, like CentOS5.
chkconfig --add elasticsearch

Otherwise if your distribution is using systemd:

sudo /bin/systemctl daemon-reload

sudo /bin/systemctl enable elasticsearch.service

Start elasticsearch
Whereis elasticsearch.