blog image

In This Tutorial wee will learn how to deploy different application in VPs hosting with AWS seervices.

 

Deploy Django to Production Using NGINX Gunicorn and Supervisor on AWS

 there are many steps need to follow for deployment of django application which are as follow:

1.Select Region

The first thing we need to do is to select Region where we want to set up our EC2 Machine , select one according to requirements

2.Now lunch an instance on AWS

Now Search EC2 on AWS. and Click on Launch Instance button to create a new EC2 instance.

Enter Name of instance......

select an os of the instance

Select Instance type for instance

New create new key pair for instance on click on generate new key-pair

You can choose key-pair type and private key file formate according to requirements

 

For network settings

Select how much storage space you want for your instance

Now Click Launch Instance button

 

3.Associate Elastic IP addresses

 Click on Elastic IP

Then go to Allocate elastic IP to create IP

Click on Allocate button to create.

Once you allocate an Elastic IP address next step is to Associate Elastic IP address with you instance

Now select the instance that you want to associate as shown below

Then you can see the instance is now connected with elastic ip in instance dashboard

4.Connect to Instance

Go to the instance dashboard , select the instance you want to connect and then click on connect button

then you would seen interface like this

Then click on Connect Button

Then new terminal would be open which is your syateem to host application also. you can open this server in your local computer as well usihn the following mention methods.

Now instance and ip is created now you can connect wit h instance using many ways like

Ways to connect with instance:

i.connect with key-pair

ii. connect with ssh key

iii. connect with Putty

 

i.connect using putty

you can visit in this site https://www.putty.org/ and download Putty in your system and installed it. remember it also installed Puttygen software , using it you can regenerate the putty key as well. for this open Puttygen software from your computer

Then click on file --> load private key --> thene you can open any .ppk file and enetr generate new .ppk file for you , which you can used 

To connect instance with Putty . 

1. Open Puttuy 

Enter host or id address, then go to Connection-->SSH--->Auth-->Credential then browser the .ppk file in Private key file for authentication section that you downlaod while creating the key-pair for instance.

Note. then you can create session and save it aslo if you don't want to enter .ppk file everytime you opne Putty as well. for that go to seassion then enter one new session name and then saved it . so that next time you can just load it to open server.

then enter open button. it will open terminal with login as:   you can enter like root,ubuntu,ec2-user depend on who are you.

you can check it by commadn whoami command as well in terminal

 

 

connect with key-pair

run this command:

chmod 400 instance-key.pem

# Connect to your instance using its Public DNS
ssh -i "instance-key.pem" ubuntu@ec2-52-66-135-96.ap-south-1.compute.amazonaws.com

 

connect with ssh key

In order to connect with your instance using SSH key, you need to add your computer ssh key (id_rsa.pub) to instance authorized_keys.

To generate SSH key to mac Enter the following command in the Terminal window (Don’t do “sudo”)

ssh-keygen -t ed25519 -b 4096 -C "your_email@example.com"

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_ed25519

GitHub Docs

and then

cd ~/.ssh/

cat id_ed25519.pub

Now copy the key and paste it in instance ~/.ssh/authorized_keys file

Now run this command

ssh ubuntu@13.232.8.144

where 13.232.8.144 is Elastic IP

 

4.Inbound rule

Go to the inbound rule of the instance and then click on edit button

Allow SSH, HTTP, HTTPS traffic and any other port you want to open.

You can also use All traffic to allow all the traffic to instance but it is not recommended.

 

6.create virtual env in the terminal access you have got it 

sudo apt update
sudo apt install virtualenv 
virtualenv env_myproject

replace env_myproject with the name you like for you environment

Clone your django project

To clone a project you need to create and add instance SSH key to GIT.

Create instance SSH key run following command (Don’t do “sudo”)

ssh-keygen -t ed25519 -C "your_email@example.com"

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_ed25519

Add instance id_ed25519.pub to GIT to clone project.

Or you can simply clone the https: project clone and then setup all the requirements.txt file , database setup, then makemmigrations ,and other needed requirements.

 then you can test your systemin your browser by running command like python manage.py runserver 0.0.0.0:800 and run in the browser by ipv4_of_your_instance:8000 port.

 

 

 

....................................................................................................................................................................

https://github.com/geekyshow1/GeekyShowsNotes/blob/main/nginx/Deploy_Django_Nginx.md

ssh-keygen -t rsa -C "dreamtocometrue@example.com"
ls ~/.ssh
ssh-keygen -t rsa -f ~/.ssh/id_rsa_panta148 -C "your_email@example.com"
ssh-add ~/.ssh/id_rsa_panta148
cat ~/.ssh/id_rsa_panta148.pub

[Unit]
Description=nepalmuhar.com.gunicorn socket

[Socket]
ListenStream=/run/nepalmuhar.com.gunicorn.sock

