Website Vidya
DeploymentStep-by-Step Guide

How to Install LAMP Stack on Ubuntu 24.04

Learn how to install lamp stack on ubuntu 24.04 with Apache 2.4, MySQL 8.0, PHP 8.3, UFW firewall rules, and Apache virtual host configurations.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Install LAMP Stack on Ubuntu 24.04
AI Executive SummaryCORE CIS AI

5 min read

Learn how to install lamp stack on ubuntu 24.04 with Apache 2.4, MySQL 8.0, PHP 8.3, UFW firewall rules, and Apache virtual host configurations.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Installing and Configuring Apache HTTP Server
  • Step 2: Installing and Securing MySQL 8.0 Database Server
Grounded in full engineering runbookCORE CIS Ai Engine

The LAMP stack—consisting of Linux (operating system), Apache HTTP Server (web server), MySQL (relational database management system), and PHP (server-side programming language)—is the time-tested, foundational software bundle that shaped the modern dynamic World Wide Web.

Renowned for its unparalleled simplicity, comprehensive documentation, and broad native compatibility with platforms like WordPress, Drupal, Joomla, and Magento, LAMP remains the default development environment for millions of web developers globally.

Ubuntu 24.04 LTS (Noble Numbat) packages the latest stable releases of each component natively, including Apache 2.4, MySQL 8.0, and PHP 8.3.

In this guide, we walk through how to install lamp stack on ubuntu from package installation to configuring Apache Virtual Hosts and verifying PHP processing.

Advertisement

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

  • A server or virtual machine running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • A non-root user account with sudo administrative privileges.
  • UFW firewall installed.
  • Basic familiarity with terminal command-line operations.

Step 1: Installing and Configuring Apache HTTP Server

Apache is one of the world's most widely used web servers, known for its modular architecture, robust .htaccess directory-level configuration support, and broad language modules.

First, update your local APT repository index, then install the apache2 package:

Advertisement

Allow web traffic through UFW and verify that Apache is active:

# Update package lists and install Apache:
sudo apt update
sudo apt install -y apache2

# Allow HTTP (80) and HTTPS (443) through UFW:
sudo ufw allow 'Apache Full'

# Verify Apache service status:
sudo systemctl status apache2
# Expected output confirms: Active: active (running)

Step 2: Installing and Securing MySQL 8.0 Database Server

With Apache serving web content, install MySQL to store and manage relational database records for your applications.

Install the mysql-server package using apt:

Execute the interactive security script to configure root password security and eliminate sample test privileges:

# Install MySQL Server:
sudo apt install -y mysql-server

# Run interactive security configuration:
sudo mysql_secure_installation
# Recommendations during prompts:
# - Enable VALIDATE PASSWORD component? [Y] (optional)
# - Set strong root password
# - Remove anonymous users? [Y]
# - Disallow root login remotely? [Y]
# - Remove test database? [Y]
# - Reload privilege tables now? [Y]

Step 3: Installing PHP 8.3 and Apache PHP Integration

PHP processes dynamic server-side scripts and interacts with MySQL to render HTML content for client web browsers.

Advertisement

Ubuntu 24.04 includes PHP 8.3 in the main repository archives.

Install PHP along with the Apache PHP module (libapache2-mod-php), the MySQL database connector (php-mysql), and standard web libraries:

# Install PHP and essential Apache/MySQL modules:
sudo apt install -y php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-zip

# Verify installed PHP version:
php -v
# Expected output confirms: PHP 8.3.x (cli) ...

Step 4: Configuring an Apache Virtual Host

Apache Virtual Hosts allow a single server instance to host multiple distinct domains and websites, each with its own document root and logging policies.

Create a dedicated website directory at /var/www/example.com/html and assign ownership to your non-root user:

Create a new Virtual Host configuration file in /etc/apache2/sites-available/example.com.conf:

# Create document root directory:
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

# /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/html

    <Directory /var/www/example.com/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

Enabling the Virtual Host and mod_rewrite

