How to Install LEMP Stack (Nginx, MariaDB, PHP7.2) on Ubuntu 18.04 LTS?

Nginx, MariaDB and PHP7.2 are called (LEMP). 

You can follow the steps below to install LEMP on Ubuntu 18.04 LTS – 

1. Update the Ubuntu Package.

# sudo apt update
# sudo apt upgrade

2. Install Nginx Webserver; it is a high-performance server and is used as Reverse Proxy.

# sudo apt install Nginx

3. Start and enable Nginx.
    It will auto-start Nginx at boot time.

# sudo systemctl start nginx
# sudo systemctl enable nginx

4. You can check the Nginx Version with the below command.

# nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

You can access the server IP Address in Browser, which will bring the default Nginx page.

            

We can set www-data (Nginx user) as the owner of the web directory.

# sudo chown www-data:www-data /usr/share/nginx/html -R

5. Install MariaDB with the command below; MariaDB is a replacement for MySQL.

# sudo apt install mariadb-server mariadb-client

6. Start and enable the MariaDB; it will auto-start it at boot time.

# sudo systemctl start mariadb
# sudo systemctl enable mariadb

7. Run the below command for the post-installation security script.

# sudo mysql_secure_installation

8. Enter the MySQL root password, confirm the password, and set the required configuration.

9. Verify the MariaDB version with the below command – 

# mariadb –version

It will give the below output – 

# mariadb  Ver 15.1 Distrib 10.1.43-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

10. PHP 7.2 is included with the default Ubuntu Repository for 18.04. However, please enter the below command to install php7.2 with some common extensions.

# sudo apt install php7.2 php7.2-fpm php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl

11. Start and enable the PHP 7.2-fpm; it will auto-start the PHP at boot time.

# sudo systemctl start php7.2-fpm
# sudo systemctl enable php7.2-fpm

12. Run the below command to remove the default symlink in the sites-enabled directory.

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

13. Create a new server block file inside /etc/nginx/conf.d/ directory.

# sudo nano /etc/nginx/conf.d/default.conf

14. Add the below text in default.conf

server {

  listen 80;

  listen [::]:80;

  server_name _;

  root /usr/share/nginx/html/;

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

 

  location / {

    try_files $uri $uri/ /index.php;

  }

 

  location ~ \.php$ {

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

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    include fastcgi_params;

    include snippets/fastcgi-php.conf;

  }


 # A long browser cache lifetime can speed up repeat visits to your page

  location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {

       access_log        off;

       log_not_found     off;

       expires           360d;

  }

 

  # disable access to hidden files

  location ~ /\.ht {

      access_log off;

      log_not_found off;

      deny all;

  }

}

15. Save and close it, and restart the Nginx service. 

# sudo systemctl reload nginx

16. We will test the PHP-FPM with the NGINX webserver.
     Let us create the phpinfo.php page in the root directory.

# sudo nano /usr/share/nginx/html/info.php

17. Paste the php info code as shown below – 

<?php phpinfo();>

 


Was this answer helpful?

« Back

chat