AWS django

AWS – Deploying a Django Web Server on EC2

I forget the steps every time I deploy, so here’s a proper walkthrough—for my future self.

Environment used:

  • Frontend: HTML / CSS / JavaScript
  • Backend: Django (Python)
  • AWS OS: Ubuntu Server 20.04 LTS (HVM)

We’ll be working with AWS EC2.

Push your site’s source code to GitHub, then clone it on the EC2 instance.

Project repository

In my case the project lives in a shared repository for a team side project, so I’ll reuse that.

1. Sign In to AWS and Launch an Instance

I’ll skip account creation. Once you sign in, you’ll see the dashboard:

AWS dashboard

Click Launch Instance to create a virtual machine.

Choose AMI

Select the Amazon Machine Image you need—here that’s Ubuntu Server 20.04 LTS.

Choose instance type

Pick an instance type suited to your workload (and budget). I’m using a free-tier eligible type.

Instance details

Review instance

On the review screen click Edit security groups. (You can change this later too.)

Security group

Django listens on port 8000 by default, so open port 8000; also open HTTP port 80.

If you’re running Spring or another app on port 8080, open that instead.

Don’t open every port—it’s a serious security risk. Expose only what you need.

Security group rules

Confirm the rules. AWS shows both IPv4 and IPv6 entries—that’s normal.

Finish the wizard to launch the instance. Remember to create or select an SSH key pair and keep it secure.

Connect via the Console

Instance list

Once the instance is running, click its instance ID, then choose Connect in the upper-right.

Connect to instance

Follow the prompts and an in-browser terminal will appear. Work as you would in any Linux shell.

sudo apt-get update

Update packages, then clone your repository:

git clone <your-repo-url>

Install pip:

sudo apt-get install python3-pip -y

From here replicate your local setup. If you have a requirements file:

pip install -r requirements.txt

Apply migrations and start the server:

python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py runserver 0.0.0.0:8000

This exposes Django on all interfaces.

Closing the EC2 console stops the server, so use nohup to keep it running in the background:

nohup python3 manage.py runserver 0.0.0.0:8000 &

To stop a background server, find the process:

ps -ef | grep "python3 manage.py runserver"

Then kill the relevant PIDs:

kill 21738
kill 21740

Want to avoid specifying port 8000 in the browser? Redirect port 80 to 8000:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 \
    -j REDIRECT --to-port 8000

TMI

My app uses NLP and requires MeCab. If you need it too, this guide helped me:

[Starting from scratch on EC2] Installing konlpy and mecab (Ubuntu)

Also, if Twitch’s StreamListener throws errors, pin Tweepy to version 3.10.0.