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

The LEMP stack is like a superhero team for websites, working together to make them awesome. It stands for Linux (the operating system), Nginx (the web server), MySQL (where data is stored), and PHP (which makes things dynamic).

In our guide, we’ll show you how to put together this dream team on an Ubuntu server. Ubuntu takes care of the first part, and we’ll walk you through getting the rest of the team ready for action. Get ready to supercharge your website!

We start with updating local package index.

sudo apt-get update

Installing MySql Server

A prompt for server selection will appear. Choose “Apache” from the options, and you will then be prompted to enter a password. Exercise caution when entering the password.

sudo apt install mysql-server -y

Installing Nginx

sudo apt install nginx -y

Installing PHP

Below command adds a Personal Package Archive (PPA) to your system’s list of package repositories. It provides up-to-date PHP packages for Ubuntu

sudo add-apt-repository -y ppa:ondrej/php

Below command will install PHP and drivers for respective version like 5.6, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, and 8.2

sudo apt install php5.6 php5.6-fpm php5.6-gd php5.6-zip php5.6-mysql php5.6-xml php5.6-curl php7.1 php7.1-fpm php7.1-gd php7.1-zip php7.1-mysql php7.1-xml php7.1-curl  php7.2 php7.2-fpm php7.2-gd php7.2-zip php7.2-mysql php7.2-xml php7.2-curl  php7.3 php7.3-fpm php7.3-gd php7.3-zip php7.3-mysql php7.3-xml php7.3-curl  php7.4 php7.4-fpm php7.4-gd php7.4-zip php7.4-mysql php7.4-xml php7.4-curl php8.0 php8.0-fpm php8.0-gd php8.0-zip php8.0-mysql php8.0-xml php8.0-curl php8.1 php8.1-fpm php8.1-gd php8.1-zip php8.1-mysql php8.1-xml php8.1-curl php8.2 php8.2-fpm php8.2-gd php8.2-zip php8.2-mysql php8.2-xml php8.2-curl -y

Changing PHP version

 //  this gives option to choose version from available list of version
sudo update-alternatives --config php;

// With this, we can directly changes to available specific version of php, here php5.6
sudo update-alternatives --set php /usr/bin/php5.6

Installing other required packages

sudo apt install composer git zip unzip -y

Configuring MySql Setup

sudo mysql -u play -p
Under mysql command line, execute below commands
CREATE USER 'play'@'localhost' IDENTIFIED BY '123';
GRANT ALL PRIVILEGES ON *. * TO 'play'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Configuring Nginx Setup

nginx setup to execute php files.
We need to update file: /etc/nginx/sites-available/default to execute php script. 
And make highlighted changes in commented portion of below script
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }

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

    location ~ /\.ht {
        deny all;
    }
}
sudo service mysql restart
sudo nginx -t

Useful Composer Commands

sudo composer self-update
sudo composer self-update <version>
sudo composer self-update 1.10.12
composer self-update --rollback
composer version
1.10.12
sudo composer self-update; composer version;
2.0.11
sudo composer self-update 1.10.12; composer version;
1.10.12
composer self-update --rollback; composer version;
2.0.11
Below is step to add node environment in the system.
sudo apt install curl 
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 
source ~/.bashrc  
nvm install node 
nvm install 12.18.3

1 thought on “How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu”

  1. sudo apt update
    sudo apt upgrade
    sudo apt install software-properties-common
    sudo add-apt-repository ppa:ondrej/php
    sudo apt install php8.1 php8.1-cli php8.1-fpm php8.1-apcu php8.1-opcache php8.1-common php8.1-curl php8.1-zip php8.1-mcrypt php8.1-mysql php8.1-mbstring php8.1-xml php8.1-gd php8.1-imagick php8.1-xmlrpc php8.1-maxminddb
    sudo update-alternatives –set php /usr/bin/php8.1

Leave a Comment

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

Scroll to Top