How to Install LEMP Server In Ubuntu Precise 12.04

A LEMP server refers to a server running Linux, Enginx (Nginx), MySql and PHP (or Perl/Python). It is similar to the popular LAMP server except that the underlying web server is managed by Nginx instead of Apache. In this tutorial, we will show you how to install LEMP server in Ubuntu 12.04.

Nginx vs Apache

For those who are not aware, Nginx is an open-source web server that can run faster and use lesser system resources than Apache. Under light load, the differences between Apache and Nginx is negligible. However, on heavy load, Nginx can scale accordingly and run as fast without taking up tons of memory resource. Apache is a beast by itself and can easily take up to several hundreds of RAM for heavy loads.

Note: This tutorial assumes that Ubuntu 12.04 is already installed in your system. Command lines will be used instead of graphical interface since most web server doesn’t come with a desktop manager.

Installing Nginx

In the terminal, type the following:

This will add the Nginx PPA to your repository so you are always updated with the latest stable version.

Just in case Apache is installed and running in the background, we need to stop it before Nginx can run.

Start Nginx.

If the above command doesn’t work, use this:

Open a browser and browse to “http://localhost“. For a remote web host, you should type in your IP address instead. You should see the following:


Installing and Configuring PHP

Installing PHP is easy, but getting it to work with Nginx will require some configuration.

To install PHP5 and other essential modules:

“php5-fpm” is the essential module for PHP to work in Nginx environment, so make sure it is installed.

Configuring Nginx to work with PHP

Open the “default” file in the /etc/nginx/sites-available directory.

Scroll down the list till you see the line index index.html index.htm;. Add a index.php at the end of the line, just before the “;“. It should become like this:


Next, scroll down further until you see this block of code:

Remove the first “#” at the front of end line (except the line # fastcgi_pass unix:/var/run/php5-fpm.sock;) and add an extra line try_files $uri =404; at line 2, so it becomes like this:


Save (press “Ctrl + o”) and exit (Ctrl + x) the config file.

Restart Nginx.

To test if php5 is working in Nginx, we are going to create a php file, place it in the Nginx folder and see if it shows up in the browser.

Add the following code to the blank file:

Save and exit the file.

Now, go to the browser and type “http://localhost/phpinfo.php” or “http://your-ip-address/phpinfo.php”.


If the php info shows up on the browser, then php is working fine with Nginx.

Installing MySql

In the terminal, type the following:

During the installation, it will prompt you to create the root password.


Once installed, you are done with the LEMP server setup.

Optional Install: phpmyadmin

Phpmyadmin is not part of the LEMP server setup, but it is very helpful for managing database and is often included in many web server setup.

To install phpmyadmin,

When prompted to select either “apache2” or “lighttpd”, select none and click OK.


When prompted whether to configure database with dbconfig-common, select No.


After the installation, open the Nginx’s “default” file:

Add the following code after the php block of code:


Save and exit the file. Restart Nginx.

You should be able to connect to phpmyadmin via the URL: “http://localhost/phpmyadmin” or “http://your-ip-address/phpmyadmin

Damien Damien

Damien Oh started writing tech articles since 2007 and has over 10 years of experience in the tech industry. He is proficient in Windows, Linux, Mac, Android and iOS, and worked as a part time WordPress Developer. He is currently the owner and Editor-in-Chief of Make Tech Easier.


    1. Josh, hope this is useful for you. Nginx is pretty easy to use. Once you understand how it works, the setting up is just a breeze. I have not tested it on python though.

  1. This tutorial is really cool. Everything worked so far except the last step. I’m getting “The mysqli extension is missing. Please check your PHP configuration.” in /phpmyadmin. There is no mysqli module in phpinfo.php… What can I do?

  2. I uncommented “mysqli.allow_local_infile = On” in php.ini and rebooted. Now it works :) I hope this will help someone.

    1. Thanks. in some instances, you do have to activate certain modules manually. Glad you have got it to work.

    1. You will have to add another configuration file to the “sites-available” account. You can use the “default” file as the template, but you need to add another line “server_name” at the top (remember to change the domain “” to your own domain). Do also remember to change the server_root to point to the folder where the webpages are stored. Lastly, make a symlink to this file from the “sites-enabled” folder.

  3. This is great. Thanks.
    But when I go to http://myip/phpmyadmin I get a message that the mysqli extension is missing. Any ideas?

    1. You can try to reinstall the mysql packages again.

      sudo apt-get install --reinstall mysql-server php5-mysql

  4. The tutorial works great!

    For the mysqli extension error, just go to php.ini file and at etc/php5 or etc/php and make this line “; mysqli.allow_local_infile = On” look like this “mysqli.allow_local_infile = On”. Thanks a bunch!

  5. Question. How can I change the nginx default document root directory? Thanks!
    I’m new to Nginx and want to explore

    1. In the config file under the “sites-available” folder, you can change the
      document root to point to another location.

  6. Thanks for this, really helpful – I’m having trouble setting write permission for php-fpm on the site root directory. I’ve tried changing the username used by fpm to ‘nginx’ but still no dice. Also tried adding ‘www-data’ group to the folder permissions. Any ideas? Many thanks!

  7. Thanks for your guide.
    Just a quick note: you must install python-software-properties first to use the add-apt-repository command

    1. Thanks for the feedback. Depend on the Ubuntu version you are using,
      “python-software-properties” could already be pre-installed in the system.

  8. Well, that’s embarrassing, but when I try to start Nginx for the first time, I get an error message: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use). This message occurs four times before this one: nginx: [emerg] still could not bind(). Someone have any idea about this? I followed the installation steps exactly how they’re written.

    1. It seems that there is already service (probably apache) running at port
      80. You can use the command sudo lsof -i tcp:80 to check which
      service is currently running at port 80.

  9. In the image for the fast cgi conf you have both directives uncommented. nginx won’t start because of this ambiguous configuration. BTW I would recommend using a socket rather than a TCP port, because it is faster locally.

  10. I did all above steps till “http://your-ip-address/phpinfo.php”. after typing this i get 502 bad gateway , do you have any idea about this error? what should i di in this situation?

    1. There are plenty of reasons for 502 bad gateway error. It could be that there is syntax error in your “default” configuration, or that the root is not pointed to the “www” folder, or your php5-fpm module is not properly configured.

  11. I’ve setup everything above. But how can i connect from windows machine to connect into my ubuntu machine. Pls. help.

    1. If both your Windows and Ubuntu machine are connected in the same wifi network, you can even access the webserver by typing in the IP address of the Ubuntu machine (in most cases, it will be something like

Comments are closed.