How to Install LAMP Stack on Ubuntu 24.04 LTS

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.

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
How to Install LAMP Stack on Ubuntu 24.04 LTS 1

Next, install the Apache web server using the APT package manager.

sudo apt install apache2 -y
How to Install LAMP Stack on Ubuntu 24.04 LTS 2

Verify that Apache is running by checking its status.

sudo systemctl status apache2
How to Install LAMP Stack on Ubuntu 24.04 LTS 3

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

How to Install LAMP Stack on Ubuntu 24.04 LTS 4

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'
How to Install LAMP Stack on Ubuntu 24.04 LTS 5
sudo add-apt-repository 'deb [arch=amd64] http://mariadb.mirror.globo.tech/repo/10.11/ubuntu jammy main'
How to Install LAMP Stack on Ubuntu 24.04 LTS 6

Update the package index to include the new repository.

sudo apt update
How to Install LAMP Stack on Ubuntu 24.04 LTS 7

Install the MariaDB server and client packages.

sudo apt install mariadb-server mariadb-client -y
How to Install LAMP Stack on Ubuntu 24.04 LTS 8

Check the version of MariaDB installed.

mariadb --version
How to Install LAMP Stack on Ubuntu 24.04 LTS 9

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.
How to Install LAMP Stack on Ubuntu 24.04 LTS 10
How to Install LAMP Stack on Ubuntu 24.04 LTS 11

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
How to Install LAMP Stack on Ubuntu 24.04 LTS 12

Update the package index to include the new repository.

sudo apt update
How to Install LAMP Stack on Ubuntu 24.04 LTS 13

Install PHP 8.2.

sudo apt install php8.2 -y
How to Install LAMP Stack on Ubuntu 24.04 LTS 14

Install additional PHP extensions.

sudo apt install php8.2-{mbstring,mysql,zip} -y
How to Install LAMP Stack on Ubuntu 24.04 LTS 15

Verify the PHP installation.

php --version
How to Install LAMP Stack on Ubuntu 24.04 LTS 16

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
How to Install LAMP Stack on Ubuntu 24.04 LTS 17

Add the following content.

<?php
phpinfo();
?>
How to Install LAMP Stack on Ubuntu 24.04 LTS 18

Save and exit the file.

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

How to Install LAMP Stack on Ubuntu 24.04 LTS 19

Step #6:Configure Apache Virtual Host (Optional)

Create a directory for your domain.

sudo mkdir -p /var/www/domain.com
How to Install LAMP Stack on Ubuntu 24.04 LTS 20

Next, assign the following directory ownership.

sudo chown -R $USER:$USER /var/www/domain.com
How to Install LAMP Stack on Ubuntu 24.04 LTS 21

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
How to Install LAMP Stack on Ubuntu 24.04 LTS 22

Create a sample HTML file in your domain’s directory.

sudo nano /var/www/domain.com/index.html
How to Install LAMP Stack on Ubuntu 24.04 LTS 23

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>
How to Install LAMP Stack on Ubuntu 24.04 LTS 24

Save and exit the file.

Create a virtual host configuration file for your domain.

sudo nano /etc/apache2/sites-available/domain.com.conf
How to Install LAMP Stack on Ubuntu 24.04 LTS 25

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.

How to Install LAMP Stack on Ubuntu 24.04 LTS 26

Enable the new virtual host.

sudo a2ensite domain.com.conf
How to Install LAMP Stack on Ubuntu 24.04 LTS 27

disable the default site.

sudo a2dissite 000-default.conf
How to Install LAMP Stack on Ubuntu 24.04 LTS 28

Check the Apache configuration for syntax errors.

sudo apache2ctl configtest
How to Install LAMP Stack on Ubuntu 24.04 LTS 29

Restart Apache to apply the changes.

sudo systemctl restart apache2
How to Install LAMP Stack on Ubuntu 24.04 LTS 30

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

How to Install LAMP Stack on Ubuntu 24.04 LTS 31

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:

Bash reference manual

Prasad Hole

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap