Skip to content

Instantly share code, notes, and snippets.

@ganmedia
Last active January 24, 2023 07:16
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save ganmedia/afbada8ff6ff1733e27f7fd818eb34fd to your computer and use it in GitHub Desktop.
Save ganmedia/afbada8ff6ff1733e27f7fd818eb34fd to your computer and use it in GitHub Desktop.
sudo apt-get update
sudo apt-get install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx
sudo -u postgres psql
- paste this code in postgres console:
CREATE DATABASE django_project;
CREATE USER username WITH PASSWORD 'pass1234';
ALTER ROLE username SET client_encoding TO 'utf8';
ALTER ROLE username SET default_transaction_isolation TO 'read committed';
ALTER ROLE username SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE django_project TO username;
\q
sudo pip3 install virtualenv
mkdir ~/django_project
cd ~/django_project
virtualenv env
source env/bin/activate
pip install django gunicorn psycopg2
django-admin.py startproject django_project .
nano django_project/settings.py
- add your Amazon Lightsail IP to the ALLOWED_HOSTS variable, example:
ALLOWED_HOSTS = ['YOUR_AMAZON_LIGHTSAIL_IP']
- add database configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_project',
'USER': 'username',
'PASSWORD': 'pass1234',
'HOST': 'localhost',
'PORT': '',
}
}
- add static root to the end of the settings:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
- save the settings.py file
cd ~/django_project
./manage.py makemigrations
./manage.py migrate
./manage.py createsuperuser
./manage.py collectstatic
sudo ufw allow 8000
./manage.py runserver 0.0.0.0:8000
- ctrl + c
cd ~/django_project
gunicorn --bind 0.0.0.0:8000 django_project.wsgi:application
- ctrl + c
deactivate
sudo nano /etc/systemd/system/gunicorn.service
- paste inside gunicorn.service:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/django_project
ExecStart=/home/ubuntu/django_project/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/django_project/django_project.sock django_project.wsgi:application
[Install]
WantedBy=multi-user.target
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo nano /etc/nginx/sites-available/django_project
- copy inside:
server {
listen 80;
server_name YOUR_AMAZON_LIGHTSAIL_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/django_project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/django_project/django_project.sock;
}
}
- save the file
sudo ln -s /etc/nginx/sites-available/django_project /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
sudo ufw delete allow 8000
sudo ufw allow 'Nginx Full'
@waqas80
Copy link

waqas80 commented Oct 4, 2017

I follow the setups and I am getting the following error
502 Bad Gateway
nginx/1.10.3 (Ubuntu)

@SidduH
Copy link

SidduH commented Dec 16, 2017

@waqas80 When I explored logs(/var/log/nginx/error.log), got to know that, no django-project.sock file was found ,
installed uwsgi to fix it

@Fiftyseventheory
Copy link

I can't run sudo systemctl restart nginx, the lightsail instance seems to take over port 80 and it immediately restarts the process if I kill it.

@Sanji515
Copy link

I follow the setups and I am getting the following error
502 Bad Gateway
nginx/1.10.3 (Ubuntu)

@waqas80 when writing gunicorn.service under [Service] you need to properly bind the gunicorn executable file to Unix socket within the project

@LD8
Copy link

LD8 commented May 1, 2020

I think there's a django_project.sock file missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment