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 server
service nginx start
- 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:
- Enter the config directory:
cd /etc/nginx
- Now navigate to sites-available directory.
cd sites-available
- There will be a single file present having name default, open it and edit it by typing
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{ }
-
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.
-
Now lets restart the server, to collect the changes that have been done:
service nginx restart
- Now we have to create a index.php file in our www directory
cd/user/share/nginx/www nano index.php
- Write this script here:
<
HTML
>
<
HEAD
>
<
TITLE
> hello world<
TITLE
>
</
HEAD
>
<
BODY
>
<?
php
print("Hello from neveropen");
?>
</
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