These steps should work on any previous or future version of ubuntu as well
# sudo apt update
# sudo apt upgrade
# sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl
# sudo -u postgres psql
You should now be logged into the pg shell
CREATE DATABASE <db-name>;
CREATE USER <db-admin-name> WITH PASSWORD <db-admin-password>;
ALTER ROLE <db-admin-name> SET client_encoding TO 'utf8';
ALTER ROLE <db-admin-name> SET default_transaction_isolation TO 'read committed';
ALTER ROLE <db-admin-name> SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE <db-name> TO <db-admin-name>;
\q
You need to install the python3-venv package
# sudo apt install python3-venv
# mkdir pyapps
# cd pyapps
# python3 -m venv ./venv
# source venv/bin/activate
From your local machine, create a requirements.txt with your app dependencies. Make sure you push this to your repo
$ pip freeze > requirements.txt
Create a new repo and push to it (you guys know how to do that)
# git clone https://github.com/<github-username>/<project-name>.git
You could manually install each one as well
# pip install -r requirements.txt
Add code to your settings.py file and push to server
try:
from .local_settings import *
except ImportError:
pass
Create a file called local_settings.py on your server along side of settings.py and add the following
- SECRET_KEY
- ALLOWED_HOSTS
- DATABASES
- DEBUG
- EMAIL_*
# python manage.py makemigrations
# python manage.py migrate
# python manage.py createsuperuser
python manage.py collectstatic
# python manage.py runserver 0.0.0.0:8000
Add some data in the admin area
Install gunicorn
# pip install gunicorn
Add to requirements.txt
# pip freeze > requirements.txt
# gunicorn --bind 0.0.0.0:8000 <app-name>.wsgi
Your images, etc will be gone
ctrl-c
# deactivate
# sudo nano /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
# sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=<ubuntu-username>
Group=www-data
WorkingDirectory=/home/<ubuntu-username>/pyapps/<project-name>
ExecStart=/home/<ubuntu-username>/pyapps/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
<app-name>.wsgi:application
[Install]
WantedBy=multi-user.target
# sudo systemctl start gunicorn.socket
# sudo systemctl enable gunicorn.socket
# sudo systemctl status gunicorn.socket
# file /run/gunicorn.sock
# sudo nano /etc/nginx/sites-available/<project-name>
server {
listen 80;
server_name <server-ip-address>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/<ubuntu-username>/pyapps/<project-name>;
}
location /media/ {
root /home/<ubuntu-username>/pyapps/<project-name>;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
# sudo ln -s /etc/nginx/sites-available/<project-name> /etc/nginx/sites-enabled
# sudo nginx -t
# sudo systemctl restart nginx
You will probably need to up the max upload size to be able to create listings with images (optional)
Open up the nginx conf file
# sudo nano /etc/nginx/nginx.conf
client_max_body_size 20M;
# sudo systemctl restart nginx
You may have some issues with images not showing up. I would suggest, deleting all data and starting fresh as well as removeing the "photos" folder in the "media folder"
# sudo rm -rf media/photos
Go to your domain registrar and create the following a record
@ A Record YOUR_IP_ADDRESS
www CNAME example.com
ALLOWED_HOSTS = ['<server-ip-address>', 'example.com', 'www.example.com']
server {
listen: 80;
server_name <server-ip-address> example.com www.example.com;
}
# sudo systemctl restart nginx
# sudo systemctl restart gunicorn