Setting Up Django on the Raspberry Pi

A step-by-step guide on installing Django on the Raspberry Pi and accessing it from Windows.

Django is a web framework for perfectionists (with deadlines). It lets you build high-performing, elegant web applications quickly using the Python programming language. A web framework is a software framework designed to simplify the web development process.  Basically, web sites have a common set of functionality such as accessing a database, managing sessions and cookies and creating templates to display your HTML. Web frameworks help alleviate the overhead of having to code all these functionalities. In other words a web framework saves you from having to re-invent the wheel and makes it easier for you to build your website or web application.

Getting a fresh copy of Raspbian

Raspbian is the operating system of choice and is used throughout this guide. If 20170627_143341you have an existing Raspbian running on your Raspberry Pi you can skip this step and proceed to the next one. Otherwise, you will need to download a copy of Raspbian Jessie Lite from the Raspberry Pi website and burn the Raspbian image on your SD card. You can read the instructions if you need help with the installation. After burning the image, boot your Raspberry Pi and log in with the user name pi and with the password raspberry.

20170627_145320

Enabling SSH

SSH (Secure Shell) is disabled by default on Raspbian. We need to enable it so that we could connect to our Pi from Windows. In the command line type:

$ sudo raspi-config

This will bring up the Raspberry Pi Configuration Tool. From the choices select Interfacing Options.

20170627_144957.jpg

Then select SSH.

20170627_145031

You will then be asked if you would like the SSH server to be enabled, choose yes then enter.

20170627_145053

A message will appear saying The SSH server is enabled. Just press enter to go back to the main menu.

20170627_145112

Before exiting raspi-config you also might want to change other configurations like your locale and timezone. Select finish to exit raspi-config.

20170627_145130

Now connect your Raspberry Pi to your router with an ethernet cable and reboot your Pi with:

$ sudo reboot

Connecting to your Pi using PuTTY

We’re going to use a program called PuTTY to connect to our Raspberry Pi. You can download PuTTY here. What we need is the putty.exe under the Alternative binary files. If you have a 64-bit installation of Windows then you can get the 64-bit version, otherwise the 32-bit version would do.

Before we can connect to our Raspberry Pi we need to find out its IP address, so go back and log in again to your Raspberry Pi and in the command line type:

$ hostname -I
192.168.1.4

Remember the output of the command, this is your Raspberry Pi’s IP address and we will use this to connect to your Pi. Now run the putty.exe that you downloaded, and in the Host Name (or IP address) box enter your Raspberry Pi’s IP address then click on Open.

putty.PNG

If everything goes well then you should be able to log in remotely with the user name pi and the raspberry password. If not then go back and check your internet connection.

putty1

Creating a Python Virtual Environment and installing Django

We need to create an isolated Python environment so that our system installation of Python will be untouched when we install Django. We need a package called virtualenv to do that. To install virtualenv type the following commands:

$ sudo apt-get update
$ sudo apt-get install -y virtualenv

To actually create the virtual environment type:

$ virtualenv --python=/usr/bin/python3 ~/.venv

We want to make Python 3 our virtual environment’s Python interpreter and that is what the --python=/usr/bin/python3 option is for.  To activate the virtual environment we just created type:

$ source ~/.venv/bin/activate

To deactivate the virtual environment just type deactivate.

Installing Django is simple as Pie. We can just get the package from PyPI with:

$ pip install django

Creating our first project

Before we create our first project let’s create a new directory and change into that directory.

$ mkdir django
$ cd django

To create a project type (in some cases you may have to deactivate and re-activate your Python virtual environment before you can invoke django-admin.py):

$ django-admin.py startproject mysite

This will create a directory called mysite, which contains the following files and directory.

mysite
├── manage.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Running the development server

Django comes equipped with a development server. Before we run the server we need to edit the settings.py file to include your Raspberry Pi’s IP address to allowed hosts. Make sure you’re on your project’s root directory and edit your settings.py with nano.

$ cd mysite
$ pwd
/home/pi/django/mysite
$ nano mysite/settings.py

Find the line that says ALLOWED_HOSTS = [] and place your Raspberry Pi’s IP address inside the square brackets enclosed in quotation marks followed by a comma like this.

ALLOWED_HOSTS = ['192.168.1.4',]

Save the file and exit nano. This allows us to browse our development server from our web browser.
Note: IP addresses change from time to time. To save you from trouble of having to edit your settings.py when your Raspberry Pi’s IP address change, it’s a good idea to give your Raspberry Pi a static IP address. This link gives a very good tutorial on setting up a static IP address on your Raspberry Pi.

We’re almost ready, but before we start running the server there is one last command. On your project’s root directory type:

$ python manage.py migrate

To run the server type:

$ python manage.py runserver 192.168.1.4:8000

You can now access your project by entering your Raspberry Pi’s IP address, colon then port number 8000 (in my case it’s 192.168.1.4:8000) into your web browser’s  address bar. You should see something like this, which is Django’s default page.

putty2.PNG

Congratulations! You have successfully set up Django on your Raspberry Pi and created your first Django project. On our next tutorial we will change the default page and learn about views and urls.

Advertisements

