This guide demonstrates how to install LEMP Stack on Ubuntu 20.04 server which use’s Ngnix as a server, to use Apache as a server we need to Install LAMP Stack on Ubuntu version’s 20.04 / 18.04 / 16.04
Here is the detailed step by step process to install LEMP Stack on Ubuntu version 18.04
1. Installing Nginx Web Server
The Nginx web server is the most popular web servers in the world. It’s active community and detailed documentation eventually makes it a great choice for hosting a website.
Install Nginx using ( Advanced Package Tool ) apt
:
Start off by updating your server’s package index:
sudo apt update sudo apt install nginx
Nginx registers itself with ufw
upon installation
It is recommended that you enable the most restrictive profile that will still allow the traffic you want. Since you haven’t configured SSL for your server in this guide, you will only need to allow traffic on port 80
.
To enable this type:
sudo ufw allow 'Nginx HTTP'
To verify the changes type:
sudo ufw status
sudo ufw status
command will show HTTP traffic allowed as follows:
OutputStatus: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Nginx HTTP ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6)
Now installation of Nginx server is done. You can verify that by visiting your server’s public IP address in your web browser.
http://server_domain_or_IP

2. Installing MySQL Database Server
Now that we have installed a web server, its time to install MySQL (a database management system) to store and manage the data for our site.
Install MySQL using ( Advanced Package Tool ) apt
:
sudo apt install mysql-server
Along with the amount of disk space they’ll take up, this command will show you a list of the packages that will be installed. Enter Y
to continue.
To secure the installation and modify insecure defaults, Initiate the following script:
sudo mysql_secure_installation
This script will ask if you want to configure the VALIDATE PASSWORD PLUGIN
.
Note: Always use strong passwords for database credentials.
Type Y
to enable, or any other key to continue without enabling.
VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of 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:
If you enable by typing y
, you’ll be prompted to select a password level by entering 0
or 1
or 2
.
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
Next, you’ll be asked to submit and confirm a root password:
Please set the password for root here. New password: Re-enter new password:
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :
For the rest of the questions, press Y
and hit the ENTER
key at each prompt.
If you prefer to use a password when connecting to MySQL as root, you will need to switch its authentication method from auth_socket
to mysql_native_password
.
sudo mysql
Now check which authentication method each of your MySQL user accounts use with the following command:
select user,authentication_string,plugin,host from mysql.user;
Output+------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | | auth_socket | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.00 sec)
We can observe that the root user authenticate using the auth_socket
plugin. To modify the root user to authenticate with a password, run the following ALTER USER
command. Be sure to change password
to a strong password of your choice:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Then, run FLUSH PRIVILEGES
which tells the server to reload the grant tables and put your new changes into effect:
FLUSH PRIVILEGES;
Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket
plugin:
SELECT user,authentication_string,plugin,host FROM mysql.user;
Output+------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.00 sec)
You will be able to observe that the root MySQL user now authenticates using a password.
Now to exit from MySQL CLI use the following command.
exit
To login back in to MySQL, we use sudo -u root -p
instead of .sudo mysql
sudo mysql -u root -p
Now enter the MySQL password.
3. Installing PHP
PHP is a server side scripting language which helps your code to communicate MySQL database and get information to display it on a web page or to enter given information into database.
Install PHP using ( Advanced Package Tool ) apt
:
sudo apt install php-fpm php-mysql -y
You now have your PHP components installed. Next, you’ll configure Nginx to use them.
4. Configuring Nginx to Use the PHP Processor
On Ubuntu 20.04, Nginx has one server block enabled by default and is configured to serve documents out of a directory at /var/www/html
. While this works well for a single site, it can become difficult to manage if you are hosting multiple sites.
Instead of modifying /var/www/html
, we’ll create a directory structure within /var/www
for the your_domain website, leaving /var/www/html
in place as the default directory to be served if a client request doesn’t match any other sites.
Create the root web directory for your_domain as follows:
sudo mkdir /var/www/your_domain
Next, assign ownership of the directory with the $USER environment variable, which will reference your current system user:
sudo chown -R $USER:$USER /var/www/your_domain
Then, open a new configuration file in Nginx’s sites-available
directory using your preferred command-line editor. Here, we’ll use nano
:
sudo nano /etc/nginx/sites-available/your_domain
This will create a new blank file. Paste in the following bare-bones configuration:
server { listen 80; server_name your_domain www.your_domain; root /var/www/your_domain; index index.html index.htm index.php; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; } location ~ /\.ht { deny all; } }
Activate your configuration by linking to the config file from Nginx’s sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
This will tell Nginx to use the configuration next time it is reloaded. You can test your configuration for syntax errors by typing:
sudo nginx -t
If any errors are reported, go back to your configuration file to review its contents before continuing.
When you are ready, reload Nginx to apply the changes:
sudo systemctl reload nginx
Your new website is now active, but the web root /var/www/your_domain
is still empty. Create an index.html
file in that location so that we can test that your new server block works as expected:
nano /var/www/your_domain/index.html
Include the following content in this file:/var/www/your_domain/index.html
<html> <head> <title>your_domain website</title> </head> <body> <h1>Hello World!</h1> <p>This is the landing page of <strong>your_domain</strong>.</p> </body> </html>
Now go to your browser and access your server’s domain name or IP address, as listed within the server_name
directive in your server block configuration file:
http://server_domain_or_IP
You’ll see a page like this:

If you see this page, it means your Nginx server block is working as expected.
5. Testing PHP with Nginx
You can test this by creating a new file called info.php
sudo nano /var/www/html/info.php
Type or paste the following lines into the new file. This is valid PHP code that will return information about your server:/var/www/your_domain/info.php
<?php phpinfo(); ?>
When you are finished, save and close the file by typing CTRL
+X
and then y
and ENTER
to confirm.
You can now access this page in your web browser by visiting the domain name or public IP address you’ve set up in your Nginx configuration file, followed by /info.php
:
http://server_domain_or_IP/info.php
You will see a web page containing detailed information about your server:

After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment and your Ubuntu server. You can use rm
to remove that file:
sudo rm /var/www/html/info.php
You can always regenerate this file if you need it later.
Conclusion:
Now that you have Installed LEMP STACK on Ubuntu 20.04, you can now host your website in the server. One of the popular thing you can do is install WordPress and design your own website or blog for your business.
To manage MySQL with GUI, we need to install phpMyAdmin. Operations such as the management of databases, tables, indexes, permissions, and so on are executed with the graphical user interface of phpMyAdmin.
Hope this tutorial is helpful and comment down if you have any query or issue.
