fashionProject-private

sudo apt update && apt upgrade -y
sudo apt install python3-pip python3-dev ufw nginx

create a user and give him sudo permission

adduser altocan
adduser altocan sudo
exit

ssh into server as a user

ssh altocan@IPADDRESS
sudo ufw allow ssh
sudo ufw enable
sudo apt update
sudo apt install python3-pip python3-dev nginx
sudo pip3 install virtualenv
mkdir ~/fashiondir
cd ~/fashiondir
git clone https://github.com/dev-rathankumar/fashion-pvt-04.git .
virtualenv env
source env/bin/activate

Install postgres

sudo apt-get install postgresql postgresql-contrib
sudo systemctl start postgresql.service
sudo systemctl enable postgresql.service
sudo systemctl status postgresql.service

Configure postgres

sudo passwd postgres
sudo su - postgres
psql -d postgres -c "ALTER USER postgres WITH PASSWORD 'Linode@2021';"
psql postgres
CREATE DATABASE fashion_db;
exit
exit

setup .env file

sudo nano .env  ### add the environment variables with postgres db name and password
sudo nano fashion_main/settings.py   ### add IP address in allowed host
pip install -r requirements.txt
deactivate
source env/bin/activate
python manage.py migrate
python manage.py createsuperuser

allow port 8000

sudo ufw enable
sudo ufw allow 8000
sudo ufw status

run the server

python manage.py runserver 0.0.0.0:8000
Press CTRL + C
sudo apt install gunicorn
gunicorn --bind 0.0.0.0:8000 fashion_main.wsgi
exit

Configuring gunicorn

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=altocan
Group=www-data
WorkingDirectory=/home/altocan/fashiondir
ExecStart=/home/altocan/fashiondir/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          fashion_main.wsgi:application

[Install]
WantedBy=multi-user.target
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

Configuring Nginx as a reverse proxy

sudo nano /etc/nginx/sites-available/fashion_main

server {
    listen 80;
    server_name altocanp1.website;

    location ~ ^/\.well-known {
        root /home/altocan/fashiondir;
        allow all;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/altocan/fashiondir;
    }
    location /media/ {
        root /home/altocan/fashiondir;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
sudo ln -s /etc/nginx/sites-available/fashion_main /etc/nginx/sites-enabled/
cd /etc/nginx/sites-enabled/
sudo rm default

increase file upload size to avoid 413 errors

go to http block

sudo nano /etc/nginx/nginx.conf
# add this line
client_max_body_size 100M;
sudo ufw allow 80
sudo ufw allow 'Nginx Full'
sudo ufw allow 586
sudo ufw deny 8000
sudo nano fashiondir/fashion_main/settings.py
# Add the domain name into allowed host
sudo systemctl restart nginx
sudo systemctl restart gunicorn