paint-brush
How to Install Django and Python 3+ on Raspberry Piby@simandebvu
8,983 reads
8,983 reads

How to Install Django and Python 3+ on Raspberry Pi

by Shingirayi Innocent MandebvuSeptember 17th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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!

Company Mentioned

Mention Thumbnail
featured image - How to Install Django and Python 3+ on Raspberry Pi
Shingirayi Innocent Mandebvu HackerNoon profile picture

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.


1. Install Build Tools

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

2. Download and Build Python

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.

3. Install Apache Webserver

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:


4. Setup Folders

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.

5. Configure Apache


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

Directory for Django apps

/home/pi/django-apps/

VirtualEnv Location

/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!