Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryanpadilha/10aae70fec754bd79a2cefdab0713670 to your computer and use it in GitHub Desktop.
Save ryanpadilha/10aae70fec754bd79a2cefdab0713670 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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment