Nginx-logo

Setup Nginx Web Server in Raspberry Pi with PHP, MySQL

The Nginx web server is a open source web server software which gains more users and also it uses less memory than Apache and also used for load balancing. Today we are going to show you how to setup nginx web server with PHP, MySQL database server in your Raspberry Pi.

Inital Setup:

1.Setup your SD card with Raspbian OS with this tutorial.
2.In order to control your Raspberry from your PC you can use SSH or VNC and to setup VNC server in your Raspberry Pi follow this tutorial. To Access SSH enter ssh pi@<IP> in the terminal or you can use Putty in Windows.

Instructions:

1.Enter the Raspberry Pi Terminal.

Screenshot from 2013-10-14 09:52:34
2.Install Nginx using the command in terminal.

sudo apt-get install nginx

3.Install MySQL database server using the command.

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

4.Install php5-fpm using the command. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

sudo apt-get install php5-fpm

5.After installing the packages start the Nginx Web Server using the command

sudo service nginx start

6.Enter sudo nano /etc/php5/fpm/php.ini in the terminal. Change cgi.fix_pathinfo from 1 to 0. ie cgi.fix_pathinfo=0. Save and exit the file. Then restart php5-fpm by

sudo service php5-fpm restart

7.Then we must configure nginx. Open the nginx configuration file by sudo nano /etc/nginx/sites-available/default. Then do the following
i)Add index.php to index line.

root /usr/share/nginx/www;
index index.php index.html index.htm;

ii)Uncomment the lines in “location ~ .php$ block” as given below

location ~ .php$ {
# fastcgi_split_path_info ^(.+.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

iii)Then save the file and restart nginx.

Now you your web server will start working. Visit Raspberry Pi’s IP in your PC to check whether its working.

Check PHP:

1.Move to the directory /usr/share/nginx/www
2.Then sudo nano index.php
3.Paste the contents in the file and save it.

 <?php
phpinfo();
?>

4.Now open <IP>/index.php you will see the PHP configuration in your Raspberry Pi.

Screenshot from 2013-10-15 14:48:38

[Also Read: Creating a Raspberry Pi Web Server Using Python]

Enjoy :)
Any questions comment here.

Srinivasan

Srinivasan

Content Creator at learn2crack
Am an active blogger who loves programming, gaming, computer and internet tricks.
  • cybernet2u

    i’m just curious, why is this different from ubuntu ?
    there are no specific settings for raspberry … like mysql caching due to the fact that raspberry has ONLY 800MHZ core …

    • http://128.199.224.11/ Raj Amal

      There are no specific settings for raspberry. Raspbian OS is a debian based OS which is similar to Ubuntu

  • Leke

    Any reason to use NGINX instead of Lighty?

    • http://128.199.224.11/ Raj Amal

      Nginx is light, uses less memory.

  • erik

    libapache2-mod-auth-mysql is not needed