Tips / Nginx

How to install Nginx, PHP5 / PHP-FPM, MySQL on Ubuntu 13.04

On this tutorial we will go through the installation process of Nginx, PHP with PHP-FPM with MySQL support on Ubuntu 13.04. Nginx is a fast & high perfromance web server, PHP is the most popular programming language of the web, and MySQL is the most used database server, that combination makes what we call LEMP stack (Linux, Nginx, MySQL and PHP).

On this how to, we will configure Nginx to handle the PHP dynamic files to PHP-FPM daemon, which is a fastcgi backend that will process all the PHP information. On Ubuntu 13.04, PHP-FPM binaries are available by default, so there is no need to install extra repositories into your system.

Pre requisites

Update apt sources

sudo apt-get update

Install MySQL

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

When asked, enter the root password try to generate a strong password, you will need it later in order to use phpmyadmin or the mysql shell console. After that, it is a good practice to run mysql_secure_installation script to secure your MySQL installation:


Start mysql server

Installing Nginx

Nginx is part of the default Ubuntu 13.04 repositories, so installing Nginx is a simple task:

sudo apt-get install nginx -y

Install PHP FPM and other common PHP modules

sudo apt-get -y install php5-common php5-fpm php5-mysql php5-curl php5-gd php-pear php5-mcrypt php5-memcache php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl 

Configure Nginx to serve PHP Files using PHP-FPM

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

There will be a section with the heading starting with # pass the PHP scripts to FastCGI, as you see below:

        # pass the PHP scripts to FastCGI server listening on
        #location ~ .php$ {
        #       fastcgi_split_path_info ^(.+.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #       # With php5-cgi alone:
        #       fastcgi_pass;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #       fastcgi_index index.php;
        #       include fastcgi_params;

Delete all those lines and paste this:

        location ~ .php$ {
                fastcgi_split_path_info ^(.+.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                # add the following line in for added security.
                try_files $uri =403;

Save the file and restart php-fpm

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

Then restart Nginx

sudo service nginx restart

Testing your php-nginx installation

Create a .php file with the following content inside and save it in your website folder and name it phpinfo.php (/usr/share/nginx/html/phpinfo.php in my server):

Open your browser and load that php file using http://your.ip.addr.ess/phpinfo.php or At that time you should have a working Nginx, PHP-FPM with MYSQL support on Ubuntu 13.04.

Popular search terms:

  • nginx fpm
  • depot de php-cgi et libapache2-mod-php5-common ubuntu 13 04
  • Install php5-mysql php5-fpm in ec2 ubuntu
  • sudo apt-get install nginx php5-fpm

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Paulo

    Very good guide but I do discover memcache not necessary if use php-fpm?

    because other said that Fastcgi has cache already in Nginx server

    is this correct or memcache can mix safely with fcgi server? thanks for your time.

    • admin

      Fastcgi_cache is good, but RAM cache (memcached) is better, faster. Other thing you can do is to mount your Nginx fastcgi_cache or proxy_cache in a tmpfs (RAM) partition, that won’t need memcached and will go directly trough RAM memory.