This tutorial is going to show you how to install Nginx, MariaDB and PHP7.2 (LEMP) on Ubuntu 18.04. You can follow this tutorial on a VPS (Virtual Private Server) or on a local Ubuntu 18.04 computer. PHP7.2 is included in Ubuntu 18.04 and has a minor performance improvement over php7.2.

Prerequisite

To follow this tutorial, you need to have an Ubuntu 18.04 OS running on your local computer or on a remote server. If you are looking for a VPS (Virtual Private Server), then I recommend Linode, from which you can get a 1G RAM server for only 5 USD per month. You can also get $10 free credit by using the promo code DOCS10 if you are a new customer. Without further ado, let’s get started.

Step 1: Update Software Packages

Before we install the LEMP stack, it’s a good practice to update repository and software packages by running the following commands on your Ubuntu 18.04 OS.

sudo apt update
sudo apt upgrade

Step 2: Install Nginx Web Server

Nginx is a high performance web server and very popular these days. It also can be used as a reverse proxy and caching server. Enter this command to install Nginx Web server.

sudo apt install nginx

After it’s installed, we can enable Nginx to auto start when Ubuntu is booted by running the following command.

“Enabled” indicates that auto start at boot time is enabled and we can see that Nginx is running. Notice that the above command will not immediately quit after running. You need to press “q” to make it quit.

Check Nginx version.

nginx -v

Output:

nginx version: nginx/1.14.0 (Ubuntu)

Now type in the public IP address of your Ubuntu 18.04 server in the browser address bar. You should see the “Welcome to Nginx” Web page, which means Nginx Web server is running properly. If you are installing LEMP on your local Ubuntu 18.04 computer, then type 127.0.0.1 or localhost in the browser address bar.

Note that some cloud server providers may block all incoming traffic by default, so if your browser can’t connect to Nginx web server, you may need to set firewall rules in your control panel.

Finally, we need to make www-data (Nginx user) as the owner of web directory. By default it’s owned by the root user.

sudo chown www-data:www-data /usr/share/nginx/html -R

Step 3: Install MariaDB Database Server

MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who are concerned that Oracle might turn MySQL into a closed-source product. Enter the following command to install MariaDB on Ubuntu 18.04.

sudo apt install mariadb-server mariadb-client

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

When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.

Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security. (Notice that Y is capitalized, which means it is the default answer. )

By default, the MaraiDB package on Ubuntu uses unix_socket to authenticate user login, which basically means you can use username and password of the OS to log into MariaDB console. So you can run the following command to login without providing MariaDB root password.

PHP extensions are commonly needed for content management systems (CMS) like WordPress. For example, if your installation lacks php7.2-xml, then some of your WordPress site pages may be blank and you can find an error in Nginx error log like:

Step 5: Create a Nginx Server Block

A Nginx server block is like a virtual host in Apache. We will not use the default server block because it’s inadequate to run PHP code and if we modify it, it becomes a mess. So remove the default symlink in sites-enabled directory by running the following command. (It’s still available as /etc/nginx/sites-available/default.)

sudo rm /etc/nginx/sites-enabled/default

Then create a brand new server block file under /etc/nginx/conf.d/ directory.

sudo nano /etc/nginx/conf.d/default.conf

Paste the following text into the file. The following snippet will make Nginx listen on IPv4 port 80 and IPv6 port 80 with a catch-all server name.

Step 6: Test PHP

To test PHP-FPM with Nginx Web server, we need to create a info.php file in the document root directory.

sudo nano /usr/share/nginx/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. Now in the browser address bar, enter server-ip-address/info.php. Replace sever-ip-address with your actual IP. If you follow this tutorial on your local computer, then type 127.0.0.1/info.php or localhost/info.php.

You should see your server’s PHP information. This means PHP scripts can run properly with Nginx web server.

Congrats! You have successfully installed Nginx, MariaDB and PHP7.2 on Ubuntu 18.04. For your server’s security, you should delete info.php file now to prevent hacker seeing it.

sudo rm /usr/share/nginx/html/info.php

That’s it! I hope this tutorial helped you install LEMP stack on Ubuntu 18.04 LTS. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care.