How to Install Linux, Apache, MariaDB, and PHP (LAMP Stack) on Debian and Ubuntu

Learn step-by-step how to install and configure the LAMP stack on Debian and Ubuntu systems.

Publish date: 11/8/2024

The LAMP stack — consisting of Linux, Apache, MariaDB (or MySQL, which has mostly been depreciated in favor of MariaDB), and PHP is a popular solution for hosting dynamic websites and web applications.

This combination makes it easy for just about anyone to deploy data-intensive sophisticated apps like WordPress, Joomla, Drupal, and more. Far beyond what just standard static HTML could offer.

That said, in this guide, you'll learn how to install and configure the LAMP stack on Debian and Ubuntu systems.

First things first though:

Understanding the LAMP stack

Before diving into the installation process, it's essential to understand the components that make up the LAMP stack:

  1. Linux: The operating system that provides a stable and secure foundation.
  2. Apache: The web server that handles HTTP requests and serves web content to users.
  3. MariaDB: A powerful database management system, serving as an alternative to MySQL, known for its performance and reliability.
  4. PHP: A server-side scripting language designed for web development.

Together, these components allow you to create fully dynamic web applications.

Benefits of using the LAMP stack

Choosing the LAMP stack for your web server offers many advantages:

  • Flexibility: The stack supports various applications, from simple blogs to complex web applications, allowing for plenty of customization.
  • Compatibility: Each component of the LAMP stack is well-supported across multiple Linux distributions, ensuring smooth integration and operation (though most people/businesses stick with one Linux distribution).
  • Community support: With a large and active community, finding solutions to issues and accessing comprehensive documentation is incredibly easy.
  • Cost-effective: Being open-source, the LAMP stack eliminates licensing costs.

Best hosting for your LAMP stack

Being that we're on xTom's blog and all right now, we might be just a little biased, but we think if you give us a try, you'll agree that we're great too.

We have plenty of options for hosting your LAMP stack depending on the scale of your project:

Now that you've had a quick run down on what the LAMP stack is, why you'd want to use it, and how you can host it, let’s get started with setting up your LAMP environment on Debian or Ubuntu.

1. Update your system

Before installing new software, you should always make sure your system's package repositories are up to date.

This minimizes the risk of encountering issues during installation and ensures that you have access to the latest features and security patches.

sudo apt update
sudo apt upgrade -y

2. Install Apache web server

Initial setup

Install Apache using the package manager:

sudo apt install apache2 -y

Configure the service

Start and enable Apache:

sudo systemctl start apache2
sudo systemctl enable apache2

Verify installation

Visit your server's IP address in a web browser:

http://your_server_ip

If successful, you should see Apache's standard welcome page and something similar to this (this is on Debian, of course):

This is the default welcome page used to test the correct operation of the Apache2 server after installation on Debian systems. If you can read this page, it means that the Apache HTTP server installed at this site is working properly. You should **replace this file** (located at /var/www/html/index.html) before continuing to operate your HTTP server.

If you are a normal user of this web site and don't know what this page is about, this probably means that the site is currently unavailable due to maintenance. If the problem persists, please contact the site's administrator.

3. Install MariaDB

Basic installation

Install the MariaDB server and client:

sudo apt install mariadb-server mariadb-client -y

Service configuration

Start and enable the MariaDB service:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Security setup

Run the security script:

sudo mysql_secure_installation

Follow the prompts to:

  • Set root password
  • Remove anonymous users
  • Disallow remote root login
  • Remove test database
  • Reload privileges

4. Install PHP processing

Core installation

Install PHP and essential modules:

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

Web server integration

Restart Apache to recognize PHP:

sudo systemctl restart apache2

Verify PHP setup

Check your PHP version:

php -v

If successful, you should see something similar to:

PHP 8.2.24 (cli) (NTS)

Copyright (c) The PHP Group

Zend Engine v4.2.24, Copyright (c) Zend Technologies
with Zend OPcache v8.2.24, Copyright (c), by Zend Technologies

5. Configure Apache settings

Adjust directory settings

Edit the Apache configuration:

sudo nano /etc/apache2/mods-enabled/dir.conf

Update the DirectoryIndex directive:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html
</IfModule>

It's telling Apache to:

  • First look for index.php
  • If that's not found, then look for index.html

Apply changes

Restart Apache:

sudo systemctl restart apache2

Test the installation

6. Create a test file

Create a PHP info page:

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

Add PHP information code:

<?php phpinfo(); ?>

Verify and clean up

  1. Visit http://your_server_ip/info.php (you should see your PHP information if it's working)
  2. If it's working, remove the test file:
sudo rm /var/www/html/info.php

7. Set up firewall protection

Install UFW (Debian only)

For a quick and easy firewall, I like using UFW (Uncomplicated Firewall), which ships by default on Ubuntu and uses the much more complicated nftables in the background.

On Debian run:

sudo apt install ufw -y

Configure rules

Set up basic web traffic rules:

# Allow SSH first (very important to prevent lockout)
sudo ufw allow 22/tcp

# Add HTTP
sudo ufw allow 80/tcp

# Add HTTPS
sudo ufw allow 443/tcp

# Now enable UFW
sudo ufw enable

Verify security

Check firewall status:

sudo ufw status

If successful, you should see something similar to:

root@xtom-test:~# sudo ufw status
Status: active

To                         Action      From

--                         ------      ----
22/tcp                     ALLOW       Anywhere        
80/tcp                     ALLOW       Anywhere    
443/tcp                    ALLOW       Anywhere     
22/tcp (v6)                ALLOW       Anywhere (v6)  
80/tcp (v6)                ALLOW       Anywhere (v6)  
443/tcp (v6)               ALLOW       Anywhere (v6)

Conclusion

Congrats! By following this guide, you've established a complete LAMP stack environment on your Debian or Ubuntu server.

This setup provides:

  • A configured Apache web server
  • A secured MariaDB database
  • PHP processing capabilities
  • Basic security protections

Your server is now ready to host dynamic web applications.

Remember to:

  1. Regularly update all components
  2. Monitor system logs
  3. Perform routine backups
  4. Follow security best practices
  5. Keep documentation handy

Thanks for reading, and here's to modern dynamic applications!