Tag Archives: Debian

Matters relating to the Debian Linux operating system

Setting up a Raspberry Pi Zero W

Here on GeoThread, we have taken delivery of one of the new Raspberry Pi Zeros for some projects – so here is a blog on how we set it all up for some of the work we get up to at Cranfield University. We’ve had quite a few projects with these devices over the years – reported here on GeoThread, starting back in 2016 with the Raspberry Pi 3, with various tutorials presented. The Raspberry Pi Zero is the latest in the line of these excellent, inexpensive microcomputers, see https://www.raspberrypi.org/products/raspberry-pi-zero/. Following on from the Pi A and B series devices, the Zero is a great entry level machine for learning coding and software development. We have a Raspberry Pi Zero W – the ‘W’ meaning it also has wireless and Bluetooth connectivity.

To get us going, we bought a kit that contained not only the Pi, but also an acrylic case and a power supply. There are many such kits available – we selected the Vilros offering. We also bought the new Raspberry Pi Camera v2 to fit in the case.

Raspberry Pi Zero, Vilros Starter Kit

Note that we have also bought (shown at the bottom centre), an HDMI Mini Type C (male) to HDMI Normal Type A (female) Monitor Cable Display Adapter – the Raspberry Pi Zero only has a HDMI Mini socket. In addition, you may also need to buy a single (or dual) Micro USB Male To USB 2.0 Female OTG (USB On the Go) cable for connecting a USB keyboard (or a USB keyboard and mouse). The HDMI and USB cables/adapters are to be used to get the unit up and running in the first instance, using a keyboard, mouse and monitor. Once the configuration is complete and the WiFi is connected, we will run the Raspberry Pi remotely and so disconnect the keyboard and screen.

We also bought one of the new ‘version 2’ cameras for the Raspberry Pi. This is an 8MPixel camera made by Sony, and a huge improvement over the original camera. It can fit into the case. The case has three alternative lids – shown here is the version with the camera mounting and aperture built in.

Raspberry Pi Zero and unboxing the camera v2

One thing to note immediately is that the Raspberry Pi Zero has a different camera cable connector size to the earlier models of the Pi. This means that you will need to obtain a new ribbon cable connector to replace the one provided. You can easily buy these separately, but the Vilros kit (like other kits) thoughtfully provide this.

Raspberry Pi Zero Camera v2 and case

Raspberry Pi Zero camera socket

The camera fixing has a small plastic bar that can be gently prised outwards to allow the old connector ribbon cable to be slipped out. The new cable can then be slotted in and the plastic bar pushed back into place to secure it. Be sure to insert the cable the right way up to allow contact.This is a delicate operation, but the cable is reasonably robust.

Raspberry Pi Zero Camera v2 ribbon cables

In the image above, the old connector has been removed, and the new one inserted. Note the new connector is very short, allowing it to be fitted inside the case. We are nearly ready to assemble the parts together in the case.

Raspberry Pi Zero Case Camera v2 – ready for fitting

The camera, with its new connector, can now be fitted to the Raspberry Pi Zero. The Zero has another plastic clip for slotting in the ribbon cable. As before, prise away the locking bar and gently insert the cable. When tightly fitted (the right way up to allow contact again), the bar can be pushed back in to grip the cable in place.

Raspberry Pi Zero Case Camera v2 – fitting the camera into the case

Once fitted, the case can be put together. Note that the Vilros kit comes with a small heatsink that can be fitted to the Pi to cool the 1GHz ARM CPU chip. However, with the camera fitted in the case, there is no room for the heatsink also. Time will tell if that is an issue!

Raspberry Pi Zero Case and Camera v2, fully Assembled

The Vilros kit comes with the power supply, fitted with the customary MicroUSB plug.

Raspberry Pi Zero Power Supply

Note that the power socket is the socket to the right, as shown.

Raspberry Pi Zero sockets

