Install LEMP Stack on Ubuntu 22.04 with Easy Steps

Posted on

Install LEMP Stack on Ubuntu 22.04 with Easy Steps

In this article, we want to teach you How To Install LEMP Stack on Ubuntu 22.04. LEMP is an open-source web application stack used to develop web applications. The term LEMP is an acronym that represents L for the Linux Operating system, Nginx (pronounced as engine-x, hence the E in the acronym) web server, M for MySQL database, and P for PHP scripting language. Setting up a LEMP stack is essential for deploying web applications.

LEMP enjoys good community support and is used around the world in many highly scaled web applications. Nginx is the second most widely used web server in the world following Apache. This guide provides a straightforward approach to install LEMP Stack

You can now proceed to the guide steps below on the Orcacore website to set up LEMP Stack Ubuntu 22.04.

Before you start LEMP Stack Ubuntu 22.04 Setup, you need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.

Also, you need a domain name that points to your server’s IP address.

Now follow the steps below to complete this guide on LEMP Stack Ubuntu 22.04.

1. LEMP Stack Install Nginx

Nginx is available in the Ubuntu default repository. first, update the APT packages with the following command:

sudo apt update

Then, install Nginx on Ubuntu 22.04 with the following command:

sudo apt install nginx -y

We assumed that you have enabled the ufw firewall with the requirements. you need to allow connections to Nginx.

Check the available applications through the UFW with the following command:

sudo ufw app list
available applications through the UFW

In this article, you only need to allow Nginx to HTTP traffic on port 80 with the following command:

sudo ufw allow 'Nginx HTTP'

Now you can verify the changes with the following command:

sudo ufw status

Your output should be similar to this:

Now you can test that your Nginx web server is up and running by typing your domain name or your server’s public IP address in your web browser.

If you don’t have a domain name that points to your domain name, you can use your server’s public IP address.

To get your IP address you can use the following command:

hostname -I

Or you can use the curl tool to get it:

curl -4 icanhazip.com

Now you can access Nginx’s default landing page by typing the IP address that you have got in your web browser:

http://your_domain_or_IP

If you see this page, means that your web server is successfully installed and HTTP traffic is enabled for it.

At this point, you are done with installing Nginx the first part of the LEMP stack on Ubuntu 22.04. Let’s start to install MySQL on Ubuntu 22.04.

2. LEMP Stack Install MySQL

Here, you need to install MySQL the database system to store and manage data for your site with the following command:

sudo apt install mysql-server -y

For more security, it’s recommended to run a security script that comes with the pre-installation of MySQL:

sudo mysql_secure_installation

You will be asked some questions. The first is to configure VALIDATE PASSWORD PLUGIN. you can leave it blank safely. but if you choose to set a password for it choose a strong one.

Next, is to set a password for the MySQL root user. from there, you can press Y for the rest of the questions to accept the defaults.

When you are finished, log in to your MySQL console with the following command:

sudo mysql

Then you can exit from the MySQL console with the following command:

mysql> exit

At this point, you are done with installing MySQL the second part of the LEMP stack on Ubuntu 22.04. Let’s start to install PHP on Ubuntu 22.04.

3. LEMP Stack Install PHP

You have Nginx installed to serve your content and MySQL installed to store and manage your data. Now you can install PHP to process code and generate dynamic content for the webserver.

You can install PHP and its dependencies with the following command:

sudo apt install php8.1-fpm php-mysql -y

Now you need to configure Nginx to use the PHP components.

4. Configure Nginx to use PHP components

You can create Nginx server blocks that are similar to Apache virtual hosts. First, create the root web directory for your domain with the following command:

sudo mkdir /var/www/your-domain

Then, assign ownership of the directory with the $USER environment variable with the following command:

sudo chown -R $USER:$USER /var/www/your-domain

Next, open a new configuration file in the Nginx’s sites-available directory with your favorite text editor, here we use vi:

sudo vi /etc/nginx/sites-available/your-domain

Then, add the following bare-bones configuration to the file:

server {
    listen 80;
    server_name your_domain www.your_domain;
    root /var/www/your_domain;

    index index.html index.htm index.php;

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

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

    location ~ /.ht {
        deny all;
    }

}

When you are finished, save and close the file.

Now you need to activate your configuration by linking to the config file from Nginx’s sites-enabled directory with the following command:

sudo ln -s /etc/nginx/sites-available/your-domain /etc/nginx/sites-enabled/

You can test your configuration for no syntax error with the following command:

sudo nginx -t

In your output you should see:

If you see errors, open the file again and check for typos and missing characters. To apply these changes reload Nginx:

sudo systemctl reload nginx

Now you need to create a file in the webroot named index.htm so that you can test your new server blocks:

vi /var/www/your-domain/index.html

Then, add the following content to your file:

<html>
  <head>
    <title>your_domain website</title>
  </head>
  <body>
    <h1>Hello World!</h1>

    <p>This is the landing page of <strong>your_domain</strong>.</p>
  </body>
</html>

Now type your domain name or IP address in your web browser:

http://your_domain_or_IP

You will see a page like this:

If you see this page, means that your server blocks work correctly. Now your LEMP stack is fully configured on Ubuntu 22.04.

In the next step, you need to create a PHP script to test that Nginx can handle PHP files.

5. How To Test PHP with Nginx on Ubuntu 22.04

The LEMP stack should now be completely set up on Ubuntu 22.04. you can test it to validate that Nginx can hand .php files.

Create a file in the webroot directory named info.php with the following command:

vi /var/www/your-domain/info.php

Add the following PHP code to the file:

<?php
phpinfo();

Save and close the file, when you are finished.

you can access the PHP information page by visiting the domain name or public IP address you’ve set up in your Nginx configuration file, followed by info.php:

