Building A Web Server With A Raspberry Pi


It’s was about time I had a Linux machine at home and with the Raspberry Pi I had a perfect opportunity to get a cheap stand alone Linux build.

I’m going to go through setting up the Pi as a local web server for a PHP / MySQL development environment with support for SSH and VNC to remove the need for an extra monitor, keyboard and mouse (or an expensive KVM), as well as an FTP server so changes made on my development clients can automatically update the files on the Pi.

The Bits

By the time my Pi arrived from RS Online it seemed to be available everywhere but at least they sent the 512Mb RAM revision 2 board. The completed server cost around £50:

Item Price
Raspberry PI Box Transparent (Clear) £5.69
Raspberry Pi Type B Single Board Computer 512Mb £30.87
Genuine HTC TC-B250 Uk Mains Charger £3.49
Transcend – Flash memory card – 32 GB – Class 10 – SDHC £15.50
Total £55.55

The Pi is connected to a network switch and power only once setup, however I used a monitor through the HDMI and a USB keyboard and mouse during setup. I found the case on Amazon and was impressed by it’s quality, snug fit and strong with vents for cooling, the item is still available at time of writing here.

Flashing The SD

Needing only simple services and wanting a simple install I went for the standard Soft-float Debian “wheezy” from the Raspberry Pi site, this was unzipped and the .img file burned using the recommended Win32 Image Writer. There is a full guide over on eLinux.org covering flashing for the Pi however this is a simple process of unzip and burn.

Initial Boot

On inital boot a simple menu with initial options is presented, here you can edit some simple system settings, enable ssh and desktop on boot (which you will want to do), set your password (which you will also want to do) as well as update the config utility itself. Having gone for a large SD I used the option to expand the disk to the full capacity too. No drivers were needed for the keyboard / mouse or ethernet connection (with an active internet connection), if you don’t go for the desktop on boot option than you can simply enter startx from the prompt to launch the LXDE GUI, to restart manually use sudo shutdown -r now. It is worth noting that some of the options from the utility will be followed with a little bit of lag (during which the cursor flashes at the bottom of the screen in most cases) before coming up with the next config screen.

Static IP

For the Pi to be useful as a server the device needs to use a static IP, this can be done with a series of ifconfig commands or more easily by rewriting the /etc/network/interfaces config file to the following config set (replace gateway IP with that of your router / set the static IP to whatever you like, this will be used to connect to the device). Editing can be done using nano or classic vi which are pre-installed

Once that is done, restart the Pi again and we will now be static.

VNC

tightvncserver is a free and easy to use VNC server service which will allow for full remote desktop. To install:

Once installed, new VNC servers can be launched and killed with:

The DISPLAY value is a simple integer that allows you to separate out and set options for multiple shared desktops, use the –help flag or man to get a full list of options. But make sure to set geometry (the resolution) bearing in mind that this will also have an interface around when viewed through a client in windowed mode. I initially set mine to full 1920×1080 then realised this was less than useful as I rarely run the client at full screen (although it looks nice / works perfectly).

For the client I used tightVNC in Windows, when connecting use the IP address and display value like a port number, e.g. 192.168.1.101:1.

When looking into getting the VNC service to launch on boot the only solutions I seemed to find were quite hacky (due to x-windows / login generally being required before the service is started), I will most likely look into this issue again in the future and post whatever solution I end up with, but I have left this for now.

SSH

Assuming SSH was enabled from the inital boot utility then you will be able to connect from clients already. Simply use ssh in Linux or Putty in Windows. Connect to the IP set earlier in the interfaces config file, using the user pi and your password.

Additionally with SSH enabled you can now use your IDE or editor of choice on client machines an upload / sync to the development Pi using SFTP, again using the static IP set on port 22 with the credentials for your account on the Pi.

Web Server

Let’s install some standard stuff using apt, use the install command for each package listed below or space delimit them into a single command. Before getting started it’s also a good idea to run an auto update using apt too.

  • apache2
  • php5
  • php-pear
  • mysql-server

Now we have a basic version of a LAMP (Linux, Apache, MySQL, PHP) stack in place with a webroot of /var/www in your file system.

Once you’ve finished installing new packages (and configured a root password for MySQL), it’s a good idea to also do a quick autoclean:

Composer

For dependency management I am going to use the web development industries latest favourite toy (and with good reason) composer. Create a directory and then from the directory in terminal use:

And as a bonus add the following line to ~/.bashrc:

Now composer can be invoked from terminal simply using composer

Virtual Host

To help make our development server more useful, let’s set up a virtual host example to allow access to different ‘sites’ both on the Pi and on other machine on the local network (e.g. my windows machine where I will be using Zend Studio).

First setup a folder in your webroot (/var/www/) for your project, then backup the content of /etc/apache2/sites-enabled, then remove the contents and create a new file with a name relevant to your project, edit this with:

Replace the hostname and directory with the correct values for your site / project, I normally tend to use .dev hostname TLDs. I am using the default log locations here which can also be changed for individual projects to give better error and audit log trails.

Additionally you will need to add the site to your hosts file, open up the file /etc/hosts/ and add the line:

Do the same on clients, replacing the IP address with the static address setup for the Pi, (in windows the hosts file is hidden away in /windows/system32/drivers/etc/hosts, note that you may need to restart your networking. You can check that the DNS entry has been setup correctly with a simple ping on the hostname which should now resolve to the correct IP address.

Git

For version control I’m going to utilise Git on client machines as well as Github. Git itself can be downloaded for most platforms from the Git downloads page, once installed (make sure to include the bundled Bash goodness) for basic setup just set your name and email address for identification (as per the GitHub setup guide):

Remember that these settings will be visible on GitHub so in some cases you may want to use a dummy email address. Now you can clone / push / pull from GitHub as well as any other public Git repositories.

Summary

With all of the above I now have a nice local dev server allowing for simple staging within a pretty standard environment, with composer setup starting new projects (or just poking around any given framework becomes a breeze see my gist for composer syntax) and with SSH / SFTP / VNC / Git support we have the basic tools we need for administration and use.

About Ingmar Boddington

Codemonkey, Sheffield, UK
This entry was posted in Linux and tagged apache2, composer, Debian, git, MySQL, PHP, Raspberry Pi, SSH, VNC. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">