≡ Menu

How to Configure Nginx to Execute PHP Using PHP-FPM

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
  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 =

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
  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.

Add your comment

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

Comments on this entry are closed.

  • rax December 12, 2013, 2:04 am

    A handy tutorial, thanks,

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

    Great article -thanks.

  • Manish Sapkal November 4, 2015, 1:35 am

    I have same problem. I have already setup environment for node.js and nginx. Now I would like to run php with nginx. I have already installed php and configured nginx for php. But, my browser showing script/code of php instead of executing/run it. I have placed same question on stackoverflow here : http://stackoverflow.com/questions/33409539/nginx-shows-php-code-instead-of-executing
    I have made same changes in ‘sites-available/default’ and ‘/etc/php5/fpm/pool.d/www.conf’ as you describe. I have posted by config files in about link, If you wish to get more detail. Please guide me, where to look? It is very urgent. Thanks

  • Salah February 21, 2016, 12:09 pm

    You saved my day 🙂 Thank you so much