Install LAMP Stack on Ubuntu 24.04/Debian 12
The LAMP stack—comprised of Linux, Apache, MySQL (or MariaDB), and PHP—remains the gold standard for web hosting and development. It is a reliable, well-documented, and widely supported environment that powers a significant portion of the internet.
Whether you are setting up a local development environment or a production-ready web server, knowing how to install the stack manually provides the ultimate control over your software configuration. In this guide, we will provide a comprehensive, step-by-step walkthrough to install a professional LAMP stack on Ubuntu 24.04 and Debian 12.
Prerequisites
Before beginning the installation, ensure your system meets the following requirements:
- A server running Ubuntu 24.04 or Debian 12.
- A user account with sudo privileges.
- An active internet connection to download the necessary packages.
Step 1: Update System Repositories
The first step in any server setup is ensuring the system's package list is up to date. This prevents installation errors and ensures you receive the latest security patches.
# Update the package list and upgrade all existing software
sudo apt update && sudo apt upgrade -y
Step 2: Install Apache Web Server
Apache is one of the most trusted and versatile web servers globally. It handles HTTP requests and serves your web content to the world.
# Install the Apache2 package
sudo apt install apache2 -y
# Start and enable Apache to ensure it launches automatically on boot
sudo systemctl start apache2
sudo systemctl enable apache2
Once the installation is complete, you can verify that the server is running by visiting http://localhost (or your server's IP address) in a web browser. You should see the default Apache "It works!" page.
Step 3: Install MariaDB Database Server
While MySQL is the original standard, MariaDB is the community-developed, high-performance fork that has become the preferred database choice for many Linux distributions, including Debian.
# Install the MariaDB server and client packages
sudo apt install mariadb-server mariadb-client -y
# Start and enable the MariaDB service
sudo systemctl start mariadb
sudo systemctl enable mariadb
Step 4: Secure the MariaDB Installation
By default, MariaDB installations are not fully secured for production environments. You should run the built-in security script to disable remote root access and remove anonymous users.
# Run the security script
sudo mysql_secure_installation
When prompted, follow these best practices:
- Set a root password: Choose a complex, secure password.
- Remove anonymous users: Say "Y" to remove them.
- Disallow remote root login: Say "Y" to prevent unauthorized external access to the root account.
- Remove the test database: Say "Y" to delete the default test entries.
- Reload privilege tables: Say "Y" to apply all changes.
Step 5: Configure a Dedicated Database User
For security reasons, you should never use the root user for your web applications. Instead, create a dedicated administrative user with specific permissions.
# Access the MariaDB prompt
sudo mysql
# Inside the MariaDB prompt, execute the following (replace 'your_password' with a strong password):
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
Step 6: Install PHP and Configure Apache
PHP is the engine that processes server-side scripts. We will install PHP along with the necessary modules for Apache and MariaDB.
# Install PHP and the required modules
sudo apt install php libapache2-mod-php php-mysql -y
# Configure Apache to prioritize .php files over .html files
sudo sed -i "s/index.html/index.php index.html/" /etc/apache2/mods-enabled/dir.conf
# Restart Apache to apply the new configuration
sudo systemctl restart apache2
# Create a test file to verify the PHP installation
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test.php
You can now confirm the installation by visiting http://localhost/test.php. You should see a page displaying your detailed PHP configuration.
Step 7: (Optional) Install phpMyAdmin
If you prefer a graphical web interface for managing your databases (similar to phpMyAdmin), you can install it easily:
# Install phpMyAdmin and required extensions
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-curl -y
# Enable the mbstring module
sudo phpenmod mbstring
# Restart Apache to apply changes
sudo systemctl restart apache2
Step 8: Configure Firewall (UFW)
If your server has a firewall active, you must allow traffic on ports 80 (HTTP) and 443 (HTTPS).
# Allow Apache traffic
sudo ufw allow 'Apache Full'
# Reload the firewall
sudo ufw reload
Summary Overview
| Component | Purpose | Key Command / Action |
|---|---|---|
| Apache2 | Web Server | sudo apt install apache2 |
| MariaDB | Database Server | sudo apt install mariadb-server |
| PHP | Scripting Engine | sudo apt install php libapache2-mod-php |
| Security | User/Access Control | mysql_secure_installation |
| Firewall | Network Security | sudo ufw allow 'Apache Full' |
Conclusion
Congratulations! You have successfully deployed a complete LAMP stack on your Ubuntu 24.04 or Debian 12 server. This professional environment is now ready to host dynamic applications like WordPress, Laravel, or any custom PHP-based projects. Your server is correctly configured with a secured database and a web server optimized for PHP performance.