DEV Community

Devashish Roy
Devashish Roy

Posted on

Django deployment on AWS

Today we are going to deploy our Django project in AWS. Our Django project’s code are stored in the GitHub repository, and we have an EC2 instance created and running with ubuntu 18.04 LTS operating system in AWS. We are going to see the steps to deploy our Django project from GitHub to AWS EC2 instance.

Prerequisite

Before going to the steps and setting up our server, we have a checklist, go through it and make sure it’s done before going through the steps.

Checklist

  1. Django Project

    You must have a Django project which is completed or it runs without any error, and the Django project must stored inside the django_deployment directory.

  2. GitHub repository

    After having the Django project you must push all the codes into the GitHub repository.

  3. AWS (Amazon Web Services)

    You will need a free or paid AWS account with an EC2 instance created and running with ubuntu 18.04 LTS and also it should accept all the public request from http and https method.

Setup our EC2 instance

⚠️ Note: For my convenience I am renaming EC2 instance to cloud computer. From now on whenever their is cloud computer it means EC2 instance.

Till now we have created our cloud computer and it’s running ubuntu 18.04 LST operating system. Now our task is to download and configure the software package to run our Django project. To run our Django application we need:

  1. python3-dev
  2. python3-venv
  3. python-pip
  4. SQLite
  5. git

So let’s install all the required software dependencies for our project. Before doing so we are going to update our system for the latest software packages.

Run these two commands to update and upgrade all the software packages installed in the system.

apt-get update
apt-get upgrade
Enter fullscreen mode Exit fullscreen mode

By using these two commands we have completed our system update.

Now we are going to install all the software packages we need for our Django project.

apt-get install -y python3-dev python3-venv sqlite python-pip git
Enter fullscreen mode Exit fullscreen mode

By using the above command we have installed the required software packages.

At this point we have done our cloud computer setup.

Next we are going to setup our Django project in the cloud computer.

Django project setup

We are going to save all of our work in the /usr/local/workspace directory. So from now on whenever I use WORKSPACE directory it means /usr/local/workspace directory.

First we are going to create an apps directory inside our WORKSPACE directory. In this directory we are going to store our all projects.

mkdir -p /usr/local/workspace/apps
Enter fullscreen mode Exit fullscreen mode

Now we are going to clone our Django project from GitHub.

git clone https://github.com/<username>/github_repo.git /usr/local/workspace/apps/django_deployment
Enter fullscreen mode Exit fullscreen mode

Using the above git command we have cloned our Django project from GitHub to our cloud computer and it is stored inside the WORKSPACE/apps/django_deployment directory.

Now we going to create and env directory inside our WORKSPACE directory. Inside this directory we are going to store virtual environments for our projects.

mkdir -p /usr/local/workspace/env
Enter fullscreen mode Exit fullscreen mode

Now let’s create a virtual environment for our Django project.

python3 -m venv /usr/local/workspace/env/django_deployment
Enter fullscreen mode Exit fullscreen mode

After creating the virtual environment we need to install the requirements of our Django project, all the requirements of our Django project are stored in the requirements.txt file inside the project directory.

/usr/local/workspace/env/django_deployment/bin/pip install -r /usr/local/workspace/apps/django_deployment/requirements.txt
Enter fullscreen mode Exit fullscreen mode

The above command will install all the requirements of our Django project inside the virtual environment.

Now we are going to run migrations and create static files for our Django project.

/usr/local/workspace/env/django_deployment/bin/python /usr/local/workspace/apps/django_deployment/manage.py makemigrations
/usr/local/workspace/env/django_deployment/bin/python /usr/local/workspace/apps/django_deployment/manage.py migrate
/usr/local/workspace/env/django_deployment/bin/python /usr/local/workspace/apps/django_deployment/manage.py collectstatic --noinput
Enter fullscreen mode Exit fullscreen mode

At this point we are done with the Django project setup.

Next is to install and configure our supervisor and nginx server so that we can accept and process requests and give them responses.

Server setup

We need supervisor and nginx, two software packages, supervisor to run our Django project’s python files and nginx to serve Django project’s static files.

Let’s first install both the software packages.

apt-get install -y supervisor nginx
Enter fullscreen mode Exit fullscreen mode

Configure supervisor

supervisor uses configuration files to manage processes. All the configuration files of supervisor is stored inside /etc/supervisor/conf.d

Create a configuration file for supervisor.

nano /etc/supervisor/conf.d/django_deployment.conf
Enter fullscreen mode Exit fullscreen mode

This command will open a code editor inside the terminal. We need to write our configuration inside this. The configuration file includes this

[program:django_deployment]
environment =
    DEBUG=0
