Learn How to deploy Django on Nginx and Gunicorn

Django Deployment on Nginx and Gunicorn

This article explains about various steps to deploy Django application on Amazon Web Server using nginx and gunicorn.
Django Deployment on Nginx and Gunicorn

Let's begin with the steps

1. Creating instance in Amazon Web Server

  1. Go to https://aws.amazon.com/ and create new account if you don't have any or log in to your existing account.
  2. Click on Services and go to Compute section and select EC2.
  3. Go to Create Instance section and click on Launch instance.
  4. Now choose an Amazon Machine Image (AMI), here i have used Ubuntu Server 16.04 LTS 64-bit (x86).
  5. Now choose an Instance Type, select t2.micro because it is free tier eligible, then click on Review and launch button.
  6. Now, you will land on Review Instance Launch. In this step click on launch. Here a pop-up appears for selecting key-pairs (it is a security key). You can select an existing key-pair if you have or create one if you don't have. Click on launch instance. It will create and download .pem file. Keep this file secure and place it in a directory which you can use later on while running your project.
  7. Finally your instance is launched. For viewing it, go to Services>Compute >EC2 and select "Running Instances". It will show you all the running instances.
  8. Now select your instance and under security groups column, click on launch-wizard.
  9. Now under inbound tab, select edit and further click on "Add Rule". You will find SSH rule already present there. Add three more rules. When you click on add rule button, you will find a new appended row to the existing ones with type "Custom TCP". In this row add port number 8000 (django and gunicorn uses 8000 port default) and then add two more rules. One for HTTPS and another for HTTP. Finally hit the save button.
  10. Now again select your instance from dashboard and copy Public DNS(ipV4) and IPV4 Public IP. Keep these two descriptions safe for future use.

2. Connect with your instance and upload files in it

  • Now select your instance and click on "Actions" button and then select connect. A pop will appear showing commands to access your instance. Now open terminal and go to directory where you have placed your .pem file. and run the following command. This is basically for giving required permissions.
         Desktop$ chmod 400 yourfile.pem
        
    Then, copy example command from same pop-up and run in terminal.
          Desktop$ ssh -i "rajat4665.pem" ubuntu@ec2-52-14-64-58.us-east2.compute.amazonaws.com
        
    When your terminal user name is converted to ubuntu@ip-XXX-XX-XX-XX it means you are connected to your instance.
  • Download filezilla. It is available for both windows and ubuntu. Here i downloaded it from ubuntu software appstore.
  • Now open filezilla. First, go to edit and select preferences and go to SFTP and click on add key file. Here you have to add your key pair (.pemfile ). Paste IPV4 Public IP (description of instance) in host, enter user name "ubuntu" and leave password blank, then enter port number 22 (default port of SFTP is 22). Finally click on quick connect and then press ok. Now your server is connected to your local system.
  • Open your django project in your computer. Paste static and templates folders inside app folder.
  • Now on the left side of filezilla dashboard is your local pc directories and on the right side is your

3. Configure your remote server, setup gunicorn and Nginx

  • Enter following command:
         sudo apt update
         sudo apt upgrade
    if any pop-up appear while upgrading click on default option.
         sudo apt install python3-pip python3-dev libpq-dev postgresql postgresqlcontrib nginx curl
        
  • Install, create and activate virtual environment
    1. Installing virtual environment
           sudo apt install python3-venv
         
    2. Creating virtual environment
           python3 -m venv ./myenv
        
    3. Activating virtual environment
           cd myenv
           cd bin
           source activate
      
    Now you can see your terminal user is connected with prefix of (myenv), this means your virtual environment activated successfully.
    4. Now create a txt file with name "requirements" (You can give any other name also.)
           touch requirements.txt
           sudo nano requirements.txt
         
    then paste folllowing details their and save it.
         Django==2.1.4
         gunicorn==19.9.0
         Pillow==5.3.0
         psycopg2==2.7.6.1
         psycopg2-binary==2.7.6.1
         pytz==2018.7
         requests==2.21.0
         urllib3==1.24.1
         
    Before executing this file, type this command
           pip3 freeze
        
    It will display installed packages in virtual environment. Now we need to install requirements.txt file through terminal. First go to file location and run following command:
           pip3 install -r requirements.txt
        

