Nginx and Django: A Beginner’s Guide to SSL Certificate Setup

Ali Saleh
2 min readApr 15, 2023

--

Nginx and Django: A Beginner’s Guide to SSL Certificate Setup

If you’re running a Django application on Nginx, it’s important to have SSL (Secure Sockets Layer) enabled to protect the data being transmitted between the server and the client. This tutorial will show you how to set up SSL on Nginx for your Django application.

Prerequisites 📋

Before we start, you should have the following:

  • A Django application running on Nginx
  • A domain name
  • A valid SSL certificate

Step 1: Install Certbot 🔑

Certbot is a free, open-source software tool that automates the process of getting an SSL certificate for your web server. To install Certbot, run the following command:

sudo apt-get install certbot python3-certbot-nginx

Step 2: Obtain an SSL Certificate 📜

To obtain an SSL certificate, run the following command and follow the prompts:

sudo certbot --nginx -d your_domain.com

This will obtain an SSL certificate for your domain and configure Nginx to use it.

Step 3: Update Nginx Configuration 🛠️

Once you have obtained an SSL certificate, you need to update your Nginx configuration file to use it. Open the configuration file in your favorite text editor (in this example, we’ll use Nano editor):

sudo nano /etc/nginx/sites-available/your_site

Find the server block and add the following lines inside the server block:

listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/your_domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem;

Save the file and exit the text editor.

Step 4: Test and Reload Nginx 🚀

Before you restart Nginx, you should test the configuration to ensure there are no syntax errors:

sudo nginx -t

If there are no errors, reload Nginx to apply the new configuration:

sudo systemctl reload nginx

Conclusion 🎉

That’s it! You have successfully set up SSL on Nginx for your Django application. By enabling SSL, you’re ensuring that the data transmitted between the server and the client is secure and encrypted.

Remember to renew your SSL certificate before it expires to avoid any downtime. You can do this by running the following command:

sudo certbot renew

🎉 Thanks for reading

Hey there — I hope you found this article informative and helpful. If you have any questions or comments, please feel free to reach out to me via email (alisheikh1114@gmail.com). I am always eager to connect with readers and help them with any technical challenges they may be facing.

👨‍💻 Let’s connect on social media

Don’t forget to follow me on Medium if you are interested in reading about Web Dev Stacks, Big Data, and Open Source.

Find me online: GitHubLinkedInTwitterFacebookInstagram

🤝 Share the knowledge

If you found this article useful, please consider sharing it with your friends and colleagues who might benefit from it. Sharing is caring, and it helps spread knowledge and insights to those who need it the most.

--

--

Ali Saleh

🎉 Tech Enthusiast ✨ Full Stack Developer 💙 Web / Big Data / Open Source ~ Find me online: https://www.linkedin.com/in/iamalisaleh