Last active
January 24, 2023 07:16
-
-
Save ganmedia/afbada8ff6ff1733e27f7fd818eb34fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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
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.
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
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
I follow the setups and I am getting the following error
502 Bad Gateway
nginx/1.10.3 (Ubuntu)