Quick Start guide to install LEMP server on Ubuntu 12.04 (Linux + Nginx + MySql + PHP )

I have seen some online guides about this but really was after one specific to Ubuntu 12.04. So here goes:

Step 1 – Installing Nginx

First we need to add the Nginx source

sudo add-apt-repository ppa:nginx/stable

Next, as we always do after adding a source, we need to update

sudo apt-get update

And now we install it

sudo apt-get install nginx

Next, we must start the Nginx service.

sudo /etc/init.d/nginx start

To check if it is successful, try going to http://localhost/ in your browser. If you see a Welcome to Nginx page, it has been successful.

Step 2 – Installing PHP5:

Install PHP and any required extensions

sudo apt-get install php5-cli php5-cgi php5-fpm php5-mcrypt php5-mysql

Edit the default Nginx site config (if you are not familiar with vim, try entering ‘nano’ instead)

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

The changes you will need to make are as follows:

index index.html index.htm;


index index.html index.htm index.php;


root /srv/http/nginx;

^this path may be different in your install, it doesn’t matter as we are changing it!

root /var/www;

3) uncomment the following lines

 location ~ \.php$ {
     fastcgi_index index.php;
     include fastcgi_params;

Now quit vim (‘:wq’) and restart the nginx service.

sudo /etc/init.d/nginx restart

If the folder /var/www/ doesn’t exist, create it

sudo mkdir /var/www/

Step 3 – Install MySQL

And finally,

sudo apt-get install mysql-server

Step 4 – Check it works!

Create a test file

vim /var/www/test.php

Add this line

<?php phpinfo();?>

Save and quit vim (:wq) and go to http://localhost/test.php in your browser.
If you see a page displaying all your PHP settings, then it works!

Fork me on GitHub

8 thoughts on “Quick Start guide to install LEMP server on Ubuntu 12.04 (Linux + Nginx + MySql + PHP )”

  1. Small amendment, but for…

    sudo add-apt-repository ppa:nginx/stable

    …you need to have first installed python-software-properties.

    sudo apt-get install python-software-properties

  2. Olá amigo.
    Preciso de ajuda.
    ja tentei de tudo segui as instruções á risca mas o nginx não instala.
    dá a mensagem abaix:
    Lendo lista de pacotes … Pronto
    Construindo arvore de dependencias
    Lendi a informação de estado …pronto
    nginx ja é a versão mais nova.
    0 pacotes instalados, 0 pacotes novos instalados, 0 a serem removidos e 31 não atualizados,,,,, dai dá uma serie de erros e o erro abaixo
    ERROR: Module usblp does not exist in /proc/modules

    Pode me ajudade nisso/


  3. Hey man,

    Maybe you should put a little PHP code to make sure that the MySQL server works and will work with PHP. I’ve followed tons of guides like these over and over and MySQL has been a bitch.

  4. Thanx for the guideance. I spend about 30 minutes to figure out why ngnix does not accept my changes in ../sites-available/default.
    I fact i did something i always do. I made a backup of the configfile and put it under /sites-available/default_bak. Unfortunatly I forgot that ngnix includes all files in that directory, which leads to strange results. After removing my backup configfile everything worked fine.

  5. I found that nginx by default was talking to php5-fpm on the Unix socket, not on, meaning I got a 502 Bad Gateway error when following the instructions.

    I was able to fix this by uncommenting:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    instead of

    Cheers :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>