Web server on raspberry pi – how to setup?

Introduction

In this post I will let you know how to set up web server on raspberry pi. There are many web servers for raspberry pi. In out example we will be setting up apache web server. Apache is a popular web server application and we will install it on raspberry pi to server web pages

 Step 1 -Install APACHE

First install apache 2 package by using the following command in to the terminal

sudo apt-get install apache2 -y

 Step 2 – Test the Web Server

By default, Apache puts a test HTML file in the web folder. This default web page is served when you browse to http://localhost/ on the Pi itself, or http://192.168.1.9 ( IP address of Pi) from another computer on the network.

Browse to the default web page either on the Pi or from another computer on the network and you should see the following:

default_html_page

This means you have Apache working!

Step 3 – Changing the default Page

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

Navigate to this directory in the Terminal and have a look at what’s inside:

cd /var/www/html

ls -al

This will show you:

step3_htme_page_location

Step 4- Install Php

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 remove index.html file and add index.php with following content

<?php echo "hello world"; ?>
<?php echo date('Y-m-d H:i:s'); ?>
<?php phpinfo(); ?>

Now open the index.php page. You will get following:

dynamic_php_page

You are now done setting up web server on raspberry pi which can server dynamic pages.

In the next post we will use this web server to post real time data from arduino/raspberry pi and also control different appliances from the web page.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.