One of the reasons that I purchased a Raspberry Pi was so that I can do local website development. Of course, I could have easily set this all up with Windows, but my desktop is already cluttered enough. And with a Raspberry Pi Webserver, I can set up scipts to run independently of my desktop. Here’s how to setup a Raspberry Pi Webserver
What You Will Need:
- Raspberry Pi
Setup Static IP Address
It would be very annoying to have a website that always changes its address. One day it might be 192.168.0.100 and the next day it might be 192.168.0.105. To avoid this confusion, let’s setup a static ip address on the raspberry pi. Now the old way of doing this would be to edit the network interfaces file, but the new way is to edit the dhcpcd.conf file. Before you begin, you will need to know the default gateway of your router and your domain name server.
To get the default gateway, type in the following command:
ip route | grep default
This will display the default gateway; write this number down somewhere. To get your domain name server type in the following:
This will display the domain name server. It might be the same address as your default gateway. Finally, you need to add the following lines in the dhcpcd.conf file. Type in the following:
sudo nano /etc/dhcpcd.conf
Then add the following lines at the bottom of the file:
<br /> interface eth0<br /> static ip_address=192.168.50.2/24<br /> static routers=192.168.50.1<br /> static domain_name_servers=192.168.50.1<br />
For static ip_address you can choose any unused address on your network. Keep in mind that your network might be configured slightly different than mine so change the numbers accordingly. The static router is the default gateway of your router and static domain name server is your domain name server. You should have obtained this information from the previous step. If you are connecting via wireless lan and not ethernet, change interface eth0 to
interface wlan0<br />
Finally, reboot your rasbperry pi by typing in
sudo reboot Next time you SSH into your pi, you need to use to the new ip address that you assigned it.
So for right now, we are not going to get fancy with a Node JS server or anything like that. We are going to setup a simple apache server to serve html files. Open up terminal and type in the following:
sudo atp-get update
sudo apt-get install apache2 -y
This will install the apache server. To test that it works, navigate to
http://localhost on the raspberry pi or to the Pi’s iP address (192.xxx.xxx) on any other device. You should be greeted by the following welcome page. Remember that the default location of your website is the var/www/html directory.
Unless you’re building a truly static website, you will also need the assistant of PHP to perform some server-side tasks. To install php type the following in terminal:
sudo apt-get install php5 libapache2-mod-php5 -y
You can through a php file in the website directory to test that it is working. And that is it. You now have a webserver running on the raspberry pi.