amber-code

How To Install Nginx On A Raspberry Pi Running Jessie

I have installed a web server on my Raspberry Pi before. But when I went to do it this last time I noticed a few things have changed.

This post will walk you through some of the hiccups I had when following the official Raspberry Pi documentation on how to install Nginx on a raspberry pi running Jessie.

Here is the official documentation. I would suggest following along with these steps.

First things first, installing Nginx

It’s pretty straightforward to install Nginx on a Raspberry Pi…simply type in the following command,

sudo apt-get install nginx

The first hiccup I had was apt-get returned an error suggesting I update my package manager.

So I simply typed

sudo apt-get update

After a few minutes, I was able to execute the first command with no problems (you might not have to do this).

To start the Nginx server on your Raspberry Pi simply type

sudo /etc/init.d/nginx start

You can now test that your server is installed by visiting http://localhost/ in your browser.

If you want to just run static HTML files you can stop here.

However, if you want to install PHP which gives you the ability to run a scripting language and write more dynamic websites, you will need to install a few more things.

Installing PHP on a Raspberry Pi webserver

The command you are looking for is…

sudo apt-get install php5-fpm

Time to configure some settings

Navigate to cd /etc/nginx

In this file, I notice some discrepancies. I notice that what I had as a default looked very different then what the documentation we telling me.

I tried a few times to get it to work but kept getting the following error.

Reloading nginx configuration (via systemctl): nginx.serviceJob for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.
 failed! 

Finally I just copied everything that was “suppose” to be uncommented in and reloaded the file and it worked.

So first things first.

We need to tell the server that we want the index file with the .php extension to take preference over those that are HTML.

Around line 25 you will need to add the index.php to the list so it looks like this

index index.php index.html index.htm;

Now, we need to uncomment some options. This is what I was talking about when I said it looked different. I just copied and replaced the section of options with what the documentation said I should have.

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        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.conf;
        }

This may not be a best practice but I was fed up with Googling error messages and looking in logs.

Finally we can reload this file

sudo /etc/init.d/nginx reload

Here is where things got fuzzy for me

The documentation tells us to change the index.html file to index.php to test if PHP files are being served correctly by the web server.

To do that we can navigate to the path below…

cd /usr/share/nginx/www/

And then change the file extension

sudo mv index.html index.php

I made a change to the file…went to http://localhost and didn’t see it.

I looked back in my terminal and saw that I didn’t have permissions to write to the /www folder.

I started Googling what I need to do to change the folder permissions but wasn’t sure of the right answer (by the way I am not using root as it’s advised to create a new user in Jessie).

However, I remember from a while ago that you can access the webserver files by going to the following path

/var/www/html

When I make a change to this index.php file I see the change on localhost as expected.

I am still looking into this but if someone could share thoughts on what I am missing it would be greatly apprecaited.

Here is my final test showing that PHP is running and the change to the file works.

hello-world

  • vsayanam

    I followed Raspberry Pi’s documentation which has not been updated to jessie. While following the documentation, I made one change: I created index.php file in /usr/share/nginx/html instead of /usr/share/nginx/www as the directory was not found. But this should be reflected in the file /etc/nginx/sites-enabled/default by changing ‘root=/var/www.html’ to whatever folder ngnix is programmed to look for. As this is somewhat confusing, I copied the php file from /usr/share/nginx/html folder to /var/www/html folder. Now the localhost in chromium reads the php file. However we need to make the correct entry in default file as ‘root /x/x/x’. Any help will be appreciated. Thanks in advance.