DEV Community

Sagar Jethi
Sagar Jethi

Posted on • Updated on

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

Alt Text

Introduction

LEMP is an acronym of the Linux operating system, with an Nginx (pronounced like “Engine-X”) web serve, MySQL database, PHP. LEMP stack used for developing and deploying a web application
application.
More check : About LEMP

Step 1: Update Package Repository Cache

Before you begin:

  1. Let's open the terminal using the CTRL+ALT+T keyboard shortcut or by searching for the word terminal in Ubuntu Dash.
    search terminal

  2. Next update the package repository cache, type in the following command:

sudo apt-get update

Step 2: Installing the Nginx

  1. To install Nginx, run the following command in the terminal:
 sudo apt install nginx

On Ubuntu 19.04, Nginx is configured to start running upon installation.

2.Let enable the most restrictive profile that will still allow the traffic you want. But we not configured SSL for your server, so you will only need to allow traffic on port 80.

sudo ufw allow 'Nginx HTTP'

3.Check if Nginx is installed correctly by running the Apache service status. Use the following the command:

sudo ufw status

4.To ensure Nginx is running, enter the IP address of your server in the address bar and press ENTER.:

Note: To identify the server’s public IP address, run the command:

sudo apt-get install curl
curl http://icanhazip.com

Step 3: Installing MySQL to Create and Manage Site Database

Now install MySQL, type in the following command:

sudo apt-get install mysql-server

Press y to allow the installation mysql.
During the installation, you will be prompted to set the root user password.

Step 3: Installing PHP and Configuring with Nginx

  1. To install PHP, run the following command:

Since Nginx does not contain native PHP processing like some other web servers, you will need to install php-fpm, Also php-mysql, which will allow PHP to communicate with your database backend.

sudo apt install php-fpm php-mysql
mysql --version
sudo mysql_secure_installation

Start MySQL shell

sudo mysql

You now have all of the required LEMP stack components installed
But still need to make a few configuration changes in order to tell Nginx to use the PHP processor for dynamic content.

This is done on the server block level, for that open file using following
command

sudo nano /etc/nginx/sites-available/example.com

After that add the following content, which changes the default configuration file

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.2-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

After adding this content, save and close the file. Enable your new server block by creating a symbolic link

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Then, unlink the default configuration file from the /sites-enabled/ directory:

sudo unlink /etc/nginx/sites-enabled/default

Test your new configuration file using the following command

sudo nginx -t

Step 5: Restart Nginx

sudo systemctl reload nginx

Top comments (0)