Hopefully (unlike me) you did not have to spend countless hours trying to set this up. I have written this article to save you time!
Here are 5 easy steps to install Django and Python 3+ on Raspberry Pi so you can view your site in your browser of choice.
apt install libffi-dev libbz2-dev liblzma-dev libsqlite3-dev libncurses5-dev libgdbm-dev zlib1g-dev libreadline-dev libssl-dev tk-dev build-essential libncursesw5-dev libc6-dev openssl git
You will then have to run these commands to run and build your Python version.
cd ~
wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz
tar -zxvf Python-3.6.9.tgz
cd Python-3.6.9
./configure --with-ssl --enable-optimizations
make -j -l 4
sudo make altinstall
sudo apt install python3-venv python3-pip
You can add an alias in your .bashrc file.
Now we move to configure the webserver. Of course, you can also use NGINX in place of Apache.
sudo apt-get update
sudo apt-get upgrade
sudo apt install apache2 -y
sudo apt install libapache2-mod-wsgi-py3
This command will install the Web Server Gateway Interface (WSGI) for interacting with Python 3.
You can test your apache set up by visiting the IP address on your PI with any browser on the same network. You should see something like this:
mkdir -p /home/pi/django-apps/static
cd /home/pi/django-apps
python3 -m venv djenv
source djenv/bin/activate
pip3 install django
django-admin startproject hackernoon-app .
After running this command, you should see “(djenv)” at the start of the command line. This indicates that you are using our new virtual environment as a source.
sudo nano /etc/apache2/sites-enabled/000-default.conf
This will open the default configuration page. Add these lines within the <VirtualHost></VirtualHost> tags. Ideally as the last segment. For the sake of clarity- let’s make some assumptions.
__App Name __ |
hackernoon-app |
---|---|
|
/home/pi/django-apps/ |
|
/home/pi/django-apps/djenv/ |
Your configuration must look like this:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/pi/django-apps/static
<Directory /home/pi/django-apps/static>
Require all granted
</Directory>
<Directory /home/pi/django-apps/hackernoon-app/hackernoon-app>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess hackernoon-app python-path= /home/pi/django-apps python-home=/home/pi/django-apps/djenv
WSGIProcessGroup hackernoon-app
WSGIScriptAlias / /home/pi/django-apps/hackernoon-app/hackernoon-app/wsgi.py
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
You can now save and exit by pressing “CTRL” and “X” simultaneously. “Y” then “Enter”.
We will then proceed to restart Apache.
sudo systemctl restart apache2
Now you should be able to view your site in your browser of choice!