[Install]
WantedBy=sockets.target

sudo nano /etc/systemd/system/nepalmuhar.com.gunicorn.service

Syntax:-
[Unit]
Description=your_domain.gunicorn daemon
Requires=your_domain.gunicorn.socket
After=network.target

[Service]
User=username
Group=groupname
WorkingDirectory=/home/username/project_folder_name
ExecStart=/home/username/project_folder_name/virtual_env_name/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/your_domain.gunicorn.sock \
          inner_project_folder_name.wsgi:application

[Install]
WantedBy=multi-user.target

Example:-
[Unit]
Description=nepalmuhar.com.gunicorn daemon
Requires=nepalmuhar.com.gunicorn.socket
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/home/nepalmuhar
ExecStart=/home/nepalmuhar/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/nepalmuhar.com.gunicorn.sock \
          newsportal.wsgi:application

[Install]
WantedBy=multi-user.target

sudo systemctl start nepalmuhar.com.gunicorn.socket
sudo systemctl start nepalmuhar.com.gunicorn.service

sudo systemctl enable nepalmuhar.com.gunicorn.socket
sudo systemctl enable nepalmuhar.com.gunicorn.service

sudo systemctl status nepalmuhar.com.gunicorn.socket
sudo systemctl status nepalmuhar.com.gunicorn.service

sudo nano /etc/nginx/sites-available/nepalmuhar.com

Syntax:-
server{
    listen 80;
    listen [::]:80;
    
    server_name your_domain www.your_domain;
    
    location = /favicon.ico { access_log off; log_not_found off; }
    
    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://unix:/run/your_domain.gunicorn.sock;
    }
    
    location  /static/ {
        root /var/www/project_folder_name;
    }

    location  /media/ {
        root /var/www/project_folder_name;
    }
}

Example:-
server{
    listen 80;
    listen [::]:80;

    server_name 159.69.22.45;

    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://unix:/run/nepalmuhar.com.gunicorn.sock;
    }

    location  /static/ {
        root /var/www/nepalmuhar;
    }

    location  /media/ {
        root /var/www/nepalmuhar;
    }
}


sudo ln -s /etc/nginx/sites-available/nepalmuhar.com /etc/nginx/sites-enabled/nepalmuhar.com

sudo systemctl daemon-reload
sudo systemctl restart nepalmuhar.com.gunicorn

sudo service nginx restart

STATIC_URL = 'static/'
STATIC_ROOT = "/var/www/nepalmuhar/static/"

MEDIA_URL = '/media/'
MEDIA_ROOT = "/var/www/nepalmuhar/media/"

 

sudo chown -R root:root nepalmuhar   # to provide root permission to root user

Syntax:- scp -P Remote_Server_Port Source_File_Path Destination_Path
Example:- scp -P 1034 miniblog.zip raj@216.32.44.12:

Syntax:- unzip zip_file_name
Example:- unzip miniblog.zip

 

# local to server
scp -r css root@159.69.22.44:/var/www/nepalmuhar/static
# transfer local css file in following directory

# server to local
scp -r root@159.69.22.44:/var/www/media /home/user/projects
#transfer media folder into following directory


ssh root@159.69.22.44

 mkdir -p /home/user/3.projects/clientproject/nepalmuhar
 scp root@159.69.22.45:/home/latest_production_nepalmuhar.sql /home   #server to local
 sudo scp root@159.69.22.45:/home/latest_production_nepalmuhar.sql /home  #server to local
 sudo scp root@159.69.22.45:/home/latest_production_nepalmuhar.sql /home/user/3.projects/clientproject/nepalmuhar



#to import local database file to mysql server's database's file which is already exist
 mysql -u username -p latest_production_nepalmuhar < latest_production_nepalmuhar.sql
 sudo mysql -u username -p latest_production_nepalmuhar < latest_production_nepalmuhar.sql
 sudo mysql -u root -p latest_production_nepalmuhar < latest_production_nepalmuhar.sql


#to export database file
mysqldump -h 127.0.0.1 -P 3306 -u root -p latest_production_database > production_db_backup_backup_local.sql (on local system database)



#to take backup of database file
mysqldump -u username -p latest_production_database_nmae > mydatabase_backup.sql
#it will make backup file in current directory on which you hit this command


#to login to database server and download database file in local directory
mysqldump -h 159.69.22.44 -u user_name -p latest_production_database > mydatabase_backup.sql





 
 
 


About author

author image

Amrit Panta

Python developer, content writer



3 Comments

Amanda Martines 5 days ago

Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod Pinterest in do umami readymade swag. Selfies iPhone Kickstarter, drinking vinegar jean.

Reply

Baltej Singh 5 days ago

Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.

Reply

Marie Johnson 5 days ago

Kickstarter seitan retro. Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.

Reply

Leave a Reply

Scroll to Top