Skip to content

Instantly share code, notes, and snippets.

@ArcRanges
Last active January 18, 2024 09:36
Show Gist options
  • Save ArcRanges/991aba387a29501c3f0176f9e1e26168 to your computer and use it in GitHub Desktop.
Save ArcRanges/991aba387a29501c3f0176f9e1e26168 to your computer and use it in GitHub Desktop.
Up and Running on AWS EC2 using Django with files from Git in less than 5 minutes

How to Setup Django in AWS EC2 Instance in less than 5 minutes

I will be using Windows 10, Git Bash, Windows Powershell

Requirements:

- Django Project uploaded on Git
- AWS EC2 Account

Initialization: EC2 Setup:

  1. Log in to AWS
  2. Go to AWS Console
  3. On Instances, Launch Instance
  4. Choose (Free Tier) Ubuntu Server 18.04 LTS (HVM)
  5. Choose Free Tier Instance Type
  6. Click on Step 6 - Configure Security Group
  7. Add Rule HTTP
  8. Review and Launch , Ignore warning, Launch
  9. Choose a key pair or create a new one your choice
  • EC2 Setup - Done *

AWS: Ubuntu Server Setup

Using Windows Powershell
  1. update permissions on keypair file
chmod 400 keypair.pem
  1. ssh to the server: example AWS EC2 Public DNS = ec2-54-219-133-135.us-west-1.compute.amazonaws.com
ssh -i "keypair.pem" ubuntu@<AWS EC2 Public DNS>
  1. Choose Yes, Enter
  2. Run some commands
sudo apt-get update
sudo apt-get install python-pip  python-dev nginx git
* yes *
sudo apt-get update
sudo pip install virtualenv
git clone https://github.com/YourDjangoProjectRepo/YourDjangoProjectRepo.git
cd YourDjangoProjectRepo
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
pip install django bcrypt django-extensions
pip install gunicorn
cd YourDjangoProjectRepo *(yes, it's the same name as the parent folder)*
sudo vim settings.py

Edit File: Settings.py

Inside settings.py modify these lines allowed host public IP address, I for INSERT
ALLOWED_HOSTS = ['<AWS EC2 Public DNS>']

Add this line to the bottom of the file

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

Save Changes by pressing Escape, type :wq

Initialize Static Folder

cd ..
python manage.py collectstatic

Test gunicorn for errors

Must be inside Django Root Folder
gunicorn --bind 0.0.0.0:8000 JCmain.wsgi:application

Quit Process CTRL + C (if no errors)

Edit Gunicorn Config

sudo vim /etc/systemd/system/gunicorn.service

Copy below code

[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/YourDjangoProjectName
ExecStart=/home/ubuntu/YourDjangoProjectName/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/YourDjangoProjectName/YourDjangoProjectName.sock YourDjangoProjectName.wsgi:application
[Install]
WantedBy=multi-user.target

Now on Terminal

i
Shift + Insert (or type code manually)
ESC
:wq

More Setup

sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo vim /etc/nginx/sites-available/YourDjangoProjectName

Copy Code

server {
  listen 80;
  server_name <AWS EC2 Public DNS>;
  location = /favicon.ico { access_log off; log_not_found off; }
  location /static/ {
      root /home/ubuntu/YourDjangoProjectName;
  }
  location / {
      include proxy_params;
      proxy_pass http://unix:/home/ubuntu/YourDjangoProjectName/YourDjangoProjectName.sock;
  }
}

On terminal

i
Shift + Insert (or write manually)
ESC
:wq

Some more setup commands

sudo ln -s /etc/nginx/sites-available/YourDjangoProjectName /etc/nginx/sites-enabled
sudo nginx -t
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart

Congratulations, Visit Your Website at Http://(AWS EC2 Public DNS)

Common Error: Error 502, Bad Gateway,
Most likely Solution: Check gunicorn.service file for typographical errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment