> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Install a LAMP Stack on Ubuntu

> Install a LAMP stack on Ubuntu with sudo apt install apache2 mariadb-server php libapache2-mod-php php-mysql, secure MariaDB and test PHP. AlmaLinux steps too.

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.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Intermediate | 20 minutes | Ubuntu 22.04 / 24.04, Debian 12 / 13, AlmaLinux / Rocky 9 / 10 | SSH access with sudo, 1 GB RAM or more |

## Before you start

* LAMP means **L**inux, **A**pache, **M**ariaDB or MySQL, and **P**HP. Choose it when your app relies on `.htaccess` files; otherwise a [LEMP stack](https://help.freakhosting.com/portals/other/how-to-install-a-lemp-stack.md) 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

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo apt update
> sudo apt install apache2 mariadb-server php libapache2-mod-php php-mysql
> ```
>
> Apache and MariaDB start automatically. `libapache2-mod-php` loads PHP into Apache, so no extra service is needed.

> [tab] AlmaLinux / Rocky 10
>
> ```bash theme={null}
> sudo dnf install httpd mariadb-server php php-mysqlnd
> sudo systemctl enable --now httpd mariadb php-fpm
> ```
>
> On these systems Apache runs PHP through PHP-FPM by default, so `php-fpm` must be running.

> [tab] AlmaLinux / Rocky 9
>
> The default `php` package on release 9 is PHP 8.0, which is end-of-life upstream. Install the PHP 8.2 stream first:
>
> ```bash theme={null}
> sudo dnf module install php:8.2
> sudo dnf install httpd mariadb-server php-mysqlnd
> sudo systemctl enable --now httpd mariadb php-fpm
> ```

## How to open the firewall

> [tab] Ubuntu / Debian (ufw)
>
> ```bash theme={null}
> sudo ufw allow 'Apache Full'
> ```
>
> This Ubuntu profile opens ports 80 and 443. You can also allow `80/tcp` and `443/tcp` one by one.

> [tab] AlmaLinux / Rocky (firewalld)
>
> ```bash theme={null}
> sudo firewall-cmd --permanent --add-service=http --add-service=https
> sudo firewall-cmd --reload
> ```

## How to secure MariaDB

```bash theme={null}
sudo mysql_secure_installation
```

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](https://help.freakhosting.com/portals/other/how-to-create-a-mysql-database-and-user.md):

```bash theme={null}
sudo mariadb
```

```sql theme={null}
CREATE DATABASE appdb DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'Choose-A-Strong-Password-1';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

## How to add your site

Follow the virtual host steps in [install Apache](https://help.freakhosting.com/portals/other/how-to-install-apache.md): 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:

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo apt install php-curl php-gd php-intl php-mbstring php-xml php-zip
> sudo systemctl restart apache2
> ```

> [tab] AlmaLinux / Rocky
>
> ```bash theme={null}
> sudo dnf install php-gd php-intl php-mbstring php-xml
> sudo systemctl restart php-fpm
> ```

## How to check it worked

Create a test file in the site's web root:

```bash theme={null}
echo '<?php echo "PHP " . PHP_VERSION . " via " . php_sapi_name() . "\n";' | sudo tee /var/www/example.com/public_html/info.php
```

Request it on the server:

```bash theme={null}
curl -H "Host: example.com" http://127.0.0.1/info.php
```

Expected output on Ubuntu 24.04:

```text theme={null}
PHP 8.3.6 via apache2handler
```

`apache2handler` means Apache's PHP module ran the file. On AlmaLinux / Rocky you see `fpm-fcgi` instead. Delete the file afterwards:

```bash theme={null}
sudo rm /var/www/example.com/public_html/info.php
```

> [!WARNING]
>
> 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](https://help.freakhosting.com/portals/other/how-to-install-multiple-php-versions.md).

### 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](https://help.freakhosting.com/portals/other/how-to-install-phpmyadmin.md). Protect it with HTTPS and a strong password.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Install Apache](https://help.freakhosting.com/portals/other/how-to-install-apache.md)
>
> Virtual hosts in detail.

> [card] [Install WordPress](https://help.freakhosting.com/portals/other/how-to-install-wordpress-on-a-vps.md)
>
> Run WordPress on your stack.

> [card] [Free SSL with Certbot](https://help.freakhosting.com/portals/other/how-to-get-a-free-ssl-certificate-with-certbot.md)
>
> Add HTTPS to Apache.

> [card] [Install a LEMP stack](https://help.freakhosting.com/portals/other/how-to-install-a-lemp-stack.md)
>
> The same with Nginx.

***

> [!NOTE]
>
> **Checked against the official [Ubuntu Server PHP documentation](https://ubuntu.com/server/docs/how-to/web-services/install-php/) and [Red Hat Enterprise Linux 9 PHP guide](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/installing_and_using_dynamic_programming_languages/assembly_using-the-php-scripting-language_installing-and-using-dynamic-programming-languages)** 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.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.