Nur Rony
Polyglot Programmer, DevOps and Forever Learner

Setup and configure LEMP stack in Ubuntu 14.04

Setup and configure LEMP stack in Ubuntu 14.04

LEMP is stand for Linux, Nginx (pronounced Engine x), MySQL and PHP and altogether we can make a web server up and running.


I am assuming that you have a running Ubuntu 14.04 Server and Linux part have taken care of as well. The steps in this post require the user to have root privileges. If you do not have any root privilege then you can ask for it to your server administrator.

Install Nginx

I don't like writing too much description and jump to the commands that does the work. Run the following command in your terminal.

sudo apt-get update;  
sudo apt-get install -y nginx  

Now you have Nginx installed and should have it already running. If it is not running you can start, restart, stop, reload or syntax checking of configuration file of Nginx using the following commands

sudo service nginx start #start Nginx server  
sudo service nginx restart #retart Nginx server  
sudo service nginx reload #reload Nginx server  
sudo service nginx stop #stop Nginx server

You can confirm that nginx has installed an your web server by directing your browser to your IP address.

Install MySQL

MySQL is a powerful database management system used for organizing and retrieving data. To install MySQL, open terminal and type in these commands:

sudo apt-get update  
sudo apt-get install mysql-server php5-mysql -y  

During the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell.
Once you have installed MySQL, we should activate it with this command:

sudo mysql_install_db  

Finish up by running the MySQL set up script:

sudo /usr/bin/mysqlsecureinstallation 

The prompt will ask you for your current root password. Type it in and hit enter. If all goes fine then you will see the following lines.

Enter current password for root (enter for none):  
OK, successfully used password, moving on...  
Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps. It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes. Answering those the output will be looks like below

By default, a MySQL installation has an anonymous user, allowing anyone  
to log into MySQL without having to have a user account created for  
them.  This is intended only for testing, and to make the installation  
go a bit smoother.  You should remove them before moving into a  
production environment.

Remove anonymous users? [Y/n] y  
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This  
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y  
... Success!

By default, MySQL comes with a database named 'test' that anyone can  
access.  This is also intended only for testing, and should be removed  
before moving into a production environment.

Remove test database and access to it? [Y/n] y  
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far  
will take effect immediately.

Reload privilege tables now? [Y/n] y  
 ... Success!

Cleaning up...  

Cool!! We have done with MySQL installation and configuration.

Installing PHP

We need to make one small change in the php configuration.Open up php.ini:

sudo nano /etc/php5/fpm/php.ini  

Find and replace the line cgi.fix_pathinfo=1 and replace 1 to 0. If you find this line as commented uncomment it by removing #. Final result will look like below


If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. Save and Exit. We need to make another small change in the php5-fpm configuration.Open up www.conf:

sudo nano /etc/php5/fpm/pool.d/www.conf

Find the line, listen = and change the to /var/run/php5-fpm.sock.

listen = /var/run/php5-fpm.sock

save and exit

Restart php-fpm using following command

sudo service php5-fpm restart  

Configure Nginx for PHP

Open up the default virtual host file.

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

and replace server block with the following piece of lines

server {  
        listen   80;

        root /usr/share/nginx/www; #replce this line with your document root path
        index index.php index.html index.htm;


        location / {
                try_files $uri $uri/ /index.html;

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www;

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;



Testing your configuration

To test your configuration please create a file called info.php with following code

 * Filename: info.php
 * Location: In your document root directory 

Then save and exit

Restart Nginx server

sudo service nginx restart 

Go to http://[your-ip-or-domain-name]/info.php. You should see a PHP configuration page if all goes fine.

If you face any problem.. just give me a shout to blow my eardrums!!!. Good Luck!!!