Question

How to set up django app + redis + celery

I have recently deployed a Django app. The app uses a Redis server and a celery worker to send emails and celery-beat to perform some scheduled tasks. I am unable to find a proper doc that explains how to set up redis + celery worker for a django app in digitalocean. Please explain how to do this setup


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there,

I could suggest starting with the following:

  • Make sure to first install Redis on your server:

https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-redis-on-ubuntu-20-04

  • Then deploy your Django app as normal:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

  • After that, you need to install Celery and Redis in your virtual environments:
pip install celery 
pip install redis 
  • Next, in your settings.py file add the Redis details:
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
  • After that once your Django instance is up and running, you can also start your Celery worker, eg:
celery -A celearyapp worker -l info

For more information I could suggest the Celery official docs on how to get started with Celery and Django:

https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html#using-celery-with-django

Let me know if you have any questions!

Best,

Bobby