
Part 1: Installing Raspbian, Setting Up the System and Django Project, and First Git Commit
Install Raspbian on a SDCard on macOS:
sudo dd bs=1m if=/path/to/raspbian/image.img of=/dev/rdisknumber conv=syncWe 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 nmapYou will need to find out your router IP address scheme, to do so run this command:
netstat -rn |grep defaultAccording to the scheme used run:
nmap -A xxx.xxx.x.0/24Now, SSH to it using:
ssh pi@xxx.xxx.x.(x)xThe default password for pi user will be raspberry, therefore you have to change it immediately:
passwdUpdate Raspbian:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgradeRemove 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 localesSelect en_GB.UTF-8
Add a user specifically for the Django project:
sudo adduser name_of_system_userMake the user a sudoer so he can access system services, install packages,… :
sudo gpasswd -a name_of_system_user sudoSwitch to the Django project system user:
sudo su — name_of_system_userInstall 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.pyInstall the Virtualenv package:
pip3 install virtualenvMemo: 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_repoDuring 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_projectChange to the new directory
cd django_projectCreate a virtual environment that will use Python3:
virtualenv venv -p python3Activate the virtual environment:
source venv/bin/activateTo get out of the virtual env, especially important not to forget as you swap between different.
deactivateInstall NGINX that will process all requests addressed to the server:
sudo apt-get install nginxInstall the database packages:
sudo apt-get install postgresql postgresql-contribInstall Supervisor in order to run Django and Gunicorn as background tasks:
sudo apt-get install supervisorEnable it:
sudo systemctl enable supervisorStart it:
sudo systemctl start supervisorSetting the database
Change to database user:
sudo su — postgresCreate a PostgreSQL user (possibly differentiate from the system user):
createuser name_of_db_userCreate its database:
createdb name_of_db —owner name_of_db_userAdd a password to the user:
psql -c “ALTER USER name_of_db_user WITH PASSWORD ‘the_password’”Exit the postgres setup:
exitWith 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 databaseStart a Django project:
django-admin startproject name_of_projectChange 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 — listWill give you:
user.name=First name Last name
user.email=firstname@example.com
Initialise the git repository
git initCheck the repository status
git statusAdd 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:
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class# C extensions
*.so# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec# Installer logs
pip-log.txt
pip-delete-this-directory.txt# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/# Translations
*.mo
*.pot# Django stuff:
*.log
local_settings.py
db.sqlite3# Flask stuff:
instance/
.webassets-cache# Scrapy stuff:
.scrapy# Sphinx documentation
docs/_build/# PyBuilder
target/# Jupyter Notebook
.ipynb_checkpoints# IPython
profile_default/
ipython_config.py# pyenv
.python-version# celery beat schedule file
celerybeat-schedule# SageMath parsed files
*.sage.py# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/# Spyder project settings
.spyderproject
.spyproject# Rope project settings
.ropeproject# mkdocs documentation
/site# mypy
.mypy_cache/
.dmypy.json
dmypy.json# Pyre type checker
.pyre/# SQLite database files
*.sqlite3
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 freezeYou 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.txtInstall packages from requirements.txt:
pip3 install -r requirements.txtAdd 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_packageTo upgrade all packages using the requirements.txt list:
pip3 install --upgrade -r requirements.txtOr uninstall the package using:
pip3 uninstall name_of_packageTo upgrade pip:
pip3 install --upgrade pip