Déployer des applications Django

introduction

Une fois que nous avons fini de développer l'application Web, elle doit être hébergée afin que le public puisse y accéder de n'importe où. Nous verrons comment déployer et héberger une application sur une instance AWS EC2 en utilisant Nginx comme serveur Web et Gunicorn comme WSGI.





AWS EC2

Amazon Elastic Compute Cloud (Amazon EC2) est un service Web qui fournit une puissance de calcul évolutive dans le cloud. Nous installons et hébergeons nos applications Web sur une instance EC2 après avoir sélectionné une AMI (OS) à notre discrétion. Nous en parlerons plus en détail dans les sections suivantes.





NGINX

Nginx est un serveur Web open source. Nous utiliserons Nginx pour gérer nos pages Web au besoin.





GUNICORN

Gunicorn est une implémentation côté serveur de l'interface de passerelle de serveur Web (WSGI), qui est couramment utilisée pour exécuter des applications Web Python.





WSGI - Utilisé pour transférer une requête d'un serveur Web vers un backend Python.





Nous n'utiliserons pas le serveur par défaut fourni avec django en production.





Déployer l'application

Nous lancerons une instance EC2 sur AWS en nous connectant à la console aws.





  • 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