in

LinuxLinux

How To Install [LEMP] Linux, NginX, MySQL & PHP on Ubuntu 18.04.2 Digital Ocean Server

Tutorial to install & deploy the LEMP Server on Ubuntu 18.04.2 in Digital Ocean

Install LEMP Stack on Ubuntu 18.04.2

Install LEMP on Ubuntu 18.04.2: Ubuntu is one of the most used server-side operating systems. This is due to the ease interface and it has many features that can be implemented on the server side too.

The “LEMP” stack is a group of software that pre-installed on the server to run the dynamic web apps on the server. The LEMP can be expanded as “Linux, NginX, MySQL & PHP”. The Linux is the base operating system software, Nginx is the web server which handles the request from inside and outside the server, MySQL is the database for the server & finally, The PHP is the programming language which the web apps based on the PHP language.

This article is suitable only for Ubuntu 18.04.2 version. 

Main Index:

Prerequisites:

In this article, You will lean, how to install the LEMP stack on Ubuntu 18.04.2 server through the step by step installing method and by through the “one-click app” installation method.

If you don’t have the Digital ocean account. Create the new Digital Ocean Account and get “100$” free credit to run web apps.

Install LEMP on Ubuntu 18.04.2 – Step by Step Method:

Just follow the below steps to install the LEMP stack in Ubuntu 18.04.2 server.

Jump To:

Install Ubuntu 18.04.2 on Digital Ocean:

Follow the article to download and install ubuntu 18.04.2 on Digital Ocean server.

Install Nginx Web Server:

Here we are going to deploy the Nginx server, Which will help you run the dynamic web apps in the server. Nginx is the most advanced one, everyone uses this server edition for their websites.

For installing nginx server, we can use the “apt” packet management. This suite will help you install the nginx server in your web server.

  • sudo apt update
  • sudo apt install nginx
Note: In Ubuntu 18.04.2, You just need to configure the Nginx server upon installation

Now it is the time to start the firewall settings. Nginx registers itself with ufw upon installation. This article doesn’t cover information about HTTPS configuration and so it is recommended to restrict use the most restricted profile. By following the below command, you will enable only the port 80 of your server.

  • sudo ufw allow ‘Nginx HTTP’

To verify the status,

  • sudo ufw status

The output of the command is,

Output

Status: active

To Action From
— —— —-
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)

Now you can load your domain or IP Address to check from browser side. If you don’t know how to add domain to nginx server, then follow the below command.

Check this too  Linux Ubuntu 18.04.2 LTS Edition Released, Download & What's New In it!!

Finding IP Address of your server:

To find the IP address of your web server,

  • ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s/\/.*$//’

The IP Address of your web server will print as the output.

http:// Your_IP_Address

copy the IP Address and paste in your browser, You will see the below image in the browser.

nginx has been installed
nginx has been installed

Installing MySQL Database:

You have successfully installed the Ubuntu 18.04.2 & nginx web server. Now it’s the time to install MySQL database on your server. Follow the below instructions to install the MySQL database. The MySQL is mainly used to store the information provided by the user on the server.

sudo apt install mysql-server

The above command will install the list of packages related to the MySQL and it will consume some space in your server. You need to enter “Y” and hit the “Enter” button.

sudo mysql_secure_installation

Now, you will be asked to configure the “VALIDATE PASSWORD PLUGIN

If you enable this feature, The MySQL will rejects the request from wrong password. So you need to be clear while entering the password and don’t forgot, save it some where else locally. Please enter the strong password.

Enter “Y” and hit the “Enter” button.

VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of the password and allows the users to set only those passwords which are secure enough.

Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:

Now, you will be asked to enter the password level

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters, and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

Despite whether you chose a “Validate Password Plugin“, You MySQL server will ask you about confirming the password that you entered. This is the superuser or administrative account and so you will have the increased privileges. Don’t forget to choose the Strong Password to the MySQL login.

Due to the “Validate password Plugin”, Your password strength will be shown on your screen. Now click enter button, you will be asked to “change the password”. If you are willing to change the password press “Y” and enter the new password otherwise, press “N” and click Enter button to proceed.

You will be asked more question in this setup, you can skip all the questions by entering “Y” & hit “Enter” button for each prompts.

Then finally enter the below code to exit the MySQL.

mysql> exit

Note: Don’t copy the “mysql>“. Just type “exit” & hit Enter button.

Installing PHP & Configuring to Nginx Server:

To generate dynamic data in the web server, you must need the PHP. Now it is the time to install the PHP on your server.

Check this too  How To Install Ubuntu 18.04.2 on Digital Ocean Server Using Putty

The nginx server does not contain php as the native application and so you need to install in the server separately. To install php, you need the “php=fpm“, fpm stands for “fastCGI process manager“.

  • sudo apt install php-fpm php-mysql

To install the php-fpm, copy the above code and install php in your server. “php-mysql” is mainly used for creating the communication between the PHP & MySQL database. This is done on ther server block level.