Before the case can be clicked into place, there is one last thing needed – the operating system. This is installed on an SD card.
We followed the very clear instructions on the Raspberry Pi website, here https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up/3. We downloaded the latest Raspbian img file and flashed it onto a new Micro-SD card. To do this on a MacBook Pro, we used the Etcher app (v1.4.4).

SD card Flashing with Etcher

Once the Micro SD card was flashed (e.g. the img file was copied onto it with Etcher), we could insert it carefully into the SD socket on the Raspberry Pi Zero. At this point the case was all clipped together.
We are now ready for the next stage of the project!

Cookbook – Building a LAMP Server on the Raspberry Pi computer

Raspberry_Pi_LogoPurpose: In this project we wanted to use one of the amazing Raspberry Pi computers (http://www.raspberrypi.org/) to build a fully functional ‘LAMP’ environment – that is ‘Linux’ ‘Apache’ ‘MySQL’ and ‘PHP’. On this Cranfield University site, we describe how we did it. Only the basic commands are placed here, see the links to other websites for extra explanations and post-install configurations etc.

Obtaining a Raspberry Pi

We suggest buying a Raspberry Pi with a case and power supply, as well as SDHC card with Raspian OS. However, for experimentation purposes, you may also wish to buy another 4Gb SDHC card (for compatible types, see http://elinux.org/RPi_VerifiedPeripherals before purchase). Then you can simply swap cards to try our different configurations (remember, always shut the Pi down before removing the card). You will also need to obtain an Ethernet cable, an HDMI to HDMI cable, a wired USB keyboard and mouse, and a TV that accepts HDMI input. We are also assuming you have a home router with Internet access that the Ethernet cable will plug into.

The initial configuration is done sitting in front of the plugged-in TV, but once ‘SSH’ (and potentially ‘TightVNC’) are installed, the Pi can be left plugged into the router and then be accessed remotely via a separate laptop, connected say wirelessly to the router, either via a secure shell ‘SSH’ client such as ‘Putty’ (http://www.chiark.greenend.org.uk/~sgtatham/putty/), or the graphical ‘TightVNC’ client (http://www.tightvnc.com/). These latter tools need to be installed on the separate computer/laptop first. Do I need TightVNC? TightVNC will be useful if you intend to run graphical programmes off the Pi – however, you don’t need it if you are configuring the Pi just as a server (e.g. web server or database server). In which case Putty will be fine.

New SDHC Card image

If you buy a pre-loaded Raspian card, you don’t need this step, but if you did start with a blank SDHC card these are the steps you need. Take a new SDHC – fat32, 4Gb empty card – formatted if not new. Note, it is best to use an external SDHC USB reader/writer device, rather than the slot built into your laptop if your laptop is not pretty newish.

To format SDHC cards, first download the utility ‘SDformatter’ (https://www.sdcard.org/downloads/formatter_3/). Select ‘Options’ -> ‘format size adjustment on’. Use this tool, not the Windows format programme.

For advice on easy set up of the card, see http://elinux.org/RPi_Easy_SD_Card_Setup.

Next, download the latest Pi Operating System, called Raspian ‘Wheezy’, from http://www.raspberrypi.org/downloads.

If you download a windows file, you can unzip the contents to reveal the ‘.img’ imagefile.

Next, to write the image to the blank card, use the SDHC Image writer from https://launchpad.net/win32-image-writer -(now at)> http://sourceforge.net/projects/win32diskimager/

Other scary options that some people use include the ‘flashnul-1rc1′ or the ‘fedora-arm-installer-1.0.3-7-x32’ tools. The ‘flashnul-1rc1’ in particular needs especial care and attention (being in Russian comrade!)

Once Raspian is unpacked some files will be visible on the card in Windows – but don’t edit them in windows! Some files are hidden to Windows so it doesn’t look like a full card. Insert the card then into the Pi and bootup. This should start up and kick off raspi-config. The default account/password is:

User: pi                 Password: raspberry

You should be running in terminal mode, type ‘startx’ to kick off the graphical user window environment (this is something a lot of guides seem to fail to mention!)

startx

Spend a little time now exploring the graphical interface. The menus and system options are available from a drop down menu in the top left corner. Note the very first time you run, you may be presented with the system configuration tool ‘raspi-config’, explained below.

The rest of the article assumes an Ethernet cable connected to the Internet is plugged in. Note there are wireless USB dongles that should work with the Pi ‘out of the box’, instructions for configuring that are elsewhere here on Geothread – see http://www.geothread.net/cookbook-configuring-wifi-on-raspberry-pi/.

Basic Configuration

You may find you are either running raspi-config immediately, or else this useful tool can be started at the command line withn the command ‘sudo raspi-config’. Note many of the commands in this tutorial are preceded by ‘sudo’ – meaning run the command with root ‘superuser’ authority.

Once running raspi-config, you can select from a few useful options from its menu, thus:

expand_rootfs

This option ‘inflates’ the filesystem to fill SD card. This makes the whole capacity of the SDHC card available for storage – a good idea!

ssh server – Enable

The ssh ‘secure shell’ allows programmes like ‘putty’ on a PC to run a terminal session onto the Pi. However to do this the ssh server needs to be running and enabled on the Pi.

update

Also select the option ‘update’ to update the raspi-config itself.

Once you ‘Finish’, you will pass back to the command line.

Next, you must update the core operating system files. You enter:

sudo dpkg-reconfigure tzdata 
sudo apt-get update 
sudo apt-get upgrade 
sudo apt-get dist-upgrade

This updates the system software – if the Wheezy image was up to date this shouldn’t take too long. If it wasn’t time to make a cup of tea! Open source software gets updated FAR more frequently that proprietary software, so you will need to run the last three commands from time to time to keep the Pi up to date.

To be able to access the Pi remotely, install the TightVNC server if required. This gives a graphical ‘X-Windows’ GUI onto Raspberry from remote laptop/PC/Mac. Note you also need to install TightVNC client on your separate laptop/computer that will be used to access the Pi.

sudo apt-get install tightvncserver

Following the excellent instructions at http://www.neil-black.co.uk/raspberry-pi-beginners-guide#.UVWzqBfIbJY, or the more recent update at http://www.neil-black.co.uk/the-updated-raspberry-pi-beginners-guide#.VFPzeL5urww, next you need to configure the tightvncserver. Create a new file called ‘tightvncserver’ in the init.d directory (nano is a text editor programme):

sudo nano /etc/init.d/tightvncserver

Into this file, enter the following:

#!/bin/sh 
# /etc/init.d/tightvncserver 
VNCUSER='pi' 
case "$1" in 
    start) 
        su $VNCUSER -c '/usr/bin/tightvncserver :1' 
        echo "Starting TightVNC Server for $VNCUSER " 
        ;; 
    stop) 
        pkill Xtightvnc 
        echo "TightVNC Server stopped" 
        ;; 
    *) 
        echo "Usage: /etc/init.d/tightvncserver {start|stop}" 
        exit 1 
        ;; 
esac 
exit 0

Give the script executable permission:

sudo chmod 755 /etc/init.d/tightvncserver

Now start or stop the service manually thus (this is the basic model used for dealing with all Debian processes).

sudo /etc/init.d/tightvncserver start
sudo /etc/init.d/tightvncserver stop

Make the TightVNC server start every time the Raspberry Pi starts.

sudo update-rc.d tightvncserver defaults99

Setting an IP Address

By default the Pi uses Dynamic Host Control Protocol (DHCP) meaning it may get a new ‘IP’ address each time it boots up. If we are using the Pi as a web server/database server this could be a nuisance. If so, we can force the Pi to used a ‘fixed IP’ address so it always remains the same.

Following the excellent instructions at http://www.neil-black.co.uk/raspberry-pi-beginners-guide#.UVWzqBfIbJY, set the IP address from DHCP to fixed. This will make it easier to locate your webserver later.

First, identify the current computer network ‘IP’ address, type:

ifconfig

On a home network, it is likely to be something like 192.168.1.xx. We will fix the Pi as ‘192.168.1.100’. Edit the network interfaces configuration file with the text editor ‘nano’, thus:

sudo nano /etc/network/interfaces

Replace the lines

iface lo inet loopback 
iface eth0 inet dhcp

With

iface lo inet loopback 
iface eth0 inet static 
address 192.168.1.100 
netmask 255.255.255.0 
gateway 192.168.1.1

Note we used 192.168.1.100  (this being a private network subnet address)

See also http://my-music.mine.nu/images/rpi_raspianwheezy_setup.pdf for more guidance.

Now reboot the Pi. To do this, type in:

sudo shutdown -r now

This reboots the Pi ‘now’ and then hopefully you can log on again, but this time potentially as a remote session with SSH or TightVNC from your laptop (where the laptop has a wireless connection to the router). Note the IP address is now fixed as noted above. If you have a laptop/PC on the same home network, you can open a command prompt and run ‘ping 192.168.1.100’ to see if you receive a response from the Pi.

If you use tightvnc, rather than ssh, then on your laptop/pc vncclient installation, you need to enter in the address of the Pi for the connection, e.g. 192.168.1.100:1. Don’t omit the ‘:1’ at the end of the address!! You can also explore the options to set the screen size ‘geometry’ to 1024×728 with a colour depth of 24bits.

Linux Commands

You need to be familiar with Linux commands. Learning Linux can be daunting – some good documentation is here http://www.debian.org/doc/#manuals – best is the one page reference card here http://www.debian.org/doc/#other.

In the next steps, now the basics are in place, we proceed to the installtions of the web and database server software. See also http://www.wikihow.com/Make-a-Raspberry-Pi-Web-Server

Apache Web Server and PHP

To install the Apache web server and PHP (find out more here http://www.apache.org/ and here http://php.net/), type:

sudo apt-get install apache2 php5 libapache2-mod-php5

To ‘restart’, ‘stop’ or ‘start’ apache, use the following command (varying the last word)

sudo service apache2 restart

Note the Apache webroot location for your webfiles is ‘/var/www

The Apache log files are put in ‘/var/log/apache2

MySQL Database Server

sudo apt-get install mysql-server mysql-client php5-mysql

You need to enter in a database system password, such as ‘raspberry’! Once installed restart and you can hopefully access the MySQL command line prompt thus:

sudo service mysql restart 
mysql  -u pi –p

Check the interactive logon works as user pi. Try and ‘use’ a database and select records:

use mysql 
select * from users;

Use <ctrl>+c to quit

For logging, see http://serverfault.com/questions/71071/how-to-enable-mysql-logging

PHP

PHP should have been installed also. In the /var/www webroot, create a new file called testphp.php with the ‘nano’ editor, and enter the following. Once created, run the file in the your laptop/pc browser to see the Pi PHP status:

sudo nano /var/www/testphp.php

Enter the text oneliner below into the file.

<?php phpinfo(); ?>

Your laptop/pc web browser ‘should be able to see this web page served up. Enter the IP address we set earlier, plus this file, thus:

In web browser, enter the URL http://192.168.1.100/testphp.php

Hopefully you should see the PHP status page appear.

Mysqli

In PHP you should use the recommended MySQLi command extension to access the MySQL database. It ‘should’ be installed already – but look down the phpinfo() web page above to find it and ensure it is shown as ‘enabled’.

To learn more, see http://codular.com/php-mysqli         (but do check the phpinfo() output above first)

Phpmyadmin

To manage the MySQL database, we recommend the excellent ‘phpmyadmin’ MySQL web management console. All the MySQL administration can then be undertaken through a web page.

See http://www.dingleberrypi.com/2012/09/tutorial-install-phpmyadmin-on-your-raspberry-pi/

apt-get install phpmyadmin

Select ‘apache2’ as the web server.

Create a phpmyadmin password, such as ‘raspberry’ (or whatever – but don’t forget it!)

You should now be able to call up the phpmyadmion home page at:

http://192.168.1.100/phpmyadmin – login as root/raspberry

Note user privileges to the MySQL database are all managed via phpmyadmin. Each user account needs to have specified the host from which the user will come to access the database. The ‘%’ (all servers) option didn’t work for us and so we needed to repeat a configuration for servers: ‘127.0.0.1’, ‘192.168.1.100’, ‘localhost’ and ‘raspberry pi’. A bit fiddly – essentially you open up the privileges for the user and duplicate the settings varying the host each time. To help with this, note that once phpmyadmin is installed, one can see the various options the phpmyadmin user has pre-assigned. Note that connecting to MySQL from the SSH terminal uses ‘localhost’, whereas connecting from a webserver can use ‘127.0.0.1’ – so the source varies according to source. One needs to experiment – hmm!

Remember later that if you ever receive a ‘500’ error on the webserver when trying to access the database, it is likely due to the incorrect privileges. You may need to look at the webserver apache logfiles in a new window (‘/var/log/apache2’) to actually see the error:

sudo more /var/log/apache

or better still, to see only new lines as they appear in the logfile:

sudo tail –f /var/log/apache2

VSFTP

We need the means to get web pages we write off the laptop/pc onto the Pi webserver. For this, we need an ftp daemon running to upload files onto Pi – we suggest using vsftp, it is the best (but not the only) ftp server for debian.

sudo apt-get install vsftpd

Based on the instructions at http://www.instructables.com/id/Raspberry-Pi-Web-Server/step9/Install-an-FTP-server/, now edit the vsftp configuration file.

sudo nano /etc/vsftpd.conf

Search down through the file and change the following lines:
anonymous_enable=YES Change To anonymous_enable=NO
#local_enable=YES Change To local_enable=YES
#write_enable=YES Change To write_enable=YES

Also, add a line to the bottom of the file:
force_dot_files=YES
Quit the editor and restart the vsftp server.

sudo service vsftpd restart

We suggest NOT following the additional steps in some guides for messing (post install) with pi account line in passwd file etc. We trashed an installation doing this. You ‘should’ be able to just sudo apt-get the vsftp server and reboot – as we did second time round. Make the minor fixes to vsftp configuration file described above. But we suggest to do these edits and nothing else.

On your laptop/pc, you need an ‘ftp’ client . We suggest the freely available ‘filezilla’ ftp client (https://filezilla-project.org) for transferring files across by ftp. ftp://192.168.1.1 and Login to the Pi as user pi. Save the configuration to access the Pi to facilitate future access.

Web files

Set webroot file privileges

sudo chown -R pi /var/www

Now a set of website files can be copied from the PC/laptop with filezilla to this folder and accessed from the laptop/pc webbrowser thus: http://192.168.1.100

Disk capacity free

Here’s how you can establish how much disk space is left over. After all of the above we had used about 1.8Gb on the 4Gb SDHC card.

df –h

Shutting Rasperry Pi down

sudo shutdown –h now

To reboot/restart it when running

sudo shutdown –r now

Epilogue

The Raspberry Pi is an excellent training tool for LAMP installations and can even be serviceable for light operational tasks. It is an educational tool though and a really great way to learn Linux, Apache, PHP, MySQL etc… One great advantage is the SDHC card filesystem. You can keep several installations and configurations on different cards and, as long as machine is correctly powered down, swap cards and reboot as required.

Backing up these SDHC cards is not easy and not always predictable. Tools like the win32diskimager and flashnul-1rc1 can image a card to a ‘.img’ file, but the file may a. not fit back onto a formatted card of same capacity, and b. not work anyway after reboot. Googling shows there are a lot of approaches taken – which is best though isn’t clear! Have fun!