How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04

The LAMP software stack is a group of software that can be used to create dynamic web pages & apps. LAMP describe a Linux operating system, Apache web server, MySQL database and PHP for dynamic processing.

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04
Prerequisites
Very first we need to connect through root user
$ ssh root@your_server_ip
Please complete the login process by accepting the warning about host authenticity, if it appears, then providing your root authentication (password or private key). If it is your first time logging into the server with a password, you will also be prompted to change the root password. Once you are logged in as root, we’re prepared to add the new user account that we will use to log in from now on. This example creates a new user called “aroham”, but you should replace it with a username that you like: Create a New User
adduser aroham
To add these privileges to our new user, we need to add the new user to the “sudo” group.
$ usermod -aG sudo aroham
On the server, as the root user, enter the following command to temporarily switch to the new user
su - aroham
Now we are ready to processing setup LAMP, All of the software we will be using for this procedure will come directly from Ubuntu’s default package repositories. This means we can use the apt package management suite to complete the installation.
Step 1: Install Apache and Allow in Firewall
As this is our first time using “apt” for this session, we need updating our local package index.
$ sudo apt-get update
Now we can install server
$ sudo apt-get install apache2
Since we are using a sudo command, these operations get executed with root privileges. It will ask you for your regular user’s password to verify your intentions. Once you’ve entered your password, apt will tell you which packages it plans to install and how much extra disk space they’ll take up. Press Y and hit Enter to continue, and the installation will proceed.
Set Global ServerName to Suppress Syntax Warnings
Next, we will add a single line to the /etc/apache2/apache2.conf file to suppress a warning message. Open up the main configuration file with your text edit:
$ sudo nano /etc/apache2/apache2.conf
Inside, at the bottom of the file, add a ServerName directive, pointing to your primary domain name. If you do not have a domain name associated with your server, you can use your server’s public IP address:
. . .
ServerName server_domain_or_IP
Save and close the file when you are finished. As we chnages in config file so its necessary that all syntex are ok or not so we check for syntax errors by typing:
$ sudo apache2ctl configtest
Since we added the global ServerName directive, all you should see is:
Output
Syntax OK
Now we need to restart Apache to implemnt our chnages
$ sudo systemctl restart apache2
Adjust the Firewall to Allow Web Traffic
if you have the ufw firewall running, as outlined in our initial setup guide, you will need to make sure that your firewall allows HTTP and HTTPS traffic. You can make sure that UFW has an application profile for Apache like so:
$ sudo ufw app list
Output
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH
If we look at the Apache Full profile, it should show that it enables traffic to ports 80 and 443:
$ sudo ufw app info "Apache Full"
Output
Output
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.

Ports:
  80,443/tcp
Allow incoming traffic for this profile:
$ sudo ufw allow in "Apache Full"
With the new firewall rule added, you can test if the server is up and running by accessing your server’s domain name or public IP address in your web browser.
http://server_domain_or_IP
You will see the default Ubuntu 16.04 Apache web page, which is there for informational and testing purposes.
Step 2: Install MySQL to Manage Site Data
We can install this easily through this command
$ sudo apt-get install mysql-server
Now it will prompt to supply a root (administrative) password for use within the MySQL system. If we need different MySQL User then we can create
mysql -u root -p
It will prompt root password, to enter the root password and process further This example creates a new user called “arohamdb”, but you should replace it with a username that you like:
mysql > CREATE USER 'arohamdb'@'localhost' IDENTIFIED BY 'password';
At this point, new user has no permissions to do anything with the databases. Therefore, the first thing to do is to provide the user with access to the information they will need.
mysql > GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.
mysql > FLUSH PRIVILEGES;
Step 3: Install PHP for Processing
We now have Apache installed to serve our pages and MySQL installed to store and manage our data. Now we need to install PHP to access our web pages We can once again leverage the apt system to install our components. We’re going to include some helper packages as well, so that PHP code can run under the Apache server and talk to our MySQL database:
$ sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql
This should install PHP without any problems. We’ll test this in a moment. In most cases, we’ll want to 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 and we need to 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
It will look like this:

    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

as we need its look up for index.php first so we chnage like this

    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

Save and close the file by pressing Ctrl-X. You’ll have to confirm the save by typing Y and then hit Enter to confirm the file save location. After this, we need to restart the Apache web server in order for our changes to be recognized. You can do this by typing this:
$ sudo systemctl restart apache2
Step 4: Create a PHP File to Test Configuration
Your LAMP stack should now be completely set up. Now we need to test. We can do this by creating a test PHP file in our document root. Open a new file called serverinfo.php within your document root in your text editor:
$ sudo nano /var/www/html/serverinfo.php
Type or paste the following lines into the new file. This is valid PHP code that will return information about our server:
<?php
phpinfo();
?>
When you are finished, save and close the file. Now, you can visit this page in your web browser by visiting your server’s domain name or public IP address followed by /serverinfo.php:
http://server_domain_or_IP/serverinfo.php
You should see a web page that has been generated by PHP with information about your server
Conclusion
We have configured LAMP on Ubuntu 16.04 server. This gives you a very flexible foundation for serving web content to your visitors.
See Also
How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 16.04

Now is the time you start getting more online