This is not a complete guide to deploying Django with nginx & uwsgi. There are plenty parts that you either have to skip or include. It's up to you, depending on your project. Don't follow this blindly. Use your brain, stackoverflow and google the shit out of it!
You can choose to create SSH keys to login if you want. If not, you will get the password sent to your email to login via SSH
To generate a key on your local machine
$ ssh-keygen
Hit enter all the way through and it will create a public and private key at
~/.ssh/id_rsa
~/.ssh/id_rsa.pub
You want to copy the public key (.pub file)
$ cat ~/.ssh/id_rsa.pub
Copy the entire output and add as an SSH key for your Host provider
If you setup SSH keys correctly the command below will let you right in. If you did not use SSH keys, it will ask for a password. This is the one that was mailed to you
$ ssh root@YOUR_SERVER_IP
It will ask for a password, use something secure. You can just hit enter through all the fields. I used the user "djangoadmin" but you can use anything
adduser djangoadmin
usermod -aG sudo djangoadmin
Now we need to setup SSH keys for the new user. You will need to get them from your local machine
You need to copy the key from your local machine so either exit or open a new terminal
exit
You can generate a different key if you want but we will use the same one so lets output it, select it and copy it
$ cat ~/.ssh/id_rsa.pub
$ ssh root@YOUR_SERVER_IP
Navigate to the new users home folder and create a file at '.ssh/authorized_keys' and paste in the key
cd /home/djangoadmin
mkdir .ssh
cd .ssh
nano authorized_keys
Paste the key and hit "ctrl-x", hit "y" to save and "enter" to exit
You should now get let in as the new user
$ ssh djangoadmin@YOUR_SERVER_IP
sudo nano /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
sudo systemctl reload sshd
See which apps are registered with the firewall
sudo ufw app list
Allow OpenSSH
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status
We are now done with access and security and will move on to installing software
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 [database_name];
CREATE USER [database_user] WITH PASSWORD '[postgres_password]!';
ALTER ROLE [database_user] SET client_encoding TO 'utf8';
ALTER ROLE [database_user] SET default_transaction_isolation TO 'read committed';
ALTER ROLE [database_user] SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE [postgres_password] TO [database_user];
\q
You need to install the python3-venv package
sudo apt install python3-venv
mkdir [prodject_name]
cd [prodject_name]
git clone https://github.com/yourgithubname/yourgithubproject.git django
python3 -m venv ./venv
source venv/bin/activate
You could manually install each one as well
pip install -r requirements.txt
psql -d [database_name]_db -f [database_name].psql
python manage.py migrate
python manage.py createsuperuser
python manage.py collectstatic
Install uwsgi
pip install uwsgi
uwsgi --http :8000 --module project/core.wsgi
Install nginx
sudo apt-get install nginx
sudo /etc/init.d/nginx start # start nginx
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
Add A record IP
server {
if ( $host !~* ^(milkshakesanddreams.com)$ ) {return 444;}
server_name milkshakesanddreams.com;
access_log /home/milkshakes/milkshakes/logs/milkshakesanddreams.com/nginx_access.log;
error_log /home/milkshakes/milkshakes/logs/milkshakesanddreams.com/nginx_error.log;
root html;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
client_max_body_size 20m;
location / {
uwsgi_pass unix://home/milkshakes/milkshakes/deploy/sockets/milkshakesanddreams.com.sock;
include uwsgi_params;
#limit_req zone=default burst=10 nodelay;
}
location /static/ {
alias /home/milkshakes/milkshakes/public/milkshakesanddreams.com/static/;
log_not_found off;
expires 365d;
}
location /media/ {
alias /home/milkshakes/milkshakes/public/milkshakesanddreams.com/media/;
log_not_found off;
expires 365d;
}
location = /robots.txt {
alias /home/milkshakes/milkshakes/public/milkshakesanddreams.com/robots.txt;
access_log off;
log_not_found off;
}
location = /favicon.ico {
alias /home/milkshakes/milkshakes/public/milkshakesanddreams.com/static/favicon.ico;
access_log off;
log_not_found off;
expires 365d;
}
location ~ /\. {
deny all;
}
listen 80;
}
[uwsgi]
root_folder = milkshakes
project_folder = project
root_app = core
username = milkshakes
domain = milkshakesanddreams.com
base = /home/%(username)
plugins = python3
chdir = %(base)/%(root_folder)
home = %(base)/%(root_folder)/virtualenv
pythonpath = %(base)/%(root_folder)/%(project_folder)
module = %(root_app).wsgi:application
master = true
processes = 3
socket = %(base)/%(root_folder)/deploy/sockets/%(domain).sock
chown-socket = www-data:www-data
chmod-socket = 770
vacuum = true
uid = %(username)
gid = %(username)
logto = %(base)/%(root_folder)/logs/%(domain)/uwsgi.log
logfile-chown = true
sudo ln -s /mysite/deploy/test.gr/nginx.conf /etc/nginx/conf.d/test.gr.conf
Create nginx_access.log
cd /home/milkshakes/milkshakes/logs/milkshakesanddreams.com/
sudo touch nginx_access.log
Give permissions nginx_access.log
chown -R www-data:root /home/milkshakes/milkshakes/logs/milkshakesanddreams.com/nginx_access.log;
Create nginx_error.log
cd /home/milkshakes/milkshakes/logs/milkshakesanddreams.com/
sudo touch nginx_error.log
Give permissions nginx_error.log
chown -R www-data:root /home/milkshakes/milkshakes/logs/milkshakesanddreams.com/nginx_error.log;
Create uwsgi.log
cd /home/milkshakes/milkshakes/logs/milkshakesanddreams.com/
su milkshakes
touch uwsgi.log
sudo ln -s /mysite/deploy/test.gr/uwsgi.ini /etc/uwsgi/vassals/test.gr.ini
su milkshakes
cd /home/milkshakes/milkshakes/
touch deploy/milkshakesanddreams.com/uwsgi.ini
https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html