In this article, we will learn how to install the LAMP stack on Ubuntu 24.04. LAMP stands for Linux, Apache, MySQL/MariaDB, and PHP. This combination of software (LAMP) is widely used for hosting dynamic websites and web applications. We will go through each component of LAMP step-by-step to ensure a successful installation.
Table of Contents
Prerequisites
- AWS Account with Ubuntu 24.04 LTS EC2 Instance.
- A user with sudo privileges.
- Basic Knowledge of Linux, Apache, MySQL/MariaDB, and PHP (LAMP).
Step #1:Install Apache Web Server on Ubuntu
First, update the package manager cache to ensure your package index is up to date.
sudo apt update

Next, install the Apache web server using the APT package manager.
sudo apt install apache2 -y

Verify that Apache is running by checking its status.
sudo systemctl status apache2

Confirm that Apache is serving the default web page by visiting Public IP address
in your browser.

Step #2:Install MariaDB Database Server on Ubuntu
Add the MariaDB signing key and repository.
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'

sudo add-apt-repository 'deb [arch=amd64] http://mariadb.mirror.globo.tech/repo/10.11/ubuntu jammy main'

Update the package index to include the new repository.
sudo apt update

Install the MariaDB server and client packages.
sudo apt install mariadb-server mariadb-client -y

Check the version of MariaDB installed.
mariadb --version

Step #3:Secure MariaDB Database Server on Ubuntu
Secure your MariaDB installation by running the security script.
sudo mysql_secure_installation
- Press ENTER for the current root password (default is none).
- Answer ‘n’ to switch to unix_socket authentication
- Answer ‘Y’ and set a strong root password.
- Answer ‘Y’ to remove anonymous users.
- Answer ‘Y’ to disallow root login remotely.
- Answer ‘Y’ to remove the test database.
- Answer ‘Y’ to reload privilege tables.


Step #4:Install PHP on Ubuntu
To get the latest version of PHP, add the Ondrej Sury PPA.
sudo add-apt-repository ppa:ondrej/php

Update the package index to include the new repository.
sudo apt update

Install PHP 8.2.
sudo apt install php8.2 -y

Install additional PHP extensions.
sudo apt install php8.2-{mbstring,mysql,zip} -y

Verify the PHP installation.
php --version

Step #5:Test PHP Installation on Ubuntu
Create an info.php file in the web root to test PHP.
sudo nano /var/www/html/info.php

Add the following content.
<?php
phpinfo();
?>

Save and exit the file.
Visit http://Public IP address/info.php in your browser to see the PHP configuration page.

Step #6:Configure Apache Virtual Host (Optional)
Create a directory for your domain.
sudo mkdir -p /var/www/domain.com

Next, assign the following directory ownership.
sudo chown -R $USER:$USER /var/www/domain.com

The $USER environment specifies the currently logged-in user. This implies that the website directory will be owned by the logged-in user and not by root.
Next, assign directory permissions.
sudo chmod -R 755 /var/www/domain.com

Create a sample HTML file in your domain’s directory.
sudo nano /var/www/domain.com/index.html

Add the following content.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome to our sample domain!</title>
</head>
<body>
<h1>Success! The sample domain virtual host is working!</h1>
</body>
</html>

Save and exit the file.
Create a virtual host configuration file for your domain.
sudo nano /etc/apache2/sites-available/domain.com.conf

Add the following configuration.
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and exit the file.

Enable the new virtual host.
sudo a2ensite domain.com.conf

disable the default site.
sudo a2dissite 000-default.conf

Check the Apache configuration for syntax errors.
sudo apache2ctl configtest

Restart Apache to apply the changes.
sudo systemctl restart apache2

Visit Public IP address
in your browser to confirm that the virtual host is working.

Conclusion:
In conclusion, installing the LAMP stack on Ubuntu 24.04 involves setting up Apache, MariaDB, and PHP, each with its own configuration steps. This guide walked you through updating package repositories, installing the necessary components, securing your database, and testing your web server and PHP configuration. Additionally, we covered optional steps for configuring Apache virtual hosts, allowing you to host multiple websites on a single server. Following these steps to install LAMP ensures a robust and flexible environment for developing and hosting web applications.
Related Articles:
Shell Script to find empty files and directories
Reference: