How to Configure Nginx to Execute PHP Using PHP-FPM

by Luke P. Issac on December 11, 2013

Nginx is pronounced as “Engine-X”, which is a web server and reverse proxy server. Nginx is well known for its speed and ability to handle large number of requests simultaneously with optimal use of resources.

PHP-FPM stands for “PHP-FastCGI process manager”. CGI refers to the common gateway interface which is scripted to work as a interface between the web server and dynamic content serving programs. It listens on a port much like the web server itself does, and passes the request between the PHP and web server.

This tutorial provides instructions on how to install and configure Nginx with PHP-FPM, which will help you to execute PHP programs in Nginx.

As we discussed earlier in Apache vs Nginx, when compared to Nginx, Apache is relatively slow while handling heavy load and processing large number of requests.

1. Install Nginx

You can either install Nginx from source, or install it using the package management tool that comes with your distro.

For example, on Ubuntu you can install nginx using apt-get as shown below.

$ sudo apt-get install nginx

Start the nginx server as shown below:

$ sudo service nginx start

Go to http://{your-ip-address} and make sure you see the Nginx’s welcome page.

2. Install PHP5-FPM

Next, install php5-fpm using the package management tool that comes with your distro.

For example, on Ubuntu you can install php5-fpm using apt-get as shown below.

$ sudo apt-get install php5-fpm

3. Add PHP Configuration to Nginx

Next, modify the /etc/nginx/sites-available/default file, and add the following lines.

$ sudo vi /etc/nginx/sites-available/default
server {
  listen   80;
  root /usr/share/nginx/www;
  index index.php index.html index.htm;
  server_name example.com;
  location / {
    try_files $uri $uri/ /index.html;
  }
  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/www;
  }
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  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;
  }
}

4. Set listen Parameter in php5-fpm www.conf

Next, we need to make the following changes in the php-frpm configuration.

By default, you’ll see the following listen entry in the www.conf file

$ sudo vi  /etc/php5/fpm/pool.d/www.conf
listen =  127.0.0.1:9000

In the www.conf file, leave everything as it is, and replace the above listen line with the one shown below.

$ sudo vi  /etc/php5/fpm/pool.d/www.conf
listen = /var/run/php5-fpm.sock

5. Restart the Nginx and PHP5-FPM and Test it

Restart php5-fpm and nginx as shown below

$ sudo service nginx restart

$ sudo service php5-fpm restart

Finally, create the following index.php file in the nginx document root, and test it.

$ sudo vi /usr/share/nginx/www
<?php
  phpinfo( );
?>

Finally, open your browser and go to http://localhost/index.php (or use your ip-address), which will execute the index.php file and display the php information.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 2 comments… read them below or add one }

1 rax December 12, 2013 at 2:04 am

A handy tutorial, thanks,

2 Mark Greenall January 10, 2014 at 2:13 pm

Great article -thanks.

Leave a Comment

Previous post:

Next post: