This tutorial will walk you through each step in detail on how to configure your django websocket application on a Ubuntu 20.10 server. This article assumes you are familiar with Django and have a ubuntu remote server running up. To learn more about Django, checkout – Django Tutorial
First, let’s see what all we will be using to put this into production,
- Nginx – Web and Proxy Server
- Daphne – our ASGI (Asynchronous Server Gateway Interface) server which will serve our Django application
- Redis Backend Server – which will handle our web socket connections (ws://)
Nginx configuration
Install Nginx and Supervisor
$ sudo apt install nginx supervisor
In your /etc/nginx/sites-available/ folder create your server and add the below content,
upstream redis_backend_server{
server localhost:6379;
}
upstream app_server {
server localhost:9090;
}
server {
listen 80;
listen 443 ssl;
keepalive_timeout 700;
ssl_certificate <path to your cert>;
ssl_certificate_key <path to your key>;
server_name foo.com www.foo.com;
access_log <path to your access logs>;
error_log <path to your error logs>;
add_header X-Frame-Options SAMEORIGIN;
add_header Content-Security-Policy "frame-ancestors self https://foo.com";
location /static/ {
root /var/www/staticfiles/;
}
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
location / {
include proxy_params;
proxy_pass http://app_server;
}
location /ws {
proxy_pass http://redis_backend_server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_ssl_certificate <path to your cert>;
proxy_ssl_certificate_key <path to your key>;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Note – Replace foo with your IP or your domain name.
Now save the configuration and restart Nginx,
$ sudo service nginx reload
Check if the configurations are done correctly as below,

Once that is done, let’ move on to our actual application server.
Daphne Configuration
Install Daphne
$ pip3 install daphne
Test if things are working,
$ daphne -p 8001 project.asgi:application
You should see something similar in your terminal,

Go to <server-ip>:8001 in your browser to check if the application is working.
In the /etc/supervisor/conf.d/ folder create django_server and add the content below,
[fcgi-program:django_server]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:9090
# Directory where your site's project files are located
directory= <path>
# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=<path to daphne> -u /run/daphne/daphne%(process_num)d.sock --endpoint fd:fileno=0 --access-log - --proxy-headers project.asgi:application
# Number of processes to startup, roughly the number of CPUs you have
numprocs=1
# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d
# Automatically start and recover processes
autostart=true
autorestart=true
# Choose where you want your log to go
stdout_logfile=<path to your asgi logs>
redirect_stderr=true
Note – replace project with your project name
once that is done we have to create a dir for our sockets to run,
$ sudo mkdir /run/daphne/
Restart and update the supervisor configurations,
$ sudo supervisorctl reread
$ sudo supervisorctl update
I know it’s a lot to take in, we just have one more step to get our server up and running. So stay with me.
Redis Backend Server Configuration
This is the server that will handle all our web socket connections that the Daphne server forwards.
Install redis server
$ sudo apt install redis-server
Edit the Redis configurations to run as a service with our systemd
$ sudo nano /etc/redis/redis.conf
Change “supervised no” to “supervised systemd” in the config file
$ sudo systemctl restart redis.service
Check if the server is Active and running
sudo systemctl status redis
You should get the below response

Finally with all in place and running, you should be seeing your application up and running on your server.

Similar Reads
Python | Django-allauth setup and Configuration
User registration is one of the most essential parts of a web application. django-registration-redux and django-alluth are the most famous registration apps available in Django. This tutorials series deals with setup, configuration, and customization of django-allauth and serve as a guide for new us
5 min read
Configuring Django Apps for Heroku
In this article, we will learn how to configure a Django app for Heroku step-by-step. Heroku is a cloud-based application deployment and management service which supports several programming languages such as Ruby, Java, Node.js, Scala, Clojure, Python, PHP, and Go. It requires a Procfile, this file
2 min read
Token Authentication in Django Channels and Websockets
Prerequisites: Django, WebSockets, Django channels, Token authentication The most popular Django topics right now are WebSockets and Django channels because they make it possible to communicate quickly and easily while working in real-time without constantly refreshing the page. When working with th
13 min read
Simple chat Application using Websockets with FastAPI
In this article, we'll delve into the concept of WebSockets and their role in facilitating bidirectional communication between clients and servers. Subsequently, we'll embark on the journey of building a real-time multi-client chat application. This application will empower numerous users to connect
6 min read
Django Sign Up and login with confirmation Email | Python
Django by default provides an authentication system configuration. User objects are the core of the authentication system. Today we will implement Django's authentication system. Modules required: Django install, crispy_forms Django Sign Up and Login with Confirmation EmailTo install crispy_forms yo
7 min read
Joke Application Project Using Django Framework
Django is a high-level Python-based Web Framework that allows rapid development and clean, pragmatic design. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database SQLlite3, etc. Today we will create
2 min read
How To Integrate Ajax with Django Applications
Django is one of the most popular web frameworks for building robust and scalable web applications. However, many modern applications require asynchronous features, enabling real-time interactions without the need to reload the entire page. This is where Ajax (Asynchronous JavaScript and XML) comes
5 min read
Automatic Creation Date for Django Model Form Objects
In many web applications, itâs often important to keep track of when an object was created and updated. This can be useful for auditing, sorting, or simply understanding the timeline of our data. Django provides a simple way to automatically manage these timestamps using the auto_now_add and auto_no
4 min read
Change the Django Default Runserver Port
Changing the default run server port in Django is a simple yet useful adjustment for various development scenarios. By specifying a different port, you can avoid conflicts, manage multiple projects, and test your application in different environments. Remember to adjust your development and testing
3 min read
Creating first web application using Bottle Framework - Python
There are many frameworks in python which allow you to create a webpage like bottle, flask, django. In this article, you will learn how to create a simple app using bottle web framework. Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file
2 min read