Image for post
Image for post

Part 1: Installing Raspbian, Setting Up the System and Django Project, and First Git Commit

Summary

Install Raspbian on a SDCard on macOS:

sudo dd bs=1m if=/path/to/raspbian/image.img of=/dev/rdisknumber conv=sync

We will use SSH to connect to the headless Raspberry (not connected to a monitor). Once done, go to the SDCard, in the boot folder create a file named ssh (without an extension). Unmount the card, plug in your Raspberry. To find the IP address of your Pi, you can use Fing on your smartphone or use the command below that will list the IP addresses of every devices connected to your network (you will have to test each one out though). First install Homebrew if not already on your system, nmap a network analysis tool:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then nmap:

brew install nmap

You will need to find out your router IP address scheme, to do so run this command:

netstat -rn |grep default

According to the scheme used run:

nmap -A xxx.xxx.x.0/24

Now, SSH to it using:

ssh pi@xxx.xxx.x.(x)x

The default password for pi user will be , therefore you have to change it immediately:

passwd

Update Raspbian:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

Remove the Perl error caused by missing locales:

export LANGUAGE=en_GB.UTF-8
export LANG=en_GB.UTF-8
export LC_ALL=en_GB.UTF-8
sudo locale-gen en_GB.UTF-8
sudo dpkg-reconfigure locales

Select en_GB.UTF-8

Add a user specifically for the Django project:

sudo adduser name_of_system_user

Make the user a sudoer so he can access system services, install packages,… :

sudo gpasswd -a name_of_system_user sudo

Switch to the Django project system user:

sudo su - name_of_system_user

Install pip 3 (Python program to install Python3 packages) and Virtualenv to create multiple projects with Django that won’t interfere with one another on the local machine (each package installed under this virtual environment only):
Download and install pip3:

wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py

Install the Virtualenv package:

pip3 install virtualenv

Memo: to install a package from a URL, Github for example:

pip3 install -e git://github.com/name_of_user/name_of_repo#egg=name_of_repo

During the pip freeze command (to write down all requirements for the project), it will write down the correct URL where to install the repo from.

Create a folder for the whole Django project

mkdir django_project

Change to the new directory

cd django_project

Create a virtual environment that will use Python3:

virtualenv venv -p python3

Activate the virtual environment:

source venv/bin/activate

To get out of the virtual env, especially important not to forget as you swap between different.

deactivate

Install NGINX that will process all requests addressed to the server:

sudo apt-get install nginx

Install the database packages:

sudo apt-get install postgresql postgresql-contrib

Install Supervisor in order to run Django and Gunicorn as background tasks:

sudo apt-get install supervisor

Enable it:

sudo systemctl enable supervisor

Start it:

sudo systemctl start supervisor

Setting the database
Change to database user:

sudo su - postgres

Create a PostgreSQL user (possibly differentiate from the system user):

createuser name_of_db_user

Create its database:

createdb -O name_of_db_user name_of_db

Add a password to the user:

psql -c "ALTER USER name_of_db_user WITH PASSWORD 'the_password'"

Exit the postgres setup:

exit

With that Python 3 dependencies:

sudo apt-get install python3 python3-gi python3-dbus python3-systemdsudo apt-get install python3-psycopg2 python3-dev
sudo apt-get install libpq-dev

Then Django, psycopg2 and Gunicorn (to

pip3 install django psycopg2 gunicorn
psycopg2 is an adapter to help Python use the PostgreSQL database

Start a Django project:

django-admin startproject name_of_project

Change directory to root project directory (where manage.py is situated, the inside the name_of_project folder created by the last terminal command). Then install Git packages (version-control system for tracking changes in source code during software development), configure your username and email address.

sudo apt-get install git-coregit config — global user.name “username”git config — global user.email “email_address”

View all Git settings, for now only user settings:

git config — list

Will give you:

Initialise the git repository

git init

Check the repository status

git status

Add a .gitignore file to project root in order to stop git from committing files that are either irrelevant to the project or containing sensitive information, here is a generic file:

Add a requirements.txt file to root project (where manage.py is)
To do so use pip3 to list all packages added using the package installer:

pip3 freeze

You can also simply run the command below to create or overwrite a requirements.txt files with the packages installed by pip and the version you are using:

pip3 freeze > requirements.txt

Install packages from requirements.txt:

pip3 install -r requirements.txt

Add files and make your first commit:

git add .
git commit -m “add_description”

See this article to check out useful commands when using Git on a daily basis.

In the future you may need to upgrade or delete the packages installed with pip, or even upgrade pip itself. Here are the commands to run:

If some requirements are out of date upgrade them using:

pip3 install --upgrade name_of_package

To upgrade all packages using the requirements.txt list:

pip3 install --upgrade -r requirements.txt

Or uninstall the package using:

pip3 uninstall name_of_package

To upgrade pip:

pip3 install --upgrade pip

Using programming for marketing and work efficiency

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store