In the previous post, we discussed about setting up nginx on a Raspberry Pi. To serve dynamic content, a server side language like PHP and a database server like MySQL is essential. To execute the PHP scripts we will use PHP-FPM, which is a FastCGI implementation. To accelerate performance we will install Alternative PHP Cache(APC) also.

sudo bash
apt-get install php5-fpm php-apc

Edit the default site’s configuration as below to include the location block:

server {
        root /usr/share/nginx/www;
        index index.html index.htm index.php;
        server_name localhost;
 
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

Restart nginx server, and create a php page to test everything looks OK.

service nginx restart
nano /usr/share/nginx/www/info.php

<?php
phpinfo();
?>

Now access your php page http://<your_raspberry_address>/info.php. If you see the php configuration, FPM is setup correctly.

Now we will install MySQL and the very popular administration tool phpMyAdmin.

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

During the installation of MySQL, you will be prompted to enter the password for the root user of MySQL. You will also be asked to choose between Apache or lighthttpd to be used as the web server. You dont need to select anything here since we use nginx.

During phpMyAdmin installation, you will be asked to configure database for phpMyAdmin. Answer Yes here.  Enter the root user password that you created in the previous step when you are asked for it.

Also, create a link to phpMyAdmin directory in the www directory.

ln -s /usr/share/phpmyadmin /usr/share/nginx/www/phpmyadmin

Now access http://<your_raspberry_address>/phpmyadmin, and login as root user with the previously set password. If you get a 403 Forbidden error, that probably because index.php is not included in the default document list. Edit /etc/nginx/sites-available/default and make sure that index.php is included in the default document list under server block as below.

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

You are now all set to use PHP/MySQL in nginx!