Install Memcached on Ubuntu for your Django app

KwekuQ
KwekuQ
Aug 10, 2017 · 3 min read

In last weeks post I wrote on how to run a django app in a production environment, we used gunicorn, supervisor and nginx in that instance to run the app and serve it to the outside world. This time around I want to focus on running memcached to cache certain elements of our web app in django. If you would like to get get some idea of the benefits of caching read this article. If you would also like to find out how you can encorporate memcached into your django app read this tutorial from django themselves.

I will assume that you followed my post on how to run django in prod environment, as I will be using the same project as I did there, same users and same structure. You will need to have setup supervisor, but I will still go through that.

The basic steps that will guide you through this are as follows:

  1. Setting up OS + User
  2. Write memcached bash script
  3. Setup supervisor to run bash script
  4. Link django to memcached sock file
  5. Setting up OS + User

I have mentioned that I normally use Digital Ocean to run most of my apps, however there are other various service providers (ramnode)you can choose from. I wrote a blog post on this topic, find it here on my blog.

Login to your server and run update.

apt-get update
apt-get upgrade

We need a user with sudo rights. So let us add a new user and give them sudo privileges.

adduser prod_userusermod -aG sudo prod_user

Now that we have sorted all of that out let us switch to that user

su prod_user

2. Write memcached bash script

In our script we will need to specify the user, the amount of memory we want memcached to use and also the location of the sock file.

To run the memcached command we will need to pass these parameters in. You can find more details on each param here.

I normally create my bash scripts within my project directory. As mentioned earlier I will assume you followed this guide on how to run django in a production environment.

Lets cd into our project

cd prod_project

Once in the folder we can create our bash file

sudo nano prod_memcached.bash

Once we are in you can copy and paste the below script, changing the different parameters to suite your project setup.

#!/bin/bash
NAME="prod_memcached"
SOCKETFILE="/home/prod_user/prod_project/prod_memcached.sock"
USER=prod_user
MEMORYUSAGE=128

#-d don't need this as will be used with supervisor

exec memcached -m $MEMORYUSAGE -u $USER memory -s $SOCKETFILE

Once done, ctrl+x then save the file.

We now need to make sure our script is executable.

sudo chmod u+x prod_memcached.bash

Now we can proceed to setting up supervisor to run our script for us.

3. Setup supervisor to run bash script

Supervisor will help run our bash script and monitor that it is always running. You can get more info here incase you are still new to it.

We can start by installing supervisor.

sudo apt-get install supervisor

Lets create our supervisor file.

sudo nano /etc/supervisor/conf.d/prod_memcached.conf

Once we are in the file, we can copy and paste the script below.

[program:prod_memcached]
command=/home/prod_user/prod_project/prod_memcached.bash
user=prod_user
stdout_logfile=/home/prod_user/prod_project/logs/prod_memcached.log
redirect_stderr=true
autostart=true
autorestart=true
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8

Once this is done, ctrl+x then save the file.

Now we can run our supervisor process and see what happens.

sudo systemctl restart supervisor
sudo systemctl enable supervisor

Now lets check the status of our supervisor process

sudo supervisorctl status prod_memcached

Hopefully you get a response like

prod_memcached                    RUNNING   pid 9700, uptime 0:13:13

Once that is sorted we can set django to use the sock file.

4. Link django to memcached sock file

The easiest way is to directly link our django settings file to point to the sock file that our bash script created.

There are some interesting points raised here that I think should be taken into account. I recommend reading this tutorial on how to properly setup your django app to use memcached.

However let us paste the following code in our settings.py file.

CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'KEY_PREFIX': 'prod_project',
'LOCATION': '/home/prod_user/prod_project/prod_memcached.sock',
}
}

Now our django app is pointing at the sock file for its memcached caching. We can restart our django app and after a few loads the site should theoretically be way faster.

This process took me a while to get right initially, however I now have scripts I just mod and try keep updated.

Good luck to you!

Written by

KwekuQ

Follow F1. Write code. Be passionate.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade