Install LEMP Stack on Ubuntu 20.04

Let’s define first what LEMP Stack is.

LEMP is an open-source web application stack that we may use to create web apps. LEMP’s abbreviation stands for Linux Operating System, Nginx (pronounced engine-x), web server, MySQL database, and PHP programming language.

Now, let’s proceed to the installation.

Step 1: Install the Nginx Web Server

We will use Nginx, a high-performance web server, to show web pages to our site visitors. This program will be obtained using the apt package manager.

sudo apt update
sudo apt install nginx

Enter Y when prompted to confirm that you wish to install Nginx. The Nginx web server will be active and running on your Ubuntu 20.04 server.

Type the address you received in your web browser to access Nginx’s default landing page:

http://server_domain_or_IP

Step 2: Install MySQL

If your web server is up and running, you must install the database system to store and manage data for your website. MySQL is a widely used database management system in PHP settings.

Use apt once more to obtain and install this software:

sudo apt install mysql-server

After you’ve completed the installation, see if you can access the MySQL console by typing:

sudo mysql

Output:

Step 3: Install PHP

You’ve set up Nginx to serve your content and MySQL to store and manage your data. You can now install PHP to execute code and generate dynamic content for the webserver.

sudo apt install php-fpm php-mysql

When prompted, type Y and ENTER to confirm the installation

Step 4: Configure Nginx to Use the PHP Processor

Create the root web directory for domain_name as follows:

sudo mkdir /var/www/domain_name

Next, assign ownership of the directory using the $USER environment variable, which will refer to your current system user:

sudo chown -R $USER:$USER /var/www/domain_name

Then, in Nginx’s sites-available directory, create a new configuration file.

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

This will generate a new empty file. Copy and paste the following bare-bones configuration:

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/ /index.php?$args;
    }

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

    location ~ /\.ht {
        deny all;
    }

}

Link to the configuration file from Nginx’s sites-enabled directory to activate your setup:

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

Then, under the /sites-enabled/ directory, unlink the default configuration file:

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

You can test your configuration for syntax errors by typing:

sudo nginx -t

If any issues are reported, return to your configuration file and double-check its contents before proceeding.

When you’re finished, reload Nginx to make the changes take effect:

sudo systemctl reload nginx

Leave a Reply

Your email address will not be published. Required fields are marked *