Custom Django URL Shortener App

Instructions on how to build project within one hour can be viewed at: https://www.youtube.com/watch?v=ctuSR6UHcuQ
Instruction on how to deploy: https://youtu.be/hYMTvK5MpQI

Building Your First Custom App in 1 Hour

This project is a great beginner friendly project. In this tutorial we walk through how to build a URL shortening service. You can check out the youtube tutorial above on a code walk through. We use django as the backend, html with bootstrap as the front end and ORM with SQLite3 for data management. All files relevant to build the application are included in this respository

Deploy to a Cloud Server

Set Up Server

You can use something like Linode or Digital Ocean to follow these instruction.

Step 1: Create a linode or droplet (I use the $5 tier for this project)

Step 2: Use terminal or any ssh client to login with root and run:
ssh root@IP
apt update && apt upgrade -y

Step 3: Set hostname for server. I used test-server. You can use whatever you want.
hostnamectl set-hostname test-server

Step 4: Connect host ip and hostname
run nano /etc/hosts and add your server ip, click tab and then your hostname (from step 3)

Step 5: Install some dependencies
run sudo apt install python-pip virtualenv ufw

Step 6: Create a limited user and give sudo privlidges

run adduser USERNAME <---pick anything here. Enter password and skip through the rest of the questions.
then run adduser USERNAME sudo
logout as root by typing exit and log in with your username: ssh username@IP

Step 7: Set up some firewall rules and enable
sudo ufw default allow outgoing
sudo ufw default deny incoming
sudo ufw allow ssh
sudo ufw allow 8000
sudo ufw enable
sudo ufw status (check to ensure its up and running)

Step 8: Setup ssh keys on your local computer
run ssh-keygen -b 4096
leave defaults
run ssh-copy-id username@IP to push them to your server
optional: If you have multiple ssh key pairs then run ssh-add ~/.ssh/{name of ssh key}

Step 9:Remove root login and password auth
run sudo nano /etc/ssh/sshd_config
Set permit root login to no and uncomment passwordauthentication and set it to no

Step 10: Reboot the server
run sudo reboot

Deploy Django Project to Server

Step 11: Change to your virtualenv
run virtualenv vevn -p python3
run source venv/bin/activate
You should now see something like (venv) on your terminal line

Step 12: Transfer Django project from local computer to server
run scp -r {local path to project folder} username@IP:~/

Step 13: Update settings.py. Navigate to settings.py in your project folder
Set Debug=False
Update Allowed_Hosts =['Add Your IP or Domain Name']
Add static root with the following command STATIC_ROOT = os.path.join(BASE_DIR, ‘static') to the settings file
Save and exit

Step 14: Back out to the directory that has manage.py
Run python manage.py collectstatic

Step 15: Install Gunicorn and Nginx
run pip install gunicorn
run sudo apt install nginx libpq-dev

Step 16: Check to see if gunicorn can host your django project. Change URLShortnerProject to whatever your project is called
run gunicorn --bind 0.0.0.0:8000 URLShortnerProject.wsgi

Step 17: Deactivate venv and Create gunicorn systemd file
run deactivate. The (venv) on terminal line should be gone
run sudo nano /etc/systemd/system/gunicorn.service
Paste the following and be sure to update your project name, path and username accordingly:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=username
Group=www-data
WorkingDirectory=/home/username/URLShortnerProject
ExecStart=/home/username/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/username/URLShortnerProject/URLShortnerProject.sock URLShortnerProject.wsgi:application

[Install]
WantedBy=multi-user.target

Step 18: Run the following commands to enable gunicorn:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl status gunicorn
sudo systemctl daemon-reload
sudo systemctl restart gunicorn

Step 19: Set up NGINX with GUNICORN
run sudo nano /etc/nginx/sites-available/URLShortnerProject
Paste the following and be sure update your own IP, username, path and project name
This covers http, https will be covered in a later tutorial

    server {
        listen 80;
        server_name IP;
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/username/URLShortnerProject;
        }
        location / {
            include proxy_params;
            proxy_pass http://unix:/home/username/URLShortnerProject/URLShortnerProject.sock;
        }
    }

Step 20: Link and test nginx config


Link: sudo ln -s /etc/nginx/sites-available/URLShortnerProject /etc/nginx/sites-enabled
Test: sudo nginx -t

Step 21: Change UFW Rules
sudo ufw delete allow 8000
sudo ufw allow 'Nginx Full'

Step 22: Reload Nginx and Gunicorn
sudo systemctl restart gunicorn
sudo systemctl restart nginx

Now visit your ip and check out your django website on a cloud server