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
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:
Before diving into the installation process, it's essential to understand the components that make up the LAMP stack:
Together, these components allow you to create fully dynamic web applications.
Choosing the LAMP stack for your web server offers many advantages:
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.
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
Install Apache using the package manager:
sudo apt install apache2 -y
Start and enable Apache:
sudo systemctl start apache2
sudo systemctl enable apache2
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.
Install the MariaDB server and client:
sudo apt install mariadb-server mariadb-client -y
Start and enable the MariaDB service:
sudo systemctl start mariadb
sudo systemctl enable mariadb
Run the security script:
sudo mysql_secure_installation
Follow the prompts to:
Install PHP and essential modules:
sudo apt install php libapache2-mod-php php-mysql -y
Restart Apache to recognize PHP:
sudo systemctl restart apache2
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
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:
index.phpindex.htmlRestart Apache:
sudo systemctl restart apache2
Create a PHP info page:
sudo nano /var/www/html/info.php
Add PHP information code:
<?php phpinfo(); ?>
http://your_server_ip/info.php (you should see your PHP information if it's working)sudo rm /var/www/html/info.php
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
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
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)
Congrats! By following this guide, you've established a complete LAMP stack environment on your Debian or Ubuntu server.
This setup provides:
Your server is now ready to host dynamic web applications.
Remember to:
Thanks for reading, and here's to modern dynamic applications!