How to install LEMP on Ubuntu 14.04 LTS

Updated on December 19, 2019

LEMP is a group of open source software stack used to setup web server, database and PHP on a Linux operating system to serve dynamic web pages. LEMP stands for Linux, Nginx (pronounced as Engine X) web server, MySQL database and PHP. Another stack called LAMP (alternative to LEMP) is also available in which Nginx web server is replaced with Apache. In this tutorial, I will show you how to install LEMP on Ubuntu (Well, we’ll not be installing Linux, of course).

Step 1: Prerequisites

Before we start the installation, make sure you have root or sudo privileges (best practices of sudo setup). Then login to your server either using PuTTY or SSH.

Also Read: 10 things you probably wouldn’t have tried in PuTTY ?

Also Read: [Linux] :Top 12 security features to Enable in SSH server !

Update local package index of the server

#sudo apt-get update

Step 2: Install Nginx web server

NGINX is a free, open-source HTTP server known for its high performance, stability, rich feature set, simple configuration and low resource consumption. Run the below command to download and install Nginx from Ubuntu’s default package repository.

#sudo apt-get install nginx

By default, Nginx daemon will be started after installation.

#ps -eaf|grep nginx
 root 2200 1 0 11:34 ? 00:00:00 nginx: master process /usr/sbin nginx
 www-data 2201 2200 0 11:34 ? 00:00:00 nginx: worker process
 www-data 2202 2200 0 11:34 ? 00:00:00 nginx: worker process
 www-data 2203 2200 0 11:34 ? 00:00:00 nginx: worker process
 www-data 2204 2200 0 11:34 ? 00:00:00 nginx: worker process

Obtain server IP address and access the below URL in your browser replacing the server IP address. Nginx’s default landing page would be displayed as shown in the below image:


Nginx default landing page

With this, you have successfully installed Nginx. Learn to setup basic nginx configuration.

Also Read: Articles on Nginx errors, fixes and configurations.

Step 3: Install MySQL database

In this step, we shall be installing MySQL, a database management system to store and manage the website data. Ubuntu’s default package repository will have MySQL version up to 5.5. So, if you run the below command, then mysql-server version 5.5 will be installed.

#apt-get install mysql-server

To install the latest version of MySQL server i.e., version 5.7, you’ll need to add the newer APT package repository from the official website of MySQL. Go to MySQL APT repository page and click Download at the bottom right corner. Right click on “No thanks, just start my download” and copy link address

Download .deb package to your server

# wget

Install the downloaded .deb file using dpkg – Package Manager.

# dpkg -i mysql-apt-config_0.8.3-1_all.deb

You’ll see a prompt that asks you “which MySQL product do you wish to configure?”. The MySQL server option, which is highlighted, would say your current MySQL version, e.g., mysql-5.6 (in my case). Hit ENTER and scroll down to mysql-5.7 using the arrow keys, and hit ENTER again, which comes back to the first prompt. Now scroll down to ‘Ok’ using the arrow keys and hit ENTER to exit.

Update package index on the server – This step is very important.

#apt-get update

Finally, install the MySQL server version 5.7 from the repository. If you have already installed MySQL v5.6, then it would upgrade the necessary packages to 5.7.

#apt-get install mysql-server

During the installation, you’ll be prompted to create a root password, which would be needed later to create and manage databases. Remember to choose a secure password.

That’s it! You have now installed MySQL server version 5.7, but its configuration is not exactly complete yet.

Initialize the MySQL data directory

If you are using MySQL version 5.7.6, you should initialize the data directory by running the below command:

#mysqld --initialize

However, you may end up with a below error:

2017-05-16T08:40:11.580471Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2017-05-16T08:40:11.580644Z 0 [ERROR] Aborting

Because in MySQL version 5.7, the data directory was initialized automatically. So you can ignore this ERROR.

If you are running MySQL versions earlier than 5.7.6, then you can use the below command:


Securing MySQL installation

To improve the security of your MySQL installation, run the below command:

# mysql_secure_installation

