Pi Web Server

Convert your  35$ Raspberry Pi  into a Web Server and host your own Web site

Strongly recommend using a Raspberry Pi 2 for this project. A Pi based webserver will work with the older Pi models but might be slow.

A note on Security  : Sharing a website / files on the Internet requires that you make your Pi Web server to be “Internet facing” – by forwarding port 80 from your router. But before doing so, you should research and be aware of the attendant security risks and some of the measures needed to address them. A foot note at the bottom of the guide will give you a few pointers.

Overview :

  • Download the latest Rasbian
  • Prepare a SD / Micro SD card to receive the downloaded Images.
  • Un compress and Transfer the downloaded Raspbian Image to the SD / Micro SD card.
  • Install Raspbian.
  • Configure Raspbian for a static network.
  • Install  Apache Web Server
  • Install PHP and Associated Packages
  • Install MySql
  • Configure your router to access your Webserver and a domain name that points to your webserver.

Time and Difficulty :

  • Time : Between 45 and 60 mins  once you have the Raspbian Image file and the rest of the software.
  • Newbie Difficulty level : Moderately easy ; Will need to use terminal and type in commands.

What you will need :

  • Raspberry Pi 2 Model B or one of its clones like Banana Pi M1 or M2 or Banana Pro
  • Ethernet connection or a supported USB Wifi adapter for the Pi
  • SD or Micro SD Card that fits into your Pi (4GB or above is recommended)
  • USB Keyboard and Mouse
  • Connection to a TV or Monitor

You should seriously consider :

  • Learning about SSH and how to remotely access the Pi from another machine on your network.
  • Learning about Port forwarding settings for your Router.

STEP 1 : Install Raspbian

  1. The easiest way to install Raspbian is goto the Raspberry Pi downloads page and and download Noobs.
  2.  Follow the steps described in the Noobs setup Guide to get your Rasbian running. In the initial setup page, Select Raspbian and avoid the rest of the options for this project. The default login and password are “pi‘ and “raspberry” respectively.
  3. Change the default password of the Pi during the first boot sequence in the Raspberry Pi configuration menu.  You can always go back to the Pi configuration menu by typing in ‘sudo raspi-config‘ at the command prompt.
  4.  Complete the Setup, restart the Pi with a ‘ sudo reboot‘ command.

 

STEP 2 : Give the Pi a static IP address

By default, the Pi is set to ask for a dynamic IP address from the router. You will need to change this setting to a fixed IP address since you will be hosting a webpage on the Pi and need the address to be fixed.

Assigning a Static IP address to the Pi can either be done at the Router level (which is fairly easy but the steps to do this are router’s model and brand specific) or at the Raspberry Pi level (outlined below).

Note :  replace the x in the lines below with what appears on your screen as you type in the commands.

Log on Raspbian and enter the following commands at the terminal :

  1. ifconfig” (ifconfig displays details of your current network connection )
  2. Write down the values next to inet add:192.168.x.xxx. These numbers is the IP address that your Pi is currently at.
  3. Also write down the Broadcast Range (Bcast) and Subnet Mask (Mask) that appear next to the inet add.
  4.  To get information from your router, type in “sudo route -n“.  This will give you the gateway and destination values.
  5. Write down the Gateway = 192.168.x.x & Destination = 192.168.x.x

We have now obtained all of the data that we need to setup our Raspberry Pi with a static IP address, it’s time to save it to a config file.

Type in “sudo nano /etc/network/interfaces”  ( nano is a text editor and will open the file interfaces located in the folder /etc/network)

In nano, look for the the line that reads “iface eth0 inet dhcp”. This line is telling the ethernet “eth0” networking interface to use “dhcp” (dynamic IP). Firstly, replace “dhcp or manual” with “static”.

Next , add the following lines directly below the line you just altered, with the data you had written down earlier.

address your.chosen.IP.address
netmask your.netmask
network  your.destination
broadcast your.broadcast.range
gateway your.gateway

The file should look something similar to the image below :

staticip

Save your file and exit out of nano by pressing ctrl+x

Reboot your Pi by typing in “sudo reboot

Once rebooted, your Pi will be on a static IP address. Crosscheck this by typing in “ifconfig” again.

 

STEP 3 : Install Apache, PHP and associated Libraries

For your Raspberry Pi to serve up websites, you will need to install a webserver.  Apache is a program that listens for server access requests from Internet browsers and grants them, if permitted. So if you want anyone to be able to access a website on your Raspberry Pi—including yourself—you need to install a Web server.

To install Apache, type in “sudo apt-get install apache2″

Type in the IP address of the Pi onto the browser of another computer on your network and you should see “It works!” The default landing page of Apache.

itworks

To allow your Apache server to process PHP files, you’ll need to install PHP5 and the PHP5 module for Apache.

At the Terminal, Enter “sudo apt-get install php5 libapache2-mod-php5 -y

 

Once all the software is installed, restart Apache by typing in “sudo service apache2 restart

Your tiny little Webserver is now humming!

 

 

The website is hosted at “/var/www/index.html” on your Pi.

