Getting Started with LEMP: Installing Nginx, MySQL, PHP on Ubuntu 18.04 (Part 3 of 3)

Welcome to the final installment of this 3 part guide! In the previous section, we went over how to configure server blocks, move our test pages into the proper directories, creating symbolic links, and editing our host file.

In part 3, we will be installing MySQL and PHP, configuring security settings, and ensuring Nginx can serve PHP files. Let’s get started.

Installing MySQL

As we have in other installations, we should update the package list before installing MySQL. You can accomplish that with the command below:

$ sudo apt update && sudo apt install mysql-server -y

You can verify the installation by running sudo service mysql statusand you should see the following output:

Status of MySQL

Press qto quit out of the service status page.

Configuring Security Settings for MySQL

You can now run the command mysql_secure_installationand will be prompted to create a root password for MySQL. You will be asked to set up the Validate Password Plugin. This isn’t really necessary so we can skip this by choosing the “No” option.

After being prompted to choosing a secure password, you will be asked a series of questions. For this tutorial, we will be choosing “Yes” for each option.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y 

To test your MySQL server installation run the following code to verify your version: sudo mysqladmin -p -u root version

A clean install of MySQL should result in this version page.

You’ve now successfully installed and configured MySQL on your soon to be complete Ubuntu LEMP stack!

Installing PHP

Nginx needs some configuration and tuning before it can start processing PHP files. There’s an easy solution to this. We can install php-fpm.

Let’s begin by updating the package lists and installing php-fpmalso while installing php-mysqlso the MySQL database can communicate with PHP.

$ sudo apt update && sudo apt install php-fpm php-mysql -y

You can check to make sure PHP was installed correctly with the command: php --versionand seeing the following output:

$ php --version
PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul  4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies

Your version of PHP may vary pending on when you are going through this installation. In this version, I’ve downloaded and installed PHP version 7.2.7. 

Also, depending on the version of Nginx and PHP you are running, you may need to manually configure the location of the PHP socket that Nginx connects to. You can list the contents for the directory./var/run/php Run the following command and confirm the output.

$ ls /var/run/php/  php7.2-fpm.sock

Configuring Nginx

We’re almost done! We need to make some changes to our Nginx server blocks. The location of your server blocks may vary, but for our tutorial, ours are located in/etc/nginx/sites-available/ /etc/nginx/sites-available/

Let’s go into nano and edit the lemptest1 file.

$ sudo nano /etc/nginx/sites-available/lemptest1

Scroll down to where you find index.htmland add index.php (make sure to add it before the HTML string.)

We’re also going to make sure our domains are listed by the server_name line.

Add index.php to the list of file types.

Finally, we are going need to uncomment some lines by removing the #signs before the lines listed below in bold and also changing the version number of the php-fpm socketto whatever was listed in an earlier step when running $ ls /var/run/php/:

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass;

See the screenshot below if you are still confused.

You will need to do this for every server block you configured, so in our case, we will need to repeat this process for nano /etc/nginx/sites-available/ For the sake of keeping this guide short, I won’t repeat myself so just run through the same steps for each server block.

Test PHP Files and Processing

Now it’s time to test all of our work. To see if PHP is running properly, we’re going to create a new PHP file called info.php. This file will live in the document root directory located in within our domians. We will place an info.php file in both/var/www/ /var/www/

$ sudo nano /var/www/

Type the following code into nano:


Save and close out of nano. If done successfully, you should be able to go see the following page.

You’ll see this page if PHP has been configured correctly.

The phpinfo()string outputs a large amount of information about the current state of PHP. This is helpful when testing but it is importnat to remove this file if you ever put your server out into production.

What’s Next?

That’s it! We’ve successfully configured a LEMP stack server in Ubuntu by installing Nginx, MySQL, and PHP. Now you can build anything really from an FTP server, mail server, or even live chat relay systems.

This is a very manual installation process and there are much easier ways to configure servers with everything already built in. Services like Docker, Vagrant, and even Amazon’s AWS EC2systems get servers configured quickly in just a matter of seconds, but it’s important to understand the underlying technologies in your server when it comes to troubleshooting issues on the fly.

Hope you enjoyed or at least learned something while following along. Follow me on Twitter @jovansn0wand visit my personal website at

Please leave suggestions, questions, or input in the comment section. Cheers!