How to Install LEMP Stack on Ubuntu 20.04

lemp-stack-logo

In this article, we will have explained the necessary steps to install and configure the LEMP Stack on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of Linux, Apache, MySQL, and PHP. Due to its modular nature, the components can easily be swapped out. With LEMP, Apache is replaced with the lightweight yet powerful Nginx.

Install LEMP Stack on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install Nginx.

Nginx is available on the default Ubuntu 20.04 repos. Run the following command to install Nginx Web server:

sudo apt install nginx

Once installed, check to see if the Nginx service is running:

sudo systemctl nginx status
sudo systemctl start nginx
sudo systemctl enable nginx

Verify access to Nginx from the browser by using the address, http://Your-Server.IP_or_hostname. You should land on the Nginx HTTP server test page which basically shows that the Nginx web server is successfully installed and working.

Step 3. Install MariaDB Database Server.

Run the following command to install MariaDB on Ubuntu 20.04:

sudo apt install mariadb-server mariadb-client

Once completing installation MariaDB will start automatically. Check MariaDB version by typing:

sudo systemctl status mysql

Then, issue the mysql_secure_installation command to improve the security of the MariaDB installation:

sudo mysql_secure_installation

Install LEMP Stack on Ubuntu 20.04

Step 4. Install PHP in Ubuntu 20.04.

The final component of your LEMP Stack on Ubuntu 20.04 is PHP. PHP7.4 is included in Ubuntu 20.04 repository, to install PHP and PHP-FPM, run the following command which will also install some required additional packages:

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

Step 5. Configure Nginx for PHP.

The next step you that need to complete is to modify the Nginx configuration file:

cd /etc/nginx/sites-available/
nano /etc/nginx/sites-available/your_domain.com.conf

Paste the following content:

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

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;
}
}

To enable the server configuration that we just created, run the following command:

ln -s /etc/nginx/sites-available/your_domain.com.conf /etc/nginx/sites-enabled/your_domain.com.conf

Restart PHP and reload the NGINX configuration:

sudo systemctl restart php-fpm
sudo nginx -s reload

Step 6. Configure Firewall.

It is recommended that you enable the ufw firewall and add a rule for Nginx:

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx HTTP'
sudo ufw enable

Step 7. Testing PHP

Now you should create info.php file to test PHP to do so type following:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Then, open the following link in your web browser and we will be able to see all the information about PHP and its other configurations: http://your-domain/phpinfo.php (replacing your IP address with the one above).

That’s all you need to do to install LEMP Stack on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.