Tutorial – Install Nginx and PHP on Raspbian

This is the first in a three part tutorial series on how to install and run NGINX on a Raspberry Pi. The first concentrates on installing and configuring Nginx web server and PHP on Raspbian OS, the second talks about installing MySQL server on Raspberry Pi, the third will walk you through installing WordPress on Nginx.

For many, Apache2 has become too bloated, and uses more resources than it needs to. Nginx is a very light-weight web server, that works great on a Raspberry Pi.

Prerequisites

Before doing this tutorial I assume that you are familiar with Terminal, and are proficient and comfortable in command line interface.

Step 1 – Install Nginx

To begin, using terminal or accessing directly, type the following command to ensure everything’s up-to-date before installing the required packages:

sudo apt-get update && sudo apt-get upgrade

Chances are there are packages that’ll need updating by running this command. If prompted, hit ‘Y’ to install the updates. Now type the following command to install Nginx:

sudo apt-get install nginx

There we go, we’ve now got nginx installed. Next, we want to launch nginx and test that it’s working ok. Type the following command into the command line interface:

sudo /etc/init.d/nginx start

If all goes well, you should see this message:

Launch nginx on Raspbian

If you visit your Raspberry Pi’s IP address in your web browser, you should see the default Nginx page, which looks a little something like this:

Nginx on a Raspberry Pi

Great success! You now have Nginx running on your Raspberry Pi. Next, we’ll install PHP.

Step 2 – Install PHP

The packages required for PHP to work with Nginx is a little different to those when using PHP with Apache2. PHP runs as a FastCGI interface, so we need to install the PHP FPM package by typing the following command:

sudo apt-get install php5-fpm

Once we’ve done that we’re almost ready to rock. We next need to make a few minor changes to the Nginx configuration to begin using PHP with Nginx.

Step 3 – Configure Nginx and PHP

You’re almost there. Similar to Apache2, Nginx works on a virtual hosts principle, but is arguably easier to configure multiple vhosts on Nginx. We’ll look at this in a later tutorial.

Next, we’re going to adjust the default configuration to allow Nginx to start serving content using PHP. Begin by typing the following command:

sudo nano /etc/nginx/sites-available/default

The Nano editor will appear. If you’re more comfortable using VI, use this command instead:

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

Scroll down the configuration file until you find the ‘server’ block with the following lines contained within it:

#listen 80; ## listen for ipv4; this line is default and implied
 #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

Uncomment them both out by removing the first #, so that they read like this:

listen 80; ## listen for ipv4; this line is default and implied
 listen [::]:80 default_server ipv6only=on; ## listen for ipv6

This allows Nginx to listen on port 80 (the default http port) and listen for both IPv4 and IPv6 requests. Next we need to edit the name of our server to match our domain. In my care, the domain is dingleberrypi.com but be sure to substitute this with your own domain or IP address.

# Make site accessible from http://localhost/
 server_name www.raspipress.com;

Change the index line from this:

 index index.html index.htm;

To this:

 index index.php index.html index.htm;

This allows files with the name index.php to be served up as the default index page, and takes priority over index.html and index.htm. Now we need to tell the configuration how to handle PHP files. Scroll down until you see this:

#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;
 #}

Uncomment some of the lines so that it reads like this:

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;
 }

Be careful to edit the correct lines. We’re using a socket, so uncomment the configuration accordingly. Finally, we need to change the PHP configuration to fix a possible security vulnerability. Type the following command to edit the PHP configuration file:

sudo nano /etc/php5/fpm/php.ini

Scroll down (or search for the line using CTRL + W in nano) for:

;cgi.fix_pathinfo=1

Uncomment the line by removing the semicolon from the start, and change the value from 1 to 0. Save and exit nano.

Configure PHP with Nginx on Raspberry Pi

All we need to do now is restart php5-fpm and nginx to make the configuration changes take:

sudo /etc/init.d/php5-fpm reload && sudo /etc/init.d/nginx reload

Step 4 – Test the PHP configuration

Now all that’s left to do is to test to ensure PHP and Nginx are correctly configured. Navigate to the default document root:

cd /usr/share/nginx/www

Create a file called index.php:

sudo nano index.php

Add the following line to the file, save and exit nano:

<?php phpinfo(); ?>

