On this page
- What do you need before installing the stack?
- How do you install Apache, MariaDB and PHP on Ubuntu or Debian?
- How do you install the stack on Rocky Linux or AlmaLinux?
- How do you secure the database and create an application user?
- How do you add a virtual host for your domain?
- Which firewall ports should be open?
- How do you prove PHP and the database work together?
- How do you add HTTPS?
- How do you keep the stack healthy?
- Questions
What do you need before installing the stack?
You need a fresh VPS, a sudo user and, if the site will have a name, a DNS record that points to the server. A small site with a few hundred visits a day runs comfortably on 2 GB of RAM; MariaDB and Apache together sit around 200 to 400 MB idle, and the rest is room for PHP workers and the page cache of the kernel.
- Log in as a normal user with sudo rights, as shown in your first SSH session and creating a sudo user.
- Point
example.netandwww.example.netat198.51.100.24(and the AAAA record at2001:db8:4f2a::24) using the steps in DNS records. - Update the package index and installed packages first.
# Ubuntu / Debian
sudo apt update && sudo apt -y upgrade
# Rocky Linux / AlmaLinux
sudo dnf -y upgradeThe versions you get depend on the release: Ubuntu 24.04 ships PHP 8.3, Ubuntu 22.04 ships PHP 8.1, Debian 12 ships PHP 8.2 with MariaDB 10.11, Debian 13 ships PHP 8.4 with MariaDB 11.8, and Rocky or Alma 9 start on PHP 8.0 with newer streams available through dnf module.
How do you install Apache, MariaDB and PHP on Ubuntu or Debian?
One apt command pulls in all three layers plus the PHP module for Apache and the MySQL driver for PHP.
sudo apt install -y apache2 mariadb-server php libapache2-mod-php \
php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip php-intl
sudo systemctl enable --now apache2 mariadb
apache2 -v
mariadb --version
php -vIf you prefer Oracle MySQL on Ubuntu, swap mariadb-server for mysql-server; Ubuntu 24.04 provides MySQL 8.0 in its main archive. Debian no longer carries Oracle MySQL, so on Debian the MySQL-compatible choice is MariaDB, which speaks the same protocol and works with the same php-mysql driver.
The libapache2-mod-php package runs PHP inside Apache with the prefork worker. That is the simplest layout and fine for one site. If you plan to host several sites or want per-site users, use PHP-FPM instead, described in PHP and PHP-FPM.
How do you install the stack on Rocky Linux or AlmaLinux?
On the RHEL family Apache is called httpd, and PHP runs through PHP-FPM by default, wired to Apache by /etc/httpd/conf.d/php.conf.
# optional: choose a newer PHP stream before installing
sudo dnf module list php
sudo dnf module reset -y php
sudo dnf module enable -y php:8.2
sudo dnf install -y httpd mariadb-server php php-fpm php-mysqlnd \
php-cli php-gd php-mbstring php-xml php-intl php-opcache
sudo systemctl enable --now httpd mariadb php-fpm
httpd -v
mariadb --version
php -vFor Oracle MySQL instead of MariaDB, install mysql-server and enable mysqld; the AppStream repository carries MySQL 8.0. Do not install both servers on the same machine, because they claim the same port and data directory.
How do you secure the database and create an application user?
Run the bundled hardening script, then give each application its own database and account rather than handing it root.
sudo mariadb-secure-installation # MySQL: sudo mysql_secure_installationAnswer yes to removing anonymous users, disabling remote root login and dropping the test database. On Debian and Ubuntu the MariaDB root account already authenticates through the Unix socket, so sudo mariadb works without a password and that is the safest default to keep.
sudo mariadb
CREATE DATABASE shopdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'shop'@'localhost' IDENTIFIED BY 'Use-A-Long-Random-Pass-91';
GRANT ALL PRIVILEGES ON shopdb.* TO 'shop'@'localhost';
FLUSH PRIVILEGES;
EXIT;The database listens on localhost only by default. Keep it that way unless another server must connect; the remote database access guide covers the safe way to open it.
How do you add a virtual host for your domain?
Create a document root owned by your deploy user and a small virtual host file that names the domain.
sudo mkdir -p /var/www/example.net/public
sudo chown -R deploy:deploy /var/www/example.netOn Ubuntu and Debian, save this as /etc/apache2/sites-available/example.net.conf:
<VirtualHost *:80>
ServerName example.net
ServerAlias www.example.net
DocumentRoot /var/www/example.net/public
<Directory /var/www/example.net/public>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.net-error.log
CustomLog ${APACHE_LOG_DIR}/example.net-access.log combined
</VirtualHost>sudo a2ensite example.net.conf
sudo a2dissite 000-default.conf
sudo a2enmod rewrite
sudo apache2ctl configtest && sudo systemctl reload apache2On Rocky or Alma, put the same block in /etc/httpd/conf.d/example.net.conf, replace the log lines with ErrorLog /var/log/httpd/example.net-error.log and CustomLog /var/log/httpd/example.net-access.log combined, then run sudo apachectl configtest && sudo systemctl reload httpd. Because the files live under /var/www, SELinux labels them correctly; if you copy files in from elsewhere, run sudo restorecon -Rv /var/www/example.net. More on labels is in SELinux basics.
Which firewall ports should be open?
Only SSH, HTTP and HTTPS need to be reachable from the internet; the database stays closed.
# Ubuntu / Debian with ufw
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status
# Rocky / Alma with firewalld
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-servicesThere is no network firewall in front of your VPS in the console, so the rules inside the operating system are the whole filter. See ufw or firewalld for more detail.
How do you prove PHP and the database work together?
Drop a short script into the document root that connects with the account you created, load it once, then delete it.
cat > /var/www/example.net/public/dbtest.php <<'EOF'
<?php
$db = new mysqli('localhost', 'shop', 'Use-A-Long-Random-Pass-91', 'shopdb');
echo 'PHP ' . PHP_VERSION . ', database ' . $db->server_info . PHP_EOL;
EOF
curl -s http://example.net/dbtest.php
rm /var/www/example.net/public/dbtest.phpA line such as PHP 8.3.x, database 10.11.x-MariaDB means Apache handed the request to PHP and PHP reached the database over the local socket. A blank page usually means a missing php-mysql (or php-mysqlnd) extension; check php -m | grep mysqli.
How do you add HTTPS?
Certbot with the Apache plugin requests a free certificate and edits the virtual host for you.
# Ubuntu / Debian
sudo apt install -y certbot python3-certbot-apache
# Rocky / Alma (EPEL provides certbot)
sudo dnf install -y epel-release
sudo dnf install -y certbot python3-certbot-apache mod_ssl
sudo certbot --apache -d example.net -d www.example.net
sudo certbot renew --dry-runRenewal runs from a systemd timer that the package installs. The Certbot guide covers wildcard names and DNS challenges.
How do you keep the stack healthy?
Patch all three layers through the package manager, watch the logs and size the PHP and database memory to the plan.
- Apply updates weekly with
sudo apt upgradeorsudo dnf upgrade, or turn on automatic security updates. - Read errors in
/var/log/apache2/or/var/log/httpd/andsudo journalctl -u mariadb. - Back up the database with
mariadb-dumpon a schedule and add the optional daily backups from the console; daily backups explains both. - When traffic outgrows the plan, deploy a larger one and move the site, following moving to a larger plan.
If Apache will not start, run sudo apache2ctl configtest (or apachectl configtest) and look at sudo ss -tlnp | grep -E ':80|:443' to see whether another program holds the port.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about LAMP stack: Apache, MariaDB, PHP
Should I pick MariaDB or MySQL for a LAMP server?
Either works with PHP through the same mysqli and PDO drivers. MariaDB is the default on Debian and an easy install everywhere; choose MySQL 8 when an application documents features only MySQL has. Run one, not both.
Is mod_php or PHP-FPM better with Apache?
mod_php is simpler for a single site. PHP-FPM with the event worker uses less memory under load and lets each site run as its own user, which is why the RHEL family uses it by default. See PHP-FPM pools.
Where do I upload my site files?
Into /var/www/example.net/public, owned by your deploy user. Use SFTP or rsync over SSH; no FTP server is needed.
Why does my PHP page download instead of running?
Apache is not passing .php files to PHP. On Ubuntu or Debian confirm libapache2-mod-php is installed and the module is enabled with a2query -m php8.3; on Rocky or Alma make sure php-fpm is running and restart httpd.
How much RAM does a LAMP server need?
A personal site or small shop fits in 2 GB. Busy shops with many PHP workers and a larger InnoDB buffer pool are more comfortable with 4 GB or more.
Can I use the WordPress image instead?
Yes. The WordPress app image deploys a ready web stack on Ubuntu 24.04, which saves these steps if WordPress is all you need.