LAMP stack which is the combination of Linux as the Operating System, Apache as the web server, MySQL as the database management system and PHP as the server side Script Language has become the most popular open source and free software combination used for web hosting globally. In this tutorial we are going to explain how you could set up a LAMP stack yourselves.

First we have to update the system using following commands. Remember to include “sudo” if you are not login as a root or alternatively you could become a root user using “sudo su” command and then execute the installation commands without “sudo”.

sudo apt-get update

sudo apt-get upgrade

Install LAMP Stack's Apache2 Web Server

First let’s install Apache2 Web Server. Following command will install the Apache2 Web Server on your Ubuntu machine.

sudo apt-get install apache2

You can type “http://localhost” or “http://127.0.0.1” to access your web server locally or “http://<your public ip address>” to access it from outside. This will display the “Apache2 Ubuntu Default Page” which can be found at “/var/www/html/index.html”.

Set up Firewall

Let’s enable and configure UFW Firewall for security. You can enable the Firewall by executing the below command.

sudo ufw enable

Below mentioned command will provide the status of the Firewall

sudo ufw status

Below command will provide a numbered list. This is helpful in removing the services.

sudo ufw status numbered

Let’s check the services available and enable the services we need.

sudo ufw app list

sudo ufw app info “<service>”

sudo ufw allow in “Apache Full”

Enable “Apache Full” if the server needs to be accessible from outside via “http” and “https”.

Install LAMP Stack's MySQL Server

Let’s install MySQL database server, so that web sites hosted on Apache Web Server can store data. Execute the below command to install MySQL Server on your machine.

sudo apt-get install mysql-server

After the installation we have to make the installation secure by executing the below command.

mysql_secure_installation

You can decide to enable the “VALIDATE PASSWORD PLUGIN” and allow MYSQL to generate an error when it encounter a weak password or it is fine to leave it disabled. Then you can change the root password or leave it as it is. Next remove anonymous user accounts, test databases and disable remote root logins. Finally make the changes effect immediately.

Finally we have to install and configure PHP to complete the LAMP stack. Execute the below commands to install PHP and link it with Apache2 and MySQL.

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

Now we have to restart Apache Web Server and check status by executing the below commands

sudo systemctl restart apache2

sudo systemctl status apache2

Finally, create a “info.php” file at “/var/www/html/” with the below code to check PHP is working.

<?php phpinfo() ?>

When you access this via localhost or your ip address it will display a detailed description of your PHP installation. Now you can go ahead and host your website.