http://your_domain_or_IP/info.php

After checking the information about your PHP server, it’s recommended to remove it for more security.

sudo rm /var/www/your-domain/info.php

Conclusion

At this point, you have learned to Install LEMP Stack on Ubuntu 22.04. The LEMP stack in Ubuntu 22.04 is used to host dynamic websites and web applications by combining Linux, Nginx, MySQL (or MariaDB), and PHP. It provides a fast, secure, and scalable server environment for web development.

Hope you enjoy it. Please subscribe to us on Facebook, YouTube, and Twitter.

How To Install LAMP Stack on Ubuntu 22.04

Install LEMP Stack with Docker Compose on Ubuntu

LEMP Stack Nginx MariaDB PHP on Debian 12

Install LEMP Stack on Rocky Linux 9

LEMP Stack Setup on AlmaLinux 9

FAQs

Where is the default Nginx web root directory?

The default web root is /var/www/html/.

What does LEMP stand for?

LEMP stands for Linux, Nginx, MySQL (or MariaDB), and PHP/Python/Perl.

Alternative Solutions for Installing a LEMP Stack

While the above guide provides a solid foundation for setting up a LEMP stack, alternative approaches exist. Here are two different methods to achieve the same goal: using Tasksel and utilizing Docker Compose.

1. Using Tasksel

Tasksel is a Debian/Ubuntu tool that simplifies the installation of multiple related packages as a coordinated "task." It offers a more streamlined, semi-automated approach compared to manually installing each component. While Tasksel doesn’t directly install a complete LEMP stack, it can be used to install the LAMP stack, after that, you can replace Apache with Nginx.

Explanation:

Tasksel works by presenting a list of predefined tasks to the user. When a task is selected, Tasksel automatically installs all the packages associated with that task. This eliminates the need to manually install each package individually and ensures that all necessary dependencies are included.

Steps:

  1. Install Tasksel:

    sudo apt update
    sudo apt install tasksel
  2. Run Tasksel:

    sudo tasksel

    A text-based interface will appear. Use the arrow keys to navigate to "LAMP server" and press Spacebar to select it. Then, press Enter to proceed with the installation.

  3. Configure MySQL:

    During the installation, you’ll be prompted to set a root password for MySQL. Choose a strong password.

  4. Remove Apache:

    sudo apt remove apache2*
    sudo apt purge apache2*
  5. Install Nginx:

    sudo apt install nginx
  6. Configure Nginx (Follow the steps from the original article):

    Refer to steps 4 and 5 in the original guide to configure Nginx to work with PHP and your domain. This involves creating a server block and testing the PHP configuration.

  7. Firewall Configuration (if needed):

    Make sure to adjust your firewall rules (using ufw) to allow traffic to Nginx (port 80 and optionally port 443 for HTTPS).

Code Example: (Demonstrating Apache removal)

sudo apt remove apache2*
sudo apt purge apache2*

This code first removes Apache2 packages and then purges the configuration files. This step ensures that there are no conflicts between Apache and Nginx.

Advantages:

  • Simpler initial installation of MySQL and PHP.
  • Handles dependencies automatically.

Disadvantages:

  • Requires manually removing Apache and installing Nginx afterwards.
  • Less control over individual component versions.
  • Relies on the specific packages defined within the Tasksel "LAMP server" task.

2. Utilizing Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. Instead of installing each component directly on the host operating system, Docker Compose allows you to define a LEMP stack as a set of interconnected containers, each running a specific service (Nginx, MySQL, PHP).

Explanation:

Docker Compose uses a YAML file (typically named docker-compose.yml) to define the services, networks, and volumes required for the application. Each service corresponds to a Docker container. Docker handles the creation, starting, and networking of these containers.

Steps:

  1. Install Docker and Docker Compose: Follow the official Docker documentation for installing Docker Engine and Docker Compose on Ubuntu 22.04.

  2. Create a docker-compose.yml file:

    version: "3.8"
    services:
      nginx:
        image: nginx:latest
        ports:
          - "80:80"
          - "443:443"
        volumes:
          - ./nginx/conf.d:/etc/nginx/conf.d
          - ./app:/var/www/html
        depends_on:
          - php
    
      php:
        image: php:8.1-fpm
        volumes:
          - ./app:/var/www/html
    
      db:
        image: mysql:8.0
        environment:
          MYSQL_ROOT_PASSWORD: your_root_password
        volumes:
          - db_data:/var/lib/mysql
    
    volumes:
      db_data:
  3. Create necessary directories:

    mkdir nginx
    mkdir nginx/conf.d
    mkdir app
  4. Create an Nginx configuration file (e.g., nginx/conf.d/default.conf):

    server {
        listen 80;
        server_name localhost;
        root /var/www/html;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ .php$ {
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
  5. Start the containers:

    docker-compose up -d

Code Example: (Complete docker-compose.yml file shown above)

This docker-compose.yml file defines three services: nginx, php, and db. It sets up the necessary ports, volumes, and dependencies for a functional LEMP stack. Remember to replace your_root_password with a secure password.

Advantages:

  • Isolated environment: Each component runs in its own container, preventing conflicts.
  • Reproducibility: The docker-compose.yml file defines the entire stack, making it easy to recreate on different systems.
  • Scalability: Docker Compose can be used to scale individual services as needed.
  • Version control: Easily specify and manage different versions of each component.

Disadvantages:

  • Requires familiarity with Docker and Docker Compose.
  • Increased resource overhead compared to native installations.
  • Can be more complex to configure initially.

These alternative methods offer different approaches to installing a LEMP stack on Ubuntu 22.04. Tasksel provides a semi-automated approach by replacing Apache and docker offers the benefit of containerization. The best choice depends on your specific needs, technical expertise, and desired level of control.