Select Page
Welcome to our Support Center
< All Topics

Complete Guide: How To Install Linux, Apache, MySQL, PHP (LAMP stack) on Ubuntu

Complete Guide: How To Install Linux, Apache, MySQL, PHP (LAMP stack) on Ubuntu

Introduction

Setting up a web server environment is essential for hosting dynamic websites and web applications. In this comprehensive guide, we’ll walk you through the complete installation process of the LAMP Stack (Linux, Apache, MySQL, PHP) from scratch.

What is a LAMP Stack?

A LAMP stack is a popular open-source web development platform that consists of four main components:
 

  • Linux – Operating system
  • Apache – Web server software
  • MySQL – Database management system
  • PHP – Server-side scripting language

Prerequisites

Before starting this tutorial, ensure you have:

  • A fresh Ubuntu 20.04 or newer version
  • Root or sudo access
  • Basic command-line knowledge
  • Stable internet connection

For production environments, consider using reliable hosting providers like HostingHome, which offers optimized VPS hosting and dedicated servers perfect for LAMP deployments.

Setting Up LAMP Stack (Linux + Apache + MySQL + PHP)

 

Step 1: Update Your System

First, update your package manager and system packages:

bash
sudo apt update && sudo apt upgrade -y

Step 2: Install Apache Web Server

Apache is one of the most popular web server software solutions for hosting websites:

bash
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2

Verify Apache installation by visiting your server’s IP address in a browser. You should see the Apache default page.

Configure the firewall to allow web traffic:

bash
sudo ufw allow 'Apache Full'
sudo ufw enable

For detailed instructions on installing Apache, please refer to this guide.

Step 3: Install MySQL Database Server

MySQL provides robust database hosting capabilities for your web applications:

bash
sudo apt install mysql-server -y
sudo systemctl start mysql
sudo systemctl enable mysql

Secure your MySQL installation:

bash
sudo mysql_secure_installation
Follow the prompts to set a root password and remove insecure defaults. This is crucial for secure hosting environments.

This script will prompt you to configure the Validate Password Plugin, which helps enforce strong password policies for MySQL users.

VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin?Press y|Y for Yes, any other key for No:
If you enable validation, the script will prompt you to choose a password validation level. Selecting level 2—the strongest level—will enforce strict password requirements. This means you’ll encounter errors when setting passwords that don’t include a mix of uppercase and lowercase letters, numbers, special characters, or if the password is based on common dictionary words.
There are three levels of password validation policy:LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary filePlease enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

After installing MySQL, you’ll be prompted to submit and confirm a root password:

Please set the password for root here.New password:Re-enter new password:

For the remaining prompts, press Y, followed by ENTER to:

  • Remove anonymous users
  • Disallow remote root logins
  • Remove the test database
  • Reload privilege tables to apply changes

These steps help secure your MySQL installation.

For detailed instructions on installing MySQL, please refer to this guide.

Step 4: Install PHP

PHP enables dynamic content generation for your web hosting environment:

bash
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y

Test PHP installation by creating a test file:

bash
sudo nano /var/www/html/info.php

Add the following content:

<?php phpinfo(); ?>
Visit http://your-server-ip/info.php to verify PHP is working correctly.

Conclusion

Setting up a LAMP stack (Linux, Apache, MySQL, PHP) from scratch offers full control over your web server environment and remains a widely adopted solution for web hosting. Apache’s compatibility with a broad range of applications and its extensive community support make it ideal for shared and general-purpose hosting scenarios.

The LAMP stack provides a reliable and time-tested foundation for hosting websites, applications, and databases. For production environments, it’s beneficial to work with trusted hosting providers like HostingHome, who offer the infrastructure, support, and expertise to ensure seamless operations.

To maintain a secure and efficient hosting setup, always keep your stack components up to date, monitor performance, and follow best practices for system and application security. With the right configuration and ongoing maintenance, your LAMP stack will be a stable and scalable platform for powering your web projects.