command = /usr/local/workspace/env/django_deployment/bin/uwsgi --http-socket :9000 --wsgi-file /usr/local/workspace/apps/django_deployment/django_server/wsgi.py
directory = /usr/local/workspace/apps/django_deployment
user = root
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/django_deployment.log
stderr_logfile = /var/log/supervisor/django_deployment_err.log
Enter fullscreen mode Exit fullscreen mode

Press CTRL + X, it will prompt yes or no to save it, press y and then enter to confirm and after that it will ask for the file name which we have already provided at the time of nano command as django_deployment.conf, so press enter. It will save and exit the code editor.

This will create a program / process named django_deployment and it will run our Django project using wsgi.py file.

Now need to update our process list and restart the processes.

supervisorctl reread
supervisorctl update
supervisorctl restart django_deployment
Enter fullscreen mode Exit fullscreen mode

This three commands will update the process list and restart our django_deployment process. To check the status of the processes you can use status option with supervisorctl command, this will list all the processes with their current status.

supervisorctl status
Enter fullscreen mode Exit fullscreen mode

Now we are done with the supervisor configuration.

Next we are going to configure nginx so that we can serve the static files of our Django project.

Configure nginx

nginx also uses configuration files to manage the server configurations. All the configuration files of nginx is stored inside /etc/nginx directory and the sites available to serve, their configurations are stored inside the /etc/nginx/sites-available directory.

For our Django project we are going to create a configuration file for nginx to serve the static files.

nano /etc/nginx/sites-available/django_deployment.conf
Enter fullscreen mode Exit fullscreen mode

This command will open a code editor inside the terminal. We need to write our configuration inside this. The configuration file includes this

server {
    listen 80 default_server;

    location /static {
        alias /usr/local/workspace/apps/django_deployment/static;
    }

    location / {
        proxy_pass        http://127.0.0.1:9000/;
        proxy_set_header  Host                $host;
        proxy_set_header  X-Real-IP           $remote_addr;
        proxy_set_header  X-Forwarded-For     $remote_addr;
        proxy_set_header  X-Forwarded-Proto   $scheme;
        proxy_redirect    off;
    }
}
Enter fullscreen mode Exit fullscreen mode

Press CTRL + X, it will prompt yes or no to save it, press y and then enter to confirm and after that it will ask for the file name which we have already provided at the time of nano command as django_deployment.conf, so press enter. It will save and exit the code editor.

This will create the configuration file for nginx to server our static files which will create and stored inside the /usr/local/workspace/apps/django_deployment/static directory

Now we need to remove a file /etc/nginx/sites-enabled/default and in place of this file we need to create a link from a file to the configuration file /etc/nginx/sites-available/django_deployment.conf

rm /etc/nginx/sites-enabled/default
ln -s /etc/nginx/sites-available/django_deployment.conf /etc/nginx/sites-enabled/django_deployment.conf
Enter fullscreen mode Exit fullscreen mode

Now we need to restart the server so that the updated configuration comes in action.

systemctl restart nginx.service
Enter fullscreen mode Exit fullscreen mode

Now we are done with the nginx configuration.

So, till at this point we can see our Django project running and live on internet, and we can check it in the web browser.

Update changes

Update local computer and push to GitHub

First we are going to change and update our files inside the local machine, and then we will commit these changes and push it to the GitHub.

Pull the updated file from GitHub to cloud computer

Now we will pull the updated files from GitHub.

To pull the file change directory to the project directory

cd /usr/local/workspace/apps/django_deployment
Enter fullscreen mode Exit fullscreen mode

Now run the pull request

git pull
Enter fullscreen mode Exit fullscreen mode

Now git will pull all the updated files for you and update your code.

Effect the changes

Now we are going to run migrations and create static files for our Django project.

Note: Go through these steps if you have done changes in the database and static files. And if you are in doubt just go through these, if their will be any changes they will take care of it.

/usr/local/workspace/env/django_deployment/bin/python /usr/local/workspace/apps/django_deployment/manage.py makemigrations
/usr/local/workspace/env/django_deployment/bin/python /usr/local/workspace/apps/django_deployment/manage.py migrate
/usr/local/workspace/env/django_deployment/bin/python /usr/local/workspace/apps/django_deployment/manage.py collectstatic --noinput
Enter fullscreen mode Exit fullscreen mode

Restart the supervisor process

Now restart the supervisor process to effect the changes.

supervisorctl restart django_deployment
Enter fullscreen mode Exit fullscreen mode

Time to check our live site

Now it’s time to hit the web browser with the URL. After going to the URL you can see your live Django project direct from your cloud computer.

Top comments (0)