1080*80 ad

Setting Up a LEMP Stack on Ubuntu 18.04: Nginx, MariaDB, and PHP 7.2

A Comprehensive Guide to Installing a LEMP Stack on Ubuntu 18.04

Ready to build a high-performance web server? The LEMP stack is a powerful and popular choice for hosting dynamic websites and applications. It’s an acronym representing a suite of open-source software: Linux as the operating system, Eginx (pronounced “Engine-X”) as the web server, MariaDB (a fork of MySQL) as the database server, and PHP for processing dynamic content.

This combination is renowned for its stability, efficiency, and ability to handle high traffic loads. This guide will walk you through the complete process of setting up a robust LEMP stack on an Ubuntu 18.04 server.

Prerequisites

Before we begin, you should have a fresh Ubuntu 18.04 server instance and a non-root user with sudo privileges.

Step 1: Installing the Nginx Web Server

Nginx is a modern, high-performance web server that excels at serving static content and acting as a reverse proxy. Its event-driven architecture makes it incredibly efficient under load.

First, update your server’s package index to ensure you have access to the latest versions.

sudo apt update

Next, install Nginx using the apt package manager.

sudo apt install nginx

Once the installation is complete, you’ll need to configure your firewall to allow web traffic. Ubuntu’s UFW firewall comes with pre-configured profiles for Nginx.

  • Nginx HTTP: Opens port 80 (standard, unencrypted web traffic).
  • Nginx HTTPS: Opens port 443 (TLS/SSL encrypted traffic).
  • Nginx Full: Opens both port 80 and 80.

For now, we will just allow standard HTTP traffic.

sudo ufw allow 'Nginx HTTP'

You can verify the status of your firewall with sudo ufw status. To check if Nginx is running correctly, open a web browser and navigate to your server’s public IP address. You should see the default Nginx welcome page.

Step 2: Installing MariaDB for Database Management

With our web server running, it’s time to install a database system. MariaDB is a community-developed fork of the MySQL database and serves as an excellent, fully compatible, drop-in replacement.

Install the MariaDB server package with the following command:

sudo apt install mariadb-server

After the installation, it is critically important to run the included security script. This script will remove some insecure default settings and lock down access to your database system.

sudo mysql_secure_installation

This crucial script will guide you through several prompts:

  • Setting a root password.
  • Removing anonymous users.
  • Disallowing root login remotely.
  • Removing the test database.
  • Reloading privilege tables.

It is highly recommended that you answer “Y” (yes) to all prompts for a secure setup.

Step 3: Installing PHP for Server-Side Processing

We have a web server to handle requests and a database to store information. Now we need PHP to process code, connect to the database, and generate dynamic content for the user.

Nginx does not contain native PHP processing like some other web servers. Therefore, we need to install php-fpm (FastCGI Process Manager). We will also install php-mysql, a package that allows PHP to communicate with MariaDB.

sudo apt install php-fpm php-mysql

Your core LEMP stack components are now installed. The final, and most important, step is to configure them to work together.

Step 4: Configuring Nginx to Use PHP

Nginx uses configuration files called “server blocks” (similar to virtual hosts in Apache) to manage website settings. We need to create a new server block for our domain and tell it how to handle PHP files.

First, create a new directory for your project in /var/www/. Replace your_domain with your actual domain name.

sudo mkdir /var/www/your_domain

Next, create a new server block configuration file.

sudo nano /etc/nginx/sites-available/your_domain

Paste the following configuration into the file. Be sure to replace your_domain with your own domain name. This configuration is a solid starting point for most PHP applications.

server {
    listen 80;
    root /var/www/your_domain;
    index index.php index.html index.htm;
    server_name your_domain www.your_domain;

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

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

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

This configuration tells Nginx to listen on port 80, use your new directory as the web root, and look for index.php files first. Most importantly, the location ~ \.php$ block passes all files ending in .php to the PHP-FPM processor via a socket file.

Now, enable your new server block by creating a symbolic link to the sites-enabled directory and then test the configuration for errors.

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo nginx -t

If there are no errors, restart Nginx to apply the changes.

sudo systemctl restart nginx

Step 5: Creating a Test PHP File to Verify the Setup

To confirm that Nginx is correctly passing PHP requests to php-fpm, let’s create a simple test script in our web root.

sudo nano /var/www/your_domain/info.php

Add the following PHP code to the file:

<?php
phpinfo();
?>

Save and close the file. Now, visit http://your_domain/info.php in your web browser. You should see a page generated by PHP with detailed information about your server and PHP configuration.

If you see this page, your LEMP stack is working perfectly!

Security Tip: This info.php file contains sensitive information about your server environment. After confirming your setup is working, it is essential to delete this file.

sudo rm /var/www/your_domain/info.php

You now have a fully operational and high-performance LEMP stack on your Ubuntu 18.04 server, ready for you to deploy your web applications.

Source: https://kifarunix.com/how-to-setup-lemp-stack-nginx-mariadb-php-7-2-on-ubuntu-18-04/

900*80 ad

      1080*80 ad