Raspberry Pi Web Server


If you do any serious web development using PHP and MySQL, you will eventually realize the need for a test server on your local network. For years I used a re-purposed PC running Ubuntu Server. I would code the pages using Visual Studio running on my Windows workstation and then upload them to my test server. I used MySQL Workbench to connect to the MySQL database running on the server. The old PC finally died and I was casting around for alternatives when I happened upon the chapter “The Pi as a Web Server” in the Raspberry Pi User Guide by Eben Upton and Gareth Halfacree.

If you are not familiar with the Raspberry Pi, briefly it is a tiny computer on a single circuit board that was developed in the UK as an educational tool to make learning about computers and programming affordable for schools, hobbyists and the “Maker” community.

Getting the hardware

Raspberry Pi’s are readily available on Amazon and eBay. I bought a Raspberry Pi 2, Model B packaged with a plastic case and a power supply for around $50 Canadian. This particular model has a quad-core 900 MHz processor, 1 GB of RAM, a full HDMI port, an Ethernet port and 4 USB ports. The other connectors on the board will not be used for this application. The power supply connects via a micro-USB plug much like a smart phone.

The Pi uses a micro-SD card to store the operating system, software and files. A minimum capacity of 4 GB is recommended. I purchased a blank 32 GB micro-SD card along with an SD adapter to connect to my PC.

To get up and running you will need a USB keyboard and mouse and an HDMI cable to connect to an HDMI port on a TV or an HDMI to DVI adapter cable to connect to a monitor. VGA is not supported, although you can buy an HDMI to VGA adapter, they are quite expensive. I bought an HDMI to DVI cable online for under $10. You will also need a network cable to connect to your router.

Installing the software

Some Raspberry Pi’s are sold bundled with the NOOBS installer utility preloaded on a micro-SD card. If you did not purchase a micro-SD card with the installer software preloaded, you will need to install the operating system on the micro-SD card. The card can be flashed, but the easiest method is to use the NOOBS installer utility. It is a free download. If you have a blank card you will have to format it first using SD Formatter, also a free download. Once your card is ready, the NOOBS utility can be unzipped and copied to it.

Insert your prepared micro-SD card into the port on the board. Connect your USB keyboard and mouse. Connect your TV or monitor to the HDMI port. Connect your Pi to your router with the network cable. During setup, the Pi will connect to the Internet to download updates. Later on you will need to set a static IP address for the Pi in its role as a web server, but for now it is best to make sure that DHCP is enabled on your router so the Pi can grab an IP address for immediate use. With all connections made, plug in the power supply and the Pi will start up.

The NOOBS setup utility will display on your screen offering you a choice of operating systems to install. For use as a web test server, Raspbian is best. This is a version of Debian Linux modified for the Raspberry Pi. The Pi will restart to the Raspbian desktop after installation of the operating system is complete. For use as a web test server a GUI consumes resources unnecessarily. It is best to change settings so that the Pi boots up to a command line. The rest of the setup is done from there. Raspbian’s command line uses the “apt-get” tool you will be familiar with if you are a Debian or Ubuntu user. If you need a tutorial on using the Linux command line and apt-get there are plenty available online.

Use apt-get to install Apache web server, MySQL and PHP. They will be downloaded automatically. Make sure to record your username and password for MySQL. You will need them later to connect to MySQL remotely via a GUI tool running on your workstation. There are plenty of these tools available as free downloads. At this point, after using ifconfig to find out the temporary IP address assigned to the Pi by DHCP, you should be able to view the Apache and PHP confirmation pages from a browser over the network, indicating that your web server is up and running.

Final steps

Your web server needs a host name and a fixed IP address. You will also have to set the time zone and locale. These tasks can be done from the command line using the built-in raspi-config utility and nano text editor. I turned off SSH as a security measure and since I did not plan to run commands remotely on the Pi.

The most time-consuming step for me was setting up a way to see files on the Pi over the network from my Windows workstation. I used apt-get to install Samba, which allows the creation of a “share” – a folder on the Pi that can be mapped as a network drive on your workstation. There are a few steps here and I went through a lot of trial and quite a bit of error. I created a Linux user matching my Windows username and password. I then created a Samba user with the same name and password. I gave the Linux user ownership of the /var/www/html folder where the Apache server keeps the web files. An smb.conf file was created using the nano text editor. This configuration file defines the Samba “share” that you want to access, in this case the /var/www/html folder. If all works correctly, when you try to access the mapped drive over the network, the credentials you used to log into Windows will be passed to the Pi, first as a Samba user and then as a Linux user and you will be able to add and delete files from the shared folder.

Once all the setup is complete the keyboard, mouse and monitor can be disconnected. The Pi draws so little power that I just leave it running. With some work and minimal investment I now have a simple, reliable test server with a tiny physical and power consumption footprint.