Web servers software

How to Install a LAMP Stack on Ubuntu 24.04 or Debian 12

Install Apache, MySQL (or MariaDB on Debian) and PHP on Ubuntu 24.04 or Debian 12, then add a virtual host and test the stack.

LAMP stands for Linux, Apache, MySQL (or MariaDB) and PHP. It is the stack that WordPress, Joomla and most other PHP applications expect. This guide installs it on Ubuntu 24.04 LTS; the same commands work on Ubuntu 22.04, and the differences for Debian 12 are pointed out where they exist. When you finish, Apache will serve PHP pages for your domain and a database will be ready for your application.

Older tutorials on this topic used yum and chkconfig on CentOS 6, which is end-of-life. If you run a RHEL-family system, use AlmaLinux 9 or Rocky Linux 9 and their dnf packages instead; the steps below are for Ubuntu and Debian.

Requirements

  • A server running Ubuntu 24.04 LTS, Ubuntu 22.04 LTS or Debian 12.
  • A user with sudo rights (the examples use deploy).
  • Optional: a domain such as example.com pointing to the server's IP address, for example 203.0.113.10.

Refresh the package index first:

sudo apt update

Step 1: Install Apache

sudo apt install -y apache2

The package starts Apache and enables it at boot, so the chkconfig step from older guides is no longer needed. If you use the UFW firewall, allow web traffic (and make sure SSH stays allowed before you enable UFW):

sudo ufw allow OpenSSH
sudo ufw allow "Apache Full"

Visit http://203.0.113.10/ with your own address. The Apache default page confirms the web server is reachable.

Step 2: Install the database server

On Ubuntu, install MySQL (Ubuntu 24.04 ships MySQL 8.0):

sudo apt install -y mysql-server

Debian 12 does not package MySQL; install MariaDB, which is a drop-in replacement for this purpose:

sudo apt install -y mariadb-server

Then run the hardening script and answer Y to removing anonymous users, disallowing remote root logins and removing the test database:

sudo mysql_secure_installation

On both systems the database root account is authenticated through the local socket, so you log in with sudo and no password:

sudo mysql

Create a database and a separate user for your application. Replace the password with your own:

CREATE DATABASE appdb CHARACTER SET utf8mb4;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'change-this-password';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
EXIT;

Step 3: Install PHP

Install PHP, the Apache module that runs it, and the MySQL driver. Ubuntu 24.04 provides PHP 8.3 and Debian 12 provides PHP 8.2:

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

Most applications need a few more extensions. This set covers WordPress and similar software:

sudo apt install -y php-curl php-gd php-mbstring php-xml php-zip php-intl

Restart Apache so it loads the module and extensions:

sudo systemctl restart apache2

The "P" in LAMP is sometimes read as Perl or Python as well. Both interpreters are already part of a standard Ubuntu or Debian server install (perl and python3), so there is nothing extra to install unless your application asks for specific modules.

Step 4: Create a virtual host for your site

You can drop files into the default document root, /var/www/html, but a dedicated virtual host keeps sites separate. Create a directory owned by your user:

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R deploy:deploy /var/www/example.com

Create the virtual host file:

sudo nano /etc/apache2/sites-available/example.com.conf

Add:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Enable the site and the rewrite module (needed for "pretty" URLs in most PHP applications), test the configuration and reload:

sudo a2ensite example.com
sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl restart apache2

Step 5: Verify that PHP works

Create a test file in the new document root:

echo '<?php phpinfo();' > /var/www/example.com/public_html/info.php

Open http://example.com/info.php. You should see the PHP information page with "Apache 2.0 Handler" as the Server API and a mysqli section further down. Remove the file when you are done, since it reveals your configuration:

rm /var/www/example.com/public_html/info.php

Maintenance and troubleshooting

  • Service control: sudo systemctl status apache2 mysql (the service is mariadb on Debian). Use reload for Apache configuration changes and restart after installing PHP extensions.
  • Logs: Apache writes to /var/log/apache2/; database problems show up in sudo journalctl -u mysql.
  • If the browser downloads .php files or shows their source, the PHP module is not enabled. Check that /etc/apache2/mods-enabled/ contains php8.*.load and restart Apache.
  • Updates: sudo apt update && sudo apt upgrade covers Apache, PHP and the database, because all three come from the distribution's repositories.

Reference: the Ubuntu Server guide for Apache and its PHP page.

Frequently asked questions

Should I use mod_php or PHP-FPM with Apache?

libapache2-mod-php is the simplest setup and is fine for a single site. PHP-FPM runs PHP as its own service, which lets Apache use its threaded event model and lets different sites run as different users. Consider it when you host several sites or need HTTP/2 performance.

What is the quickest way to manage databases without the command line?

phpMyAdmin is available as the phpmyadmin package. If you install it, protect it: serve it only over HTTPS and restrict access by IP address or an extra password, because it is a frequent target of automated login attempts.

Why can't my application log in to the database as root?

The root account uses socket authentication, which only works for the system root user on the server itself. Applications should use a dedicated account with a password, like appuser above, limited to the one database they need.

How do I add HTTPS?

Once your domain resolves to the server, request a free Let's Encrypt certificate with Certbot's Apache plugin. It creates the port 443 virtual host and renews the certificate automatically.

Share story:

Continue reading other guides on How VPS.