Updated: 28 September 2026 · Applies to: Ubuntu 24.04 LTS
Ubuntu 16.04 is no longer supported, so this guide now uses Ubuntu 24.04 LTS (Debian 12 works the same way).
Install Apache, MariaDB and PHP on Ubuntu 24.04 in a few commands.
Before you start
- A VPS or dedicated server with a fresh Ubuntu 24.04 / Debian 12 or AlmaLinux 9 / Rocky Linux 9 installation, and root or sudo access.
- A domain or subdomain pointing to the server (an A record), if you want HTTPS.
- The system updated:
apt update && apt upgradeordnf upgrade.
Install the packages
apt update apt install apache2 mariadb-server php libapache2-mod-php php-mysql php-xml php-mbstring php-curl php-zip php-gd php-intl
Secure MariaDB with mariadb-secure-installation. Ubuntu 24.04 installs PHP 8.3 and MariaDB 10.11.
Open the firewall
ufw allow 80,443/tcp # Ubuntu / Debian with UFW firewall-cmd --permanent --add-service=http --add-service=https && firewall-cmd --reload # AlmaLinux / Rocky
Check it works
echo '<?php phpinfo();' > /var/www/html/info.php
Open http://SERVER-IP/info.php and delete the file afterwards.
Create the Apache site
Create /etc/apache2/sites-available/example.com.conf (Ubuntu/Debian) or /etc/httpd/conf.d/example.com.conf (AlmaLinux/Rocky):
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Ubuntu/Debian: a2ensite example.com && a2enmod rewrite && systemctl reload apache2. AlmaLinux/Rocky: apachectl configtest && systemctl reload httpd.
Add a free SSL certificate
apt install certbot python3-certbot-apache # Ubuntu / Debian dnf install epel-release && dnf install certbot python3-certbot-apache # AlmaLinux / Rocky certbot --apache -d example.com -d www.example.com
Certbot installs a timer that renews the certificate automatically; test it with certbot renew --dry-run.