Introduction: Raspberry Pi - Web Server / Wireless Access Point (WAP)

What you need

  • Raspberry Pi model B+ (or B) with Power supply
  • Ethernet cable
  • WiFi adapter
  • SD Card 4GB + with Raspbian

Preparation

This tutorial completely assumes you have your Pi already set up and ready to go. If it is not ready/prepared please proceed to the following tutorial that can be found on the Raspberry Pi website.

Please make sure to expand the SD card, or you most likely will run out of space! To do this you need to firstly open Terminal on your Pi and type the following:

sudo raspi-config

Then hit enter. Upon doing so, so you will be given a list of different settings. Make sure you use the first option which is “Expand Filesystem”.

Next you want to completely shutdown your Raspberry Pi to plug in the WiFi dongle and the ethernet cable. To test that all is working, start up your Pi and type in:

ifconfig -a

If ‘wlan0’ is present as seen in the image below, everything is working as per usual.

Step 1: Software Installation

Installing Software

Now we are going to need to install the software to the Pi, we will do this through the following lines which should be inserted into Terminal:

sudo apt-get update
sudo apt-get install hostapd isc-dhcp-server

By updating everything we are able to get the most up to date software.

Step 2: DHCP Server Setup

DCHP Server Setup

The next step will require you to edit the DHCP configuration file that sets up the DHCP server. The Dynamic Host Configuration Protocol (DHCP) is a standardised network protocol used on Internet Protocol (IP) networks for dynamically distributing network configuration parameters, such as IP addresses for interfaces and services.

To edit the file you need to enter this into Terminal:

sudo nano /etc/dhcp/dhcpd.conf

Once your in the file, you want to find the following lines:

option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

These 2 lines show mean the Pi will be connected to a domain and you need to comment them out using the ‘#’ key as that’s not what we want. This can be seen below:

#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

Also found in this file is the following:

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented. 
#authoritative;

You need to remove the comment on the word “authoritative;”, so it looks like this:

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.authoritative;

After this, go to the bottom of that document and add the following lines:

