Raspbian Preferences Menu Location

Raspbian Preferences Menu Location

I touched on the fact that the Raspberry Pi is capable of so many different things in my first article. Today I’m going to go through setting it up for something dear to my heart: something called a LAMP.

LAMP is an acronym for Linux, Apache, MySQL, and PHP. I’m not going to assume that you have any knowledge of web development, Linux, or how servers operate.

I’ve been a web developer for a number of years now and when SCIFI.radio asked me to be the official technical director I jumped at the chance! How could I not, when it combines the two things I’m extremely passionate about? My duties are essentially to keep the website running and fix any issues that may occur.

I’m explaining this for a good reason; as a developer of any sort, testing is key. It wouldn’t look very good if I pushed changes to the server and it broke things right away! Enter what is known as a “dry server” or “testing server”.

This is basically a web server installed on a local computer and it mimics your production environment as closely as possible. It’s important to have something like this so that I can go through multiple code revisions without breaking something, or test out new ideas without worrying about draining the server’s resources. I wanted to see if I could create a full Linux based development environment using this small computer.

The first step is to make sure that secure shell access – also known as SSH – is set up on the Pi running Raspbian. This allows us to connect to it from another computer in our local network. Load up your MicroSD card with Raspbian, plug in the Pi, and follow along!

There are many different configurations for the Pi, but by default the RPi3 boots directly into desktop mode. This will make things easy for those that might not know the command line very well. For this tutorial I’m going to assume that you have the RPi hooked up to a display, and that you have a keyboard and mouse attached. These are needed for at least the first step.

Raspberry Pi Configuration Screen

Raspberry Pi Graphical Configuration Screen

Once your Pi has finished booting to the desktop you want to open the main menu, find preferences, and then “Raspberry Pi Configuration”. Alternatively you can open a terminal window and type in:

sudo raspi-config

This will open the command line version of the configuration tool.

If you’re using the desktop method, click on the interfaces tab, or in the command line use the arrow keys to select Advanced Options. In both modes you’ll see a list of options, one of which will be titled SSH. In desktop mode make sure that Enabled is checked, and in terminal mode use the arrow keys to select SSH. The command line tool will ask if you want the SSH server to be enabled: choose yes, and it will confirm that SSH is enabled. Clicking OK in the desktop tool has the same effect, but without a confirmation. You might need to reboot the Pi if the SSH server wasn’t already enabled.

Now that our server is enabled, we need to get the IP address. The easiest way to get this is through the terminal. Many people prefer the desktop especially if they’re coming from Windows, but the Linux Command Line Interface (or CLI for short) is extremely handy to learn for a number of reasons especially if you’re looking at becoming a web developer. Time to open up our trusty terminal and type the following:

ifconfig

This will show us all our network devices and the IP addresses that they’ve been assigned by the router. It’s possible to configure the Pi to use a static IP address so that it never changes, but I won’t go into that in this article.

Depending on if you’ve plugged your RPi directly into your router, or if you’re using the RPi3’s built in WiFi controller, will depend on which one of these devices you want to look at. I’m connecting with WiFi, so I want to look at “wlan0”. If you’re using a network cable, you want to look at “eth0”. The address we’re looking for is the “inet addr”. On local networks this address will likely be similar to 192.168.x.x. In my case my RPi is 192.168.0.13, but yours might be different. The important thing is to write this address down as we’ll use it to connect to the RPi from another computer.

Bitvise SSH Client Configuration Screen

Bitvise SSH Client Configuration Screen

There are so many SSH client programs out there that I’m not even going to begin to tell you which one I think you should use. The one I use is named Bitvise. It’s free and offers a built-in FTP client which comes in really handy.

Download and install your client of choice. Now to enter the information to connect to the Pi! Every program is different, but should be somewhat similar. Under the “Host” box, we need to put in the inet address we wrote down earlier. Leave the port as 22; it’s the default for SSH servers.

The default user name on the RPi3 is “pi” and the default password is “raspberry” (without quotes!). Most programs I’ve used allow you to enter your information every time or save the username and password for later. With our information entered correctly we can click “Login”, and a new terminal window is opened for us.

Now that we can connect to our Pi, it’s time to start setting up the rest of the server. We can do this either with our newly set up SSH access or we can do it directly on the Pi itself. The commands will be the same either way, and you’ll quickly see why I said learning the CLI would be a good thing to do!

Unless you’re downloading a lot of things from Github or various other places on the internet, most of the time you install something on the Pi it is going to be via the command line. If you’ve already spent some time on Google looking up things to do with the Pi, you’ve likely already seen that almost every single tutorial out there uses the CLI at least at some point especially for installing software. You might have seen things like “sudo” and “apt-get”, or any other number of possibilities that might be confusing, especially if you’re not used to the command line or Linux in general. I’ll explain the commands we’ll be using, one of which I’ve already used in this article: the “sudo” command.

Think of “sudo” like running something as administrator on Windows. It essentially gives the command an elevated set of permissions, allowing it to do things like make changes to the system. Whenever we do something that requires something to be changed – editing configuration files, starting or stopping services, installing or removing software – we need to start out with the “sudo” command. It lets Raspbian know that it can make this change. If you want to learn more about the sudo command, you can visit Linux.com where they have a very nice introduction to it. The second command we’re going to use is “apt-get” – essentially telling Raspbian to download a piece of software and it’s dependencies, and then install it. The nice people over at TecMint have a great little article all about apt-get and it’s various uses.

So now we have our SSH server set up and can connect to it from another computer in our network, it’s time to start getting our local server up and running. The first step is installing the Apache web server. Apache is an open-source web server run by the Apache Software Foundation and is in use on many, many different servers today both public and private. To install Apache on our Pi, we simply run the following command:

