· 6 years ago · Feb 25, 2020, 09:58 AM
1https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04
2
3https://www.digitalocean.com/community/tutorials/how-to-install-the-django-web-framework-on-ubuntu-18-04
4
5https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04
6
7https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04
8
9https://www.digitalocean.com/community/tutorials/how-to-set-up-object-storage-with-django
10
11Prerequisites:
12- MobaXTerm
13- DigitalOcean Account
14
15Steps:
16- Create a Droplet [ubuntu, 5usd, enable backup, datacenter = Bangalore, authentication = one-time password (later we will enable ssh auth)
17- Copy IP address generated by DigitalOcean.
18- Check email send by DigitalOcean
19- Go to MobaXterm --> session --> ssh --> paste the IP address in "Remote host" field, username = root --> Click OK
20- Copy password from email --> paste in MobaXterm terminal after root@ipadress password:
21- Paste again the same password in [current] UNIX password
22- Enter new password
23- Close tab and reopen
24
25https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04
26
27- after root@hostname:# --> adduser sammy
28- enter password
29- provide permissions --> usermod -aG sudo sammy
30- Open CMD --> ssh-keygen --> rename files to `sammy` in .ssh folder --> repeat same process for `root` user
31- Copy root.pub key --> in MobaXterm --> go to /root/.ssh/authorized_keys --> edit and paste
32- Copy mitch.pub key --> in MobaXterm --> go to /home/mitch/.ssh/authorized_keys --> edit and paste (might need to create .ssh and authorized_keys manually)
33- open CMD --> ssh sammy@ipaddress --> test login
34
35- open MobaXterm --> session --> ssh --> Remote host = ipadderss --> username = sammy --> advanced SSH settings --> enable "User private key" --> find/browser for sammy PUBLIC key --> Click OK
36- now under user sessions --> edit session for root ipaddress and repeat the above setp while selecting root PUBLIC key --> Click OK
37
38- in MobaXterm terminal after root@hostname: --> sudo nano /etc/ssh/sshd_config --> set "PasswordAuthentication" to no --> save the file and exit
39
40- sudo ufw app list
41- sudo ufw allow openssh
42- sudo ufw enable
43- sudo ufw status
44- sudo apt update
45- sudo apt install python3.7
46- python3.7 --> to check shell --> exit()
47- sudo update -alternatives --install /usr/bin/python python /usr/bin/python3.6 2
48- sudo update -alternatives --install /usr/bin/python python /usr/bin/python3.7 1
49- sudo update-alternatives --config python
50- set selection to "python3.7" number
51- sudo apt-get install -y python3-pip
52- pip3 --> to check
53- sudo -H pip3 install virtualenv
54- cd /home/sammy
55- mkdir djangoprojectdir
56- cd djangoprojectdir
57[Inside /djangoprojectdir]
58- virtualenv djangoprojectenv
59- source djangoprojectenv/bin/activate
60- pip3 install gunicorn
61- pip3 install django==2.2.2
62- cd ..
63[Inside /sammy]
64- django-admin startproject mysite djangoprojectdir
65
66- Edit settings.py --> ALLOWED_HOSTS = localhost, ipaddress --> add OUR apps to INSTALLED_APPS --> then add line AUTH_USER_MODEL = 'account.Account' --> TEMPLATES -> DIRS add os path --> COPY STATIC FILES CODE below from our local to settings.py
67
68- Drag all `app` [account, blog, personal, templates, requirements] folders from local to /djangoprojectdir in MobaXterm
69
70- Drag urls.py from local to MobaXterm and remove MEDIA root code from below
71- Create new dir "static" and drag logo from local "static" to MobaXterm "static"
72
73- cd djangoprojectdir
74[Inside /djangoprojectdir]
75- pip3 install -r requirements.txt
76- python manage.py makemigrations
77- python manage.py migrate
78- python manage.py collectstatic
79
80- sudo ufw allow 8000
81- python manage.py runserver 0.0.0.0:8000
82- Check ipaddress:8000 in browser
83- gunicorn --bind 0.0.0.0:8000 mysite.wsgi
84
85https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04
86
87- deactivate [deactivates virtualenv]
88- pip3 install -r requirements.txt + pip3 install gunicorn
89
90- In MobaXterm sidebar FTP --> go to /etc/systemd/system --> create file 'gunicorn.socket' --> paste the code from digitalocean docs
91
92- In MobaXterm sidebar FTP --> go to /etc/systemd/system --> create file 'gunicorn.service' --> paste the code from digitalocean docs --> make appropriate changes --> "mysite.wssgi", user, dir, env...
93
94- sudo systemctl start gunicorn.socket
95- sudo systemctl enable gunicorn.socket
96- file /run/gunicorn.sock
97- sudo systemctl status gunicorn.socket
98
99[If error --> sudo shutdown -r now]
100
101- curl --unix-socket /run/gunicorn.sock localhost --> should get HTML file syntax
102
103[If change gunicorn service file --> either shutdown server by sudo shutdown -r now or do --> sudo systemctl daemon-reload --> sudo systemctl restart gunicorn.socket gunicorn.service]
104
105- sudo systemctl status gunicorn
106
107- sudo apt install nginx
108- go to /etc/nginx/sites-available --> create new file "djangoproject" --> paste digitalocean docs code --> add ipaddress and change "root" path
109
110- sudo ln -s /etc/nginx/sites-available/djangoproject /etc/nginx/sites-enabled
111- sudo nginx -t
112- sudo systemctl restart nginx
113- sudo ufw delete allow 8000
114- sudo ufw allow 'Nginx Full'
115- Go to browser and paste ipaddress --> check
116
117
118- 2 ---------------------------------------------------------------------------
119
120- In DigitalOcean --> Create Domain/DNS --> enter domain 'hello.com'
121
122- In Hostname add "@" --> 3600 --> create record
123- In Hostname add "www" --> 3600 --> create record
124
125- Namecheap --> Manage --> add Nameservers --> ns1.digitalocean.com --> ns2 and ns3
126
127- In MobaXterm --> settings.py --> add domain with and without "www" both to ALLOWED_HOSTS
128- service gunicorn restart
129- go to /etc/nginx/sites-available/djangoproject --> edit file djangoproject --> add domain name to server_name without comma seperation. Add both www and simple version domain name
130
131- sudo systemctl restart nginx
132- sudo systemctl status gunicorn.service
133- go to domain name eg. hello.com in incognito mode --> check
134
135[If error --> sudo shutdown -r now or /sites-available --> copy contents of 'default' file safely, delete the file and create again]
136
137- 3 ---------------------------------------------------------------------------
138
139https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04
140
141- sudo add-apt repository ppa:certbot/certbot
142- sudo apt install python-certbot-nginx
143- sudo nano /etc/nginx/sites-available/djangoproject
144- sudo ufw status
145- sudo certbot --nginx -d hello.com -d www.hello.com
146- enter email
147- choose 2
148- sudo certbot renew --dry-run
149
150[If error --> sudo ufw allow https , sudo ufw delete allow '443/tcp' or check the number 442/443]
151
152- 4 ---------------------------------------------------------------------------
153
154https://www.digitalocean.com/community/tutorials/how-to-set-up-object-storage-with-django
155
156- In DigitalOcean --> Create Spaces --> datacenter --> restrict file listing --> hello-space --> create a space
157
158- Create New Folder in space named "hello-static"
159- Go to API section in left side bar --> Generate Key --> Name = hello-space-key --> Secure Secret and hello-static-key
160
161- In MobaXterm --> pip3 install boto3 --> pip3 install django-storages [Do in virtual env]
162- Delete static_cdn folder
163- In settings.py --> add storages to INSTALLED_APPS --> configure AWS keys and all below settings.py
164
165- cd /home/sammy/djangoprojectdir
166[Inside /djangoprojectdir]
167
168- source djangoprojectenv/bin/activate
169- pip3 install boto3
170- pip3 install django-storages
171- python manage.py collectstatic
172- sudo nano /etc/nginx/sites-available/djangoproject --> change "root" to "alias" --> save
173- sudo shutdown -r now
174- wait 20 sec
175- relogin
176- python manage.py createsuperuser
177
178- cd ..
179[Inside /sammy]
180- chown sammy djangoprojectdir/
181
182cd djangoprojectdir
183[Inside /djangoprojectdir]
184- chown sammy db.sqlite3 [If you are using sqlite3]
185
186- ls -l --> to check
187
188- pip3 install python-decouple
189- Create new file in /djangoprojectdir "settings.ini" --> make releveant changes
190- make appopriate changes in "settings.py"
191
192[If secret key of django in settings.ini has % then concatenate another "%%"
193
194- service gunicorn restart
195
196
197- 5 ---------------------------------------------------------------------------
198
199- Google Account --> myaccount.google.com --> security --> enable 2-Step verification --> Create App passwords --> other --> any name --> generate
200
201- Add email Backend code in settings.py and make relevant changes
202- service gunicorn restart