subnet 192.168.42.0 netmask 255.255.255.0 {
    range 192.168.42.10 192.168.42.50;
option broadcast-address 192.168.42.255;
option routers 192.168.42.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Finally you are going to want to save the file by pressing Control-O, then press Enter, then Control-X.

Next we want the DHCP to serve requests for wireless connections. So we need to put this into Terminal:

sudo nano /etc/default/isc-dhcp-server

… and find INTERFACES=“” and change it to INTERFACES=“wlan0”

Then save and close the file.

Step 3: Setup 'wlan0'

Next we want to setup wlan0 to give static IP addresses. wlan0, simply means wireless local area network where 0 is the number of the wifi dongle (starts from 0 and increments by 1).

To-do so you need edit the file by entering the following into the command line:

sudo nano /etc/network/interfaces

Then you will need to find the line autowlan0 and comment it out, and in front of every line afterwards by adding a '#' in-front of the line. By doing so, we are basically removing any old wlan0 configuration settings.

Now we want to add the settings that we require for our wireless access point. Add the following lines of code in the configuration file opened above.

iface wlan0 inet static 
    address 192.168.42.1 
netmask 255.255.255.0

Once this is done, save and close the file.

A netmask is a 32-bit mask used to divide an IP address into subnets and specify the networks available hosts. Then assign a static IP address to the wifi adapter by running:

sudo ifconfig wlan0 192.168.42.1

Step 4: Configure the Access Point

Now we can configure the access point details, these include the SSID, which is simply the technical term for a network name, and the network password. Through these settings, we will set up a password-protected network so only people with the password can connect to it. So, we want to create a new file by running the code:

sudo nano /etc/hostapd/hostapd.conf

Then copy in the following lines of code:

interface=wlan0
driver=rtl871xdrv
ssid=YourWirelessAccessPoint
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=wirelesspassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Now you want to save and close this file. This file essentially pertains all the access point’s settings;

SSID - controls the name of the network. This is visible by other devices such as the computer.

Channel - the wifi channels are basically a spectrum of frequency space that the WiFi signal can transmit to. You really want to avoid transmitting at the same frequency as nearby wireless access points as the frequencies overlap..

WPA_passphrase - is the password set to prevent unwanted users from accessing your network.

WPA - Wi-Fi Protected Access and Wi-Fi Protected Access II (WPA2) are two security protocols and security certification programs developed by the Wi-Fi Alliance to secure wireless computer networks, for the purpose of this tutorial we will use WPA 2.

Now we are going to want the Raspberry Pi to find this configuration file, so run:

sudo nano /etc/default/hostapd

Then find the line:

#DAEMON_CONF=“"

and change it to:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

This allows the Raspberry Pi’s background process to use the configuration file.

Step 5: Configure Network Address Translation

Setting up NAT will allow multiple clients to connect to the WiFi and have all the data 'tunneled' through the single Ethernet IP. (But you should do it even if only one client is going to connect)

Run;

sudo nano /etc/sysctl.conf

Then scroll to the bottom and add on a new line:

net.ipv4.ip_forward=1

Save the file. This will start IP forwarding on boot up. Now run the follwing line, this will activate it.

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Then you want to create a translation from ethernet (eth0) to wifi (wlan0). This can be done through:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

Now this is done, we need to make this happen upon reboot as we do not want to constantly re-do code in order to start up the wireless access point. Run the following line of code to do this:

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Then run,

sudo nano /etc/network/interfaces 

and add the following to the end of the file and then save and close.

up iptables-restore < /etc/iptables.ipv4.nat

Step 6: Finishing the Wireless Access Point

Okay, now that we know it works, time to set it up as a 'daemon' - a program that will start when the Pi boots.Run the following commands sudo service hostapd start sudo service isc-dhcp-server startyou can always check the status of the host AP server and the DHCP server with sudo service hostapd statussudo service isc-dhcp-server statusTo start the daemon services. Verify that they both start successfully (no 'failure' or 'errors')Then to make it so it runs every time on boot sudo update-rc.d hostapd enable sudo update-rc.d isc-dhcp-server enable

Awesome! Now that everything is working as it should, we now have to set it up as a daemon - which essentially is a background process that starts as soon as the Pi boots. You will need to run:

sudo service hostapd start
sudo service isc-dhcp-server start

To start the daemon services. Verify that they both start successfully (no 'failure' or 'errors')Then to make it so it runs every time on boot

sudo update-rc.d hostapd enable 
sudo update-rc.d isc-dhcp-server enable

Step 7: Setting Up a Webserver

Install Apache

First install the apache2 package by typing the following command in to the Terminal:

sudo apt-get install apache2 -y

Apache is the most widely used web server software, it is an open source software available for free and it is fast, reliable, and secure.

Once it is installed, you can test that all is working by visiting http://localhost/, it should show up:


It works!
This is the deault web page for this server.

If that shows up, you've done everything correctly so far! You now need to have ownership of the file index.html as only the root has access to it. To-do this you need to navigate to the directory through the command line:

cd /var/www/html

Then:

sudo chown pi: index.html

This default web page is just a HTML file on the filesystem. It is located at /var/www/html/index.html

Install PHP (Optional)

PHP is a server side programming language that can do all sorts of things: evaluate form data sent from a browser, build custom web content to serve the browser, talk to a database, and more!

To allow your Apache server to process PHP files, you'll need to install PHP5 and the PHP5 module for Apache. Type the following command to install these:

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

Now you want to remove the index.html, and switch it with index.php.

sudo rm index.html<br>sudo nano index.php

Then add some data into it. For the purpose of this tutorial I put the PHP info script into the index.php page.

<?php phpinfo(); ?>

Now your complete! To test this is working look at the image at the top of the page.