├── README.md
└── URLShortnerProject
├── URLShortnerApp
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── admin.cpython-37.pyc
│ ├── forms.cpython-37.pyc
│ ├── models.cpython-37.pyc
│ ├── serializers.cpython-37.pyc
│ ├── urls.cpython-37.pyc
│ └── views.cpython-37.pyc
├── admin.py
├── apps.py
├── forms.py
├── models.py
├── serializers.py
├── static
│ └── images.text
├── templates
│ └── myform
│ │ └── form.html
├── tests.py
├── urls.py
└── views.py
├── URLShortnerProject
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
├── manage.py
└── requirements.txt
/README.md:
--------------------------------------------------------------------------------
1 | # Custom Django URL Shortener App
2 | Instructions on how to build project within one hour can be viewed at: https://www.youtube.com/watch?v=ctuSR6UHcuQ
3 |
Instruction on how to deploy: https://youtu.be/hYMTvK5MpQI
4 |
5 | ## Building Your First Custom App in 1 Hour
6 |
7 | 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
8 |
9 | ## Deploy to a Cloud Server
10 |
11 | ### Set Up Server
12 |
13 | You can use something like Linode or Digital Ocean to follow these instruction.
14 |
15 | Step 1: Create a linode or droplet (I use the $5 tier for this project)
16 |
17 | Step 2: Use terminal or any ssh client to login with root and run:
18 | ```ssh root@IP```
```apt update && apt upgrade -y```
19 |
20 | Step 3: Set hostname for server. I used test-server. You can use whatever you want.
21 |
```hostnamectl set-hostname test-server```
22 |
23 | Step 4: Connect host ip and hostname
24 |
run ```nano /etc/hosts``` and add your server ip, click tab and then your hostname (from step 3)
25 |
26 | Step 5: Install some dependencies
27 |
run ```sudo apt install python-pip virtualenv ufw```
28 |
29 | Step 6: Create a limited user and give sudo privlidges
30 |
31 | run ```adduser USERNAME``` <---pick anything here. Enter password and skip through the rest of the questions.
32 | then run ```adduser USERNAME sudo```
33 | logout as root by typing ```exit``` and log in with your username: ```ssh username@IP```
34 |
35 | Step 7: Set up some firewall rules and enable
36 |
37 | ```sudo ufw default allow outgoing```
38 | ```sudo ufw default deny incoming```
39 | ```sudo ufw allow ssh```
40 | ```sudo ufw allow 8000```
41 | ```sudo ufw enable```
42 | ```sudo ufw status``` (check to ensure its up and running)
43 |
44 | Step 8: Setup ssh keys on your local computer
45 | run ```ssh-keygen -b 4096```
leave defaults
46 | run ```ssh-copy-id username@IP``` to push them to your server
47 | optional: If you have multiple ssh key pairs then run ```ssh-add ~/.ssh/{name of ssh key}```
48 |
49 | Step 9:Remove root login and password auth
50 | run ```sudo nano /etc/ssh/sshd_config```
51 | Set permit root login to no and uncomment passwordauthentication and set it to no
52 |
53 | Step 10: Reboot the server
54 | run ```sudo reboot```
55 |
56 | ### Deploy Django Project to Server
57 |
58 | Step 11: Change to your virtualenv
59 | run ```virtualenv vevn -p python3```
60 | run ```source venv/bin/activate```
61 | You should now see something like (venv) on your terminal line
62 |
63 | Step 12: Transfer Django project from local computer to server
64 | run ```scp -r {local path to project folder} username@IP:~/```
65 |
66 | Step 13: Update settings.py. Navigate to settings.py in your project folder
67 | Set ```Debug=False```
68 | Update ```Allowed_Hosts =['Add Your IP or Domain Name']```
69 | Add static root with the following command ```STATIC_ROOT = os.path.join(BASE_DIR, ‘static')``` to the settings file
70 | Save and exit
71 |
72 | Step 14: Back out to the directory that has manage.py
73 | Run ```python manage.py collectstatic```
74 |
75 | Step 15: Install Gunicorn and Nginx
76 | run ```pip install gunicorn```
77 | run ```sudo apt install nginx libpq-dev```
78 |
79 | Step 16: Check to see if gunicorn can host your django project. Change URLShortnerProject to whatever your project is called
80 | run ```gunicorn --bind 0.0.0.0:8000 URLShortnerProject.wsgi```
81 |
82 | Step 17: Deactivate venv and Create gunicorn systemd file
83 | run ```deactivate```. The (venv) on terminal line should be gone
84 | run ```sudo nano /etc/systemd/system/gunicorn.service```
85 | Paste the following and be sure to update your project name, path and username accordingly:
86 | ```
87 | [Unit]
88 | Description=gunicorn daemon
89 | After=network.target
90 |
91 | [Service]
92 | User=username
93 | Group=www-data
94 | WorkingDirectory=/home/username/URLShortnerProject
95 | ExecStart=/home/username/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/username/URLShortnerProject/URLShortnerProject.sock URLShortnerProject.wsgi:application
96 |
97 | [Install]
98 | WantedBy=multi-user.target
99 | ```
100 |
101 | Step 18: Run the following commands to enable gunicorn:
102 |
103 | ```sudo systemctl start gunicorn```
104 | ```sudo systemctl enable gunicorn```
105 | ```sudo systemctl status gunicorn```
106 | ```sudo systemctl daemon-reload```
107 | ```sudo systemctl restart gunicorn```
108 |
109 |
110 | Step 19: Set up NGINX with GUNICORN
111 | run ```sudo nano /etc/nginx/sites-available/URLShortnerProject```
112 | Paste the following and be sure update your own IP, username, path and project name
113 | This covers http, https will be covered in a later tutorial
114 |
115 | ```
116 | server {
117 | listen 80;
118 | server_name IP;
119 | location = /favicon.ico { access_log off; log_not_found off; }
120 | location /static/ {
121 | root /home/username/URLShortnerProject;
122 | }
123 | location / {
124 | include proxy_params;
125 | proxy_pass http://unix:/home/username/URLShortnerProject/URLShortnerProject.sock;
126 | }
127 | }
128 | ```
129 |
130 | Step 20: Link and test nginx config
131 |
132 |
Link: ```sudo ln -s /etc/nginx/sites-available/URLShortnerProject /etc/nginx/sites-enabled```
133 |
Test: ```sudo nginx -t```
134 |
135 | Step 21: Change UFW Rules
136 | ```sudo ufw delete allow 8000```
137 | ```sudo ufw allow 'Nginx Full' ```
138 |
139 | Step 22: Reload Nginx and Gunicorn
140 | ```sudo systemctl restart gunicorn```
141 | ```sudo systemctl restart nginx```
142 |
143 | Now visit your ip and check out your django website on a cloud server
144 |
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/__init__.py:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/__pycache__/__init__.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/satssehgal/URLShortnerDjango-/b57a62606ebb29ed071f4fd4542f26e4c8004a9e/URLShortnerProject/URLShortnerApp/__pycache__/__init__.cpython-37.pyc
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/__pycache__/admin.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/satssehgal/URLShortnerDjango-/b57a62606ebb29ed071f4fd4542f26e4c8004a9e/URLShortnerProject/URLShortnerApp/__pycache__/admin.cpython-37.pyc
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/__pycache__/forms.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/satssehgal/URLShortnerDjango-/b57a62606ebb29ed071f4fd4542f26e4c8004a9e/URLShortnerProject/URLShortnerApp/__pycache__/forms.cpython-37.pyc
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/__pycache__/models.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/satssehgal/URLShortnerDjango-/b57a62606ebb29ed071f4fd4542f26e4c8004a9e/URLShortnerProject/URLShortnerApp/__pycache__/models.cpython-37.pyc
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/__pycache__/serializers.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/satssehgal/URLShortnerDjango-/b57a62606ebb29ed071f4fd4542f26e4c8004a9e/URLShortnerProject/URLShortnerApp/__pycache__/serializers.cpython-37.pyc
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/__pycache__/urls.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/satssehgal/URLShortnerDjango-/b57a62606ebb29ed071f4fd4542f26e4c8004a9e/URLShortnerProject/URLShortnerApp/__pycache__/urls.cpython-37.pyc
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/__pycache__/views.cpython-37.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/satssehgal/URLShortnerDjango-/b57a62606ebb29ed071f4fd4542f26e4c8004a9e/URLShortnerProject/URLShortnerApp/__pycache__/views.cpython-37.pyc
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/admin.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 | from URLShortnerApp.models import URLData
3 |
4 | admin.site.register(URLData)
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/apps.py:
--------------------------------------------------------------------------------
1 | from django.apps import AppConfig
2 |
3 |
4 | class UrlshortnerappConfig(AppConfig):
5 | name = 'URLShortnerApp'
6 |
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/forms.py:
--------------------------------------------------------------------------------
1 | from django import forms
2 |
3 | class URLDataForm(forms.Form):
4 | EnterURL=forms.CharField(label='Enter Your URL ', max_length=1000, widget=forms.TextInput(attrs={'placeholder': 'Shorten URL Here'}))
5 |
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/models.py:
--------------------------------------------------------------------------------
1 | from django.db import models
2 |
3 | class URLData(models.Model):
4 | URLID=models.CharField(max_length=1000)
5 | ShortURL=models.CharField(max_length=1000)
6 |
7 | def __str__(self):
8 | template = '{0.URLID}, {0.ShortURL}'
9 | return template.format(self)
10 |
11 |
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/serializers.py:
--------------------------------------------------------------------------------
1 | from rest_framework import serializers
2 | from . models import URLData
3 |
4 | class URLDataSerializers(serializers.ModelSerializer):
5 | class Meta:
6 | model=URLData
7 | fields='__all__'
8 |
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/static/images.text:
--------------------------------------------------------------------------------
1 | Feel Free to use your own images. If you are putting this into production then you cannot use static fields
2 |
--------------------------------------------------------------------------------
/URLShortnerProject/URLShortnerApp/templates/myform/form.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
61 | Short N' Sweet
67 |Made with:
116 | 117 |
126 |
130 |
134 |