sudo apt-get install apache2 -y

This tells Raspbian to download and install the Apache web server and by adding the -y to the end of it, we’re telling it that we’re allowing it to go ahead and proceed. Without the -y it will ask us before installing anything.

Apache2 Default Page on Raspbian

The Apache2 Default Page on Raspbian

You’ll see a number of lines giving you information about its process, from downloading, to installing, to enabling. Once it’s finished, we open up a web browser to see if everything worked properly. The web address you want to type into your address bar, is the same inet address that we wrote down earlier: 192.168.0.13 for example. You should see a page come up that has “Apache2 Debian Default Page” at the top, and a bunch of information below that that tells you about your Apache web server. Congratulations! Our second step is complete! This isn’t very useful so far, however. All that Apache by itself can display, is text and images. Basic HTML files, with no real functionality. And that’s where the rest of things come into play.

The first thing we need to do, is navigate through our file system to the directory in which Apache stores its default webpage. Still in our terminal, we use the “cd” command, which quite literally stands for “change directory”:

cd /var/www/html/

Once there, you can use the “list” command to see what files are in the directory. The command is:

ls -l

This shows you all the files and directories. Using the “-l” flag on the end allows us to see what permissions the file or directory has, who owns it, the file size, and the time it was last modified. We have one file in this directory right now, and it’s called “index.html”. This is owned by root, so we’ll use sudo if we want to edit it. Using the full command to open it in the Nano editor would be:

sudo nano index.html

There are two types of web development. The first is called “front end” and is often referred to as design. It’s the look and feel of a website; the HTML, CSS, and Javascript code that people interact with. The second type is “back-end” development, which usually involves using some type of scripting language such as PHP, Python, or Perl. Users don’t typically see the back-end aspect of things as it is what’s responsible for connecting to databases, or generating dynamic content for display. And this brings us to our next step: installing PHP. PHP is a wonderful web-based scripting language that is capable of many things, especially on the Raspberry Pi! This time we’re going to install multiple packages at once using the apt-get command, but first we need to make sure that the repository list is up to date. We do that with the command:

sudo apt-get update

It may take a few minutes to finish but after it has processed the repositories, we can use the command:

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

We’re installing both the “php5” package and the “libapache2-mod-php5” package. The first one is the PHP server itself, while the second is the Apache “plugin” that allows the two to talk to each other. Now we make sure the PHP server actually works! Lets make sure we’re in the /var/www/html/ directory and we’ll make a file called “phpinfo.php”. We’ll do this by opening the Nano editor, and specifying that file name instead of a file that already exists. The command is:

sudo nano phpinfo.php

This opens up a blank window that we can add some PHP code into. For our test we’ll use a built-in PHP function that displays information about the server, so we need to add the following code into our Nano editor:

<?php
phpinfo();
?>
The PHP Informational Page

The PHP Informational Page Running on the RPi3

Note that the brackets and semicolon at the end are very important, as are the opening and closing tags. Once you’ve added those lines, press “control+x” to exit Nano and it will ask you if you want to save the modified buffer. Press “y” for yes, and then enter to accept the file name, and it will dump you back to the command line.

Checking our files with “ls -l” again, will show us that we now have two files in the directory. To test the file, open up a browser and enter the address of your Pi again, but add “/phpinfo.php” to the end of it. It should look similar to “192.168.0.13/phpinfo.php”. If all has gone well, you should see a page show up that has a lot of information about the PHP server. You don’t have to worry about any of this at the moment; if the page shows up, the server is working! If you don’t see the information page, don’t worry quite yet. We can try restarting the Apache server with the following command in the terminal:

sudo service apache2 restart

A database of some kind goes hand-in-hand with most development, whether that database is a simple text file or a much more complex system using one of the many database servers out there. If you’re going to work with PHP in a web development setting, chances are you’ll also be working with MySQL. MySQL is a robust yet relatively light database server and is used in web servers worldwide. Whether it’s storing a complex interaction of user permissions, or storing simple things like a list of your Blu-ray collection, it’s something that every back-end developer eventually encounters.

Now we’re getting familiar with how to install software! We’re going to install two packages again: “mysql-server” and “php5-mysql”. Just like before, we use sudo and apt-get for this:

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

During this install process, it will show a screen asking you for a “root” password for your MySQL server. You can choose anything you want as your password, but make sure it’s something you’ll remember! You’ll need it for any administrative tasks that you’re going to do. It will ask you to enter it twice, just to make sure that you typed it correctly both times. Once it’s finished we need to restart the Apache server. This is to make sure that MySQL is available to the PHP and Apache servers.

sudo service apache2 restart

We now have a full web server set up that can serve web pages across our local network! You could create a tracking system for your movie collection. Test web pages you create before uploading them to their final server. A few things to note though. The first is there are many different ways you can configure Apache, that I didn’t even begin to touch on. Setting up multiple websites is one example of this.

You can change the PHP configuration for a development machine so that it shows you problems right on the page. Error reporting is disabled by default. It’s always helpful to look through the documentation for these programs, especially if you’re having issues with something. Any time you make a change to the configuration files you’ll need to restart the Apache server for the changes to take effect.

You can do so many things with this, even if it’s just setting one up to learn! The Raspberry Pi makes a powerful little web server for home or development use, and so many different things can be done with the servers that we installed today. I can use PHP to control the GPIO pins on the RPi. I can use Python and some smart-phone programming with MySQL to track people coming into my home. The possibilities are almost limitless, and defined mostly by your imagination, drive to complete them, and your desire to learn!

What will you do with your new Raspberry Pi server setup? Let us know in the comments!

-30-

Dan Vanderwood

Dan Vanderwood