Tips / Nginx

How to install LEMP on Ubuntu 14.04

We all know what LAMP is, right? It means Linux, Apache, MySQL and PHP. It was the traditional stack where most developers built their apps for the past decade. However, on this tutorial we will replace the traditional MySQL server with a cool replacement called MariaDB, and Apache with the all mighty Nginx. On this tutorial based on Ubuntu 14.04 LTS you will learn how to replace the old LAMP with new LEMP stack.

Why MariaDB and Nginx?

MariaDB is now the new DB standart. It was built by the original developers of MySQL and is guaranteed to stay open source. MariaDB is a MySQL binary replacement, it works exactly the same as MySQL, but offers better speed, performance, new extensions and cool features to make your database administration and usage way better than with traditional MySQL installations. For a full list of features and a vs MySQL comparison please check out MariaDB’s official website.

Nginx on the other hand┬áhas been powering million of websites and replacing old Apache installations with its new architecture to improve speed, performance and scalability. With commercial and non commercial versions, Nginx is now a new standart in the dev community. And that’s why we are going to use it on this tutorial.

Installing LEMP on Ubuntu 14.04


  • Ubuntu 14.04 up and running
  • Root access

Installing Nginx

If you are using Apache, make sure to stop it to avoid any port conflicts:

service apache2 stop
apt-get install nginx
service nginx restart
update-rc.d nginx defaults

Installing MariaDB

apt-get install mariadb-server mariadb-client -y
service mysql start
update-rc.d mysql defaults

By default MariaDB does not set any password for the root user. Let’s set secure MariaDB a little bit more by running the mysql_secure_installation script, this script will set a new password and also remove the test database that comes with the initial MariaDB installation.


It wil look like this, press “y” for all the questions.

Enter current password for root (enter for none): <-- press enter
Set root password? [Y/n]
New password:
Re-enter new password:
Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n]
Reload privilege tables now? [Y/n]

Installing PHP-FPM and other PHP extensions

Now that MariaDB is installed and secured, we can start with PHP-FPM. On the old LAMP stack, PHP used to work as an Apache module, it was called mod_php. On the new LEMP stack we will install PHP-FPM (FastCGI Process Manager) daemon, that runs by its own on a different service than the web server. It’s scalable, independent and way faster than mod_php. Let’s explore how to get it work really fast, run this commands:

apt-get install php5-fpm
service php5-fpm start
update-rc.d php5-fpm defaults

Install some additional PHP modules

apt-get install php5-mysqlnd php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Configuring Nginx + PHP-FPM

In order to get your PHP files parsed by Nginx, you will have to create a basic Nginx configuration.
Read this guide to configure your nginx.conf file properly: Nginx Configuration Example
Other links that may be useful: How to create Nginx virtual hosts (Server Blocks) & 8 Essential Nginx Commands

Once you finished configuring nginx.conf, we will create a virtual host file:

Example of a default WordPress installation:

nano -w /etc/nginx/conf.d/

Paste this inside:

server {
access_log off;
error_log logs/ warn;

listen 80;
root /path/to/your/;
index index.php index.html index.htm;

location / {
# WordPress Rewrite rules
try_files $uri $uri/ /index.php?$args;

# PHP-FPM parsing
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;

Note: remember to replace “/path/to/your/” with your real document root path, and with your real domain name.

As you see, we included PHP-FPM parsing configuration using php5-fpm.sock, a socket defined in PHP-FPM configuration.┬áLet’s configure that PHP-FPM socket so our Nginx can serve PHP files without problems:

By default PHP-FPM should be listening on the socket /var/run/php5-fpm.sock, however, let’s make sure that it is configured in that way.

Edit this file:

nano -w /etc/php5/fpm/pool.d/www.conf

Find this variable:

listen =

And make sure it looks like:

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

This will make PHP-FPM listen on the TCP socket.

Apply this permissions to avoid gateway errors:

chmod go+rw /var/run/php5-fpm.sock

Reload PHP-FPM to apply changes:

service php5-fpm reload

Test your LEMP stack

Open your browser and surf your local IP: or

You should see the default Nginx welcome page.

If you want to test that PHP-FPM is working, simply create a phpinfo file as you see below:

nano -w /path/to/your/

Paste this inside, and after that save and close the file:

<?php phpinfo(); ?> 

Then launch it from your browser: or, you should see something like this:


And that’s all, we have our LEMP stack working and ready to serve all kind of applications.


Installing the LEMP stack should be an easy task for you now, remember that technologies are changing every day and you should never stick to old stuff if you want to keep improving your apps in terms of development, speed and scalability. Try our LAMP stack tutorial and let us know if you face any issues, we will gladly help.

Popular search terms:

  • semalt com
  • NginxTutorials LinuxSysadminConfiguration&OptimizingTipsandTricks
  • nginx tips
  • nginx tutorial

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking