LEMP Stack is a group of popular software that you can use together to serve dynamic web pages. LEMP stands for Linux, Nginx (Engine-X), MySQL and PHP.

As you may have noticed, the title says we’re going to install MariaDB, and not MySQL. MariaDB is regarded as having increased performance over MySQL, but you can check out comparisons for yourself.

If you’d like to host a CMS such as WordPress or Joomla on a VPS, for example, then LEMP is a great choice to host and serve your website. The backend data is stored in MariaDB, while the dynamic processing is handled by PHP.

Step 1: Install Nginx Web Server

Nginx is a fast and efficient web server. A Web Server basically displays web pages to our visitors.

The software we’re installing is already in Ubuntu’s default repositories so all we have to do is run the `apt` suite, and since this is the first time we’re using it, we’ll start by updating our package index to make sure we’re using the latest package listings. After that, we’ll install the server.

$ sudo apt update
$ sudo apt install nginx

sudo apt install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 0 to remove and 94 not upgraded.
Need to get 0 B/3,588 B of archives.
After this operation, 43.0 kB of additional disk space will be used.
Selecting previously unselected package nginx.
(Reading database ... 59092 files and directories currently installed.)
Preparing to unpack .../nginx_1.14.0-0ubuntu1.1_all.deb ...
Unpacking nginx (1.14.0-0ubuntu1.1) ...
Setting up nginx (1.14.0-0ubuntu1.1) ...

Check if it’s working by typing:

$ systemctl status nginx
systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2018-10-05 14:59:55 PDT; 21h ago
Docs: man:nginx(8)
Main PID: 1542 (nginx)
Tasks: 2 (limit: 2270)
CGroup: /system.slice/nginx.service
├─1542 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─1543 nginx: worker process

The best way to check is by visiting your IP or domain name. If it’s working, then you’ll see the default page included when you install Nginx.

nginx_default_page

Step 2: Install MariaDB

Run the command:

$ sudo apt install mariadb-server

Next, secure your MariaDB installation.

$ sudo mysql_secure_installation
sudo mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none):
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
 
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

If you haven’t set a MariaDB password already and are prompted with `Enter current password for root (enter for none)` then just hit enter to move on - you’ll set the password next.

You should be prompted to set the root password for MariaDB. Answer `Y` to this.

`Set root password? [Y/n]`

And set your password. Make sure it’s different than your SSH password, for security reasons.

Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
... Success!

Test if MariaDB is set up properly. Type in the following command to run it, and enter the password you just set at the prompt:

$ sudo mysql -u root -p

If successful you should see a welcome message.

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 49
Server version: 10.1.34-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>

Step 3: Install PHP7.3 and Configure Nginx to use the PHP Processor for Dynamic Content

Update: We've updated the guide to installing PHP7.3 since it's the latest version.

Now that you’ve got Nginx to serve pages and MariaDB to handle your data, you’ll need something to generate dynamic pages. This is where PHP comes in.

Nginx does not come with PHP processing so we’ll have to install it ourselves. We’ll be installing `php-fpm`, which stands for PHP FastCGI Process Manager, and then we’ll tell Nginx to pass requests to PHP for processing.

We'll first have to add a third party repository to be able to install PHP7.3. You may not have the `add-apt-repository` command available, so we'll install `software-properties-common` first. To do both of these things, run the following command:

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php

With the new repository added, now just update your package index:

$ sudo apt update

Now we can install PHP7.3 and some common extensions for it. We're also installing these extensions because you'll find that many software like CMS ( WordPress, Joomla etc) require these extensions from time to time, so it's good to have them readily installed.

$ sudo apt install php7.3 php7.3-fpm php7.3-mysql php-common php7.3-cli php7.3-common php7.3-json php7.3-opcache php7.3-readline php7.3-mbstring php7.3-xml php7.3-gd php7.3-curl

After you've installed PHP7.3, you can start it, enable auto-start on boot, and check it's status. You can do this with the following commands:

sudo systemctl start php7.3-fpm
sudo systemctl enable php7.3-fpm
systemctl status php7.3-fpm

NOTE: If you find that the cursor gets stuck in the `status` output, you can press `q` for `quit` and it'll go back to normal.

Now you’ve got all the components of the LEMP Stack, but we still have to tell Nginx to use PHP for dynamic content.

This will be done at the server block level. To tell Nginx to use PHP we’ll edit our website’s server block. We’ll create a new server block in the `/etc/nginx/sites-available` directory. We’ll assume that we’ve got the domain `example.com`. You can use whatever you’d like instead of `example.com`, however we recommend you use your own domain or your server’s IP.

$ sudo nano /etc/nginx/sites-available/example.com

We’re not editing the default server block, but are creating a new one because it’ll be easier to get back to the initial configuration if we need to.

Now add the following content to your new server block - it’s a slightly modified version of the initial one.

server {
    listen 80;
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
    
    server_name example.com;
    
    location / {
        try_files $uri $uri/ =404;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }
    
    location ~ /\.ht {
        deny all;
    }
}

Save and close the file when you’re finished.

Now we’ll have to enable the server block in `/etc/nginx/sites-enabled`. We do this by linking the file from the `/etc/nginx/sites-available` directory.

$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Everything should be set up. Let’s test it out in the command line:

$ sudo nginx -t

If there are any errors then go back through the tutorial and check if you did everything correctly. If not, leave a comment and we’ll try to help.

Reload Nginx to apply the changes you made:

$ sudo systemctl reload nginx

You’ve finished installing LEMP Stack. There’s one more easy thing we should do to make sure everything’s working properly.

Step 4: Testing PHP Processing

Everything should be set up, but we should check that Nginx is communicating with PHP.

To do this we’ll create a simple PHP file that Nginx will serve us and we’ll see in the browser that PHP is generating pages.

Create a test `php` file in your Nginx document root:

$ sudo nano /var/www/html/info.php

Now add the following content. This is valid PHP code that generates a page containing information about our server:

Save and close the file when you’re finished.

Now visit the page by typing `https://yourdomain_or_IP/info.php` in your browser.

You should see a page generated by PHP and containing structured information about your server.

If you see this page then Nginx is successfully communicating with PHP.

One more thing that you have to do is remove `/var/www/html/info.php`. This is because it makes your server vulnerable and others can see important information about your server - and we don’ want that.

Simply remove the file:

$ sudo rm /var/www/html/info.php

With that we can conclude that you’ve successfully installed LEMP Stack. Congratulations!


Vlad

Tech Support

LIMITED TIME SPECIAL 💀

Enter your email below to get 20% OFF on any of our Linux VPS plans and receive weekly deals on our services!