Enable your new Virtual Host configuration using a2ensite, enable the essential rewrite module for clean SEO URLs, and reload Apache:

# Enable the Virtual Host configuration:
sudo a2ensite example.com.conf

# Enable the Apache URL rewrite module (required by WordPress and frameworks):
sudo a2enmod rewrite

# Test Apache syntax configuration:
sudo apache2ctl configtest
# Output confirms: Syntax OK

# Restart Apache web server:
sudo systemctl restart apache2

Step 5: Testing Dynamic PHP Processing in Apache

Confirm that Apache executes PHP scripts correctly rather than downloading them as raw text files.

Create a test file named info.php inside your document root:

Query the page via curl or visit the URL in your web browser:

# Create PHP verification file:
echo '<?php phpinfo(); ?>' > /var/www/example.com/html/info.php

# Test HTTP response using curl:
curl -I http://example.com/info.php
# Expected output confirms: HTTP/1.1 200 OK

# Remove test file for security:
rm -f /var/www/example.com/html/info.php

Step 6: Securing the LAMP Web Server with Let's Encrypt SSL

Production web servers must encrypt web traffic using HTTPS to safeguard user authentication sessions and sensitive transactional data.

Install Certbot alongside the Apache Certbot plugin to automate certificate acquisition and scheduled renewals:

Execute Certbot to generate trusted TLS certificates and automatically configure Apache SSL virtual hosts:

Certbot installs automated systemd timers to seamlessly renew certificates prior to their expiration.

# Install Certbot and Apache plugin:
sudo apt install -y certbot python3-certbot-apache

# Obtain and configure SSL certificate for your domain:
sudo certbot --apache -d example.com -d www.example.com

# Verify automated certificate renewal dry-run:
sudo certbot renew --dry-run

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name

Cause: Apache lacks a global ServerName directive in /etc/apache2/apache2.conf.

Resolution: Add ServerName 127.0.0.1 to the bottom of /etc/apache2/apache2.conf and restart Apache.

echo 'ServerName 127.0.0.1' | sudo tee -a /etc/apache2/apache2.conf && sudo systemctl restart apache2

Access denied for user 'root'@'localhost' in MySQL

Cause: MySQL uses auth_socket by default on Ubuntu, requiring sudo for root logins.

Resolution: Log in using sudo: sudo mysql -u root.

sudo mysql -u root

403 Forbidden error when visiting Apache Virtual Host

Cause: Incorrect filesystem permissions on /var/www/ or missing Require all granted in Virtual Host.

Resolution: Set directory permissions to 755: sudo chmod -R 755 /var/www/example.com.

sudo chmod -R 755 /var/www/example.com

Security Best Practices and Hardening

Ensure AllowOverride All is restricted strictly to your website's document root directory in Apache configuration.

Disable server signature banners by setting ServerSignature Off and ServerTokens Prod in /etc/apache2/conf-available/security.conf.

Secure website traffic with automated free SSL/TLS certificates via Certbot: sudo apt install -y certbot python3-certbot-apache && sudo certbot --apache -d example.com.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
Yes. By creating separate Virtual Host configuration files in /etc/apache2/sites-available/ and pointing each to a distinct document root in /var/www/, a single LAMP server can host dozens of different websites simultaneously.
Restart Apache with sudo systemctl restart apache2 and restart MySQL with sudo systemctl restart mysql.
mod_php embeds the PHP interpreter directly into every Apache worker process, which is simple to set up but memory-intensive. php-fpm processes PHP scripts in an isolated daemon, allowing Apache to run with high-performance event MPM mode.
Apache stores operational access logs in /var/log/apache2/access.log and error logs in /var/log/apache2/error.log.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Uncategorized•

How to Redirect HTTP to HTTPS in Nginx

If you’re running a website today and it’s still accessible over plain HTTP, you’re already behind. Modern browsers flag non-HTTPS sites as “Not Secure,” search engines favor encrypted websites, and users are far more cautious about where t...

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout