Django Tutorial: HTTPS/SSL Configuration πŸ”’

beginner
13 min

Django Tutorial: HTTPS/SSL Configuration πŸ”’

Welcome to the Django Tutorial series! Today, we're going to dive into the world of HTTPS/SSL configuration. This is an essential step to secure your Django web application, ensuring data privacy and protecting your users from potential threats.

What is HTTPS and SSL? πŸ’‘

HTTPS (Hypertext Transfer Protocol Secure) is an extension of HTTP, designed to secure communications on the web. SSL (Secure Sockets Layer) is a security protocol used to encrypt data transmitted between a client (browser) and a server. When you see a website address start with https://, it means that the website uses SSL to protect the communication.

Setting up HTTPS in Django 🎯

Let's get started with setting up HTTPS in our Django project. First, we need to obtain an SSL certificate. There are many certificate authorities (CAs) available, but for this tutorial, we'll use Let's Encrypt.

Step 1: Install Certbot πŸ“

Certbot is a free, automated certificate authority that supports Let's Encrypt. To install Certbot on your system, follow the instructions for your operating system:

  • Ubuntu: sudo apt-get install certbot
  • CentOS: sudo yum install certbot

Step 2: Obtain an SSL certificate πŸ’‘

Now that Certbot is installed, we can obtain our SSL certificate. Run the following command:

certbot certonly --webroot -w /path/to/your/project/public -d example.com

Replace example.com with your domain name and /path/to/your/project/public with the path to your Django project's public directory. This command will prompt you to agree to the Let's Encrypt terms and conditions and will generate the necessary SSL certificate.

Step 3: Configure Django to use HTTPS βœ…

With our SSL certificate in hand, we can now configure Django to use HTTPS. Open your project's settings.py file and find the SECURE_SSL_REDIRECT setting. Set it to True:

python
SECURE_SSL_REDIRECT = True

Now, find the SECURE_PROXY_SSL_HEADER setting and add the following if you're using an Nginx proxy:

python
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
Quick Quiz
Question 1 of 1

Why is it important to use HTTPS in a Django web application?

Advanced: SSL for Custom Domains 🎯

If you're using custom domains with Django, you'll need to update the SSL certificate accordingly. This process is similar to the one described above, but you'll need to specify the custom domain instead of the main domain.

Step 1: Obtain an SSL certificate for the custom domain πŸ’‘

Run the Certbot command, replacing example.com with your custom domain:

certbot certonly --webroot -w /path/to/your/project/public -d custom-domain.example.com

Step 2: Configure Django to use the custom SSL certificate βœ…

Open your project's settings.py file and update the SECURE_SSL_CERTIFICATE and SECURE_SSL_KEY settings with the path to your new SSL certificate and key files:

python
SECURE_SSL_CERTIFICATE = '/path/to/your/ssl/certificate.pem' SECURE_SSL_KEY = '/path/to/your/ssl/key.pem'

Don't forget to set SECURE_SSL_REDIRECT to True as well.

With this setup, Django will use the custom SSL certificate for the specified custom domain.

Quick Quiz
Question 1 of 1

If you're using custom domains with Django, where should you update the SSL certificate?

That's it for this tutorial on HTTPS/SSL configuration in Django! By following these steps, you've secured your web application, ensuring a safer experience for your users. Happy coding! πŸ’»πŸŽ‰