How To Install LEMP stack (Linux, Nginx, MySQL, PHP) on Ubuntu System

The term LEMP is an acronym of the four open-source projects:
L - Linux operating system
E - Nginx [engine x]
M - MySQL or MariaDB
P - PHP programming language
In this post, you will learn how to install Nginx, create Nginx server blocks (virtual host), install and secure MySQL, and install PHP.
Prerequisites
The first thing you should make sure you have a regular, non-root user account on your server with sudo privileges.
Step 1. Installing Nginx
Nginx is available in Ubuntu repositories. So, update the packages index and install Nginx by using the following commands on the terminal:
sudo apt update
sudo apt install nginxStep 2. Installing MySQL
Now the web server is ready. The next step is to install MySQL (a database management system). Install MySQL by using following command:
sudo apt install mysql-serverWhen the installation is finished, run the mysql_secure_installation command to improve the security of the MySQL installation:
sudo mysql_secure_installationIt will ask to set the root secret password, delete the anonymous user, and remove the test database. You should answer "Y" (yes) to all questions.
Step 3. Installing PHP
Since Nginx doesn't contain native PHP handling like some other web servers, you will need to install php-fpm, which stands "fastCGI process manager”. We will tell Nginx to pass PHP requests to this software for processing.
Install the module along with an additional helper package using following commands:php-fpm
sudo apt install php-fpm php-opcache php-cli php-gd php-curl php-mysqlStep 4: Configuring Nginx to Use the PHP Processor (create virtual hosts)
Now all of the necessary LEMP stack parts installed. Yet still, need to make a couple of configuration changes so as to tell Nginx to utilize the PHP processor for dynamic content.
This is done on the server block level (server blocks are like Apache's virtual hosts). To do this, navigate to /etc/nginx/sites-available/ directory.
Let's say our domain name is example.com. So, here I will create the new server block configuration file is named example.com, although you can name it whatever you’d like:
sudo vi /etc/nginx/sites-available/example.comAdd the following snippet, which was taken and somewhat modified from the default server block configuration file to the new server block configuration file:
In the /etc/nginx/sites-available/example.com :
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.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}This is what every one of these directives and location blocks does:
listen- Defines what port Nginx should listen on. In this case, that is port80, which is the default port for HTTP.root- Defines the website root path.index- prioritize serving files namedindex.phpserver_name- server’s domain name or public IP address.location /- It has atry_filesdirective, which checks for the existence of files matching a URI request. If Nginx cannot find it, then it will return a 404 error.location ~ \.php$- In this location block, it is pointing Nginx to thefastcgi-php.confconfiguration file and thephp7.4-fpm.sockfile to handle PHP processing.location ~ /\.ht- This location block tells the Nginx to do not server.htaccessfiles by using thedeny alldirective.
After adding this snippet, save and close the file.
The next step is to create a symbolic link from /etc/nginx/sites-available/example.com to the /etc/nginx/sites-enabled/example.com to enable this server block.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/Test your new configuration file for syntax errors by following command:
sudo nginx -tIf any errors are reported, go back and recheck your configuration file before continuing.
When you are ready, reload Nginx for the changes to take effect:
sudo systemctl reload nginxThis concludes the installation and configuration of your LEMP stack.
Good explaination