Setting up PHP-FPM with Nginx on Ubuntu

In a previous post, I discussed how to use Nginx and Apache together on Ubuntu. I have Nginx listening on port 80 and Apache listening on port 8080. I use the Nginx proxy_pass directive to pass all PHP and Django domains (virtual hosts) to Apache. I will continue to use Apache for my existing virtual hosts, but I am going to use PHP-FPM for any new PHP domains that I set up.

PHP-FPM

Since I have already installed PHP and Nginx (see: Setting up Ubuntu for Web Development and Using Nginx and Apache together on Ubuntu), I only need to install PHP-FPM.

sudo apt-get install php5-fpm

If PHP is not already installed:

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

By default, php-fpm is listening on port 9000. To verify that the port is in use:

netstat -antp

Nginx

To install Nginx

sudo apt-get install nginx

Setting up Virtual Host

I am setting up a browser-based MongoDB administration application that built with PHP. The application is placed in:

/usr/share/nginx/www/rockmongo

I need to set up the virtual host for the application.

sudo vi /etc/nginx/sites-available/rockmongo
 
server {
        root /usr/share/nginx/www/rockmongo;
        index index.php;
 
        server_name rockmongo.local;
 
        location ~ \.php$ {
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_index index.php;
              include fastcgi_params;
        }
 
        error_log /var/log/nginx/rockmongo-error.log;
}

Note: I’m using Ubuntu 12.04. In 12.10 and later replace:

fastcgi_pass 127.0.0.1:9000;

with:

fastcgi_pass unix:/var/run/php5-fpm.sock;

Now I need to enable the site

sudo ln -s /etc/nginx/sites-available/rockmongo /etc/nginx/sites-enabled/rockmongo

Restart php-fpm and Nginx

sudo service php5-fpm restart
sudo service nginx restart

Finally, I need to add the site to my hosts file

sudo vi /etc/hosts
 
127.0.0.1       rockmongo.local

Now, when I navigate to rockmongo.local, I am prompted to login.

That’s it. Now, I can run my PHP-based applications using PHP-FPM and Nginx. I now have a LAMP stack (Apache), LEMP stack (Nginx), and LEAMP stack (Nginx and Apache) running on Ubuntu – as well as a MEAN stack (Express) and MEEAN stack (Nginx and Express). Enjoy!

Be Sociable, Share!

Checkout My New Site - T-shirts For Geeks