Installing LAMP on Ubuntu 18.04 LTS

LAMP Ubuntu 18.04 LTS

Hey guys,
Today we will be learning about ‘Installing LAMP Stack on Ubuntu 18.04 LTS’

Prerequisites

  • Ubuntu 18.04 LTS installed.
  • Internet connection
  • Admin user

Introduction

Before we start let’s understand what one means by the LAMP Stack. A LAMP Stack is a bundle of open-source software that can be used to create web-based application or websites. LAMP is an acronym of :

  • Linux (Operation System),
  • Apache (Web-Server),
  • Mysql/MariaDB (Database),
  • PHP (Programming Language).

Whenever installing a LAMP Stack it’s a good habit to follow the thumb rule. i.e. GO BY THE WORDS.

Step 1: Update Ubuntu Softwares

it is a good habit to update all the software before installing something new.

sudo apt update
sudo apt upgrade

 

Step 2: Install Apache Web-Server

An Apache web-server is a free and open-source cross-platform software used to host a website or a web application which can be interacted using the internet or an intranet. This software is the one which communicates with the network and handles the requests and the responses. To install apache use the following command:

sudo apt install apache2

(You might be asked to enter the admin password).

Step 3: Install Firewalld

Firewalld is a firewall management tool which again is open source. If you have already installed this software you can skip this step else use the following command to install: 

sudo apt install firewalld

 

Step 4: Starting & Enabling Firewalld & Apache

So now we have installed Firewalld and Apache web-server, we will have to start it. Use the following code to start the Apache2 server and Firewalld respectively:

sudo systemctl start apache2
sudo systemctl start firewalld

Good, we have successfully installed and started the firewalld and apache service. But every time the OS reboots these services are interrupted and stopped, to avoid this we will have to enable the service. So every time the OS reboots they start up on there own and function properly. To do this use the following commands:

sudo systemctl enable apache2
sudo systemctl enable firewalld

 

Step 5: Adding ports and service expectations to the firewall

By default, the firewall doesn’t allow any port or service to interact with one other either inside the network or outside. For this to happen you need to add exceptions to the firewall so that it can allow any form of communication from it.

For this tutorial, we will be adding exceptions to port number 80 and 443 along with services like HTTP and HTTPs

sudo firewall-cmd --add-port=80/tcp
sudo firewall-cmd --add-port=443/tcp

sudo firewall-cmd --add-service=http
sudo firewall-cmd --add-service=https

Just like the services, the exception is also cleared once the system reboots. To make it permanent we add ‘–permanent’ at the end of the command like:

sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=443/tcp --permanent

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent

After adding the expection, it a good habit to reload the firewall for it to take effect. to reload use the below command:

sudo firewall-cmd --reload

Step 6: Install MySQL/MariaDB

MariaDB is a fork of MySQL Database, which means things one can do in MySQL can be done in MariaDB. For this tutorial we will be using MariaDB as a database instance.

To install MariaDB enter the next command:

sudo install mariadb-server

To check if MariaDB server is running or not run the below command

sudo systemctl status mariadb

MariaDB server is installed, let’s continue with its configuration. To securely configure it use the next command:

sudo mysql_secure_installation

Read and proceed with the installation according to your needs.

After the installation is complete its time to enable MariaDB service to get uninterrupted service after rebooting the OS. For that use the following command:

sudo systemctl enable mariadb

Step 7: Install PHP

PHP is a programming language used by most of the developers to create web-based software, may it be websites or web-applications. Many of the most powerful websites are created using PHP.

To install PHP on our server use the command below:

sudo apt install php <package-name>

Using the above command we will be installing PHP and <package-name> means installing any additional packages one might want to install.

Some packages are :

  • Libapache2-mod-php
  • php_mysql
  • php_pdo
  • php_mbstring
  • php_cli
  • php_gettext.

I would recommend installing all the above packages for smooth use of PHP on this server.

As apache is open-source web-server, you can make changes in how apache can work. For example you can change the default loading page. For this example we will be setting ‘.php’ file to be the apache default page.

sudo nano /etc/apache2/mod_enabled/dir.conf
// change the priority list as per your requirement by placing the default page extention to be first.

Lets restart apache to enforce the changes

sudo systemctl restart apache2

As you remember we had installed MariaDB (MySQL), we have an option to install a GUI to manage the RDBMS. For this PHPMyadmin is the perfect web-based Database Management GUI software. To install it use the following command:

sudo apt install phpmyadim

After PHPMyadmin is installed we must include it in apache config file so that apache can use it.

sudo nano /etc/apache2/apache2.conf

Add at the bottom of file the following line:

Include /etc/phpmyadmin/apache.config

Lets enable phpmyadmin

sudo phpenmod mbstring

Now restart Apache and mysql

sudo systemctl restart apache2
sudo systemctl restart mysql

Lets include an index.php page. By default the root directory is stored in /var/www/html. Lets create and add a file named index.php in it

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

Add the following line :

<?php
phpinfo();
?>

Save the file.

Step 8 : Testing the Page

You can test the page by two methods either running on localhost or running the server as a remote server and accessing it from some oher system.

// for localhost open the browser type
http://localhost
// for remote server access
http://<ip-address>/

Conclusion

Thats it for installing LAMP on Ubuntu 18 LTS. Now you can create the multiple web-based project using PHP and MySQL.

To understand and learn how to customize apache wait for the next post. till than thanks and hope this post was helpful.

Parth Shah

Full-stack developer with experience in configuring and maintaining Google Cloud Platform and Linux servers, Customizing of open-source projects. I have amazing tricks related to Cloud and servers. Can code in ASP.NET C#, PHP, Python with databases like MySQL and MS-SQL. I can work with Django 2.2(Python) and WordPress (PHP) frameworks I am a cyber security enthusiast.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *