Django-Nginx-Docker Production Setup with AWS

Hi, folks in this post we will know about the following points.

source: google
  1. Django setting configuration
  2. Nginx server setup
  3. Docker-compose configuration

I will update with more detailed in my upcoming posts. stay tuned :-)

Without taking much time lets start with real coding …. Let’s get started …..

  1. Django setting configuration
DEBUG = False

making Debug false won’t host static files for you so you have to do by serving inside the same server, dedicated server, or by using cloud CDN. In this tutorial, we will host using nginx within the same server. if you want to know more refer below URL, its ok to skip if you want to do quick start.

Note: to see in local env use python runserver — insecure please dont use this in production enviourment
STATIC_URL = ‘/static/’
STATIC_ROOT = (‘/home/<instance_user>/<project_directory>/static’)

static URL basically refer to your www.<domain>.com/static/<file_name>, Static root indicates where your static file exists, you can point to any folder it’s not mandated that it only situated at your project directory . its good practice to put inside your directory.

give access to this folder … nginx can use :-)

sudo chown -R 777 /home/nihar/Alie_cms/static

2. Nginx server setup

NGINX configuration, please look at all bold indicators ……

user <instance_user>;#1
http {
server {
listen 80;
listen 443 ssl;
ssl on;
ssl_certificate /etc/letsencrypt/live/; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/; # managed by Certbot
location ^~ /static {
autoindex on;
alias /home/<instance_user>/<project_directory>/static;
location / { proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass; #3
proxy_set_header X-Server-Ver $nginx_version;

Great, you finish looking at snippet, so let's get to know how we configure above ……… I mark them as numerical like #1,#2…#n ……… please find below with numerical …

#1 — instance username

#2 —location ^~ /static — it will find your location which is provided by alias . you can use root (am not sure how it works, it just one google away )

#3 — proxy_pass — we will put our docker expose port which we will know our next point.

I will Explain certbot in my upcoming post (basically we can use this for SSL ) stay tuned …follow me for upcoming post notification …

3. Docker-compose configuration

version: ‘3’
build: .
command: gunicorn projectname.wsgi:application -b — chdir=/. — log-file -
- .:/code
- “8001”
- “8001:8001”

Please follow below URL for docker Django configuration

you have to put gunicorn inside your requirement.txt file, make sure you put your expose port same as gunicorn port proxy_pass port. here it is :8001

That's it …….. docker-compose up — build and you good to go ……..

Thank you for reading my article, this is my first 1 so if you like just clap as much you want and leave a comment for more help ….