Deploy Django using UWSGI + Nginx in AWS Lightsail with Ubuntu 20.14 LTS
Amazon Lightsail is an interesting product of AWS Cloud. We can use VPS service, storage, and Networking with a very low budget start from $3.5 which is a good offer if we want to deploy Company Profile, CV, Portfolio site, starter Blog, or even MVP with an early-stage user with no many traffic yet.
Django is a powerful web framework for Python Programming language with so many great features and remembers that Django is released in 2005 which is until right now Django has 15 years old with so many product build, eg: Instagram, Pinterest, Spotify, etc. We can serve Django with UWSGI and Nginx as a reverse proxy to Deploy Django in production. And we will set up DNS for connecting with the domain name.
In this article, our goal is to deploy and set up a Django application for production-ready and can access the Internet using UWSGI Nginx in AWS Lightsail.
AWS Lightsail Instance
To prepare to create something in VPS AWS Lightsail, we need to create Instance first.
- Login to AWS Lightsail Console,
- Choose Instance Location (for me is Singapore, but you can choose need to depend on your need/customer location), Instance Image (Linux), and OS (Ubuntu 20.04 LTS).
3. Choose Instance Plan and give Identify name, for me it’s important to make clear identify the name with the region, like this:
4. Add tags
5. Click Create Instance to release your instance, and VPS is almost ready
After click Create Instance, wait until AWS finish their system to create Instance, and we back to the Home console and will see disables style Instance like this:
It’s mean the VPS is not ready yet. But after a few seconds/minutes VPS will ready and looks active like this:
VPS is really ready. We can see on the top right Instance card have terminal symbol, it’s mean we can log in SSH on the web by click that, and the Web terminal will look like this:
We just create VPS. And after this, we will set up OS.
Install OS Requirements
$ sudo apt-get install Nginx
$ sudo apt-get install python3-dev
$ sudo apt-get install python3-pip
$ sudo -H pip3 install uwsgi
$ sudo -H pip3 install — upgrade pip
$ sudo -H pip3 install virtualenv virtualenvwrapper
That's all software that we need to run Django Application start from Nginx, Python-Dev, UWSGI until Virtualenv to create separated Environment with OS. You can just copy and paste into your terminal and don’t forget to run sudo apt update if you need to do that.
Create Virtual Environment
Virtual Environment can create separated Environment with OS, so we are not disturbed or change Environment in the OS.
At the above, we were Install VirtualenvWrapper in point number 6. So, we just need to set up that:
$ sudo apt update
$ echo “export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3” >> ~/.bashrc
$ echo “export WORKON_HOME=~/Env” >> ~/.bashrc
$ echo “source /usr/local/bin/virtualenvwrapper.sh” >> ~/.bashrc
$ source ~/.bashrc
$ mkvirtualenv djapp
We create an environment called djapp, you can change your app name depends on your project name or whatever you want.
Install & Set up Django
Install Django with pip
$ pip3 install Django
Create a folder called djapp for our root project directory
$ mkdir djapp && cd djapp
Start Django project and naming with the config to make file config into folder config with . (dot).
$ django-admin startproject config .
Inside folder djapp we will have 2 files/folder(config & manage.py):
Let’s run the project, with command ./manage.py runserver:
Yeah, it works.
Install & Set up UWSGI
We have installed UWSGI in our machine before, so we can start test run serve using UWSGI to our Application,
$ sudo uwsgi -H /home/ubuntu/Env/djapp — http 0.0.0.0:80 — wsgi-file config/wsgi.py
it means we run the WSGI Django file with Virtual Environment that we made before named djapp in the folder /home/ubuntu/Env/djapp. And the result will look like this:
And we can access our public IP from the browser, and the result will like this
Our Django application was running, but showing an error page because we didn’t set up allowed hosts in the settings. So, we need to fill ALLOWED_HOSTS in config/settings.py with our public IP
And access IP with browser again, and the result will…
Yeay, UWSGI running well.
After we did some running tests with UWSGI, we need to set up UWSGI configuration to make UWSGI running in daemon mode, and then NGINX can process it and the goal of all is to make our Application accessible on the internet.
Let’s start to create UWSGI for service by creating a file at
Let’s start with create a file in the inside of the folder config, name it uwsgi.ini
$ nano config/uwsgi.ini
We create inside of our project folder because I want to make all of the related configurations is centralized and maintainable. And then let’s copy this configuration code
Just copy all that code, I wouldn’t explain all of the configurations, you can read docs here.
And then, we create a symlink to folder /etc/uwsgi/apps-enabled with root access: