django-deployment-aws

Contents

Share this article

Deploy the Django Application in EC2 Ubuntu Server Using ngnix & gunicorn

  1. set up an ec2 instance 

  2. edit inbound rule – allow all TCP & HTTP to 0.0.0.0/0

  3. login to your instance using the ssh key (.pem key) 

    ssh -i yourkey.pem ubuntu@instance_public_ip
  4. Install python, pip, postgres & nginx to the instance

    sudo apt update
    sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl
  5. make a folder and clone your repository to that folder

    mkdir project 
    cd project 
    git clone githubrepolink
  6. update the python package manage pip and install the virtualenv

    sudo -H pip3 install --upgrade pip
    sudo -H pip3 install virtualenv
    
  7. make a virtualenv inside the project folder and activate it

    virtualenv myprojectenv
    source myprojectenv/bin/activate
    
    
  8. move back inside the project folder > project and install all dependencies of requirements.txt file

    pip install -r requirements.txt
  9. make sure to allow the allowed host to specific ip or all of your django project’s settings.py

    ALLOWED_HOSTS = ['*']
  10. now you can try running the application to 0.0.0.0:8000

    python manage.py runserver 0.0.0.0:8000
  11.  you’ll be able to access the application in your instance public ip :8000

    http://instancepublicip:8000
  12. now although the application is running with the attached terminal if some issues evoked the terminal will be interupted and crashes the application so to automate the terminal from keep on restarting after the interuption, gunicorn is needed. To install the guicorn use 

    pip install gunicorn
  13. to check if the gunicorn is working

    gunicorn --bind 0.0.0.0:8000 myproject.wsgi
  14. now to make a persistent and robust way of starting and stopping the application is by making socket files and systemd service

    sudo vi /etc/systemd/system/gunicorn.socket
    [Unit]
    Description=gunicorn socket
    
    [Socket]
    ListenStream=/run/gunicorn.sock
    
    [Install]
    WantedBy=sockets.target
  15. now make the gunicorn.service file and paste your project dirs accordingly here

    sudo nano /etc/systemd/system/gunicorn.service
    [Unit]
    Description=gunicorn daemon
    Requires=gunicorn.socket
    After=network.target
    
    [Service]
    User=ubuntu
    Group=www-data
    WorkingDirectory=/home/ubuntu/project/myprojectdir
    ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
              --access-logfile - \
              --workers 3 \
              --bind unix:/run/gunicorn.sock \
              myproject.wsgi:application
    
    [Install]
    WantedBy=multi-user.target
  16. start, enable and check the status of the gunicorn.socket file respectively

    sudo systemctl start gunicorn.socket
    sudo systemctl enable gunicorn.socket
    sudo systemctl status gunicorn.socket
  17. check if the sock file is working using curl

    curl --unix-socket /run/gunicorn.sock localhost
  18. if some problem arises, journalctl can be used to accessed the logs

    sudo journalctl -u gunicorn
  19. restart the gunicorn service 

    sudo systemctl daemon-reload
    sudo systemctl restart gunicorn
  20. configure nginx proxypass to gunicorn; create a server block to the nginx sites-available dir

    sudo nano /etc/nginx/sites-available/myproject
    server {
        listen 80;
        server_name server_domain_or_IP;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/ubuntu/project/myprojectdir/staticfiles;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/run/gunicorn.sock;
        }
    }
  21. create a link from sites-available to sites-enabled

    sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
  22. check if the syntax is okay

    sudo nginx -t
  23. restart the nginx service 

    sudo systemctl restart nginx
  24. openup the firewall to open traffic and remove the development server port 8000

    sudo ufw delete allow 8000
    sudo ufw allow 'Nginx Full'
  25. incase of 502 Bad Gateway instead of the django application; check the nginx logs

    sudo tail -F /var/log/nginx/error.log
Scroll to Top