How to install nginx, PHP-FPM and MySQL on Ubuntu

Published June 28th, 2011 at 1:12 PM. by Joe Prochazka

(This post was updated to reflect the default file system paths used by Ubuntu 12.04 LTS. If you are using an older version of Ubuntu you should see the old paths and where to change them noted in the instructions for the step you are on.)

This weekend I decided after reading into the speed gains associated with moving from Apache to nginx to go ahead and fire up a new server and attempt to configure it to run nginx along with PHP-FPM, APC and Memcached. I must admit I was not expecting things to go as well as they did. Quite honestly besides a little bit of a learning curve figuring out rewrites using nginx the install was a breeze. The move to nginx from Apache has increased the performance of my web sites beyond what I honestly expected.

This post will cover the installation of nginx, PHP-FPM and MySQL. In order to keep the size of this post down some what I will cover the installation of APC and Memcached in a post following this one which can be found here.

The installation covered here was done on a Linux server running Ubuntu Server 10.10 Maverick Meerkat. The steps used to install these packages should however work on most other newer Ubuntu releases although I must admit I have yet to test these steps on any other version. I used only packages available in the official Ubuntu Repository. I went this route in order to keep future updates as easy and painless as possible. What follows next is the step by step route I took when installing nginx, PHP-FPM along with MySQL of course.

The first thing we will want to do before going on is to make sure our Ubuntu installation is up to date with the latest available packages.

sudo apt-get update
sudo apt-get upgrade

The next step will be to install nginx.

sudo apt-get install nginx

Then we will start nginx by running the following command.

sudo /etc/init.d/nginx start

After starting nginx enter http://<your-ip-address>/nginx-default in your web browser. After doing so you should be greeted with a "Welcome to nginx!" page. If you see this page congratulations you have a nginx server up and running and are now ready to move on to installing PHP-FPM.

Now that we have a running nginx web server we can begin the install of PHP-FPM. To install PHP-FPM simply run the following command.

sudo apt-get install php-fpm

Once the installation of PHP-FPM has completed we can begin to configure nginx to work with PHP-FPM. To do so we will need to edit a couple files. The first edit we will want to make is to up the number of default nginx worker processes from one to five. The number of processes you wish to use is up to you and would more then likely depend on your server hardware. To do this open up the file nginx.conf.

sudo nano /etc/nginx/nginx.conf

Look for the line that says “worker_processes” and change the amount of worker processes to 5 and save the file.

Next we will edit the configuration file for the default site, the one you opened in your browser earlier.

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

First off lets set index.php as one of the index files the web server will use. Look for the line under "location / {" starting with "index" and add "index.php" to this list of index files.

index index.php index.html index.htm;

Uncomment the section with the comment "pass the PHP scripts to FastCGI server listening on". You will need to make some changes to what is there by default. The main change will be adding the following if statement in order to help secure things up. Most tutorials I ran accross when learning the installation process skipped over this which after reading up on this would be a bad thing to skip over. Anyways your "pass the PHP scripts to FastCGI server listening on" section should resemble this once done. If you are running a version of Ubuntu older than 12.04 LTS make sure to change the " root" path in the following text to "/var/www".

location ~ \.php$ {
    root /usr/share/nginx/www;
    if (!-f $request_filename) {
        return 404;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;

After making these changes to our nginx configuration we will need to restart nginx.

sudo /etc/init.d/nginx restart

Now we will want to test our PHP-FPM integration by creating a file named "index.php" in the root of the default web site which should be located at "/usr/share/nginx/www/" in 12.04 LTS or in older versions of Ubuntu at "/var/www/".

sudo nano /usr/share/nginx/www/index.php

Add the following simple code line to index.php and save the file to the server.

<?php phpinfo(); ?>

Now open your web browser and goto the address http://<your-ip-address>/. After doing so you should be greeted with the output of the phpinfo() command containing information on your PHP installation. If you see this then congratulations once again, you have successfully integrated PHP-FPM into your nginx installation.

The next step we will take is to install MySQL onto the server. To install MySQL run the following command which will install both the server and the client on your server.

sudo apt-get install mysql-server mysql-client

Once you have installed these packages it is highly recommended that you run the following command and follow the step by step instructions in order to help secure your MySQL installation.

sudo mysql_secure_installation

Now lets install a few PHP modules which we will need in order for PHP to work with MySQL as well as a few of the more commonly used PHP modules. Depending on your situation you may wish to install any other needed PHP modules at this time by adding them to the following line.

sudo apt-get install php-mysql

After installing php5-mysql along with any other modules you may have needed restart the PHP-FPM process.

sudo /etc/init.d/php5-fpm restart

At this time you should now have a working installation of nginx able to utilize PHP and MySQL.

If you are interested in really upping the performance of your new install you may be interested in my next post which covers the installation of APC and Memcached which can be found here.

AdWords Ad