Ubuntu: Install LAMP Stack

This blog receives a commission from affiliate partners through the affiliate links that we share with you in our posts. Although we receive a commission for using and linking their products, all our opinions and suggestions are unbiased.

What is LAMP Stack?

LAMP stack is a group of open-source software that is usually installed together to enable a server to host websites and web applications. The acronym represents L for the Linux Operating System, A for Apache Web Server, M for MySQL, and P for PHP. In this tutorial, we will cover how to install the LAMP stack on your Ubuntu Linux Server.

Also Read: CentOS 7: Install LAMP Stack

 

Update Software Packages

Before we start the installation it is a good idea to update software packages with the following command:

sudo apt update

 

Install Apache Web Server

Next, we will install Apache Web Server:

sudo apt install -y apache2 apache2-utils

Once the installation completed the Apache service should be automatically started. Check Apache’s status with systemctl:

sudo systemctl status apache2

The output would look something like the following:

 apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Sat 2018-09-08 10:46:05 UTC; 3min 37s ago
 Main PID: 1610 (apache2)
    Tasks: 55 (limit: 505)
   CGroup: /system.slice/apache2.service
           ├─1610 /usr/sbin/apache2 -k start
           ├─1612 /usr/sbin/apache2 -k start
           └─1613 /usr/sbin/apache2 -k start

If it is a case that Apache is not running then run the following command:

sudo systemctl start apache2

Also, configure Apache to start automatically when your server boots up:

sudo systemctl enable apache2

In order to check your Apache version run the following:

apache2 -v

You will see an output like this:

Server version: Apache/2.4.29 (Ubuntu)
Server built: 2018-06-27T17:05:04

You can then type in your server IP address into a browser on your local network to see the default Apache Web Server page.

If the connection is refused, configure the following firewall rule:

sudo ufw allow http

The next step would be to set www-data(The Apache User) as the webroot.

sudo chown www-data:www-data /var/www/html/ -R
Install MySQL(MariaDB)

Once the webserver is up and running we will install MySQL(MariaDB) next. MariaDB is a community-developed fork of the MySQL relational database management system.

Run the following script to start installing MariaDB:

sudo apt install mariadb-server mariadb-client

Once the installation completed you should check the status to see if MariaDB is active with the following command:

systemctl status mariadb

Sample output:

mariadb.service - MariaDB 10.1.34 database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-09-08 11:13:27 UTC; 21s ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
 Main PID: 3473 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 27 (limit: 505)
   CGroup: /system.slice/mariadb.service
           └─3473 /usr/sbin/mysqld

If it is not running you can start it with:

sudo systemctl start mariadb

Next step is to enable MariaDB to start automatically at system boot:

sudo systemctl enable mariadb

Next, we will move on to start the MySQL installation script:

sudo mysql_secure_installation

When it prompts you for the MariaDB root password you can go ahead and press enter and then enter y to set the root password for the MariaDB server. Next, you can press enter through the remaining steps which will remove the anonymous user, disable remote root login, and remove the test database.

After all the steps completed you can check the MariaDB version information:

mariadb -- version

Output:

mariadb Ver 15.1 Distrib 10.1.34-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

 

Install PHP

Last we will go ahead and install PHP:

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

Once PHP is installed we will create a script info.php under the webroot directory:

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

The script will open a blank file and then we will add the following PHP code inside the file:

<?php
phpinfo();
?>

Once all those steps are completed we can view the info.php page in the web browser using the server IP:

http://<server_ip_address>/info.php

The page will look similar to this one below:

After you performed the test in your browser you should remove the info.php file as it could give information about your server to unauthorized users. Run the following to remove the file:

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

You can recreate it at a later stage if you require the information. After all the tests and configurations you have completed the LAMP stack installation on your Ubuntu server. It is now ready to host websites and web applications.

Chad Crouch

Chad is an experienced Systems Administrator with exposure to various technologies and enterprise ICT environments. He has a huge passion for Technologies, specifically Linux, Open Source Software, Cloud Computing, DevOps, Software & Web Development, electronics, and IoT technologies. If you enjoy our articles please support us and make a small contribution: Buy Us A Coffee