Share this post
FaceBook  Twitter  

1. Introduction

LEMP is an alternative to LAMP, the Linux based web server configuration stack using MySQL and PHP. However,  instead of Apache LEMP is deployed with Nginx ( pronounced engine-x or en-juhn-eks ) web server.  Nginx is a free, open-source, high-performance HTTP server and reverse proxy, which can be also used as a mail proxy. Nginx is acclaimed for having low memory usage and thus it is favored solution for a low RAM web server deployments such as VPS, etc. The difference in a memory consumption and serving speed between Apache and Nginx is likely to be noticeable under a heavy load. Thus, configuring Nginx should also help you to protect against sudden traffic spikes. However, the choice of using Ngnix over Apache is a matter of your environment where many aspects needs to be taken into the consideration. These  are the content to be served, configuration settings, number of modules enabled, etc. In this tutorial we will setup a basic LEMP configuration on Ubuntu Linux.

2. Notes

  • Ubuntu 12.04 LTS
  • nginx version: nginx/1.1.19
  • website installation path: /var/www
  • hostname: nginx.local

Change your DNS or /etc/hosts file to point to nginx.local.

3. Nginx

3.1. Installation

Let's start with installation of the Nginx web server. This can be easily done by:

$ sudo apt-get install nginx

The above command will install Nginx. By default it also contains a default page located in /usr/share/nginx/www.

3.2. Configuration

We will deploy our own website under /var/www, which will be accessible by http://nginx.local. First, create a /var/www directory

$ sudo mkdir /var/www

Create a simple html file:

$ sudo echo "nginx test" > /var/www/index.html

At this point we need to create a new site, which will point to this location. Create a new site configuration file /etc/nginx/sites-available/nginx.local with the following content.

server {
        root /var/www;
        index index.html;
        server_name nginx.local;
        location / {
                try_files $uri $uri/ /index.html;

Now, that we have created a new site configuration file, we need to enable it by creating a symbolic like in /etc/nginx/sites-enabled:

$ cd /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/nginx.local .

Once ready restart your nginx web server:

$ sudo service nginx restart

If configured correctly you should be able to see your simple page by navigating to http://nginx.local.

nginx install ubuntu linux

4. PHP

At this point we can add PHP to our current configuration.

4.1. Installation

First, we need to install all required packages including the fastcgi process spawner, which will be used to spawn our PHP scripts. To do that use the following command:

$ sudo apt-get install php5-cgi spawn-fcgi

4.2. Configuration

Now, edit the site's configuration file /etc/nginx/sites-available/nginx.local and add the following configuration block. New changes are highlighted in bold:

server {
        root /var/www;
        index index.php index.html;
        server_name nginx.local;
        location / {
                try_files $uri $uri/ /index.html;

       location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

Once you have made above changes, restart your Nginx web server:

$ sudo service nginx restart

Be aware that this is a simplistic configuration and further security enhancements are recommend. As you can see, when looking at the above configuration file, we are using the fastcgi process spawner to interpret any PHP requests thus we need to start it by:

$ sudo spawn-fcgi -a -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi
spawn-fcgi: child spawned successfully: PID: 2237

All should be ready now in order to test our Nginx and PHP configuration. Create an index.php testing file by copy and paste the below lines into your terminal and refresh your http://nginx.local webpage:

sudo sh -c "cat > /var/www/index.php" << EOF
nginx php include

5. MySQL

All what has left is to include MySQL database to our stack and this can be easily achieved by installation of  appropriate packages and restarting our fastcgi process spawner. First, install MySQL:

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

If you already have mysql database installed all you need is to install php5-mysql. But chances are that you have also this package available. Once installed restart the fastcgi process spawner:

$ sudo pkill php5-cgi
$ sudo spawn-fcgi -a -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi
spawn-fcgi: child spawned successfully: PID: 3718

Congratulations. Your LEMP configuration is now ready.

6. Conclusion

This article described a basic setup of LEMP web server on Ubuntu Linux. Further enhancements to this configuration can be made by creating a spawn-fcgi /etc/init.d script for an easy restart. Also make sure that you secure you upload directory if you intend to have one.

The above command will install Nginx. By default it also contains a default page located in /usr/share/nginx/www.


Who are we? is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.


You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.