Networking HowTos
Networking HowTos

Installing NGINX, PHP, and MySQL on Ubuntu 10.04 LTS

January 22, 2012 Linux, Ubuntu

Nginx (pronounced Engine-X) is a fast & lightweight HTTP and HTTPS web server (it can also act as a reverse proxy, and perform load balancing).
Its small memory footprint requirements make it great for systems with small amounts of memory, such as low end cloud servers. Nginx is great for serving static files to users, and is cable of handling more than 10,000 simultaneous connections, but it lacks the embedded module support for PHP as Apache does.
Thankfully you can use the PHP FPM FastCGI package to add PHP support to Nginx, and end up with a PHP enabled web server running on a low footprint setup. Nginx essentially offloads processing of .php files to the PHP FPM Fast CGI package, which in turn passes the interpreted script information back to Nginx, to return back to the end user.

Note: If you encounter issues with adding the 3rd party repository’s for php-fpm in this article, please see the revised article here:
Installing NGINX, PHP, and MySQL on Ubuntu 10.04 LTS using php-cgi.
This revised article linked above uses the php5-cgi module from the standard Ubuntu repositories, instead of php5-fpm from 3rd party repositories.

The how to article below describes installing the usual components for the LAMP stack (Linux, Apache, MySQL, and PHP), however the Apache part is exchanged for Nginx.
Make sure the apt sources are up to date before installing:

$ sudo apt-get update

Install MySQL:

$ sudo apt-get install -y mysql-server mysql-client

You will be asked to enter a “root” password for the MySQL server. Generate a strong password, type it in, and keep note of it for future reference.
Run through the “mysql_secure_installation” script to lock down the MySQL installation.

$ mysql_secure_installation

You will generally want to answer the prompts with “y”, apart from the first, asking if you want to change the root MySQL password. Assuming you set a strong password in the previous step, you can select “n” for this question.
Install Nginx:

$ sudo apt-get install -y nginx

To use PHP with Nginx, you require the PHP Fast CGI package. To install the PHP Fast CGI package on Ubuntu 10.04, you first need to add an additional repository:

$ sudo apt-get install -y python-software-properties
$ sudo add-apt-repository ppa:nginx/php5
$ sudo add-apt-repository ppa:brianmercer/php
$ sudo add-apt-repository ppa:brianmercer/php5
$ sudo add-apt-repository ppa:l-mierzwa/lucid-php5

Note: If you get errors here, check the comments for other repository’s you can use instead.
Update the apt package sources again to ensure the newly added repository has been updated.

$ sudo apt-get update

Install PHP and any other modules that you may require:

$ sudo apt-get install -y php5-fpm php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick \
php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-ps php5-pspell php5-recode \
php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json

Note: You can modify the above command to add/remove individual PHP packages if needed. The php5-fpm package is however required when used with Nginx.
Edit the Nginx configuration file so that it knows what to do with .PHP files:

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

There will be a section with the heading starting with “pass the PHP scripts to FastCGI server” as seen below:

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
                #fastcgi_pass   127.0.0.1:9000;
                #fastcgi_index  index.php;
                #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                #includefastcgi_params;
        #}

Uncomment the lines as shown below, and modify the “fastcgi_param” line to reflect your website folder path. In my example I am using /var/www.
Also take note of the space added to the “include” line. For some reason the example code in the config file was missing the space. Add this space (or tab) as per the below example.

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
                include fastcgi_params;
        }

Save and exit the editor.
Please note that there could be potential security issues with this generic base configuration (particularly if you are running a publicly accessible website, and allow file uploads). If you would like further information on this, please read the blog post at https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/ and also read through the Nginx documentation.
Restart PHP Fast CGI:

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

Note: php5-fpm should start automatically after being installed, but we will ensure that it is running by issuing the restart command.
Start Nginx:

$ sudo /etc/init.d/nginx start

Create a .php file containing the following PHP code and save it in your website folder and call it test.php (/var/www/test.php in my example.):

<?PHP
   phpinfo();
?>

Load up your site in a browser, and add the file /test.php to the url, and make sure it comes up with the usual PHP information page, and not just the source code for the .php file you created.
You should now have a working Nginx, PHP, and MySQL stack. Read the Nginx documentation for more detailed configuration options.
The Nginx documentation can be found on the Nginx website at http://nginx.org/

