Install LEMP Stack on Ubuntu 20.04

What is LEMP Stack

LEMP (Linux, NGINX, MySQL, PHP) is a software stack that used to be the environment for Websites and Web-applications which uses Linux as the Operating system, NGINX (Engine-X) as the web-server, MySQL as the database management system, and PHP as the Scripting language. is almost like LAMP stack with one main difference in the web-server application, in the LEMP stack we use NGINX as the web-server application which is more flexible and lightweight than Apache.

Install NGINX

For installing NGINX on Ubuntu 20.04 you can install it using the official “apt” repository.

apt install nginx

You can start the Nginx service using systemctl also you can make it run at the system startup:

systemctl start nginx
systemctl enable nginx

Now you can see the NGINX default page through your domain or Public IP Address like below:

Nginx Welcome Page

Install MySQL

MySQL is an open-source relational database management system, which is the most common database management system at the time of writing this article, MySQL is freely available through almost all of the software repositories and as you can find out from the name it uses the SQL (Structured Query Language).

Install MySQL with the following command:

apt install mysql-server

You can start and enable MySQL service using:

systemctl start mysql
systemctl enable mysql

There is a useful tool that you have when you install MySQL is “mysql_secure_installation” which can be a very huge help for beginner users, it can help you do some initial ye important configuration easily using a wizard mode, you can start the process by typing:

mysql_secure_installation

Install PHP

PHP is a general-purpose scripting language which mostly used in the development of Web-Applications.

We are install PHP 7.4 which is the latest stable version, Also we need to install the “php-fpm” extenstion which is the module that NGINX use to handle php requests:

apt install php php-fpm

After installation process you need to start and enable the php-fpm service with following two commands:

systemctl start php7.4-fpm
systemctl enable php7.4-fpm

Configure NGINX to process PHP

In this section, we are going to do some configurations on NGINX to handle PHP requests through FastCGI (php-fpm). on Ubuntu 20.04 by default php-fpm will listen on “/run/php/php7.4-fpm.sock” UNIX socket so we have to configure NGINX to forward PHP requests to that location.
just create a file called “php.conf” in “/etc/nginx/conf.d/” then copy and paste the following lines in it then save and quit:


# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
index index.php index.html index.htm;

location ~ \.(php|phar)(/.*)?$ {
fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;

Check NGINX configuration for errors with:

nginx -t

if everything was ok, restart NGINX service to take effect:

systemctl restart nginx

Verify PHP process

If you want to test that everything is working fine and your NGINX can handle PHP you can create an “info.php” file in the document root:

vi /usr/share/nginx/html/info.php

Just copy and paste the following lines in the file then save and exit:

<?php
phpinfo();
?>

Now open your browser and enter your domain/IP address along with “/info.php”

http://YOUR_DOMAIN_OR_IP_ADDRESS/info.php

If you see something like the picture below then everything is OK and you are good to go.
PHP info page

Leave a Reply

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