Part 2: Installing the required libraries & Integrating Django application with EC2 Instance:

Part 2: Installing the required libraries & Integrating Django application with EC2 Instance:

In the last part, we have created an Instance for deploying the Django application let’s take remote of the instance on the Terminal like last time we do.

Now to run the Django application on the server we need a set of applications. As our server is a fresh server so first update the server.

sudo apt-get update

sudo apt-get upgrade

Now to run the Django application on the server we need a set of applications. let’s install them.

python:

This is installed in our ubuntu server, you can check by the command below

python3 — version

Python virtual environment:

To create a different virtual environment for our application we can use the Virtual Environment. To install that use the bellow command

sudo apt-get install python3-venv

Now create a Virtual environment for our application with the following command:

python3 -m venv env

NOTE: Here env is the name of our environment you can change it if you want

Now activate the Environment:

source env/bin/activate

Django Framework:

In this series, we are using the Django framework so we need to install Django using pip.

pip3 install django

Pulling our application from the GitHub:

As this series is based on the deployment I am assuming that you have prepared your Django application production-ready and pushed it to the GitHub. If not please do that. You can also follow a video on our channel with the title “How to make Django application produ8ction ready”, please go through that and prepare your application.

If you want to use my test Django application then you can get that from https://github.com/thecorporatedoor/deploy-django-to-ec2.git

Now clone the application using the following command

git clone https://github.com/thecorporatedoor/deploy-django-to-ec2.git

Setting Nginx:

In our development, we are using the default HTTP server given by Django. But in our production we will use the Nginx server, it will perform faster. We can install Nginx using the following command:

sudo apt-get install -y nginx

Gunicorn:

This is the WSGI interface which acts as a link between our Django application and the Nginx Server using a mechanism called Unix Socket.

pip3 install gunicorn

Now if you visit the IP of your instance you can get the welcome to Nginx page. This is because we have installed Nginx and Gunicorn but we haven’t configured them.

Supervisor:

It makes sure that the application always runs in the background. If anything wrong happens to the server it automatically starts the process and makes the application running always. You can install the supervisor using the following command.

sudo apt-get install supervisor

Now we have to create the configuration so that the supervisor can read from that configuration and work accordingly. The configuration file of the Supervisor will present inside a directory path: /etc/supervisor/conf.d/

lets get into that

cd /etc/supervisor/conf.d/

Now create a configuration

sudo touch gunicorn.conf

Now open the configuration file with Nano and paste the below code

sudo nano gunicorn.conf

[program:gunicorn]
directory=/home/ubuntu/smarttuts
command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/smarttuts/app.sock smarttuts.wsgi:application
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log
[group:guni]
programs:gunicorn

Note: Here smarttuts is the name of our Django application you can change it with your application name and smarttuts.wsgi is my wsgi file name change it.

Creating a log directory:

As you can see in the file we have given the path of log file to a directory, let’s create that log directory.

sudo mkdir /var/log/gunicorn

Now tell the supervisor to read the configuration file

sudo supervisorctl reread

Now you will see guni: available

Now let’s tell supervisor to start the gunicorn in the background

sudo supervisorctl update

you can check the status by

sudo supervisorctl status

Configuring the Nginx to read the socket file we have created :

Now our next step will, to configure Nginx to read from the Socket file. All the Nginx configuration are saved in /etc/nginx/sites-available

So enter to that directory

cd /etc/nginx/sites-available

Now let’s create a configuration file for our application and Edit it Using Nano

sudo touch django.conf

sudo nano django.conf

Paste the below code

server{
listen 80;
server_name ec2-52-66-252-54.ap-south-1.compute.amazonaws.com;
location / {
include proxy_params;
proxy_pass https://unix:/home/ubuntu/smarttuts/app.sock;
}
}

Now test the configuration:

cd /etc/nginx/sites-available

if test successful we have to enable this file so that Nginx will read this file.

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

Now restart Nginx

sudo service nginx restart

now visit the website you will find the website is live but static files are not working.

要查看或添加评论,请登录

Jagadananda Saint的更多文章

  • Neural Network

    Neural Network

    Neural networks are a set of algorithms, that are designed to recognize patterns. They interpret sensory data through a…

  • K-Mean Clustering

    K-Mean Clustering

    Clustering is one of the most common exploratory data analysis techniques used to get an intuition about the structure…

  • Logistic Regression with Diabetic Detector Website?Project

    Logistic Regression with Diabetic Detector Website?Project

    Logistic Regression is a Machine Learning algorithm which is used for the classification problems, it is a predictive…

  • Linear Regression With Sales Prediction Project

    Linear Regression With Sales Prediction Project

    Linear Regression is a machine learning algorithm based on supervised learning. It performs a regression task.

  • Deploying Django Application On AWS Cloud!

    Deploying Django Application On AWS Cloud!

    New research by data management company Veritas has found out that more than 70% of organizations desire to eventually…

  • Part 6: Integrating SSL(https) with Django website

    Part 6: Integrating SSL(https) with Django website

    As a final step of our deployment, we are going to make our website more secure by adding an ssh certificate to it. For…

  • Part 5: Registering External Domain With AWS EC2 using Route53

    Part 5: Registering External Domain With AWS EC2 using Route53

    To access EC2 instance from a domain we have to link EC2 with the domain. For linking both the domains we have to use…

  • Part 4 : Creating and Configuring RDS With the Django Application

    Part 4 : Creating and Configuring RDS With the Django Application

    If your Django application using any database then you need to configure a production database. In this part, we are…

  • Part3 : Configuring Django Static files with Nginx

    Part3 : Configuring Django Static files with Nginx

    The first question in your mind should be why the static files are not working. Although it was working on your system.

  • Machine Learning Algorithms

    Machine Learning Algorithms

    ML algorithms are those that can learn from data and improve from experience, without human intervention. According to…

社区洞察

其他会员也浏览了