[Site Logo]

David's Pi Webserver Site

Using the Raspberry Pi as a Webserver

Setting Up the Raspberry Pi as a Webserver

This year (2016), I was given a Pi Zero for a Father's Day present (thanks, Pete!). So the following is a tidy-up of the notes I made whilst setting it up as a webserver.

What Sort of Pi?

Before we start, it might be worth considering what sort of Pi one should use. The Pi3 is excellent, and there's no reason why one shouldn't be able to use that to deliver whatever one wants from a webserver. But this website is running on an original Pi1, i.e. one of the very first Pis ever to be made. Does that make sense? Maybe not if you are expecting a lot of traffic (I mean thousands of hits per hour), but it makes huge sense when you realise we were all running our websites on pretty basic PCs in the 1990s, and that was never a problem.

What about the Pi Zero? Well, if the original Pi can cope with running multiple websites (this one is running three sites concurrently), then the Pi Zero should certainly have sufficient power to run multiple websites. Its main problem is that is doesn't come with networking capability out of the box - that's a pretty important requirement for a webserver! But you can buy a wifi adapter for £1.26 (as of 21 July 2016), so adding that in to the cost of a Zero isn't a big deal. So for the cost of a Pi Zero plus wifi adapter, you can have your very own fully-featured webserver. Smart!

Setting Up the Raspberry Pi

As delievered, the Pi doesn't have an SD card and it doesn't have an operating system. So we need to set that all up:

  1. You'll need to get an SD card. There's no point in getting much bigger than 8Gb; 4Gb will do, apparently. You can pop round the shops and be fleeced, or go on eBay and get one delivered within a day or two.
  2. Now you need a copy of the Pi operating system. You can download this from the Raspberry Pi downloads page:
  3. https://www.raspberry file pi.org/downloads/

    There's several operating system images to choose from. It's up to you, but I like to run the latest copy of Raspbian.
  4. Make sure the file has downloaded correctly, then:
    • unzip the download file, and
    • write the Raspbian image to your SD card.
    The instructions for doing this are all on the Raspberry Pi website:

    https://www.raspberrypi.org/documentation/installation/installing-images/README.md

  5. Now boot up the Pi, check it all works OK, then plug in the wireless dongle and set up your wireless connection.

Making Your Pi Secure

From here on, there is an excellent page outlining what you need to know: Creating a LAMP server with the Raspberry Pi. I'm not in the business of re-inventing the wheel, so I'm not going to try and "improve" on Stewart Watkiss' excellent work. But what I will do is note what *I* actually did next.

  1. It's very important to change the default admin password! We're about to put our Pi online, and the number one way in for hackers is to look for admin passwords that haven't been changed. I did more than Stewart Watkiss has on his sheet. First of all, I did a "sudo su" to become root. I then did a "passwd" to change the root password. The textbooks all say that you must never write down your password. The only way I can cope is to write down the password I chose - and keep the paper safe!
  2. Next, I did a "passwd pi" to change pi's password. (And wrote it down on the same sheet of paper)
  3. Then I added a user and set a password as in Stewart's notes:
    • useradd -m myuser
    • passwd myuser
    I wrote the new username and password down on the same sheet of paper.
  4. Still following Stewart's notes, I:
    • added my new user to all the same groups as "pi" (in /etc/group) and
    • changed the shell for "myuser"
  5. My VirginMedia router doesn't change my devices' IP addresses, so I skipped the bit about setting a static address.
  6. Stewart's notes say that SSH should be enabled by default. I checked on mine, and it was.

Setting up Your Pi as a Webserver

The next bit is pretty easy! We need to install the software to run our website(s). That requires Apache (the webserver software), MySQL (the database software - yes, we'll be able to do serious webserving with our Pi!), and PHP (web scripting software).

We need to install:

  1. Apache2: "sudo apt-get install apache2"
  2. MySQL: "sudo apt-get install mysql-server"
  3. PHP: "sudo apt-get install php5"
  4. In passing, you might want to know that the latest version of PHP is version 7 (point something or other), but the Raspberry Pi only supports version 5. That's a bit of a pain, but not the end of the world.
  5. Note: I think there's a missing step here. I think it might be necessary to install the bit of PHP that talks to MySQL: "sudo apt-get install php5-mysql"

You can now do the test and check that everything is working fine on your Pi webserver.

When you want to start sorting out an actual website, you'll need to know that the Pi stores its web pages in /var/www/html/ . That area all requires root permission to write anything, so you'll need to deal with that. Personally, I'd be inclined to change the ownership of /var/www/html/ to "myuser" ("chown myuser /var/www/html") - but only if you've made sure you've done the group permissions step above.

Making Your Server Visible on the Internet

Stewart Watkiss also includes notes on how to make your server visible over the Internet. The exact method will vary from router to router, but basically, it's a simple step of setting up your router to do "port forwarding". You need find the internal IP address of your Pi (it will be something like 192.168.0.xx) and then tell your router to redirect any requests for port 80 (the HTTP port) to your Pi. Look at Stewart's notes for more detail.

At this stage, you'll have a webserver that is visible on the Internet via your router's IP address (i.e. something like 82.165.13.42). If you're willing to pay real money, the next step is to spend about £7 once every two years to get a domain name like "argles.org.uk" and to get the Internet's DNSs to redirect that to your IP address. There's a number of providers out there, some who are more scrupulous than others. Personally, I use "1and1" (i.e. one and one) and have always found them helpful, but you may have other companies you prefer.

Happy web serving!