Lightweight Raspberry Pi 3 Web Server Using PHP And HTML

Introduction

 
This article demonstrates how to make a lightweight web server for the home using Raspberry Pi 3 and Python 3 IDE.
 
Pi can even handle a web server; so if you want to launch your own website with a proper back-end, you can easily do it. This web server is used to get a small amount of traffic and for development purposes. 
 
Requirement 
 
I used the following equipment for this Raspberry Pi web server article.
  • Raspberry Pi 3
  • Minimum 8GB SD Cord if you're using Raspberry Pi 3
  • Ethernet or Wifi
  • Putty 
  • VNC Viewer 
Optional
  • USB Keyboard
  • USB Mouse
Installation
 
In this article, I have made use of the standard operating system for the Raspberry Pi, known as Raspbian. If you haven't got this installed, then you can follow my article "Traffic Light System Using Raspberry Pi 3 and Raspbian Installation Process" at this Link.
 
How do I power my Raspberry Pi?
 
The latest Raspberry Pi Support 5Volt @ 2Ampere as a minimum input value. Arduino can accept the range of inputs (5-12 V) 1250 mA. 
 
 
Set up Wifi on Raspberry Pi.
 
 
Step 1
 
First, the Raspberry Pi must be connected to your desktop. Then, connect your wi-fi on your Pi or connect your ethernet. After that, open the terminal of your Operating System using the following code.
  1. hostname -I  
 
Step 2
 
Next, open PuTTY software and paste the Host Name (or IP Address) on this PuTTY.
 
 
After that, open the terminal box and enter your default PI name and password.
  • Username (pi)
  • Password  (raspberry)
The connection is successful.
 
 
Step 3
 
Next, update the Raspberry Pi. So, install the latest packages. You can do that using the following commands.
  1. sudo apt-get update   
  2. sudo apt-get upgrade  
or
  1. sudo apt-get update && upgrade  
Step 4
 
Next, we will need to install the Apache web server. Just use the following commands.
  1. sudo apt-get install apache2 -y  
If you don't know the IP address, you can enter the following command line.
  1. hostname -I  
Step 5
 
Open Browser, Enter IP address in the browser, and you should get an Apache instruction page.
 
 
Step 6
 
Now, we are going to edit this simple page entering the following command. 
  1. sudo nano /var/www/html/index.html  
After that, open index.html editing page. Just replace the following HTML codes. You can add new web pages as you want, in the www folder and view from the browser.
 
  1. <html>    
  2.    <body>     
  3.      <h1>Hi This  is Ravishankar</h1>     
  4.    </body>    
  5. </html>   
    Next, open the browser, enter the IP address (192.168.8.101) in a browser, and you get your HTML page.
     
     
    This is a basic server but if you are using PHP for the dynamic website, then read on. To install the PHP package, let's download PHP5 module for Apache.
    1. sudo apt-get install php5 libapache2-mod-php5 -y  
     
    Now, create a new file and edit this example page entering the following command. After that, open and edit the file. In this file, add the following PHP code.
    1. sudo nano /var/www/html/example.php  
     
    PHP Code
    1. <?php  
    2. echo "Today's date is ".date('Y-m-d H:i:s');              
    3. ?>  
    Next, open Browser, Enter IP address + Page Name in a browser, and you get your PHP page. The index page is a default page so IP address is only needed, but additionally, we can add the new web page to need for the page name (192.168.8.101/example.php).
     
     
    Finally, we have successfully created a Mini Web Server using Raspberry Pi.