4. Edit setting.py file in remote server

  • Open project folder in your remote server. Here, my project folder name is "weather". Go to weather folder (inside main weather folder) and open setting.py
           sudo nano setting.py
         
    Now, comment the following lines if they are present.
         #STATIC DIR=(os.path.join(BASE DIR,'static'))
         #print(STATIC DIR)
         #STATICFILES DIRS = [
         # STATIC DIR,
         #]
        
    then paste these two line in the end of setting.py file
         STATIC URL = '/static/'
         STATIC ROOT = os.path.join(BASE DIR, "static/")
    Copy your IPV4 Public IP from amazon instance and paste it in "ALLOWED HOST" in setting.py file.
           ALLOWED HOSTS = [ ]
        
    Put your IPV4 Public IP in it.
           ALLOWED HOSTS = ['52.14.64.58'] (like this)
        
    Now press ctrl+o for saving and hit enter. For exiting, press ctrl + x
  • Now we need to Create static files(make sure that no static files are placed in main project folder . It should be in app folder). Run following command in main project folder:
           python3 manage.py collectstatic
        
    It will create a folder named "static" in your main project folder. Now you can check whether your remote server is working or not. Run this command and paste your IPV4 Public IP:8000 in web browser.
           python3 manage.py runserver 0.0.0.0:8000
         
    Open browser XX.XX.XX.XX:8000. Here 8000 is your port number. Now you can view your project running on web-browser. Use ctrl + c to exit from local server running in terminal.
  • Configuring Gunicorn: Now Test your Gunicorn server in main project folder.
           gunicorn {bind 0.0.0.0:8000 weather.wsgi
         
    I put weather.wsgi because weather is my project name. Now you can check it through your browser XX.XX.XX.XX:8000 (XX.XX.XX.XX is your IPV4 Public IP of your amazon EC2 instance ). If it working then press ctrl+c to deactivate.
    Run this command to open gunicorn.socket file
           sudo nano/etc/systemd/system/gunicorn.socket
        
    Copy this code, paste it and save.
         [Unit]
         Description=gunicorn socket
         [Socket]
         ListenStream=/run/gunicorn.sock
         [Install ]
         WantedBy=sockets.target
        
    Run this command to Open gunicorn.service file
           sudo nano /etc/systemd/system/gunicorn.service
         
    Copy this code, paste it and save.
         [Unit]
         Description= gunicorn daemon
         Requires= gunicorn.socket
         After= network.target
         [Service]
         User= ubuntu
         Group= www-data
         WorkingDirectory= /home/ubuntu/myproject/weather
         ExecStart=/home/ubuntu/myproject/myenv/bin/gunicorn   \
         {access-logfle -  \
         {workers 3   \
         {bind unix:/run/gunicorn.sock   \
         weather.wsgi:application
         [Install]
         WantedBy=multi-user.target
        
    WorkingDirectory= is your pwd of project folder ExecStart = gunicorn directory Now we Start and enable Gunicorn socket. Enter following commands :
         sudo systemctl start gunicorn.socket
         sudo systemctl enable gunicorn.socket
        
    Now we have to check status of gunicorn. Enter following commands :
           sudo systemctl status gunicorn.socket
        
    Check the existence of gunicorn.sock. Enter following commands :
           file /run/gunicorn.sock
        
  • NGINX Setup: Create nginx files using following command under main project folder(weather in this case):
           sudo nano /etc/nginx/sites-available/weather
        
    Copy this code, paste it and save
          server {
         listen 80;
         server name 52.14.64.58;
         location = /favicon.ico f access log off; log not found off; }
         location /static/ {
         root /home/ubuntu/myproject/weather;
         }
         location / {
         include proxy params;
         proxy pass http://unix:/run/gunicorn.sock;
         }
         }
         
    if there is media file in your django project go to this link https://gist.github. com/bradtraversy/cfa565b879ff1458dba08f423cb01d71 and copy above step. Now enable the file by linking to the sites-enabled dir via terminal.
         sudo ln -s /etc/nginx/sites-available/weather /etc/nginx/sites-enabled
         
    Test NGINX configuration by entering following command:
         sudo nginx -t   //test is successful
        
    Now we need to Restart NGINX and Gunicorn.
         sudo systemctl restart nginx
         sudo systemctl restart gunicorn
        
    Now you can check in browser whether your project is working on IPV4 Public IP (on this address). Now you need not to run server manually.
  • Domain Setup: Go to local settings.py on the server and change "ALLOWED HOSTS" to include the domain.
         ALLOWED HOSTS = ['IP ADDRESS', 'example.com','www.example.com']
        
    Edit nginx configuration
          /etc/nginx/sites-available/weather
         server {
         listen: 80;
         server name xxx.xxx.xxx.xxx example.com www.example.com;
         }
         
    Now go to DNS management in your domain and fill your ip details
  • Important steps
    - Now configure amazon instance with your DNS
    - Go to services and select Networking & Content Delivery section and click on Route 53
    - Click on hosted zone
    - Now click on create hosted zones enter your domain details and paste your IPV4 Public IP in value followed by .(dot) like XX.XX.XX.XX. save it
    References

Comments

Popular posts from this blog

How To Access Kubernetes Dashboard Token

Ghost: Best Blogging Platform| How To Install Ghost On Windows

Importance of jQuery in Designer's Career