-
set up an ec2 instance
-
edit inbound rule – allow all TCP & HTTP to 0.0.0.0/0
-
login to your instance using the ssh key (.pem key)
ssh -i yourkey.pem ubuntu@instance_public_ip
-
Install python, pip, postgres & nginx to the instance
sudo apt update sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl
-
make a folder and clone your repository to that folder
mkdir project cd project git clone githubrepolink
-
update the python package manage pip and install the virtualenv
sudo -H pip3 install --upgrade pip sudo -H pip3 install virtualenv
-
make a virtualenv inside the project folder and activate it
virtualenv myprojectenv source myprojectenv/bin/activate
-
move back inside the project folder > project and install all dependencies of requirements.txt file
pip install -r requirements.txt
-
make sure to allow the allowed host to specific ip or all of your django project’s settings.py
ALLOWED_HOSTS = ['*']
-
now you can try running the application to 0.0.0.0:8000
python manage.py runserver 0.0.0.0:8000
-
you’ll be able to access the application in your instance public ip :8000
http://instancepublicip:8000
-
now although the application is running with the attached terminal if some issues evoked the terminal will be interupted and crashes the application so to automate the terminal from keep on restarting after the interuption, gunicorn is needed. To install the guicorn use
pip install gunicorn
-
to check if the gunicorn is working
gunicorn --bind 0.0.0.0:8000 myproject.wsgi
-
now to make a persistent and robust way of starting and stopping the application is by making socket files and systemd service
sudo vi /etc/systemd/system/gunicorn.socket
[Unit] Description=gunicorn socket [Socket] ListenStream=/run/gunicorn.sock [Install] WantedBy=sockets.target
-
now make the gunicorn.service file and paste your project dirs accordingly here
sudo nano /etc/systemd/system/gunicorn.service
[Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] User=ubuntu Group=www-data WorkingDirectory=/home/ubuntu/project/myprojectdir ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \ --access-logfile - \ --workers 3 \ --bind unix:/run/gunicorn.sock \ myproject.wsgi:application [Install] WantedBy=multi-user.target
-
start, enable and check the status of the gunicorn.socket file respectively
sudo systemctl start gunicorn.socket sudo systemctl enable gunicorn.socket sudo systemctl status gunicorn.socket
-
check if the sock file is working using curl
curl --unix-socket /run/gunicorn.sock localhost
-
if some problem arises, journalctl can be used to accessed the logs
sudo journalctl -u gunicorn
-
restart the gunicorn service
sudo systemctl daemon-reload sudo systemctl restart gunicorn
-
configure nginx proxypass to gunicorn; create a server block to the nginx sites-available dir
sudo nano /etc/nginx/sites-available/myproject
server { listen 80; server_name server_domain_or_IP; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/ubuntu/project/myprojectdir/staticfiles; } location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; } }
-
create a link from sites-available to sites-enabled
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
-
check if the syntax is okay
sudo nginx -t
-
restart the nginx service
sudo systemctl restart nginx
-
openup the firewall to open traffic and remove the development server port 8000
sudo ufw delete allow 8000 sudo ufw allow 'Nginx Full'
-
incase of 502 Bad Gateway instead of the django application; check the nginx logs
sudo tail -F /var/log/nginx/error.log

Contents
Share this article
- January 13, 2024
- Productivity Hacks