You Might Also Like

  • Andreas February 9, 2012 at 11:15 pm

    I got following msg, Error: can’t find signing_key_fingerprint at https://launchpad.net/api/1.0/~nginx/+archive/php5, when i do the add-apt-repository ppa:nginx/php5 command.
    Whats wrong?

    • admin February 10, 2012 at 12:39 am

      It looks like that repository doesn’t exist anymore. Try this one instead:

      sudo add-apt-repository ppa:brianmercer/php
      

      (Make sure you run the ‘sudo apt-get update’ command after adding that repository).

  • Mark April 4, 2012 at 3:59 am

    mark@server:~$ sudo add-apt-repository ppa:brianmercer/php
    Error: can’t find signing_key_fingerprint at https://launchpad.net/api/1.0/~brianmercer/+archive/php
    Seems every copy of php5-fpm for Ubuntu 10.04 is disappearing.

    • admin April 5, 2012 at 2:22 am

      Yeah, it seems to be causing a few issues. I have added an updated repository address in the howto with one that should work (at least for now):

      $ sudo add-apt-repository ppa:brianmercer/php5

      (Make sure you run the ‘sudo apt-get update’ command after adding that repository).
      Failing this, I have created another guide that uses PHP-CGI (uses standard Ubuntu repository’s) instead of PHP-FPM (uses 3rd party repository’s) which can be found here:
      http://www.networkinghowtos.com/howto/installing-nginx-php-and-mysql-on-ubuntu-10-04-lts-using-php-cgi/
      Hope this helps.

  • damusnet April 13, 2012 at 8:12 pm

    Hi there!
    Really great tutorial, and the most up to date in my opinion. Just a quick suggestion: you should warn people to execute “mysql_secure_installation” after they installed mysql, it never hurts!
    Also, you could provide some further steps regarding phpMyAdmin, and the nginx configuration for it, but that’s really optional.
    Thanks again,

    • admin April 14, 2012 at 11:20 pm

      You’re welcome. Thanks for the suggestions. I have added the tip about mysql_secure_installation into the guide. Might look at doing the phpMyAdmin and nginx config stuff in separate guides.
      Cheers.

  • Brendan April 15, 2012 at 5:40 am

    Hey, thanks for the great (and well maintained) guide!
    In case anyone runs into any issues restarting nginx after installing php5-fpm with the error
    Restarting nginx: nginx: [emerg] unknown “https” variable
    nginx: configuration file /etc/nginx/nginx.conf test failed
    Try commenting out the https line in /etc/nginx/fastcgi_params
    # fastcgi_param HTTPS $https;
    That should do the trick!

    • admin April 15, 2012 at 11:18 pm

      Thanks for the tip!
      I didn’t have that line in my fastcgi_params file, but I’m sure it will help others who come across this issue.
      Cheers.

  • Manu April 25, 2012 at 4:02 am

    Hi,
    I tried your tutorial and, at least, after 4 different tutorials and 4 new installations of the server after them I found just your tutorial and it worked (I’m so happy :D)
    But there is now a problem and I don’t know to which topic it sticks.
    If I go to the main page (let’s say http://localhost/) then I get the “Welcome to nginx!” front page (Nothing special, everything works fine).
    But if I create the test.php file in /var/www/ and browse to http://localhost/test.php I get an error that says “File not found.”. Is there something wrong (seems to be)?
    I had to create the www-folder on myswlf because php did’nt created it… Is there something wrong with the folder/file rights?
    Please I need help^^
    Greetz

    • Manu April 25, 2012 at 7:23 am

      Ok, I think I fixed it on my own^^
      I added simply in the file /etc/nginx/sites-available/default after this line:
      “fastcgi_pass 127.0.0.1:9000;”
      This line in addition:
      “root /var/www;”
      Now I get the PHPInfo displayed =) Thanks again for the great tutorial (btw: if it’s necessary: I used the NGINX v1.2 and PHP 5.3.10)

      • Kris July 7, 2012 at 4:04 am

        Thanx man! I had the same problem and adding “root /var/www;”, as you said, worked ! 🙂

  • Nick Nasty November 18, 2012 at 7:41 pm

    Hi,
    When I ran this line:
    sudo add-apt-repository ppa:brianmercer/php
    The response stated:
    You are about to add the following PPA to your system:
    Brian’s nginx
    nginx_1.2.4 for 12.04 (precise).
    Do you know where to get this for 10.04 lts as the title of this article suggests?
    Thanks,
    Nick

    • admin November 22, 2012 at 12:49 am

      Hi Nick,
      It looks like that repository doesn’t work anymore. I have updated the post above to use the following repository instead:

      $ sudo add-apt-repository ppa:l-mierzwa/lucid-php5

      I’ve just tested this on Ubuntu 10.04 and it currently works fine.
      Cheers.