YouTube video demonstration of how to install a LAMP stack on a virtual machine server

Introduction

A “LAMP” stack is a group of open source software that is typically installed together in order to enable a server to host dynamic websites and web applications written in PHP. The term is an acronym which represents the Linux operating system, Apache web server, MySQL database and PHP.

This guide will show you how to set up a virtual machine and install a LAMP stack on an Ubuntu 22.04 server. It will also show you how to store data in a MariaDB relational database, retrieve the data and serve it on a web page.

Prerequisites

In order to complete this tutorial, you will need to have a Google Cloud Platform (GCP) account.

Step 1 – Set up a Google Compute Engine virtual machine

i) Log in to your GCP console and go to the VM instances page

ii) Click Create instance.

iii. In the Name field, enter lamp-guide.

iv) In the Machine configuration section, select e2-micro for Machine type.

v) In the Boot disk section, click Change.

vi) In the Boot disk window, perform the following steps in the Public images tab:

  1. In the Operating system menu, select Ubuntu.
  2. In the Version menu, select Ubuntu 22.04 LTS.
  3. Click Select.

vii) In the Firewall section, select Allow HTTP traffic and Allow HTTPS traffic.

viii) Click Create.

Give the instance a few seconds to start up.

Step 2 – Deploying the LAMP stack on Compute Engine

In this section, you configure the LAMP stack.

Connect to your instance

i) In the Cloud Console, go to the VM instances page.

ii) In the list of virtual machine instances, click the SSH button in the row of the instance (lamp-guide) to which you want to connect. 

iii) Note down the IP address of your VM instance. You can see this address in the External IP column.

Install Apache and PHP on your instance

By creating an instance, you already have the Linux part of LAMP. In this section, you install Apache and PHP.

sudo apt-get update
sudo apt-get install apache2 php libapache2-mod-php
php -v

Test Apache and PHP

  1. Get the external IP address of your instance from the VM instances page in the Cloud Console.
  2. In a browser, enter your external IP address to verify that Apache is running:
http://[YOUR_EXTERNAL_IP_ADDRESS]

You should see the Apache test page. Make sure that you don’t use the https protocol specifier because HTTPS is not configured.

  1. Now you create a test file in the default web server root at /var/www/html/ .
cd /var/www/html/

ls
sudo nano phpinfo.php

Enter the following line in the text editor:

<?php phpinfo(); ?>

Then save and exit the nano text editor. (Ctrl + X and then press Y)

  1. Browse to the test file to verify that Apache and PHP are working together:

http://[YOUR_EXTERNAL_IP_ADDRESS]/phpinfo.php

You should see the standard PHP page that provides information about your current Apache environment.

If the page failed to load (HTTP 404), verify the following:

  • In the Cloud Console, HTTP traffic is allowed for your instance.
  • The URL uses http with the correct IP address and filename.

Install MariaDB (a relational database server)  on your instance

Install MariaDB and related PHP components:

sudo apt-get update

sudo apt-get install mariadb-server php php-mysql

Check the MariaDB server statuses

Run the following command to check whether the MariaDB database server is running: 

sudo systemctl status mariadb

If mariadb service is not running, then start the service with the following command:

sudo systemctl status mariadb

If the mariadb service is not running, then start the service with the following command:

sudo systemctl start mariadb

Let’s use the mysql client to connect to the database server:

sudo mysql

Configure MariaDB

Run the mysql_secure_installation command to improve the security of your installation. This performs steps such as setting the root user password if it is not yet set, removing the anonymous user, restricting root user access to the local machine, and removing the test database.

sudo mysql_secure_installation

STEP 3 – Testing Database Connection from PHP

sudo mysql -u root -p
CREATE DATABASE sample_database;
CREATE USER 'sample_user'@'%' IDENTIFIED BY 'ChooseYourPass@789!';
GRANT ALL ON sample_database.* TO 'sample_user'@'%';
CREATE TABLE sample_database.todo_list ( item_id INT AUTO_INCREMENT, content VARCHAR(255), PRIMARY KEY(item_id));
INSERT INTO sample_database.todo_list (content) VALUES ("My first important item");
sudo nano my-sample-data.php

Paste the following code in the my-sample-data.php script, save, and close it.

<?php

$user = "sample_user";

$password = "ChooseYourPass@789!";

$database = "sample_database";

$table = "todo_list";

try {

  $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);

  echo "<h2>TODO</h2><ol>";

  foreach($db->query("SELECT content FROM $table") as $row) {

    echo "<li>" . $row['content'] . "</li>";

  }

  echo "</ol>";

} catch (PDOException $e) {

    print "Error!: " . $e->getMessage() . "<br/>";

    die();

}

Open your browser and go to http://[YOUR_VM_EXTERNAL_IP]/my-sample-data.php to view the sample page with data from database.

If you encounter an error like “… driver not found”, restart your Apache server with the following command.

sudo systemctl restart apache2

What we accomplished in this tutorial

i) We set up a compute engine virtual machine (VM) on Google Cloud Platform (GCP);

ii) We installed a LAMP stack on the VM

iii) We populated the database with data and wrote a web page script to retrieve the data and display it on a web page which we accessed over the internet.

With the skills you learned in this blog and accompanying video you have skills to build on to build dynamic websites, develop backends for web and mobile applications, and pretty much anything you can build using the LAMP stack.

If you have comments or corrections about this blog post please write them in the comments section below and I will respond. Thank you.

References

  1. Setting Up LAMP on Compute Engine.
  2. How to Install LAMP Stack on Ubuntu 22.04
  3. How to sign up for your own GCP account.
  4. Caseray Cloud
  5. My (Ugochukwu Ukwuegbu’s) YouTube channel.