DEV Community

loading...

Deploying a Django Application with Nginx

Nina Hwang
πŸ‘©πŸ»β€πŸ’» ν™©μ±„μ˜ 🐣 Junior Backend Developer
・2 min read

A month ago, I uploaded a post about deploying a Django application using Apache2. This post is about deploying a Django application with Nginx.
I will use

  • Django
  • Anaconda3
  • AWS EC2
  • Ubuntu 18.04
  • Nginx
  • Let's Encrypt

This time, I'll skip installing virtual environment, setting up EC2 instance and other basic setups.

Application Deployment

Setting Up Nginx Server

Install Nginx

$ sudo apt-get update
$ sudo apt-get install nginx
Enter fullscreen mode Exit fullscreen mode

Install certbot to enable HTTPS

$ sudo apt-get install certbot
$ apt-get install python3-certbot-nginx
Enter fullscreen mode Exit fullscreen mode

Inside of /etc/nginx/conf.d, create a file named (your domain).conf
For example, 'domainname.com.conf'.
Edit the file you just created.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root (path to your root folder);
    server_name domainname.com;
}
Enter fullscreen mode Exit fullscreen mode

Save the file and run this command to make sure everything is fine.

$ sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

Then, load the server.

$ sudo nginx -s reload
Enter fullscreen mode Exit fullscreen mode

Obtaining the SSL Certificate

Run this command to generate the certificate

$ sudo certbot --nginx -d domainname.com
Enter fullscreen mode Exit fullscreen mode

Edit the 'domainname.com.conf'.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root (path to your root folder);
    server_name  domainname.com;

    listen 443 ssl; # managed by Certbot

    # RSA certificate
    ssl_certificate /etc/letsencrypt/live/domainname.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domainname.com/privkey.pem; # managed by Certbot

    location / {
        root (path to your django root);
        proxy_set_header X-Forwarded-Host $host:$server_port;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass https://localhost:8001;
    }

location /static/ {
        alias (path to the static folder);
        expires 7d;
    }
location /media/ {
        alias (path to the media folder);
}

    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    # Redirect non-https traffic to https
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot
}
server {
    if ($host = domainname.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;
    listen [::]:80 default_server;
    server_name domainname.com;
    return 404; # managed by Certbot
}
Enter fullscreen mode Exit fullscreen mode

Run the django server and you can see the server has been changed in response header.
Alt Text

Discussion (0)