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.

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.

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:

lemp-nginx-started

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:

lemp-nginx-add-indexphp

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:

lemp-nginx-uncomment-php

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”.

lemp-phpinfo

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

In the terminal, type the following:

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

lemp-mysql-password

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

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.

lemp-phpmyadmin-setup

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

lemp-phpmyadmin-dbconfig

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

Add the following code after the php block of code:

lemp-phpmyadmin-nginx

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