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

  • 0

Nginx, MariaDB and PHP7.2 are called (LEMP). Following are the steps to install LEMP on Ubuntu 18.04 LTS.

  1. Let us update our Ubuntu Package.

    # sudo apt update
    # sudo apt upgrade
  2. Install Nginx Webserver. It is a high-performance server and used as Reverse Proxy.

    # sudo apt install Nginx
  3. Start and Enable the Nginx. It will auto-start the 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 and it 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 
  1. Install MariaDB with below Command. MariaDB is a replacement for MySQL.

    # sudo apt install mariadb-server mariadb-client
  2. Start and Enable the MariaDB. It will auto-start the MariaDB at boot time.

    # sudo systemctl start mariadb 
    # sudo systemctl enable mariadb
  3. Run below command for the post-installation security script.

    # sudo mysql_secure_installation

  4. Enter MySQL root password and confirm the password and set the required configuration.

  5. Verify the MariaDB Version with the below command.

    # mariadb --version

It will give below output.

 # mariadb  Ver 15.1 Distrib 10.1.43-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
  1. By Default, 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
  2. 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
  3. Run the below command to remove the default symlink in the sites-enabled directory.

    # sudo rm /etc/nginx/sites-enabled/default
  4. Create a new server block file inside /etc/nginx/conf.d/ directory.

    # sudo nano /etc/nginx/conf.d/default.conf
  5. 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;
  6. Save and Close it and restart the Nginx service.

    # sudo systemctl reload nginx 
  7. We will test the PHP-FPM with NGINX Web Server. Let us create phpinfo.php page in root directory.

    # sudo nano /usr/share/nginx/html/info.php
  8. Paste the php info code as below.

    <?php phpinfo();>

Was this answer helpful?

« Back
Sign up for a newsletter