Skip to main content
To install a LAMP stack on Ubuntu 24.04 or Debian 13, run sudo apt install apache2 mariadb-server php libapache2-mod-php php-mysql, then secure the database with sudo mysql_secure_installation. Apache runs PHP files straight away. On AlmaLinux or Rocky Linux, install httpd mariadb-server php php-mysqlnd and start httpd, mariadb and php-fpm. Test with a small PHP file.

Before you start

  • LAMP means Linux, Apache, MariaDB or MySQL, and PHP. Choose it when your app relies on .htaccess files; otherwise a LEMP stack with Nginx works just as well.
  • Use a user with sudo. If you are root, leave out sudo.
  • Nothing else may use port 80. Stop Nginx first if it is installed.

How to install Apache, MariaDB and PHP

Apache and MariaDB start automatically. libapache2-mod-php loads PHP into Apache, so no extra service is needed.

How to open the firewall

This Ubuntu profile opens ports 80 and 443. You can also allow 80/tcp and 443/tcp one by one.

How to secure MariaDB

Removing anonymous users, disallowing remote root login and removing the test database are the safe answers. Then create a separate database user for each app, as in create a MySQL database and user:

How to add your site

Follow the virtual host steps in install Apache: create /var/www/example.com/public_html, add a <VirtualHost *:80> file with ServerName example.com, enable it with sudo a2ensite example.com.conf (Ubuntu / Debian) and reload Apache. Install the PHP extensions most apps ask for:

How to check it worked

Create a test file in the site’s web root:
Request it on the server:
Expected output on Ubuntu 24.04:
apache2handler means Apache’s PHP module ran the file. On AlmaLinux / Rocky you see fpm-fcgi instead. Delete the file afterwards:
Do not leave test or phpinfo() files on a live site. They reveal versions and paths.

Frequently asked questions

Apache shows the PHP code instead of running it. Why?

PHP is not loaded. On Ubuntu / Debian, check that /etc/apache2/mods-enabled/ contains php8.*.conf and php8.*.load; if not, install libapache2-mod-php and restart Apache. On AlmaLinux / Rocky, make sure php-fpm is running.

Which PHP version do I get?

Ubuntu 22.04 has PHP 8.1, Ubuntu 24.04 has 8.3, Debian 12 has 8.2, Debian 13 has 8.4 and AlmaLinux / Rocky 10 has 8.3. For a different version, see multiple PHP versions.

Can I use PHP-FPM with Apache on Ubuntu?

Yes. Install php-fpm, run sudo a2enmod proxy_fcgi setenvif and sudo a2enconf php8.3-fpm (match your version), disable the module with sudo a2dismod php8.3, then restart Apache.

How do I install phpMyAdmin?

See install phpMyAdmin. Protect it with HTTPS and a strong password.

VPS and dedicated server guides

Every server guide in one place.

Install Apache

Virtual hosts in detail.

Install WordPress

Run WordPress on your stack.

Free SSL with Certbot

Add HTTPS to Apache.

Install a LEMP stack

The same with Nginx.

Checked against the official Ubuntu Server PHP documentation and Red Hat Enterprise Linux 9 PHP guide on 4 November 2026; Ubuntu commands, SQL and the PHP test tested on Ubuntu 24.04 LTS (Apache 2.4.58, MariaDB 10.11, PHP 8.3.6). Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026