This guide demonstrates how to install LAMP Stack on Ubuntu 20.04 server which use’s Apache as a server, to use Ngnix as a server we need to Install LEMP Stack on Ubuntu version’s 20.04 / 18.04

Here is the detailed step by step process to install LAMP Stack on Ubuntu version 18.04 / 16.04

1. Installing Apache Web Server

Apache web server is one of the most popular web servers in the world, there active community and detailed documentation over server eventually makes it a great choice for hosting a website.

Install Apache using ( Advanced Package Tool ) apt:

sudo apt update
sudo apt install apache2

The sudo (Super User DO) command allows you to run programs with the security privileges.

Once you’ve entered command, apt will display you about the packages it will install and how much extra space they’ll take up. Press Y and hit ENTER to continue, and the installation will proceed.

Finally installation of apache is done. You can now verify that by visiting your server’s public IP address in your web browser.

http://your_server_or_ip_address

You will see the default Ubuntu 18.04 Apache web page, which is there for informational and testing purposes. As a result, web page should look something like below:

Apache2 default page it works

This page indicates that your web server is active.

2. Installing MySQL Database Server

Now it is time to install MySQL database where your site can store information.

Install MySQL using ( Advanced Package Tool ) apt:

sudo apt install mysql-server

This command will show you a list of the packages that will be installed, along with the amount of disk space they’ll take up. Enter Y to continue.

Once MySQL installation is complete, we should run a simple security script to remove some dangerous defaults and lock down access to your database system.

sudo mysql_secure_installation

Now 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
Please set the password for root here.
New password:
Re-enter new password:

Estimated strength of the password: 100
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. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules.

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. To do this, open up the MySQL prompt from your terminal:

sudo mysql

Regardless of whether you chose to set up the VALIDATE PASSWORD PLUGIN or not, your server will next ask you to select and confirm a password for the MySQL root user. As root is an administrative account in MySQL that has increased privileges, make sure it is a strong password, and do not leave it blank.

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)

Now change the auth_socket to mysql_native_password.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
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)
exit

3. Installing PHP and Modules

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:

In addition, include some helper packages this time so that PHP code can run under the Apache server and talk to your MySQL database:

sudo apt install php libapache2-mod-php php-mysql

This should install PHP without any problems.

To check the PHP version that we have installed, run the following command:

php -v
PHP 7.4.3 (cli) (built: Mar 26 2020 20:24:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

In most cases, you should modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell the web server to prefer PHP files over others, so make Apache look for an index.php file first.

To do this, type this command to open the dir.conf file in a text editor with root privileges:

sudo nano /etc/apache2/mods-enabled/dir.conf

As a result, it should look something like below:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Move the PHP index file (highlighted above) to the first position after the DirectoryIndex specification, like this:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

When you finish, save and close the file by pressing CTRL+X. Confirm the save by typing Y and then hit ENTER to verify the file save location.

After that restart the Apache web server in order for your changes to be recognized. Do this by typing this:

sudo systemctl restart apache2

You can also check on the status of the apache2 service using systemctl:

sudo systemctl status apache2
Sample Outputâ—Ź apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Tue 2020-04-23 14:28:43 EDT; 45s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 13581 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
  Process: 13605 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
    Tasks: 6 (limit: 512)
   CGroup: /system.slice/apache2.service
           ├─13623 /usr/sbin/apache2 -k start
           ├─13626 /usr/sbin/apache2 -k start
           ├─13627 /usr/sbin/apache2 -k start
           ├─13628 /usr/sbin/apache2 -k start
           ├─13629 /usr/sbin/apache2 -k start
           └─13630 /usr/sbin/apache2 -k start

Press Q to exit this status output.

At this point, your LAMP stack is installed and configured. Before you do anything else, we recommend that you set up an Apache virtual host where you can assign domain name in your config file.

4. Setting Up Virtual Hosts

We use virtual hosts to encapsulate configuration details and host more than one domain from a single server.

Apache on Ubuntu 18.04 has one server block enabled by default that is configured to serve documents from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, let’s create a directory structure within /var/www for our your_domain site, 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 directory for your_domain as follows:

sudo mkdir /var/www/your_domain
Now assign ownership of the directory with the <code>www-data</code> environment variable:
sudo chown -R www-data:www-data /var/www/your_domain

The permissions of your web roots should be correct if you haven’t modified your unmask value, but you can make sure by typing:

sudo chmod -R 755 /var/www/your_domain

Next, create a sample index.html page using nano or your favorite editor:

nano /var/www/your_domain/index.html

Add the sample HTML:

/var/www/your_domain/index.html
<html>
    <head>
        <title>Welcome to Your_domain!</title>
    </head>
    <body>
        <h1>Hello World</h1>
    </body>
</html>

Now save and close the file.

Conclusion:

This brings us to the end of the guide ” How To Install LAMP 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.