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

23 Jul, 2018
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:
Change to root directory and run a update :sudo -i 
apt-get update
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 
Then start the serverservice nginx start
Now run the following command:ifconfig   // it tells the ip address

Change to root directory and run a update :sudo -i 
apt-get update

sudo -i 
apt-get update

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 

apt-get install nginx php5-fpm php5-cgi php5-cli php5-common 

Then start the serverservice nginx start

service nginx start

Now run the following command:ifconfig   // it tells the ip address

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:
Enter the config directory:cd /etc/nginx
Now navigate to sites-available directory.cd sites-availableThere will be a single file present having name default, open it and edit it by typingnano defaultNow 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{

 }
Also, change below line,index index.html index.htm;toindex index.php index.html index.htm;
//to use PHP we added index.phpNow to save and exit, press Ctrl+x then press Y.Now lets restart the server, to collect the changes that have been done:service nginx restartNow we have to create a index.php file in our www directorycd/user/share/nginx/www
nano index.php
Write this script here:<HTML><HEAD><TITLE> hello world<TITLE></HEAD><BODY><?phpprint("Hello from GeeksforGeeks");?></BODY></HTML>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
Enter the config directory:cd /etc/nginx

cd /etc/nginx

Now navigate to sites-available directory.cd sites-available
cd sites-available
There will be a single file present having name default, open it and edit it by typingnano default
nano default
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{

 }

location=\.php5 {
fastcgi_split_path_info "(.*\.php)(/.*)5;


fastcgi.pass unix:/var/run/php5-fpm.
fastcgi_index index.php;
include fastcgi_params;
}

location=/\.bt{

 }

Also, change below line,index index.html index.htm;toindex index.php index.html index.htm;
//to use PHP we added index.phpNow to save and exit, press Ctrl+x then press Y.
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.
Now lets restart the server, to collect the changes that have been done:service nginx restart
service nginx restart
Now we have to create a index.php file in our www directorycd/user/share/nginx/www
nano index.php

cd/user/share/nginx/www
nano index.php

Write this script here:<HTML><HEAD><TITLE> hello world<TITLE></HEAD><BODY><?phpprint("Hello from GeeksforGeeks");?></BODY></HTML>
<HTML><HEAD><TITLE> hello world<TITLE></HEAD><BODY><?phpprint("Hello from GeeksforGeeks");?></BODY></HTML>
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
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
Comparing two dates in PHP
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?