-
Notifications
You must be signed in to change notification settings - Fork 1
/
raspberry-pi-apache-django-cheatsheet.txt
90 lines (74 loc) · 2.79 KB
/
raspberry-pi-apache-django-cheatsheet.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
CREATE WEB SERVER
From: https://pimylifeup.com/raspberry-pi-apache/
1. Install apache
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt install apache2 -y
2. Permissions
$ sudo usermod -a -G www-data pi
$ sudo chown -R -f www-data:www-data /var/www/html
SETUP DJANGO FRAMEWORK
From: https://pimylifeup.com/raspberry-pi-django/
1. Install packages
$ sudo apt install libapache2-mod-wsgi-py3
$ sudo apt install python3 python3-venv python3-pip
2. Configuring Apache for Django
$ sudo nano /etc/apache2/sites-enabled/000-default.conf
Add above </VirtualHost>:
Alias /static /home/pi/pidjango/static
<Directory /home/pi/pidjango/static>
Require all granted
</Directory>
<Directory /home/pi/pidjango/pidjango>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess django python-path=/home/pi/pidjango python-home=/home/pi/pidjango/djenv
WSGIProcessGroup django
WSGIScriptAlias / /home/pi/pidjango/pidjango/wsgi.py
3. Restart apache
$ sudo systemctl restart apache2
If error occurs, check for typos and apache status:
$ systemctl status apache2.service
Re-instalation of apache2 and ibapache2-mod-wsgi-py3 usualy helps :)
4. Setup Django project
$ mkdir -p /home/pi/pidjango/static
$ cd /home/pi/pidjango
$ python3 -m venv djenv
$ source djenv/bin/activate
$ python3 -m pip install django
$ django-admin startproject pidjango .
$ hostname -I
$ nano /home/pi/pidjango/pidjango/settings.py
Change:
ALLOWED_HOSTS = ["YOURIPADDRESS"]
Open browser: https://[YOURPIIPADDRESS] without running the Django server!
5. Additional setup
Create superuser:
$ cd /home/pi/pidjango
$ python3 manage.py createsuperuser
Re-create /static folder:
Add to the end of the settings.py file:
STATIC_ROOT = os.path.join( BASE_DIR, "static/")
$ cd /home/pi/pidjango
$ python3 manage.py collectstatic
Make database writable:
https://stackoverflow.com/questions/34173547/django-apache-error-attempt-to-write-a-readonly-database
$ cd /home/pi/pidjango
$ chmod 755 db.sqlite3
Change ownerships:
$ cd /home/pi/pidjango
$ sudo chown -R -f www-data:www.data db.sqlite3
$ sudo chown -R -f www-data:www.data .
IMPORTANT NOTES!
- if you want to install aditional python packages (ex. ppi install djangorestframework) or try to migrate models (python3 manage.py migrate) you could encouter problems with permissions.
I deal wit that in following way:
1. Give ownership to pi user:
$ sudo chown -R pi:pi .
2. Make changes
3. Return ownership to www-data
$ sudo chown -R -f www-data:www.data db.sqlite3
$ sudo chown -R -f www-data:www.data .
If yoou have better idea, please contact me!
- run `$ sudo systemctl restart apache2` for every change in the django code