Deploy Django App on #Nginx & #Gunicorn with Reverse Proxy as a Service on AWS EC2 from Git

Syed Nazrul Hassan
Syed Nazrul Hassan
57 بار بازدید - 5 ماه پیش - This video demonstrates how you
This video demonstrates how you can connect your Django App with your domain
You can set Django App  on AWS EC2  from Git and Deploy with Nginx and Gunicorn (Installed as Service) and then update the Elastic IP connected to your EC2 instance against your domain's A Record

sudo apt-get update && upgrade
sudo apt-get install vim python3 python3-pip python3-dev pkg-config libmysqlclient-dev nginx

1- Clone from git in my case it was
git clone https://github.com/syednazrulhasan/dj...

2- CD into the cloned folder in my case its djangocrm
cd djangocrm

3- Install pip3 inside project directory in my case its djangocrm
sudo pip3 install virtualenv

4- Create a  virtual environment with name as venv
virtualenv venv

5- Activate the virtual environment
source venv/bin/activate

6- Install all packages using following command  Make Sure to keep requirements.txt updated  on git)
pip install -r requirements.txt

7- Run following commands to make sure migrate and makemigrations commands are run
python3 manage.py migrate
python3 manage.py makemigrations

8- Create a super user
python manage.py createsuperuser

9- Finally run the command to django on your instance is
python3 manage.py runserver 0.0.0.0:8000

10- Install Gunicorn (INSIDE Virtual Environment)
pip install gunicorn

11- Starting Gunicorn to serve a Django application named "advacrmproject"
gunicorn --bind 0.0.0.0:8000 advacrmproject.wsgi:application

12- Deactivate virtual enviorment
deactivate

12- Create a Gunicorn Socket
sudo vi /etc/systemd/system/gunicorn.socket

#-----------------------------------------------------------------#
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target
#-----------------------------------------------------------------#

13- Find out user group and present working directory
ll
pwd

14- Create a gunicorn.service File:
sudo vi /etc/systemd/system/gunicorn.service

#-----------------------------------------------------------------#
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/djangocrm
ExecStart=/home/ubuntu/djangocrm/venv/bin/gunicorn \
         --access-logfile - \
         --workers 3 \
         --bind unix:/run/gunicorn.sock \
         advacrmproject.wsgi:application

[Install]
WantedBy=multi-user.target
#-----------------------------------------------------------------#

14- Start gunicorn enable gunicorn as a service(to start automatically along with operating system in case instance is restarted)

sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

15- Create a nginx configuration file
sudo vi /etc/nginx/sites-available/advacrmproject

#-----------------------------------------------------------------#
server {
   listen 80;
   server_name 18.144.68.12;

   location = /favicon.ico { access_log off; log_not_found off; }
   location /static/ {
       root /home/ubuntu/djangocrm;
   }

   location / {
       include proxy_params;
       proxy_pass http://unix:/run/gunicorn.sock;
   }
}
#-----------------------------------------------------------------#

16- Enable the configuration above to enable site by creating a symbolic link to configuration file
sudo ln -s /etc/nginx/sites-available/advacrmproject /etc/nginx/sites-enabled/

17- Restart Nginx
sudo systemctl restart nginx
5 ماه پیش در تاریخ 1402/11/15 منتشر شده است.
57 بـار بازدید شده
... بیشتر