Visit your raspberry pi in your web browser by typing its domain name or IP address, and you should see the PHP info page:

PHP5 and Nginx on Raspbian

That’s it! We’ve installed, configured and tested PHP5 and Nginx. Grab a brew, you’ve earned it! Any questions or comments, feel free to add them below.

Next, we’ll look at installing MySQL on Raspberry Pi and creating a new database and database user for WordPress to use.

 

 

Ste

Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

43 Responses

  1. Martin says:

    Great stuff! I assume you have to uninstall Apache first?
    If so, being a Linux newcomer in general, is removing Apache (and PHP etc.) just a simple command?

    • Ste W says:

      I would if I were you to minimise unnecessary resource usage if not for anything else:

      sudo apt-get remove apache2
  2. Neil says:

    This is excellent help. Thankyou! I have followed your instructions but have an error with no idea on how to fix. Happened after reload step of php and nginx…
    nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
    the only thing I did differently to your instructions was as I do not yet have a domain name I left server_name as default local_host.

    Can you assist please?
    Thank you.
    Neil

    • Ste W says:

      Hi Neil. It looks like IPv6 possibly isn’t supported by your router. Comment out the line:

      listen [::]:80 default_server ipv6only=on; ## listen for ipv6
      

      So that it read:

      #listen [::]:80 default_server ipv6only=on; ## listen for ipv6
      

      Change ‘local_host’ to ‘localhost’ and try that again, let me know how you get on either way.

  3. Neil says:

    Thanks Ste W for getting back to me so quickly. Yes, did that and its now running well. I have progressed through all 3 tutorials without a hitch.
    You state in another post you are seeking ideas on other tutorials. Well, like me, I presume most people following your instructions are as green as I am but have an ultimate goal in mind and not quite sure how to get there. In my case it is to use the RPI as a web server. Next steps for me are to learn the WordPress side of things (thanks to you) to setup a basic website (not necessarily a blog) then it will be how do I remote connect a desktop so I can work from my PC and give back the television to the family :). Setting up mail from my new domain name too. Then it will be how do I get my newly purchased domain name into the RPI and operate a website effectively without a static IP address from my ISP. Im told this can be done. Basically anything along these lines would be helpful.
    Regards
    Neil

  4. Thank you for that guide Ste W,is there any way to change the port of nginx to something else?Currently my nas uses the the port 80.Even after modifying the listen command in /etc/nginx/sites-available/default with some other port the site becomes unresponsive,after i change it back to 80 everything works fine again!

  5. after installing nginx, when doing sudo /etc/init.d/nginx start, i get command not found, actually there is no nginx script on the init.d folder…. any ideas?

  6. Perhaps an additional guide to running multiple sites via Nginx … pretty much like you did for Apache2 (unless the process is exactly the same?).

  7. Roel says:

    Hi i have an error just before setp 4

    [ ok ] Reloading PHP5 FastCGI Process Manager: php5-fpm.
    Reloading nginx configuration: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
    nginx: configuration file /etc/nginx/nginx.conf test failed

    what do i have to do?

    Thanks

  8. I get a 403 Forbidden error.
    I’ve heard you need to fix the permissions with chown but I’m not sure.
    I’d appreciate any help I can get.

  9. mihai ion says:

    How can you change the root folder of the site? (not enough space on sdcard)
    Thanks

  10. my nginx is opening with localhost, how can change it to “spantechnologies.in” domain in local network. Which file should i modify to do this.

  11. Bob R says:

    hello,
    success up until here:
    Reloading PHP5 FastCGI Process Manager: php5-fpm.
    Reloading nginx configuration: nginx: [emerg] “fastcgi_pass” directive is not allowed here in /etc/nginx/sites-enabled/default:69
    nginx: configuration file /etc/nginx/nginx.conf test failed

  12. YamiFrankc says:

    im getting a 502 bad gatway error when i go test the php site.
    how to fix this?

  13. proms says:

    Thank you! If’s very excellent lecture!!

  14. richpav says:

    Do you play requests? I’m trying to get Varnish working with nginx. This guy’s server is blazingly fast, but his instructions are for Varnish 3 not 4. I compiled and installed it, but at this point in time I’m trying to figure out where the config file is supposed to be.

    http://www.weedpi.com/how-to/how-this-site-was-built/

  15. Great php tutorials that covers all the basics of PHP with examples help to learn php language for Begginers Online.It can help better for php developers.Thanks for this useful post.

  16. Andrea Sciamanna says:

    I would spend some words about performances.

    Can you provide us with sone numbers, or at least some thoughts about performances?

    Also, would you rexommend it as a sort of portable development server (with remote debugging, for instance)?

    • Ste W says:

      I’ve had a few bumps with nginx, so am looking to go back to Apache 2.4. I’ll do a comparison in terms of performance once this is done 🙂

  17. kef sagat says:

    Best guide ever, you have saved me hours of research.
    kudos. 🙂

  18. Ernst Neger says:

    “Visit your raspberry pi in your web browser by typing its domain name or IP address, and you should see the PHP info page:”

    This will not work. One has to type in /index.php. Otherwise one will still be greeted by nginx and not the php module. I followed this tutorial from the first to the last line, but I cannot access the php info without directly browsing index.php as mentioned.

  19. Ernst Neger says:

    When following this guide, you have to browse to /index.php in order to see the php info. Using only the IP/URL will not work. I followed this tutorial from the first to the very last line.

  20. Francois says:

    Ernst Neger, i tried myIPaddress/index.php it said no input file specified

  1. 13th June 2014

    […] Ste Wright This is the first in a two part tutorial on how to install and run NGINX on a Raspberry Pi. The […]

  2. 15th June 2014

    […] a Raspberry Pi running Raspbian could run PHP and Nginx? I was pleasantly surprised when I found this site which detailed the procedure (spoiler alert: it’s brutally simple and almost identical to the […]

  3. 20th June 2014
  4. 20th June 2014
  5. 3rd August 2014

    […] might take a few hours to be get a reply. Once this is done, or while waiting for a reply, we can setup a Nginx web server on our Raspberry Pi. This will setup a PHP web server that we can use to build a webpage or install […]

  6. 29th September 2014

    […] actually followed the amazingly well done tutorial from RaspiPress.com almost to the T. If you are interested in doing a build yourself, Make sure to check out his […]

  7. 16th October 2014

    […] (PHP) menggunakan Server XAMPPXAMPP Apache +MySQL +PHP +PerlHow to install XAMPP or PHP and MySql ?Tutorial – Install Nginx and PHP on Raspbian – RaspiPressReadySpace Singapore Cloud and Managed HostingXAMPP Apache +MySQL +PHP +PerlSetting Up Eclipse PDT […]

  8. 4th December 2014

    […] MySQL. I have written blog posts on how to do this in the past, but the best one I have found is at Raspi Press. Follow the tutorial and you will see how easy it is to make a Raspberry Pi into a web […]

  9. 17th March 2015

    […] web servers  I decided to install Nginx (pronounced engine-x). Being new to Nginx I used this Tutorial – Install Nginx and PHP on Raspbian – RaspiPress guide to do this inital configuration. I also used this guide to tweak Nginx for […]

  10. 20th March 2015

    […] to get things going can only make things easier. The one guide i found to work well every time was this three part guide by Stephen Wright, who also runs a site/blog on his RPi. Stephen left some things out, like […]

  11. 6th August 2015

    […] I decided to have a thorough search for a good guide on this, and then finally stumbled upon a guide by Stephen Wright that worked right out of the box, and ran reasonably fast on the old RPi B. I then found a simple […]

  12. 24th August 2015

    […] can muck my way around Linux, but it’s certainly not my forté. Fortunately, I found a great tutorial for installing WordPress on a Raspberry Pi that even a Linux noob like me can […]

  13. 25th October 2015

    […] I’m running a Raspberry Pi (Model B Y think) with Raspbian, I followed the instructions at http://www.raspipress.com/2014/06/tutorial-install-nginx-and-php-on-raspbian/ to install a web server (with a few tweaks as parts appeared outdated and I also installed telnet […]

  14. 14th November 2015

    […] Part 1: Install Nginx and PHP on Raspbian […]

  15. 3rd July 2016

    […] Tutorial – Install Nginx and PHP on Raspbian […]

  16. 13th August 2016

    […] Tutorial – Install Nginx and PHP on Raspbian […]

Leave a Reply