Open In App

Create a web server and run PHP script on it using Raspberry Pi

In this post, creating a web server and running PHP script on it using Raspberry Pi will be discussed.

Web Servers are basically simple computer programs that dispense the web page when they are requested using the web client. The machines on which this program run are usually called as a server, with both the names web server and server almost used interchangeably.

Steps to create a web server and run PHP script on it using Raspberry Pi:

  1. Change to root directory and run a update :
    sudo -i 
    apt-get update
    
  2. Now install each of the packages which will be necessary for the following process,
    apt-get install nginx php5-fpm php5-cgi php5-cli php5-common 
    
  3. Then start the server
    service nginx start
    
  4. Now run the following command:
    ifconfig   // it tells the ip address
    

Now paste this ip address to computer’s browser. If everything above went well, then it will show the following text/window on the browser window.

Welcome to nginx!

To run PHP script on this web server:

  1. Enter the config directory:
    cd /etc/nginx
    
  2. Now navigate to sites-available directory.
    cd sites-available
  3. There will be a single file present having name default, open it and edit it by typing
    nano default
  4. Now find below lines and uncomment them.
    location=\.php5 {
    fastcgi_split_path_info "(.*\.php)(/.*)5;
    
    
    fastcgi.pass unix:/var/run/php5-fpm.
    fastcgi_index index.php;
    include fastcgi_params;
    }
    
    location=/\.bt{
    
     }
    
  5. Also, change below line,
    index index.html index.htm;

    to

    index index.php index.html index.htm;
    //to use PHP we added index.php

    Now to save and exit, press Ctrl+x then press Y.

  6. Now lets restart the server, to collect the changes that have been done:
    service nginx restart
  7. Now we have to create a index.php file in our www directory
    cd/user/share/nginx/www
    nano index.php
    
  8. Write this script here:




    <HTML>
    <HEAD>
    <TITLE> hello world<TITLE>
    </HEAD>
    <BODY>
    <?php
    print("Hello from GeeksforGeeks");
    ?>
    </BODY>
    </HTML>
    
    
  9. Now save it and then test everything in the browser using that IP address or type youripaddress/index.php e.g 192.168.182.1/index.php
Article Tags :