Raspberry Pi Network Server Guide with Django & SSH

By Justin

Raspberry Pi Network Server Guide with Django & SSH
In this post, we'll be setting up a Raspberry PI (Series 3 Model B) on our network so we can connect via SSH as well as setup a Django project on the PI that will be served on our network aka **hosting and serving a Django project on a Raspberry Pi **using our local network and Apache.
Adapted from official docs and moved from our github.
Formatting the SD Card is designed for Mac systems but similar steps can be taken on Windows/Linux (please leave comment if you find a solid way).

Software Downloads:

SDFormatter: to erase/format your microSD card
Etcher: to add the operating system to your microSD card
Raspbian Stretch with Desktop: A Raspberry Pi Linux/Debian Operating system. Docs

Setup the Pi

  1. Flash microSD card
    1. Download SDFormatter for your System version.
    2. Install
    3. Insert your microSD card.
    4. Open SDFormatter
    5. Select card from list
    6. Select "Quick Format" as option.
    7. Write any name you'd like
    8. Click "Format"
    1. Once downloaded, extract/unzip downloaded contents.
    2. The file is about 1.5 GB so it might take a little while to download.
  2. Download and open Etcher
  3. Run Etcher and select the Raspbian image
  4. Select the SD card drive (or ensure it's selected)
  5. Click Burn to transfer Raspbian to microSD card. Once complete, you can safetly pull the card out (it will automatically eject/unmount the card)
  6. Insert microSD card into Raspberry Pi (gold pins facing the bottom of the Pi)
  7. Power on Raspberry Pi

Get IP Address of the Pi

This step most likely will require you to connect your pi to a monitor, keyboard, and mouse. I recommend using a USB keyboard/mouse and a HDMI monitor.
  1. Turn Raspberry Pi on and ensure microSD card is inserted that contians the Raspbian Jessie Linux Operating System (above setup).
  2. Connect USB Keyboard, USB Mouse, and Monitor (through HDMI)
  3. Connect Raspberry Pi to Internet on your local network :
    • via Wifi (look for wlan0 below)
    • via Ethernet (look for eth0 below)
  4. Get the IP Address of your PI: In Terminal type:
    $ hostname -I 192.168.0.10
    The 192.168.0.10 is your IP address for the next steps. Make note of it. Another way to grab the IP address is to:
    $ ifconfig eth0 Link encap:Ethernet HWaddr b8:27:eb:49:e9:1d inet6 addr: fe80::20f:eaff:fe91:407/64 Scope:Link UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:532 errors:0 dropped:0 overruns:0 frame:0 TX packets:532 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:42256 (41.2 KiB) TX bytes:42256 (41.2 KiB) wlan0 Link encap:Ethernet HWaddr b8:27:eb:1c:bc:48 inet addr:192.168.0.10 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20f:eaff:fe91:407/64 Scope:Link inet6 addr: 2715:e000:3098:a900:c338:7e68:77c2:d3ce/64 Scope:Global UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:11842 errors:0 dropped:854 overruns:0 frame:0 TX packets:8365 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11994051 (11.4 MiB) TX bytes:1072259 (1.0 MiB)
    Notice the line inet addr:192.168.0.10 Bcast:192.168.0.255 Mask:255.255.255.0
    The numbers 192.168.0.10 are your IP Address (<ip>)
  5. Activate SSH In your Pi:
    $ sudo service ssh start
    Open terminal
    $ sudo raspi-config
    • Select Interfacing Options
    • Select SSH
    • Enable SSH by selecting YES
    • Go to Finish
  6. SSH into your Pi with ssh pi@<ip>:

Linux Users (non-Pi linux):

  1. Open Open Terminal
  2. type ssh [email protected]
  3. Accept warning about fingerprint authenticity (if any)
  4. Default password is raspberry
  5. You're in!

Windows Users:

  1. Download & Install PuTTY
  2. Open PuTTY
  3. type ssh [email protected]
  4. Accept warning about fingerprint authenticity (if any)
  5. Default password is raspberry
  6. You're In!

Apache2 + Django

Update Software:
sudo apt-get update
sudo apt-get upgrade
Install Apache2:
sudo apt-get install apache2 -y

sudo apt-get install libapache2-mod-wsgi-py3

sudo apt-get install libapache2-mod-wsgi # if using Python2
Install Pip & Django:
sudo apt-get install python-setuptools python-dev build-essential

sudo easy_install pip 

sudo pip install django==X.Y.Z #where X.Y.Z is the version number

sudo pip install django==1.10.3

sudo pip install virtualenv 

Start Django Project:
cd ~/

mkdir Dev && cd Dev

mkdir cfehome && cd cfehome

virtualenv -p python3 .

source bin/activate

pip install django==1.10.3

django-admin.py startproject cfehome

mv /home/pi/Dev/cfehome/cfehome /home/pi/Dev/cfehome/src
Apache2 Settings:
sudo nano /etc/apache2/sites-available/000-default.conf
Note: If errors happen with below, just do the following and it will re-install apache:
sudo apt-get purge apache2 # removes apache2

sudo apt-get install apache2 -y # reinstalls it

<VirtualHost *:80>
    ServerName www.example.com

    ServerAdmin webmaster@localhost

    Alias /static /home/pi/Dev/cfehome/static
        <Directory /home/pi/Dev/cfehome/static>
           Require all granted
         </Directory>

    <Directory /home/pi/Dev/cfehome/src/cfehome>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess cfehome python-path=/home/pi/Dev/cfehome/src:/home/pi/Dev/cfehome/lib/python2.7/site-packages
    WSGIProcessGroup cfehome
    WSGIScriptAlias / /home/pi/Dev/cfehome/src/cfehome/wsgi.py


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Restart Apache:
# Restart in two ways:
sudo apachectl restart
sudo service apache2 restart


# Start Apache in two ways:
sudo apachectl start
sudo service apache2 start

# Stop Apache in two ways:
sudo apachectl stop
sudo service apache2 stop
Set Ownership of Database to Pi user for Django
sudo adduser $USER www-data
sudo chown www-data:www-data /home/$USER/Dev/cfehome    
sudo chown www-data:www-data /home/$USER/Dev/cfehome/src/db.sqlite3
sudo chmod -R 775 ~/Dev/cfehome

# if above fails, try (thanks Mike!):
sudo chown -R www-data:www-data ~/Dev/cfehome
sudo chown www-data:www-data /home/pi/Dev/cfehome/src
# or if a new project
sudo chown -R www-data:www-data ~/Dev/<your-virtuaenv-name>
sudo chown www-data:www-data /home/pi/Dev/<your-virtuaenv-name>/src/
Discover Posts