You can edit the file or replace it with your own website.

STEP 4 : Pointing your Domain Name ; Handling Dynamic IP and giving access to the Pi from the world wide web.

Almost all ISPs provide a dynamic dns address to your home internet connection.  This is done for a variety of reasons – one of which is to prevent hackers from attacking a single address  consistently.

Now that you have added a host or domain into your account, we have to deal with the dynamic IP address problem. To make noip automatically update the IP address everytime your isp changes your IP address, you will need to install noip’s Dynamic Update Client.

To install the DUC on the Raspberry Pi, Enter  the following commands at the terminal :

  1. “sudo mkdir /home/pi/noip ” ( Create a folder for noip’s DUC)
  2. cd /home/pi/noip” (Change working folder to the new folder just created)
  3. sudo wget http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz” (Download the Software)
  4. sudo tar vzxf noip-duc-linux.tar.gz” (Extract the downloaded software)
  5. ls” (to show the newly folder created by extracting the archive)
  6. cd noip-2.1.9-1” (Change working directory to noip-2.1.9-1 or what ever is the newly created folder by the step above)

You will now need to compile and install the downloaded DUC  by entering :

  1. sudo make
  2. sudo make install (during the install process you will need to enter your email and password that you created at noip.com)

the Dynamic Update client is started by running  “sudo /usr/local/bin/noip2” .

The DUC service will now run until the Pi is shutdown.

To make the DUC startup automatically and run in the back ground, We will need write a small script that will execute at startup.

Recommend this excellent article on Techrepublic.

 

19 thoughts on “Pi Web Server”

    1. Setting up a wordpress site is fairly easy. The email server requires a bit more work by setting up a fqdn and a server. Will post setup guides for both these in a bit. cheers!

  1. Hey, nice tut… but i cant reach my site from outside my wlan over myhost.ddns.net, just within my wlan over my private ip? Any suggestions what might went wrong?

    Thanks in advance 😉

    1. Hey Malu. The router needs to be told that when a page is asked for at your ip address, it will need to send the request to your pi. That is, port 80 and 443 will need to be pointed the ipadress of your pi. It’s fairly simple and should only take a couple of minutes. Your routers documentation will have details of how to do this. Best of luck !

  2. Hey, i can call the website just via ip from within my wifi network, but not from another (mobile network e.g.) and neither from within my network via hostname.ddns.net.
    Any suggestions what could be the problem? I did everything according to your post…

    1. Hey Malu. Looks like all you need to do forward your port 80 on the router to your pi. That should fix it. Good luck!

  3. Hey, awesome tutorial, but I am getting stuck at step 6 of installing the DUC for NOIP.

    the ls command gives me this folder “noip-duc-linux.tar.gz” which is the file created during the extraction, however, it wont let me cd to it, saying “Not a directory”

    I need to navigate to this folder to compile and install the DUC.

    Any help is greatly appreciated .

    1. Hey Timo. It looks like a simple case of entering the correct folder name. You will need to extract the tar.gz. using the tar command and entering the exact file name. Use the ls command to list the directory and crosscheck file names. Once extracted, use the ls command again and make sure you are entering the correct folder name with the cd command. Best of luck !

      1. Haha sorry, yeah, I didn’t extract properly, so the pi wasn’t able to find the dir.
        Thanks alot man, this is an awesome resource.

    1. hey Timo. in general, TCP is used for webservers and UDP is used for streaming content like CCTV camera footage etc. I recommend both if you are unsure. If using SSL, you will need to forward port 443 as well. thanks for the upvote! Glad to be of help 🙂

  4. So after spending a lot of time wondering why I couldn’t connect to my shiny new host name from noip, I finally stumbled upon this page https://www.reddit.com/r/raspberry_pi/comments/2w1ixa/attempting_to_start_up_a_b_as_a_web_server/ . Apparently my problem was NAT reflection. Attempting to browse out to the internet and display a page that originated back behind my local router was confusing my hardware and made the page time out. Turning the Wi-Fi off on my phone and using 4G instead I could see the page externally, so it had been working the whole time.

  5. I like the way you explain things. Congratulations. No how can i call website from within network. I dont want to type ipaddress of rpi everytime i want to load it. It would be nice to call it by a name or hostname like http://myrpi.pi or something like that.
    Thanks

  6. Hi, when I go to edit my index.html files, it denies me access. I am using filezilla as there were a couple of tutorials already using filezilla but I haven’t had any luck so far. What do you recommend to use when editing/uploading a website to your web server. I also have CSS and a little bit of animation within my website – does this tutorial support all of this if executed correctly?
    Thanks heaps!!

    1. Hey. It looks like a permission issue. Make sure the settings for the website folder include read and write permissions for the user you are using to login with Filezilla. If you have read only, you will be able to view but not make any changes. Look up the chmod command to define permissions. And, yes, it should support CSS and animation.

  7. About forwarding port 80, when you are talking about security questions, do these only affect the Pi or any device connected to the same network?

    Thanks, the tutorial is pretty nice! 🙂

Leave a Reply