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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <HTML>
    <HEAD>
    <TITLE> hello world<TITLE>
    </HEAD>
    <BODY>
    <?php
    print("Hello from GeeksforGeeks");
    ?>
    </BODY>
    </HTML>

    chevron_right

    
    

  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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.