This guide demonstrates how to install LAMP Stack on Ubuntu 16.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.0418.04

Here is the detailed step by step process to install LAMP Stack on Ubuntu version 20.04 / 18.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 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-get install php libapache2-mod-php php-mcrypt 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.0.33-0ubuntu0.16.04.15 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33-0ubuntu0.16.04.15, Copyright (c) 1999-2017, 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 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

Install PHP Modules

To enhance the functionality of PHP, we can optionally install some additional modules.

To see the available options for PHP modules and libraries, you can pipe the results of apt-cache search into less, a pager which lets you scroll through the output of other commands:

apt-cache search php- | less

Use the arrow keys to scroll up and down, and q to quit.

The results are all optional components that you can install. It will give you a short description for each:

libnet-libidn-perl - Perl bindings for GNU Libidn
php-all-dev - package depending on all supported PHP development packages
php-cgi - server-side, HTML-embedded scripting language (CGI binary) (default)
php-cli - command-line interpreter for the PHP scripting language (default)
php-common - Common files for PHP packages
php-curl - CURL module for PHP [default]
php-dev - Files for PHP module development (default)
php-gd - GD module for PHP [default]
php-gmp - GMP module for PHP [default]
…
:

To get more information about what each module does, you can either search the internet, or you can look at the long description of the package by typing:

apt-cache show package_name

There will be a lot of output, with one field called Description-en which will have a longer explanation of the functionality that the module provides.

For example, to find out what the php-cli module does, we could type this:

apt-cache show php-cli

Along with a large amount of other information, you’ll find something that looks like this:

Output
…
Description-en: command-line interpreter for the PHP scripting language (default)
 This package provides the /usr/bin/php command interpreter, useful for
 testing PHP scripts from a shell or performing general shell scripting tasks.
 .
 PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
 open source general-purpose scripting language that is especially suited
 for web development and can be embedded into HTML.
 .
 This package is a dependency package, which depends on Debian's default
 PHP version (currently 7.0).
…

If, after researching, you decide you would like to install a package, you can do so by using the apt-get install command like we have been doing for our other software.

If we decided that php-cli is something that we need, we could type:

sudo apt-get install php-cli

You can install more than one module by using following command:

sudo apt-get install package1 package2 ...

Now that you have a LAMP stack installed, test that PHP is configured properly we will create a very basic PHP script called info.php

sudo nano /var/www/html/info.php

This will open a blank file, Add the following valid PHP code:

<?php
phpinfo();
?>

At last save and close the file.

Now that we have saved a valid PHP code, we can look for a some basic information.

To try this out, visit this page in your web browser:

http://your_domain/info.php

As a result, web page should look something like below:

PHP Default Page Ubuntu 16.04

As a result this page provides some basic information about your server from the perspective of PHP. In short it is useful for debugging and to ensure that your settings are being applied correctly.

Meanwhile it could actually give information about your server to unauthorized users, To block such activity we probably want to remove this file after this test.

sudo rm /var/www/html/info.php

Conclusion:

Now that we have installed LAMP STACK, 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.