To do this, you must need to create a new block configuration filer on /etc/nginx/sites-available/ directory of your server. Create the new configuration file and and name to your own domain name.

  • sudo nano /etc/nginx/sites-available/sample.com

Replace “Sample.com” with your own domain name. 
Now you need to edit the new configuration file and add the following items in the configuration file.

server {
listen 80;
root /home/s1/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name sample.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;
}
}

Note: Replace “sample.com” with your domain name.

Each block have their own purpose and they are:

listen: Which listens to the port 80, The default port is 80

root: Defines the server where the root files are stored

server_name: Represents the domain name of your server, which points all the traffic to the domain name.

location /: Server look for the respective directive for the file otherwise it will return the default “Error 404” file.

location ~ \.php$: This block handles the PHP segment of your server.

location ~ /\.ht: This block deals with the .htacess file, in default the nginx server will not deal with .htaccess file.

Now, save the file and quit the file. Then you need to enable the new server block by creating the symbolic link.

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

Note: Replace with “sample.com” to your own

Now, unlink the default configuration file.

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

To test the new configuration file just type the following command in the console.

  • sudo nginx -t

If the code throw any error, then you need to go back to the previous step to reconfigure.

If everything seems good and then you need to restart your nginx server.

  • sudo systemctl reload nginx

Now, you are done installing the most important things to run your server. Now it is the time to test your server.

Testing the PHP & Nginx on your server:

“Php Info” is the basic method to verify the PHP & nginx functioning on the web server. Creating a simple script will show you the complete details of your servers PHP version & nginx server details.

To create the dynamic web app files in Ubuntu 18.04.2, You just need to access the “Web Root” folder. Which is presented in “/home/s1/html/” location.

sudo nano /home/s1/html/info.php

The above code will create a new file inside the root folder and now copy the below code and paste in your server console.

Check this too  5+ Ways To Restart Your Apache Server On Ubuntu 18.04, 18.04.2 & 19.04 Disco Dingo

info.php

<?php
phpinfo();
?>

After pasting the codes, save and quit.

To verify your server, you need to open the below URL in your browser.

http://ip_address_of_your_server/info.php

Note: Replace “ip_address_of_your_server” with your server IP Address

Now, in the browser, you will be able to see the below images that appear on your screen.

php verify
php verify

If the above image appears on the screen, Then the PHP and nginx are successfully installed in your web server. You can start importing your dynamic web apps into the root folder through the FTP and don’t forget to delete the PHP info page.

You can delete the PHP info page by entering the below command,

sudo rm /home/s1/html/info.php

That’s it, folks, By following the above article, you will able to install the LEMP stack from the scratch on your web server.

Install LEMP on Ubuntu 18.04.2 – One-Click App Method:

Think about this, Deploying server with pre-installed LEMP stack! How cool it is, right. Digital Ocean made the whole process very easily by introducing the One Click Apps. The One Click Apps are the feature provided by the Digital Ocean, which helps you to deploy the server with Pre-installed LEMP stack.

Follow the below steps to deploy a server with Pre-installed LEMP Stack.

Create a Droplet
Create a Droplet
  • Now, go to the Marketplace Tab
  • This Marketplace tab is once called as the One-click apps
  • Here, you will able to see the list apps that are available as the pre-installed apps
Marketplace Tab
Marketplace Tab
  • Go for “LEMP” stack and scroll down
Select the LEMP Stack
Select the LEMP Stack
  • Choose the plan of your web server
  • For small dynamic websites, we suggest you go with “Standard” plans.
Droplet plan
Droplet plan
  • Select the location of your server and scroll down
Server Location
Server Location
  • Enter the name of the droplet, select the number of droplets that you want to create at this instance & finally click the Green colored button named as “Create” will deploy the server with the pre-installed LEMP stack.
Creating a Droplet
Creating a Droplet

That’s it, friends, This is the easiest way to deploy the LEMP stack server. We suggest you use the Marketplace to deploy the server. Lesser time consuming and error free.

Verdict:

Basically, you have installed the platform for your server. Still, you need to do more work to convert into dynamic web apps. We always suggest you create the LEMP stack through the Marketplace method than the conventional one. The Conventional one is the most precise one but a time-consuming one. The one click app method is fixed one but lesser timing. The choice is yours.

Article Review
  • Article Explanation
  • Content Quality
  • Images Explanation
  • Methods Explanation
4.5

Summary

For more article, visit our Home Page

Leave a Reply

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

Written by Jim Moriarty

Hello, Am Jim from Arizona, Big Fan of Holmes. Server-side handler, Network Engineer & currently working for Tec Robust. I write a server-based article.

Contact me:
tecrobust [@] gmail.com
Sub: Message to Jim.

Snap Linux

Snap Linux- How to Install on Linux Ubuntu 18.04.2 LTS & More

Trex run game online

T-Rex Run Game- Play Online on Tec Robust & Get Immersed!!