This will prompt for the root password. Enter the password you created earlier during the installation. A series of questions are asked for which you need to say yes or no. This program enables the following security feature when choose yes option:

1. You can set a password for root accounts.

2. You can remove root accounts that are accessible from outside the local host.

3. You can remove anonymous-user accounts.

4. You can remove the test database.

You can start and stop the MySQL service as below:

#service mysql start|stop|restart|status

Now MySQL is ready. In any case if you want to know more on MySQL errors, fixes and configurations.

Step 4: Install PHP

In this tutorial, we’ll be installing PHP for developing dynamic and interactive web pages. Unlike Apache, Nginx does not contain native PHP processing capabilities. So, PHP-fpm (FastCGI Process Manager) needs to be installed. PHP requests will be processed by PHP-FPM.

Like MySQL, Ubuntu’s default package repository contains PHP 5.5 version, which can be installed along with PHP5-fpm and php5-mysql extensions.

#apt-get install php5 php5-fpm php5-mysql

To install PHP package with versions above 5.5, you need to configure the repository as below:

#add-apt-repository ppa:ondrej/php

Update package index on the server – This step is very important.

#apt-get update

Now you can install PHP versions 5.6, 7.0 and 7.1 as per your need.

For installing PHP 5.6

#apt-get install php5.6 php5.6-fpm php5.6-mysql 

For installing PHP 7.0

#apt-get install php7.0 php7.0-fpm php7.0-mysql

For Installing PHP 7.1

#apt-get install php7.1 php7.1-fpm php7.1-mysql

Configure PHP processor

Open php-fpm configuration file with root privileges. Based on the versions of PHP installed, the location file might change.

For PHP 7.0

# vim /etc/php/7.0/fpm/php.ini

For PHP 7.1

# vim /etc/php/7.1/fpm/php.ini

For PHP 5.5

# vim /etc/php5/fpm/php.ini

Search for the line cgi.fix_pathinfo. This line should be un-commented [by removing semi-colon(;) at the start of the line] and the value should be set to 0. Save and close the file.


Note: Setting the value to 1 tells PHP to attempt to execute the closest file it can find, incase if a PHP file does not match exactly. This is an insecure setting that would allow users to execute PHP scripts which they aren’t allowed to.

Restart the PHP processor service

For PHp7.0

#service php7.0-fpm restart

For PHP7.1

#service php7.1-fpm restart

For PHP5.6

#service php5.6-fpm restart

For PHP5.5

#service php5-fpm restart

In any case if you want to know more – read this article PHP errors, fixes and configurations.

Step 5: Configure Nginx to use PHP processor (PHP-FPM)

We have installed webserver (nginx), MySQL (A data storage), PHP (for making dynamic and interactive web pages) and PHP-FPM (for PHP files processing). Now we need to integrate Nginx and PHP preprocessor.

Open the default Nginx server block configuration as below:

vim /etc/nginx/sites-available/default

By default, below is the configuration after removing the comments:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
    server_name localhost;
    location / {
         try_files $uri $uri/ =404;

Make changes to the configuration as shown below: (look out for the red colored lines)

server {
     listen 80 default_server;
     listen [::]:80 default_server ipv6only=on;
     root /usr/share/nginx/html;
     index index.php index.html index.htm;
     server_name server_domain_name_or_IP;
     location / {
        try_files $uri $uri/ =404;
error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;


     location ~ \.php$ {

          try_files $uri =404;

          fastcgi_split_path_info ^(.+\.php)(/.+)$;

          fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

          fastcgi_index index.php;

          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

          include fastcgi_params;



Restart Nginx

#service nginx restart

Step 6: Create a PHP file

Create a new file called index.php as shown below:

# vim /usr/share/nginx/html/index.php

Paste the below contents into the file:


Save and close the file.

Now access PHP page in your browser as shown below to see the web page that displays PHP configurations and information about the server.


Note: As a security practice, you should remove the file containing phpinfo() function, as it provides the hacker the complete information about your PHP configurations and web server.


That’s it! You are done with LEMP stack setup on Ubuntu 14.04 server.

Was this article helpful?

Related Articles

Leave a Comment