Beginners’ guide to installing Arch Linux

Introduction

In this tutorial you will lean how to install Arch Linux on VirtualBox, create a regular user account and install a desktop environment. It is also compatible with setting up Arch Linux on a real machine. Arch Linux is a GNU/Linux distribution, which aims for simplicity and full control to the end user. Please take note that this distribution may not be suitable for beginners. However, if you still wish to proceed then this guide is an excellent choice to getting you started with Arch Linux–although it is not a one-stop Arch Linux installation tutorial. It is still best to refer to the Arch Linux manuals. VirtualBox is a free and open-source virtualization software by Oracle corporation. It allows users to run nearly any operating system on a single machine and to freely switch between operating system instances running simultaneously.

Requirements

This tutorial assumes that you already have VirtualBox installed on your computer and have downloaded the Arch Linux installer ISO, which you can get from archlinux.org.

Arch Linux Virtual Machine

To begin with the installation open up virtualbox and click on New. On the Name type Arch Linux and it will automatically select the Type and Version of the operating system for you. Click on Next, here you can choose how much RAM you wish to give our virtual machine. I am just going to leave it to the default 1 gb and click on Next. Now we are asked if we would like to create a virtual hard disk, choose Create a virtual hard disk now and click on Create. For the hard disk file type we will go with VHD (Virtual Hard Disk) and click on Next. Select Dynamically allocated so it only uses space on your physical hard disk as needed and click on Next. Finally, we choose the size we like for our virtual hard disk to have. I’m going to give mine 80 gb of space and click on Create.

We are now done creating our virtual machine.  The next step is to insert the Arch ISO into our virtual CD-ROM. To do that click the title that says Storage, and under Controller: IDE select the icon that looks like a CD that says Empty. On the left most c2017-03-18-222949_674x444_scrotorner of the window there is another icon that looks like a CD, click on that and select Choose Virtual Optical Disk File… This option allows us to choose the image that we would like to load, in this case it is the Arch ISO installer. Now navigate to where you downloaded the Arch ISO, open it and click on Okay.

Now we are ready to start our virtual machine. Click on Start to begin our installation. On the opening screen choose Boot Arch Linux (x86_64) and press enter. This will boot to our live CD and automatically log in as root user. If we type ls then enter we can see an install guide that is included in the live CD in a text file named install.txt. We’re going to use the less command to view the contents of our install guide, so less install.txt allows us to scroll up and down using the arrow keys. To carry out with the installation without closing our install guide we can switch between terminals. On VirtualBox this is done by pressing the host key, which is the right ctrl key plus the function key that is bound to the terminal end. We are currently on TTY1 terminal, which is bound to F1 function key on our keyboard. So to switch to TTY2 terminal we press the right ctrl key plus F2. Just type in root to login to TTY2. To go back to TTY1 terminal just press the right control key plus F1. On a real machine the key combination is ctrl+alt+Fn.

The installation guide is divided into 3 parts. The first part is the pre-installation, the second part is the installation and the last part is configuring the system.

Pre-installation

So for the first part, the first step is to check for internet connection. Of course it has already been automatically configured when we created our virtual machine, but on a real machine depending on your set up, you just have to read through the installation guide on how to get your internet connection working and possibly open another computer and read through the manuals from Arch Linux specific to your internet connection. Please be aware that I may skip some steps in the installation process as the goal of this tutorial is to keep it as simple as possible. Again on a real machine you may have to deal with setting the keyboard layout and verifying the boot mode of your machine. Now let’s move on to updating the system clock. This is done with the timedatectl set-ntp true command. To check the status we type timedatectl status.

Next is to format our hard disk to have separate partitions for boot, swap, root and home directories. fdisk -l lists the devices available on your system. I know that /dev/sda is my hard disk because of its size. Type fdisk /dev/sdx where x is the device letter for your hard disk to begin format. To add a new partition type n. Select p for primary partition, 1 for partition number, just press enter for the first sector, and type +100M for the last sector then press enter. This will be our boot partition. Type n again to create our swap partition. Select p for primary partition, 2 for partition number, just press enter for the first sector, and type +4G for the last sector then press enter. Type n again to create our root partition. Select p for primary, 3 for partition number, just press enter for the first sector, and type +20G for the last sector then press enter. Type n again to create our home partition. Select p for primary, just press enter for the first sector and enter again for the last sector. Type p and enter to print the partition table. You should see the same output as shown in the screenshot provided. Now we are done partitioning our hard disk. Type w to save the changes and exit fdisk. VirtualBox_Arch Linux_18_03_2017_22_39_22

Now we need to format the partitions to Linux file system. We are going to format all partitions to ext4 file system except for swap. To format the boot partition type mkfs.ext4 /dev/sda1 then enter. Do this for the boot and home partitions replacing 1 in /dev/sda1 with their corresponding partition number. A swap is created with mkswap command so type mkswap /dev/sda2 and press enter. To activate our swap type swapon /dev/sda2.

After partitioning our hard disk, we can now mount our root to /mnt. The command is mount /dev/sda3 /mnt where /dev/sda3 is our root file system. The next step is to create the /mnt/boot and /mnt/home directories and mount our file systems accordingly. Type mkdir -p /mnt/boot && mkdir -p /mnt/home to create the boot and home directories, then mount with mount /dev/sda1 /mnt/root && mount /dev/sda4 /mnt/home where /dev/sda1 and /dev/sda4 are the root and home file systems respectively.

Installation of system files

Now we move on to the second part of the install guide, which is the installation of the system files. The first step is selecting the mirrors. Mirrors are computer servers that are copies of our primary Arch Linux server. These mirrors are defined in the /etc/pacman.d/mirrorslist file. We’re going to edit this file using the nano editor and move the mirror that is geographically closest to you to the top of the list because the higher a mirror is placed in the list, the higher priority it is given. So type nano /etc/pacman.d/mirrorlist and select the mirror that is closest to you. Pressing ctrl+k cuts the current line into buffer while ctrl+u pastes the line from the buffer. When you are finished editing, press ctrl+o to save the file and then ctrl+x to exit nano.

We can now install the base packages with pacstrap /mnt base.

Configuring the system

We can now move on to the last part of the install guide, which is configuring the system. The first step is to generate an fstab file with genfstab -U /mnt >> /mnt/etc/fstab. Check the resulting file afterwards with cat /mnt/etc/fstab and edit it in case of errors. Next is to change root with arch-chroot /mnt command. To set the time zone type ln -sf /usr/share/zoneinfo/Region/City /etc/localtime where /Region/City are your zone information. To adjust the time and generate /etc/adjtime type the command hwclock --systohc. To generate your locale, edit the /etc/locale.gen file and uncomment #en_US.UTF-8 and other needed localizations by removing the hash sign at beginning of the line, save it and exit then type locale-gen. Set the LANG variable by creating a /etc/locale.conf with nano and adding the line LANG=en_US.UTF-8. Now we need to create the hostname. The hostname is the name that you would like to give to your computer. To do that create a file /etc/hostname with nano and add the name that you desire. Next is to edit the /etc/hosts and add the line 127.0.1.1 myhostname.localdomain myhostname where myhostname is your chosen hostname. It is now time to set the root password. Type passwd and type your root password twice. This will be the password, that you are going to use when you log in as root. We are almost done and the second to the last step is to install the boot loader. Type pacman -S grub and wait for it to finish. When it is finished installing type grub-install --target=i386-pc --recheck /dev/sdx where x is the device letter for your hard disk. This will install grub boot loader to our hard disk. To create a grub config file first move the old config file to another file because the mkconfig cannot overwrite the old config file. So type mv /boot/grub/grub.cfg /boot/grub/grub.cfg.sav. Now type grub-mkconfig -o /boot/grub/grub.cfg to make a new grub config file. Exit the chroot environment by typing exit or pressing ctrl+d. From here we can type reboot and boot to our newly installed Arch Linux virtual machine. Optionally, for security reasons you may unmount /mnt first, but this is done automatically when you type reboot. To unmount it manually type umount -R /mnt. Reboot the system and log in as root user and type in your password. If everything goes well then you will be logged in as root. Congratulations! You have succesfully installed Arch Linux on VirtualBox.

Post-installation

Adding a regular user account

It is not wise to use our system always as the root user, so we want to create a regular user account with permission to perform root functions under sudo. This leads us to the second part of the installation which is creating a regular user account and installing a desktop environment. To create a new user type useradd -m -G wheel -s /bin/bash username. To give our new user permission to use sudo, we edit the configuration file with EDITOR=nano visudo and locate the section marked as # User privilege specification and uncomment the line that says # %wheel ALL=(ALL) ALL by deleting the hash sign at the beginning of the line. Now save the file and exit. Set the password for our user with passwd username. Type logout then relogin as the regular user. Notice the change of the prompt from # to $. This helps you know if you are logged in as root or not.

Installing LXDE desktop environment

Debian-lenny-lxde

The last step is to install a desktop environment. The first thing to do is to install xorg for graphical display. This is done with sudo pacman -S xorg xorg-xinit xorg-server xorg-server-utils xterm and then install LXDE with sudo pacman -S lxde. Before you launch LXDE, define a configuration file xinitrc with nano ~/.xinitrc and add the line exec startlxde to it then save it and exit. To launch LXDE just type startx. And that is it. I hope this tutorial has been useful in helping you get started with Arch Linux.

How to install Genymotion on Arch Linux

It’s been quite a while since I’ve updated this blog last since I’ve been quite busy on a project I’ve been working on so yes expect this blog to be inconsistent. Anyways I have a new video on installing Genymotion on Arch Linux.

The Android emulator that comes with the Android SDK is a little bit slow even on fast computers. Genymotion is a fast Android emulator so it’s a good alternative. It has plugins for Android Studio and Eclipse IDEs.

The video shows how to install Genymotion on Arch Linux, a rolling Linux distribution. It shows how to get and install the package from the AUR (Arch User Repository). Genymotion depends on VirtualBox in launching virtual devices. However, on Arch Linux some of the required modules in Genymotion and VirtualBox are not loaded automatically so in the video I also show how to install VirtualBox and load the required modules (More info on VirtualBox can be found at the wiki page).