In Depth tutorial on deploying a Django application to AWS (for free) - Part 1

Hosting With Aws – In Depth tutorial on deploying a Django application to AWS (for free) – Part 1


In Depth tutorial on deploying a Django application to AWS (for free) – Part 1

Hosting With Aws

Django is the most popular Python-based web framework for a while now. Django is powerful, robust, full of capabilities and surrounded by a supportive community.

In this video, we are going to take a look into deploying a simple Django application to an AWS ubuntu instance, with Nginx, Gunicorn & Supervisor configuration.

In this video, I have tried to explain every single step & each line of code I have written. Hopefully, this video will help you out.

Let me know the suggestions, thoughts & the doubts in the comments. Subscribe to the channel & Share this with your friends & colleagues

*NB:* I am building a server provisioning + auto deployment tool for python apps, like django, flask, fastapi etc… the site is up at pyployer.com, signup here for free early access.

Link to sample application repo:

#deployingdjango #aws #gunicorn #nginx #supervisord

you search:

Hosting With Aws

In Depth tutorial on deploying a Django application to AWS (for free) – Part 1

49 thoughts on “Hosting With Aws – In Depth tutorial on deploying a Django application to AWS (for free) – Part 1

  1. very good video.
    In case someone needs the commands that you used in aws:

    sudo apt-get upgrade -y
    sudo apt-get update -y

    1. Deploy Python virtual env
    sudo apt-get install python3-venv -y
    python3 -m venv env

    2. Activate env
    source env/bin/activate

    3. Install Django
    pip3 install django

    4. Clone the git project
    git clone https://github.com/ShobiExplains/AwsDemo.git

    5. Instal NGINX and GUNICORN
    pip3 install gunicorn ## install without sudo..
    sudo apt-get install nginx -y
    pip install psycopg2-binary

    6. Connect gunicorn
    gunicorn –bind 0.0.0.0:8000 TestProject.wsgi:application ## similar to runserver

    7. Install supervisor
    sudo apt-get install -y supervisor ## This command holds the website after we logout

    8. Config supervisor
    cd /etc/supervisor/conf.d
    sudo touch gunicorn.conf

    ##In the file file do following###
    [program:gunicorn]
    directory=/home/ubuntu/AwsDemo
    command=/home/ubuntu/env/bin/gunicorn –workers 3 –bind unix:/home/ubuntu/AwsDemo/app.sock TestProject.wsgi:application
    autostart=true
    autorestart=true
    stderr_logfile=/var/log/gunicorn/gunicorn.err.log
    stdout_logfile=/var/log/gunicorn/gunicorn.out.log

    [group:guni]
    Program:gunicorn
    ####endfile####

    9. Connect file to supervisor
    sudo supervisorctl reread

    ##if we receive the following error than we need to create the directory:
    ERROR: CANT_REREAD: The directory named as part of the path /var/log/gunicorn/gunicorn.out.log does not exist. in section 'program:gunicorn' (file: '/etc/supervisor/conf.d/gunicorn.conf')

    sudo mkdir -p /var/log/gunicorn
    sudo supervisorctl reread
    sudo supervisorctl update

    10. Check if gunicorn is running in background
    sudo supervisorctl status

    11. Create nginx configuration for django site
    sudo vim /etc/nginx/sites-available/django.conf

    ###inhalt##
    server {
    listen 80;
    server_name 18.195.252.82;

    location / {
    include proxy_params;
    proxy_pass http://unix:/home/ubuntu/AwsDemo/app.sock;
    }
    }
    ############

    12. Test file configuration

    sudo ln /etc/nginx/sites-available/django.conf /etc/nginx/sites-enabled/
    sudo nginx -t

    13. restart NGINX server
    sudo service nginx restart

    14: Open the browser:

    http://aws_instance_ip:8000/hello/andrey

  2. Hi ! the load staticfiles tag in the template files seems to be throwing an error , stackoverflow says this is due to django 3 removing this. thats why the hello/<name> seems to be throwing error. If you are using shobi's test project to deploy , do take care

  3. Hello Sobi I have project with ngnix and gunicorn. if i point my subdomain to Ec2 Elastic IP its working fine. But when i point my Domain to AWS load Balancer CNAME it not working. its going to nginx Default page. can you help me on this.

  4. nginx: [emerg] open() "/etc/nginx/sites-enabled/django.conf" failed (40: Too many levels of symbolic links) in /etc/nginx/nginx.conf:62

    nginx: configuration file /etc/nginx/nginx.conf test failed

    please help

  5. Wonderful tutorial. May I add, for some people who are still seeing "Welcome to Nginx" page, type cd /etc/nginx/sites-enabled/ and delete default file from there (sudo rm -r default). Reload nginx and reload the webpage now!

  6. if gunicorn –bind 0.0.0.0:8000 <root>.wsgi:application doesn't work you may need edit you wsgi file like this:
    import os, sys

    from django.core.wsgi import get_wsgi_application

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

    # add the project path into the sys.path
    sys.path.append('/home/ubuntu/djangomysql/mysite')

    # add the virtualenv site-packages path to the sys.path
    sys.path.append('/home/ubuntu/env/lib/site-packages')

    application = get_wsgi_application()

  7. Help!!!!
    After Step: sudo supervisorctl status

    I got-> guni:gunicorn FATAL Exited too quickly (process log may have details)

    When i check err.log: The error is
    usage: gunicorn [OPTIONS] [APP_MODULE]
    gunicorn: error: ambiguous option: –worker could match –workers, –worker-class, –worker-connections, –worker-tmp-dir

  8. i have media folder in my project which has all the images. It is working fine in production server but after deploying on ec2 same as you, images are not loaded. console of web webrowser gives error image not found. and also django admin pages looking very ugly no css is loading for admin page

  9. can you please please confirm me i also want to deploy Django application on aws but it in my app i have integrated cnn model with h5 file format can i also follow the same procedure that you mentioned or should i expose the model as rest api ? if that so please guide me how can i expose model as api

Leave a Reply

Your email address will not be published. Required fields are marked *