Install Nginx PHP and MySQL on Raspberry Pi

2015-04-12: Article has been updated with minor improvements and fixed steps to match current installation workflow.

Previously we have installed Raspbian, now we can do some actual work and install Nginx, PHP and MySQL on our Raspberry Pi. Afterwards we can use our Raspberry Pi as full-fledged home webserver.

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

If you open the browser and go to your Raspberry Pi IP address, you will see the Welcome to Nginx site. Let’s create our own. We need a directory where we will store our pages, new directory has to be amended so it is accessible for Nginx.

sudo mkdir -p /var/www/rasppi
sudo chmod -R 775 /var/www
sudo chown -R www-data:www-data /var/www

Create Nginx configuration file.

sudo vi /etc/nginx/sites-available/rasppi

and insert following config. You have to update it with IP address of your Raspberry Pi.

server {
    listen 80; # listen for ipv4
    server_name rasppi; # Add your Raspberry Pi IP address here

    access_log /var/log/nginx/rasppi.access.log;
    error_log /var/log/nginx/rasppi.error.log;

    root /var/www/rasppi;

    location / {
        root /var/www/rasppi;
        index index.php index.html index.htm;

    # Parse all .php file in the /var/www/rasppi directory
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/rasppi$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_intercept_errors on;
        fastcgi_ignore_client_abort off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;

    # Disable viewing .htaccess & .htpassword
    location ~ /\.ht {
        deny all;

Let’s allow this new config and disable default one.

sudo ln -s /etc/nginx/sites-available/rasppi /etc/nginx/sites-enabled/rasppi
sudo rm /etc/nginx/sites-enabled/default

We are done with Nginx settings, now we have to update php-fpm configuration, edit following file

sudo vi /etc/php5/fpm/pool.d/www.conf

find line with listen = /var/run/php5-fpm.sock and replace to: listen =

Create index page with PHP info so we are sure that everything works


We have to restart both services

sudo service php5-fpm restart 
sudo service nginx restart

Nginx and PHP are finished and running, open your browser and check it out. You should see the PHP info page displaying your Raspberry is running PHP 5.4. And we can install MySQL database:

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

during the installation you should be asked for root password, if this hasn’t happened, don’t worry and set it with following command

mysqladmin -u root password yourSECRETrootSQLpassword

We are done. Now we have web server with PHP and MySQL running on Raspberry Pi.