How to Install a LEMP (Linux, Nginx, MySQL, PHP) Stack on Ubuntu 16.04

How to Install a LEMP (Linux, Nginx, MySQL, PHP) Stack on Ubuntu 16.04



The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications. This is an acronym that describes a Linux operating system, with an Nginx web server. The backend data is stored in the MySQL database and the dynamic processing is handled by PHP.

In this guide, we will demonstrate how to install a LEMP stack on an Ubuntu 16.04 server. The Ubuntu operating system takes care of the first requirement. We will describe how to get the rest of the components up and running.


Step 1: Install the Nginx/1.10.3 Web Server

1. Nginx is a modern and resources efficient web server used to display web pages to visitors on the internet. We’ll start by installing Nginx web server from Ubuntu official repositories by using the apt command line.


2. Next, issue the netstat and systemctl commands in order to confirm if Nginx is started and binds on port 80.




Once you have the confirmation that the server is started you can open a browser and navigate to your server IP address or DNS record using HTTP protocol in order to visit Nginx default web page.

If you do not have a domain name pointed at your server and you do not know your server’s public IP address, you can find it by typing one of the following into your terminal:

This will print out a few IP addresses. You can try each of them in turn in your web browser.

As an alternative, you can check which IP address is accessible as viewed from other locations on the internet:

Type one of the addresses that you receive in your web browser. It should take you to Nginx’s default landing page:

If you see the above page, you have successfully installed Nginx.


Step 2: Install MySQL/5.7.20 to Manage Site Data

Now that we have a web server, we need to install MySQL, a database management system, to store and manage the data for our site.

You can install this easily by typing:

You will be asked to supply a root (administrative) password for use within the MySQL system.

After it’s installed, MariaDB server should be automatically started. Use systemctl to check its status.

The MySQL database software is now installed, but its configuration is not exactly complete yet.

To secure the installation, we can run a simple security script that will ask whether we want to modify some insecure defaults. Begin the script by typing:



Step 3: Install PHP/7.0.25 for Processing

Nginx can be used with PHP dynamic processing language interpreter to generate dynamic web content with the help of FastCGI process manager obtained by installing the php-fpm binary package from Ubuntu official repositories.

Enter the following command to install PHP7 and PHP7 extensions.


Configure the PHP Processor

We now have our PHP components installed, but we need to make a slight configuration change to make our setup more secure.

Open the main php-fpm configuration file with root privileges:

Find the line ‘cgi.fix_pathinfo=1′, uncomment it and change the value 1 to 0.

fix warning from date() in PHP

Save and close the file when you are finished.

Now, we just need to restart our PHP processor by typing:

This will implement the change that we made.


Step 4: Configure Nginx to Use the PHP Processor

Now, we have all of the required components installed. The only configuration change we still need is to tell Nginx to use our PHP processor for dynamic content.

The configuration is easy to understand (you can learn more about it here: and here:

We do this on the server block level (server blocks are similar to Apache’s virtual hosts). Open the default Nginx server block configuration file by typing:

Paste the following text into the file.

Save the changes and test the new configuration

If any errors are reported, go back and recheck your file before continuing.

When you are ready, reload Nginx to make the necessary changes:


Step 5: Create a PHP File to Test Configuration

Your LEMP stack should now be completely set up. We can test it to validate that Nginx can correctly hand .php files off to our PHP processor.

We can do this by creating a test PHP file in our document root. Open a new file called info.php within your document root in your text editor:

Type or paste the following lines into the new file. This is valid PHP code that will return information about our server:

When you are finished, save and close the file.

Now, you can visit this page in your web browser by visiting your server’s domain name or public IP address followed by /info.php:

You should see a web page that has been generated by PHP with information about your server:

If you see a page that looks like this, you’ve set up PHP processing with Nginx successfully.

After verifying that Nginx renders the page correctly, it’s best to remove the file you created as it can actually give unauthorized users some hints about your configuration that may help them try to break in. You can always regenerate this file if you need it later.


That’s it. Your LEMP server is ready to use.



Installing PhpMyAdmin on Ubuntu 16.04 LEMP