Setup Nginx, MySQL, PHP (LEMP stack) on Ubuntu 16.04

The first step to configure any Virtual Private Server or Virtual Machine is to configure a stack your application can run on. And there are some common stacks in the market, Like LAMP stack, LEMP stack, MEAN stack and many more.

In this guide, I will demonstrate how you can set up a LEMP stack on your Virtual Private Server. Here, LEMP stands for Linux, Nginx, MySQL, and PHP. The reason why LEMP stack is ubiquitous is, majority of the PHP websites can run on this stack very efficiently.

As we all know, WordPress is written in PHP, and WordPress is the most common Content Management system right now. There are more than 50% chances that you are going to host a WordPress site on a LEMP stack we are going to configure right now.

Finally, We can start the actual step-by-step guide on setting up Nginx, MySQL, and PHP on Ubuntu 16.04 server. But there are some prerequisites.

  • A clean Ubuntu 16.04 Virtual Private Server or Virtual Machine.
  • The root or sudo access to the server.

It’s good if you can get root access to the server. But, If you can’t, make sure you have sudo privileges on the server. It’s required because we are going to install various packages on the server.

So, If you are clear on prerequisites, we can get started.

Set up LEMP stack on Ubuntu 16.04

Basically, we have to install a web server (Nginx), a programming language (PHP), and a database server (MySQL) to deploy our application on the server. It’s that simple. We can start by updating the repositories that provide us with packages to install.

Execute the following command on the server to update the sources and upgrade packages if any updates are available.

$ sudo apt-get update && sudo apt-get upgrade -y

It may take a few minutes if any updates are pending on the server. Once done, we can start setting up our stack by installing Nginx.

Install Nginx web server

It’s always an easy task to install a web server on the Linux machine. We just have to execute a single command on the server to install Nginx on our Ubuntu 16.04 server. Execute the following command to install Nginx.

$ sudo apt-get install nginx -y

It will take less than a minute to install. You can verify if it’s appropriately installed by visiting your server’s Public IP address in the browser. It should show a page just like the following image.

Nginx Default Page - Setting up LEMP stack

If you can see such a page, Congratulations! Our web server is installed and is working correctly. Now, we can move on to install the MySQL database server on our VPS/VM.

Install MySQL database server

It takes a few more steps to install MySQL on our Ubuntu 16.04 server than Nginx did. But it’s effortless and straightforward. Execute the following commands to install MySQL server and MySQL client on your machine.

$ sudo apt-get install mysql-server mysql-client -y

In the process, it will ask you to set up the password of the root user in your MySQL server. Enter the strong password and hit the Enter key. It will also ask you to confirm your MySQL password. Enter the same password again and hit Enter.

It will take a few more seconds to install the MySQL server. Now, let’s check if MySQL is properly installed or not by logging in as a root user. To log in, execute the following command.

$ sudo mysql -uroot -p;

It will ask you for a root password. Enter the correct password and you should be transferred to MySQL command-line interface, the login process looks just like the following.

root@test:~# mysql -uroot -p;
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.26-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

If you can successfully log in, Congratulations! Our MySQL server is ready too! But, we still have to secure our MySQL installation. Execute the following command to secure the MySQL installation in the standard way.

$ sudo mysql_secure_installation

It will ask you some questions and you can answer those questions based on your preference. If you have no clue what to answer, follow the below-given template.

  • Would you like to setup VALIDATE PASSWORD plugin? n (It’s totally up to you)
  • Change the password for root? n
  • Remove anonymous users? Y
  • Disallow root login remotely? Y
  • Remove test database and access to it? Y
  • Reload privilege tables now? Y

Once done, our MySQL installation as secure as we need. Finally, Our MySQL server is ready to use. The last thing we have to set up on our server is PHP. Let’s do it.

Install PHP on the server

The current goto versions of PHP are PHP7.0, PHP7.1, and PHP7.2. PHP version 7.3 is released and you can install it if you want. However, make sure your application will support the PHP version you are going to install.

It is recommended to install the PHP version on which you have developed your application to ensure it’s efficiency. And also, to avoid issues of deprecated functions. In this demonstration, I am going to install PHP7.0 which is default in Ubuntu 16.04.

Execute the following command to install PHP along with some commonly required extensions of PHP.

$ sudo apt-get install php-fpm php-curl php-dom php-gd php-mcrypt php-mysql php-json php-mbstring -y

It will take a few minutes to install. Once the process is complete, verify the installation by executing the following command.

$ sudo php -v

The output of this command should look similar to the one given below.

PHP 7.0.33-0ubuntu0.16.04.5 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.33-0ubuntu0.16.04.5, Copyright (c) 1999-2017, by Zend Technologies

If you can see a similar output, Congratulations! PHP is ready to use. But, we still have to make Nginx server aware of the PHP. So that it can send .php files to PHP processor for processing. It’s the last task we have to perform. And to complete this task, we have to edit the default Nginx virtual host.

Execute the following command to open the virtual host in nano editor.

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

Remove everything from the virtual host and replace the file contents with the one given below.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Now press CTRL+X followed by Y followed by the Enter key to save the configuration file. Then, we have to restart Nginx server to apply the changes. Execute the following command to restart the Nginx server.

$ sudo service nginx restart

Now, It’s time to check if we can actually host PHP files on our server.

Test the installation

It’s very easy. We just have to confirm that Nginx is sending .php files to the PHP processor to process. And it’s not considering PHP file as any other text file. To do so, we just have to create a test PHP file in our document root. And if PHP function works properly in the test file, it will work perfectly for your application.

We will create a PHP file with phpinfo() function. This function will list PHP configuration settings in the tabular format. To create a test file, execute the following command.

$ echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/index.php

Now, try to access the public IP address of your server in the browser. It should show a page just like the following image.

PHP installation verification

If you can see this page, Congratulations! You have successfully installed and configured LEMP stack on Ubuntu 16.04 machine. Now you can host your application files at /var/www/html and your application should be live on your brand new server.

 

Conclusion

This is how you can set up LEMP stack on your Ubuntu 16.04 VPS/VM. Nginx is the great web server and it is very efficient. However, If this set up was too much for you, you can go for Apache instead. The stack with Apache instead of Nginx is called LAMP stack.

A LAMP stack is easy to set up than LEMP stack. It’s because Apache comes with a PHP module that will send PHP files to PHP processor for configuration automatically. But the PHP module is slower than PHP-FPM. If you want to use PHP-FPM with Apache, you still have to perform the extra configuration steps we performed in this guide.

We hope you loved following this guide. If you are facing any issue setting up the LEMP stack, please let us know in the comment section, we are happy to help!

Leave a Reply