How to upload your Django Website on AWS, DIgital Ocean or VPS in just 10 minutes

Upload your Django app to AWS Ubuntu with GUNICORN and NGINX

all deployment webserver
WOLFx 5 minutes
Published on: Sept. 8, 2021
Last Updated on: Sept. 12, 2022

Step by Step: How to upload Django app on any Ubuntu server

Introduction

Django is a powerful, but complex web development framework. Django makes it easy to build good sites without changing code, but getting started can be tough. After you have successfully create the app and tested it in your local development setup, it's time to go LIVE!

BUT HOW?

If you are looking for a stable solution that is going to work no matter what, when you try to deploy your Django Project on AWS/DIgital Ocean/Any other VPC, then this is a great place. 

This article will teach you everything you need to know about setting up Django Application in AWS/DIgital Ocean/Any other VPC Ubuntu. We will walk you through all the steps necessary for installing and deploying your Django application in AWS/DIgital Ocean/Any other VPC Ubuntu based server. You will learn how to deploy Django app in AWS/DIgital Ocean/Any other VPS Ubuntu or any Virtual Private Cloud (VPC) with shell access.

Follow this article if you have questions or phrases similar to these: How to upload my django app on nginx? how to make my django app live for production? How to upload Django on server? Upload Django on Ubuntu Server. Is it safe to just python manage.py runserver for production?

To solve all the above problems follow these steps.

Stage 1: Binding Gunicorn to Django app and checking if the upstream gunicorn is working fine.

Please note the deployment is incomplete without other stages

You have to login as Ubuntu user and NOT sudo su/root

1. sudo apt-get update

2. sudo apt-get upgrade

3. Optional - If it shows a popup/options then just select the pkg maintainer version.

4. python3 -m venv env

5. sudo apt-get install python3-venv

6. source env/bin/activate

7. pip3 install django

8. git clone <your-repo-url>

9. pip3 install gunicorn

10. sudo apt-get install -y nginx

11. cd to your project directory where settings.py, db.sqlite3 and all those files of your project is stored.

12. pip3 install -r requirements.txt

13. gunicorn --bind 0.0.0.0:8000 <project_name>.wsgi:application
Note: your project name is the main app name which you created in the beginning with django-admin startproject <project_name> command

14. You will see


[2021-09-08 15:20:17 +0000] [12789] [INFO] Starting gunicorn 20.1.0 
[2021-09-08 15:20:17 +0000] [12789] [INFO] Listening at: http://0.0.0.0:8000 (12789) 
[2021-09-08 15:20:17 +0000] [12789] [INFO] Using worker: sync 
[2021-09-08 15:20:17 +0000] [12791] [INFO] Booting worker with pid: 12791 

which means you have successfully bonded your gunicorn to run your Django app and your Django app is now ready to get linked with a webserver (NGINX in our case). This marks the completion of Stage 1. To test out Stage 1 success you can type in your IP with port :8000 and see your application run (make sure your AWS security policy is allowing port 8000 else you will see a 404) but the above Booting worker with pid confirms that it's working.

 


 

Stage 2: Setting up supervisor so that your Gunicorn autostarts your Django app on reboot and after first boot.

1. sudo apt-get install -y supervisor

2. cd /etc/supervisor/conf.d/

3. sudo touch gunicorn.conf

4. sudo nano gunicorn.conf This will open up an editor where you have to type in the script for gunicorn (The bind which we did in Stage 1 but now we are telling supervisor to bind gunicorn every time the server/instance start)

 
[program:gunicorn]
directory = /home/ubuntu/<path to manage.py>
command = /home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/<App Directory>/app.sock <App Name>.wsgi:application
autostart = true
autorestart = true
stderr_logfile = /var/log/gunicorn/gunicorn.err.log
stdout_logfile = /var/log/gunicorn/gunicorn.out.log

[group:guni]
programs:gunicorn

5. sudo mkdir /var/log/gunicorn, here we are creating the log folder for our gunicorn out and error logs.

6. sudo supervisorctl reread

7. If you see guni:available here it means your supervisor is all set and you have properly done everything till now.

8. sudo supervisorctl update

9. if you see guni: added process group this is another marker that you have properly done the steps.

10. sudo supervisorctl status

11. If you see guni:gunicorn RUNNING this is the third and final indicator that your gunicorn is now properly set up. After all these steps it is confirmed that your gunicorn is now bi-directionally communicating to an app.sock file which is automatically created inside your project directory.

 


 

Stage 3: Final step to link your Gunicorn upstream server with your NGINX

1. cd /etc/nginx/sites-available

2. sudo touch django.conf

3. sudo nano django.conf This will open up your nano editor where you have to type in these exact server settings.

server {
    listen 80;
    server_name <ipaddress or domain name> ;
    #server_name 192.168.0.1 yourdomain.com your_alternate_domain.com; this is how you can add multiple hosts. Do not add any comma just separate it with spaces

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/ubuntu/Appdir/app.sock;
    }
}

4. sudo nginx -t (This will test the syntax of your config file)

5. sudo ln django.conf /etc/nginx/sites-enabled/ <---- this is a very crucial step make sure there is not typing mistake here, it creates a symlink

6. sudo nginx -t

7. sudo service nginx restart

With this you now have linked your Nginx to your gunicorn upstream on app.sock so head on to your browser and type in the IP address of your instance and you will see your app live.

If you can see your website without any CSS then you have followed everything properly, follow this article on how to serve static files of your Django app in Nginx.


Conclusion

Hope you found this article helpful, please do share it on your social media, it will allow us to reach out to more people who are in need for help.

IF YOUR FIND THIS ARTICLE HELPFUL THEN PLEASE SHARE IT ON: Whatsapp Facebook Twitter
You are viewing an optimized version of this blog. See Original