How to install Linux Nginx, MySQL, PHP (LEMP Stack) on Ubuntu 16.04 / 18.04 LTS / 19.10 / 20.04 LTS
By: Rabib Galib
Tags: Phpmyadmin, MySQL, Ubuntu, Nginx, PHP

LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of a Linux operating system , a Nginx (Pronounced as Engine-X) web server, MySQL database server, and PHP for dynamic data processing. Due to its modular nature, the components can easily be swapped out. With LEMP, Apache is replaced with the lightweight yet powerful Nginx.

Step 1: Installing the Stack

This guide demonstrates how to install a LEMP stack on an Ubuntu with individual packages. Let’s connect to the server via SSH. Simply open terminal & put below command. For windows, you can download PuTTY or WinSCP

ssh root@server_ip_address

Let’s begin by updating server’s package index

sudo apt-get update

Step 2: Installing NGINX

sudo apt-get install nginx

Once you have installed it, the Nginx service should start automatically and will be enabled to start at boot time, you can check if it’s up and running. You can also check http://ip_address in your web browser which means your installation is working or not!

sudo systemctl status nginx

nginx is working

Step 3: Installing PHP

PHP is a popular server side scripting language. You can install PHP, PHP-FPM and other modules.

To check the Ubuntu version, use the following command in terminal

lsb_release -a

Install PHP 7.4 on Ubuntu 20.04

sudo apt-get install php-fpm php-mysql php-mbstring

Confirm PHP version

php -v

Install PHP 7.4 on Ubuntu 18.04/19.04/16.04

We need to add Ondrej Sury’s repository which has the latest build packages of PHP

sudo apt -y install software-properties-common

sudo add-apt-repository ppa:ondrej/php

sudo apt-get update

Now we are ready to install php7.4 with other extensions

sudo apt-get install php7.4 php7.4-fpm php7.4-mysql php7.4-mbstring

For nginx configuration, you need to stop and disable Apache service

sudo systemctl disable — now apache2

Confirm PHP version

php -v

For install all important php7.4 extensions [ Good to know ]

sudo apt-get install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl

Step 4: Configuring PHP in NGINX

PHP-FPM is to serve & process PHP scripts or PHP based web applications in nginx. Setup nginx server to serve PHP.

sudo rm -rf /etc/nginx/sites-available/default

sudo rm -rf /etc/nginx/sites-enabled/default

Now deploy below command to write config file

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

Paste the below code in config file

server {

listen 80;

root /var/www/html;

index index.php index.html index.htm index.nginx-debian.html;

server_name _;

#Or write the above line as server_name [server_IP][domain_name];

location / {

try_files $uri $uri/ /index.php?$query_string;

}

location ~ \.php$ {

include snippets/fastcgi-php.conf;

fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

# I am using php7.4-fpm, you can use other versions too

}

location ~ /\.ht {

deny all;

}

}

Enable the file by linking to the sites-enabled dir

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

Test nginx configuration

sudo nginx -t

If testing is okay then restart the nginx server

sudo systemctl restart nginx

Now you can write or git clone any php script in cd /var/www/html directory.