How to Install LEMP Server In Ubuntu

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:

sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx

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.

sudo service apache2 stop

Start Nginx.

sudo service nginx start

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

sudo /etc/init.d/nginx start

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 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:

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

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

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

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:

# location ~ \.php$ {
#   fastcgi_split_path_info ^(.+\.php)(/.+)$;
#   NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
 
#    # With php5-cgi alone
#    fastcgi_pass 127.0.0.1:9000;
#    # With php5-fpm;
#    fastcgi_pass unix:/var/run/php5-fpm.sock;
#    fastcgi_index index.php;
#    include fastcgi_params;
#  }

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:

location ~ \.php$ {
   try_files $uri =404;
   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
 
    # With php5-cgi alone
    fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm;
#  fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }

lemp-nginx-uncomment-php

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

Restart Nginx.

sudo service nginx restart

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.

sudo nano /usr/share/nginx/www/phpinfo.php

Add the following code to the blank file:

<?php phpinfo(); ?>

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.

Installing MySql

In the terminal, type the following:

sudo apt-get install mysql-server

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.

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,

sudo apt-get 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:

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

Add the following code after the php block of code:

location /phpmyadmin {
               root /usr/share/;
               index index.php index.html index.htm;
               location ~ ^/phpmyadmin/(.+\.php)$ {
                       try_files $uri =404;
                       root /usr/share/;
                       fastcgi_pass 127.0.0.1:9000;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                       include /etc/nginx/fastcgi_params;
               }
               location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                       root /usr/share/;
               }
        }
        location /phpMyAdmin {
               rewrite ^/* /phpmyadmin last;
        }

lemp-phpmyadmin-nginx

Save and exit the file. Restart Nginx.

sudo service nginx restart

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

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Damien Oh

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.