Introduction: Raspberry Pi Web Server --- Nginx - PHP - MySql

This tutorial will describe how to install and set up a light web server on Raspberry Pi that supports PHP and MySql.
Apache is the most widely used server, but Raspberry has limited resources so it is better to opt for a system that uses less resources.

There are many alternatives to Apache, in this tutorial we chose nginx.

nginx [engine x] is an HTTP and reverse proxy server, as well as a mail proxy server, written by Igor Sysoev. For a long time, it has been running on many heavily loaded Russian sites including Yandex, Mail.Ru, VK, and Rambler.

First you need to update the packages with

sudo apt-get update

sudo apt-get upgrade

Step 1: Nginx

nginx

Install nginx

sudo apt-get install nginx

create the folder

sudo mkdir /var/www

Edit the configuration file as follows

sudo nano /etc/nginx/sites-available/default
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
## 
 
server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
        listen 80;
        server_name $domain_name;
        root /var/www;
        index index.html index.htm;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
 
       
 
        # Make site accessible from http://localhost/
        server_name localhost;
 
        location /

Create a file

sudo nano /var/www/index.html

with this content

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML+RDFa 1.1//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Raspberry Web Server</title>
</head>
<body>
<p><h1 align="center">Welcome,<br />... your Raspberry Web Server is ready!! </h1>
<p align="center"><img alt="" src="http://www.emmeshop.eu/blog/sites/files/image/u1/raspberry-pi.png" style="height:300px; width:250px"></p>
</p>
<p align="center"><img src="http://www.emmeshop.eu/blog/themes/bartik/Logo-Emmeshop.png" alt="Home"></p>
<p align="center">Emmeshop Electronics --- www.emmeshop.eu </p>
<p align="center"> </p>
</body>
</html>

restart nginx

sudo service nginx restart

Open your browser with Raspberry Pi address, in this case http://192.168.0.166, you can see nginx home page.

Step 2: PHP

PHP

PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

nginx uses php-fpm to execute PHP programs.

So the next step is to install php-fpm as well as php-apc. The latter is a PHP extension for accelerating PHP performance.

sudo apt-get install php5-fpm php-apc

Edit nginx config file

sudo nano /etc/nginx/sites-available/default 
server {
          #listen   80; ## listen for ipv4; this line is default and implied
          #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
          listen 80;
          server_name $domain_name;
          root /var/www;
          index index.html index.htm;
          access_log /var/log/nginx/access.log;
          error_log /var/log/nginx/error.log; 
   
          location ~\.php$ {
                  fastcgi_pass unix:/var/run/php5-fpm.sock;
                  fastcgi_split_path_info ^(.+\.php)(/.*)$;
                  fastcgi_index index.php;
                  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                  fastcgi_param HTTPS off;
                  try_files $uri =404;
                  include fastcgi_params;
          } 
   
          # Make site accessible from  http://localhost/

          server_name localhost; 
   
          location /

Create a file to test Php

sudo nano /var/www/info.php

with this content

<? php 
phpinfo(); 
?> 

Restart the server

sudo service nginx restart

and test in your browser

Step 3: MySql

MySql

MySQL is the world's most popular open source database. Whether you are a fast growing web property, technology ISV or large enterprise, MySQL can cost-effectively help you deliver high performance, scalable database applications.

This part describes how to install MySQL as a database server and phpMyAdmin as the management interface for manipulating databases.

Install MySQL, phpMyAdmin, and php5-mysql.

sudo apt-get install mysql-server mysql-client php5-mysql phpmyadmin

During MySQL server installation process, you will be asked to configure the password for root user of MySQL.

You will also be asked to choose the web server installed in the system (Apache2 or Lighttpd). In this case, you can leave the web server selection field empty.

During phpMyAdmin installation, you will be asked to configure database for phpMyAdmin. Answer "yes". When asked to enter the password of the administrative user, provide the password.

After make a link of phpMyAdmin from /usr/share/phpmyadmin to /var/www/phpmyadmin.

sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin 

Open phpMyAdmin from your browser by going to http://<raspberrypi-ip-address>/phpmyadmin/index.php, and log in as "root" using the administrative password that you have set earlier.

Step 4: How Do We Use It?

Ok, now we have a web server running on our raspberry.
How do we use it?

We could use it to store the values of some sensors or turn outputs via web interface.

The image shows the execution of a script that reads the information from a PhidgetInterfaceKit.

In the next tutorials we will try to build a web interface that enables or disables the relays, and displays the status of the inputs and sensors connected to Raspberry.

Follow us on social to stay informed.

www.emmeshop.eu

Comments

author
fbboisclair (author)2016-11-14

Great but you should uncomment or add server_tokens off; in nginx.conf under the http block. That would avoid giving what nginx version you are running. It could help to mitigate script-kiddy.

author
Cid Felipe (author)2016-08-02

Nice!

author
permie made it! (author)2016-02-09

Very helpful. Thank you very much! Nginx is so much faster than Apache, especially on the pi.

2016-02-09 19_33_00-pi@greenroom_ ~.png
author
AgusE1 (author)2016-01-20

Work !!

author
ZsoltW (author)2015-05-26

Hi I tried to restart the web server but I got an error:

[emerg] invalid log level "server_name in /etc/nginx/sites_enabled/default:33

the code in this row: server_name localhost;

author
aadiitya_tamrakar (author)2015-03-17

author
rajimenez (author)2014-11-13

I think you meant to put:

<?php

echo phpinfo(); // NOT: php phpinfo();

?>

author
emmeshop (author)rajimenez2014-11-13

Thanks! It was a formatting error, "php" was the previous line "<?php".

author
seamster (author)2014-11-05

Nice!