Deploying Django Applications

Introduction

Once we have finished developing the web application, it must be hosted so that the public can access it from anywhere. We'll see how to deploy and host an application on an AWS EC2 instance using Nginx as the web server and Gunicorn as the WSGI.





AWS EC2

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides scalable computing power in the cloud. We install and host our web applications on an EC2 instance after selecting an AMI (OS) at our discretion. We'll talk more about this in the following sections.





NGINX

Nginx is an open source web server. We will use Nginx to server our web pages as needed.





GUNICORN

Gunicorn is a server-side implementation of the Web Server Gateway Interface (WSGI), which is commonly used to run Python web applications.





WSGI - Used to forward a request from a web server to a Python backend.





We will not be using the default server that comes with django in production.





Deploy the application

We will launch an EC2 instance on AWS by logging into the aws console.





  • EC2





  • New instance Ubuntu .





  • , , , .





  • 8000 9000, . , , .





, 'connect' ( putty ).





sudo apt-get update
      
      



python , pip django





sudo apt install python
sudo apt install python3-pip
pip3 install django
      
      



, , , django.





cd  /home/ubuntu/  
mkdir Project
cd Project
mkdir ProjectName
cd ProjectName
      
      



.

/home/ubuntu/Project/ProjectName





GitHub

, , ec2.





  • /home/ubuntu/Project/ProjectName/ )





  • git clone <repository-url>







, git pull



.





Settings.py .

settings.py .









  • Debug = False







  • ALLOWED_HOSTS







BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, β€œstatic”)
      
      



, ( STATIC_ROOT).





manage.py makemigrations
manage.py migrate
manage.py collectstatic
      
      



Nginx

Nginx





 sudo apt install nginx
      
      



/etc/nginx/sites-enabled/



, NGINX, .





sudo vi default
      
      



, .





proxy_pass http://0.0.0.0:9000 , /static/, . , ,





manage.py collectstatic
      
      



nginx





sudo service nginx start             #to start nginx
sudo service nginx stop              #to stop nginx
sudo service nginx restart           #to restart nginx
      
      



Gunicorn

pip install gunicorn
      
      



, , : /home/ubuntu/Project



, , gunicorn





gunicorn ProjectName.wsgi:application- -bind 0.0.0.0:9000
      
      



, nginx gunicorn, DNS ec2.








All Articles