Django comes with a development server that helps run Django projects in localhost. However, to make your web application available worldwide you will need a host machine. Amazon Web Services (AWS) provides EC2 (Elastic Compute Cloud) which is the most popular choice to host Django web applications. This tutorial offers a step-by-step guide to deploy django app on aws. The method described here will work on other cloud platforms as well.
Step 1: Creating an AWS account and an EC2 instance
Step 2: Move your django project to the cloud instance
Step 3: Launch app on local development server port 8000 or you custom doamin and port 80
Visit AWS Console to create you account and log in. Now, follow simple steps to create an EC2 instance with Ubuntu 22.04 or other preferred OS.
This tutorial uses Ubuntu 22.04 LTS. However, you may choose other instances or higher versions as well.
As requuired create a new key pair or select an existing key pair for your instance. This will be useful for login to your instance later on.
Now, wait for initialization to complete and make a note of your public ipv4 address. You will need it for login and you should also mention the same under ALLOWED_HOSTS list of your django project settings.py file.
You may add rules like HTTP, HTTPS etc. or simply set it to allow All Traffic
Now that you are done from the AWS side. The next step, is to simply have django and other requirements installed on your ec2 instance. After which you need to move your project code to the Unbuntu EC2 instance that we have setup in the previous step.
You may migrate your code from Git repository or simply use scp to secure copy all the project file to your aws ec2 isntance.
Run the series of below commands after logging in to your Ubuntu instance via putty, ssh or other similar tools.
sudo apt update sudo apt upgrade sudo apt install python3-pip pip install --upgrade pip
With pip you may install django and other packages needed for your project like django-crispy-forms, djangorestframework, etc.
Use of a virtualenv is also recommended
pip install virtualenv virtualenv myenv source myenv/bin/activate
If you have a requirements.txt file with all the project dependencies you may install it all via:
pip install -r requirements.txt
Run this command from your local command-line or terminal window:
scp -i *.pem django_project_code.zip ubuntu@public_ip:~/
Here, ubuntu is the user name. It may sometimes be ec2-user or root. If you have a password instead of the pem file you will be prompted to enter the same. -r option can used to recursively add all project file if you want to avpid the zip file. If you have used zip the used the below command to unzip:
sudo apt install unzip unzip django_project_code.zip
Running the app on port 8000 via django development server is easy but for a professional web application we would always recommend a web server like apache or nginx.
Visiting the manage.py file on your server and simply run the command:
python3 manage.py runserver 0.0.0.0:8000
sudo apt install screen screen [enter] python3 manage.py runserver 0.0.0.0:8000
Port 80 is the default port for http requests. To run the project on just the ipv4_address or a domain name we need to use a web-application server like apache2.
No, further changes is required to your project code but you will need to install apache2 and mod_wsgi.
sudo apt install apache2 sudo apt install libapache2-mod-wsgi-py3 sudo ufw allow 'Apache Full' sudo systemctl restart apache2
sudo chown db.sqlite3 sudo chown :www-data /path_to_database_parent_directoryAt last, run:
sudo service apache2 restartand visit the public_ip or domain name.