Setting Up a LAMP Stack on AWS EC2 with Ubuntu

In this guide, we'll walk you through the process of setting up a LAMP stack (Linux, Apache, MySQL, PHP) on an AWS EC2 instance using Ubuntu 24. This step-by-step tutorial is perfect for those looking to deploy a web server in the cloud, enabling the hosting of dynamic websites and applications.

Introduction: What is a LAMP Stack?

The LAMP stack consists of four key components:

  • Linux: The operating system.

  • Apache: A popular open-source web server.

  • MySQL: A relational database management system.

  • PHP: A server-side scripting language used to create dynamic content.

When combined, these technologies provide a robust environment for hosting websites and web applications. In this tutorial, we will install and configure each component on an AWS EC2 instance running Ubuntu 24.

Why AWS EC2 and Ubuntu 24?

AWS EC2 (Elastic Compute Cloud) provides scalable virtual machines, making it a great choice for hosting applications. We’ll be using Ubuntu 24 as it’s a lightweight, secure, and widely supported Linux distribution, well-suited for server environments.

Prerequisites

Before we begin, make sure you have the following:

  1. An AWS account: If you don’t have one, you can create it at AWS Sign-Up.

  2. Basic knowledge of AWS EC2: Familiarity with launching and managing EC2 instances.

  3. Command Line Interface (CLI) experience: Basic understanding of using the command line, as we’ll use SSH to connect to the instance.

Step 1: Launch an EC2 Instance

1.1 Choose Ubuntu 24 AMI

  1. Log in to the AWS Management Console and navigate to EC2.

  2. Click on Launch Instance and search for the Ubuntu Server 24 LTS Amazon Machine Image (AMI).

  3. Select it to proceed.

1.2 Select an Instance Type

  1. For basic testing or development, choose a t2.micro instance type (eligible for the AWS Free Tier).

  2. Click Next.

1.3 Configure Security Group

  1. Configure your security group to allow access via ports:

    • 22 (SSH): To connect to the instance.

    • 80 (HTTP): For web traffic.

  2. Ensure these ports are open to your IP address or public as needed.

1.4 Create and Download Key Pair

  1. Create a new key pair (for SSH access) and download it. This is critical, as you will need it to access your instance.

  2. Launch the instance.

Step 2: Connect to Your EC2 Instance

2.1 Use SSH to Connect

  1. Use the following command to connect to your EC2 instance (replace keypair.pem and your-instance-public-ip with your key and IP address):

     ssh -i keypair.pem ubuntu@your-instance-public-ip
    

2.2 Update System Packages

Once connected, update your system packages:

sudo apt update

Step 3: Install Apache Web Server

3.1 Installation Command

Install Apache using the following command:

sudo apt install apache2

3.2 Verifying Apache Installation

After installation, verify that Apache is running by accessing your EC2 instance’s public IP address in a web browser. You should see the default Apache welcome page.

Step 4: Install MySQL

4.1 Installation Command

To install MySQL, run:

sudo apt install mysql-server

4.2 Create Password for MySql root user:

Log in to MySQL and and run the following command:


ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_passowrd BY '<desired password>';
EXIT;

4.3 Secure Installation Process

Secure your MySQL installation by running the following command:

sudo mysql_secure_installation

Follow the prompts to

remove unnecessary users and databases.

4.4 Creating a Test Database and User

Log in to MySQL and create a test database and user:

sudo mysql -u root -p
CREATE DATABASE testdb;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 5: Install PHP

5.1 Installation Command

Install PHP and necessary modules:

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

5.2 Verifying PHP Installation

Check the PHP version to ensure it was installed successfully:

php -v

Step 6: Configure Apache for PHP

6.1 Creating a Test PHP File

Create a test PHP file to verify PHP integration with Apache:

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

Add the following content to the file:

<?php
phpinfo();
?>

6.2 Restarting Apache

Restart the Apache server to apply the changes:

sudo systemctl restart apache2

Now, visit http://your-instance-public-ip/info.php in a browser, and you should see the PHP information page.

Step 7: Testing Your LAMP Stack

7.1 Creating a Simple PHP Script

Create a PHP file to test the connection to your MySQL database:

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

Add the following content:

<?php
$servername = "localhost";
$username = "testuser";
$password = "password";
$dbname = "testdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

7.2 Accessing Your Website

Visit http://your-instance-public-ip/dbtest.php to test the database connection. If successful, you should see the message “Connected successfully.”

Step 8: Setting Up Virtual Hosts (Optional)

8.1 Creating Directory Structure

Create a directory for your new site:

sudo mkdir /var/www/html/yourdomain

8.2 Granting Permissions

Set the appropriate permissions:

sudo chown -R $USER:$USER /var/www/html/yourdomain

8.3 Creating Virtual Host Files

Create a new virtual host file for your domain:

sudo nano /etc/apache2/sites-available/yourdomain.conf

Add the following content:

<VirtualHost *:80>
    ServerAdmin webmaster@yourdomain.com
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/html/yourdomain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

8.4 Enabling New Virtual Host Files

Enable the new site and restart Apache:

sudo a2ensite yourdomain.conf
sudo systemctl restart apache2

Conclusion

Congratulations! You’ve successfully set up a LAMP stack on AWS EC2 using Ubuntu 24. You now have a fully functional web server capable of hosting dynamic websites.

Next Steps: Consider securing your server by setting up SSL, optimizing your server for better performance, and regularly backing up your database.

Troubleshooting Tips

  • SSH Connection Issues: Ensure port 22 is open in your security group and that you're using the correct private key.

  • Apache Not Running: Verify that Apache is installed and running by checking its status with sudo systemctl status apache2.

  • Database Connection Issues: Ensure MySQL is running, and verify the database credentials in your PHP script.

Additional Resources

0
Subscribe to my newsletter

Read articles from Engr. Animashaun Fisayo Michael directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Engr. Animashaun Fisayo Michael
Engr. Animashaun Fisayo Michael

Frontend Developer | Javascript programmer | Registered Mechanical Engineer (MNSE, COREN) | Facilities Management Technologies Developer