> ## 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 LEMP Stack on Ubuntu 24.04

> Install a LEMP stack on Ubuntu 24.04: Nginx, MariaDB and PHP-FPM with apt, connect Nginx to the PHP 8.3 socket and test it. Debian 13 and AlmaLinux steps too.

To install a LEMP stack on Ubuntu 24.04, run `sudo apt install nginx mariadb-server php-fpm php-mysql`, secure the database with `sudo mysql_secure_installation`, then add a server block that passes `.php` files to `unix:/run/php/php8.3-fpm.sock`. Test with `sudo nginx -t`, reload Nginx and open a PHP test page. On Debian 13 the socket is `php8.4-fpm.sock`.

| 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

* LEMP means **L**inux, **E**ngine-x (Nginx), **M**ariaDB or MySQL, and **P**HP. It runs most PHP apps, such as WordPress.
* Use a user with `sudo`. If you are `root`, leave out `sudo`.
* Allow ports 80 and 443 in your firewall, as in [install Nginx](https://help.freakhosting.com/portals/other/how-to-install-nginx.md).
* PHP version by release: Ubuntu 22.04 has 8.1, Ubuntu 24.04 has 8.3, Debian 12 has 8.2, Debian 13 has 8.4, AlmaLinux / Rocky 10 has 8.3. Need another version? See [multiple PHP versions](https://help.freakhosting.com/portals/other/how-to-install-multiple-php-versions.md).

## How to install Nginx, MariaDB and PHP

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo apt update
> sudo apt install nginx mariadb-server php-fpm php-mysql
> ```
>
> All three services start automatically. Check your PHP version, which is part of the socket name:
>
> ```bash theme={null}
> php -v
> ```

> [tab] AlmaLinux / Rocky 10
>
> ```bash theme={null}
> sudo dnf install nginx mariadb-server php-fpm php-mysqlnd
> sudo systemctl enable --now nginx mariadb php-fpm
> ```

> [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 instead:
>
> ```bash theme={null}
> sudo dnf module install php:8.2
> sudo dnf install nginx mariadb-server php-mysqlnd
> sudo systemctl enable --now nginx mariadb php-fpm
> ```

## How to secure MariaDB

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

Answer the prompts. Removing anonymous users, disallowing remote root login and removing the test database are the safe choices. More detail is in [secure MySQL and MariaDB](https://help.freakhosting.com/portals/other/how-to-secure-mysql-and-mariadb.md).

## How to connect Nginx to PHP-FPM

PHP-FPM (FastCGI Process Manager) runs PHP as its own service. Nginx hands it every `.php` request through a socket file.

**1. Create the site folder**

```bash theme={null}
sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
```

**2. Create the server block**

> [tab] Ubuntu / Debian
>
> Create `/etc/nginx/sites-available/example.com`:
>
> ```nginx theme={null}
> server {
>     listen 80;
>     listen [::]:80;
>     server_name example.com www.example.com;
>
>     root /var/www/example.com;
>     index index.php index.html;
>
>     location / {
>         try_files $uri $uri/ =404;
>     }
>
>     location ~ \.php$ {
>         include snippets/fastcgi-php.conf;
>         fastcgi_pass unix:/run/php/php8.3-fpm.sock;
>     }
>
>     location ~ /\.ht {
>         deny all;
>     }
> }
> ```
>
> Change `php8.3` to match `php -v` (for example `php8.4` on Debian 13). Then enable it:
>
> ```bash theme={null}
> sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
> ```

> [tab] AlmaLinux / Rocky
>
> Create `/etc/nginx/conf.d/example.com.conf`:
>
> ```nginx theme={null}
> server {
>     listen 80;
>     listen [::]:80;
>     server_name example.com www.example.com;
>
>     root /var/www/example.com;
>     index index.php index.html;
>
>     location / {
>         try_files $uri $uri/ =404;
>     }
>
>     location ~ \.php$ {
>         try_files $uri =404;
>         fastcgi_pass unix:/run/php-fpm/www.sock;
>         fastcgi_index index.php;
>         include fastcgi_params;
>         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
>     }
> }
> ```
>
> With SELinux enforcing, label the folder so Nginx may read it:
>
> ```bash theme={null}
> sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"
> sudo restorecon -Rv /var/www/example.com
> ```

**3. Test and reload Nginx**

```bash theme={null}
sudo nginx -t
sudo systemctl reload nginx
```

## How to check it worked

Create a small test file:

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

Request it through Nginx 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 fpm-fcgi
```

`fpm-fcgi` proves Nginx passed the request to PHP-FPM. The patch number may be newer on your server. Delete the file afterwards:

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

> [!WARNING]
>
> Never leave `phpinfo()` or test files on a live site. They show paths and settings that help attackers.

## Frequently asked questions

### I get "502 Bad Gateway". What is wrong?

Nginx cannot reach PHP-FPM. Usually the socket name in `fastcgi_pass` does not match the installed PHP version, or `php-fpm` is not running. See [fix 502 Bad Gateway](https://help.freakhosting.com/portals/other/how-to-fix-502-bad-gateway-nginx.md).

### My browser downloads the .php file instead of running it. Why?

The `location ~ \.php$` block is missing or the site you edited is not enabled. Run `sudo nginx -T | grep -n fastcgi_pass` to see what Nginx actually loaded.

### Should I use MariaDB or MySQL?

Both work for WordPress and most PHP apps. Ubuntu and Debian ship MariaDB as `mariadb-server`. See [install MySQL or MariaDB](https://help.freakhosting.com/portals/other/how-to-install-mysql-or-mariadb.md).

### How do I add HTTPS?

Run Certbot once your domain points to the server. See [free SSL with Certbot](https://help.freakhosting.com/portals/other/how-to-get-a-free-ssl-certificate-with-certbot.md).

## 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 WordPress](https://help.freakhosting.com/portals/other/how-to-install-wordpress-on-a-vps.md)
>
> Put WordPress on your LEMP stack.

> [card] [Create a database and user](https://help.freakhosting.com/portals/other/how-to-create-a-mysql-database-and-user.md)
>
> One database per app.

> [card] [Increase PHP upload size](https://help.freakhosting.com/portals/other/how-to-increase-php-upload-size-and-memory-limit.md)
>
> Raise upload and memory limits.

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

***

> [!NOTE]
>
> **Checked against the official [Ubuntu Server PHP documentation](https://ubuntu.com/server/docs/how-to/web-services/install-php/), [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) and [Debian 13 release notes](https://www.debian.org/releases/trixie/release-notes/whats-new.en.html)** on 4 November 2026; Ubuntu commands tested on Ubuntu 24.04 LTS (Nginx 1.24.0, 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.