
If you need a fast and stable web stack for PHP apps, the Install LEMP Stack on Ubuntu 26.04 LTS process gives you a clean base to start from. This guide shows how to set up Nginx, MariaDB, and PHP-FPM in a way that is easy to follow, secure, and useful for real work.
I wrote this as a Linux server tutorial for beginners and intermediate users who want more than a copy-paste install. Each step explains both what you do and why it matters, so you can understand the stack instead of blindly running commands.
Ubuntu 26.04 LTS is a strong choice for a modern PHP hosting environment because current guides commonly pair it with Nginx, PHP 8.5, and MariaDB 11.x or MySQL 8.4. That makes it a practical base for WordPress, Laravel, and custom PHP apps.
In this article, you will learn how to configure LEMP Stack on Ubuntu 26.04 from the ground up, test each service, and avoid the most common mistakes. By the end, you will have a working web server, database server, and PHP processing layer that can handle real traffic.
Prerequisites
Before you start this Install LEMP Stack on Ubuntu 26.04 setup, make sure you have the following:
- Ubuntu 26.04 LTS server with network access.
- A sudo user or root access for installing packages and editing config files.
- A domain name is optional, but useful if you plan to add HTTPS later.
- SSH access to your server so you can work safely from the terminal.
- Basic terminal tools such as
apt,systemctl,nanoorvim, andcurl.
Step 1: Update your system
Refresh package lists
sudo apt update
This command checks the Ubuntu repositories and downloads the newest package index. You need this because an outdated package list can cause install errors or pull older software than expected.
Upgrade installed packages
sudo apt upgrade -y
This upgrades installed packages to their latest available versions. It matters because security fixes and dependency updates reduce conflicts when you install Nginx, PHP, and MariaDB next.
Expected output
You should see package lists being read and upgraded packages being processed.
Reading package lists... Done
Building dependency tree... Done
The following packages will be upgraded...
Step 2: Install Nginx
Install the web server
sudo apt install nginx -y
This installs Nginx, the web server that will sit in front of your PHP app. Nginx is a good fit here because it handles static content efficiently and passes dynamic PHP requests to PHP-FPM.
Enable and start the service
sudo systemctl enable nginx
sudo systemctl start nginx
enable makes Nginx start after reboot, and start launches it now. This is important because a server stack should survive restarts without manual work.
Check service status
sudo systemctl status nginx
You want to see active (running) in the output.
Active: active (running)
That confirms Nginx installed correctly and is listening for web traffic.
Step 3: Install the database server
Choose MariaDB or MySQL
For this guide, MariaDB is a solid default because recent Ubuntu 26.04 tutorials commonly use it in LEMP builds. It gives you a mature database layer for WordPress, Laravel, and many PHP apps.
Install MariaDB
sudo apt install mariadb-server -y
This installs the database engine that stores your application data. You need a separate database server because web code should not mix with data storage logic, and that separation improves reliability and maintainability.
Enable and start MariaDB
sudo systemctl enable mariadb
sudo systemctl start mariadb
This makes the database start on boot and begins the service immediately. That matters because your PHP app cannot connect to the database if the database server is not running.
Check the status
sudo systemctl status mariadb
A healthy service should show active (running).
Step 4: Secure MariaDB
Run the security script
sudo mysql_secure_installation
This script hardens the default MariaDB install. It matters because database servers should not keep test data, anonymous users, or weak defaults on a live system.
What this script usually does
- Sets or confirms a root password policy.
- Removes anonymous users.
- Disables remote root login in common setups.
- Removes the test database.
- Reloads privilege tables.
These changes reduce risk right after installation, which is exactly when many servers stay vulnerable because people skip the hardening step.
Step 5: Install PHP and PHP-FPM
Install PHP packages
sudo apt install php php-fpm php-mysql php-curl php-xml php-mbstring php-zip php-gd -y
This installs PHP, PHP-FPM, and common extensions. PHP-FPM is the process manager Nginx uses to execute PHP code, and the extensions add database support, encoding support, archive handling, and image functions.
Why this step matters
Nginx does not run PHP by itself. It needs PHP-FPM to process .php files, and the right extensions help your web app work without errors.
Verify PHP version
php --version
Example output:
PHP 8.5.x (cli) (built: ...)
Ubuntu 26.04 guides commonly use PHP 8.5, so seeing that version is a good sign your system is on the expected track.
Step 6: Configure PHP-FPM with Nginx
Check the PHP-FPM socket
ls /run/php/
You should see a socket file such as:
php8.5-fpm.sock
This socket is the connection point between Nginx and PHP-FPM. If the socket name does not match your config, PHP pages will fail with a 502 error.
Create an Nginx server block
sudo nano /etc/nginx/sites-available/lemp.conf
Add this example config:
server {
listen 80;
server_name your-domain.com;
root /var/www/lemp;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
This file tells Nginx where your site lives and how to pass PHP requests to PHP-FPM. It also blocks hidden files, which helps protect sensitive config data.
Why each part matters
rootpoints Nginx to your site files.indextells Nginx which file to load first.try_filesprevents broken routing.fastcgi_passconnects Nginx to PHP-FPM through the socket.deny allprotects hidden files from public access.
These settings form the core of a working How To Install LEMP Stack on Ubuntu 26.04 deployment.
Enable the site
sudo mkdir -p /var/www/lemp
sudo ln -s /etc/nginx/sites-available/lemp.conf /etc/nginx/sites-enabled/
The directory holds your web files, and the symlink activates the virtual host. That separation lets you manage many sites cleanly on one server.
Test and reload Nginx
sudo nginx -t
sudo systemctl reload nginx
nginx -t checks for syntax errors before you reload the service. That matters because one typo in a config file can break the site for everyone.
Step 7: Test PHP processing
Create a test file
sudo nano /var/www/lemp/info.php
Add this content:
<?php
phpinfo();
This simple file proves PHP is being processed by the server and not downloaded as plain text. It is a useful test because it confirms the full path from browser to Nginx to PHP-FPM.
Open it in a browser
Visit:
http://your-domain.com/info.php
If everything works, you will see the PHP information page. That means your web server and PHP layer are talking correctly.
Remove the test file after checking
sudo rm /var/www/lemp/info.php
Do this because phpinfo() exposes environment details that you should not leave public on a live server.
Step 8: Connect PHP to the database
Create a database and user
sudo mysql
Then run:
CREATE DATABASE lempdb;
CREATE USER 'lempuser'@'localhost' IDENTIFIED BY 'StrongPasswordHere';
GRANT ALL PRIVILEGES ON lempdb.* TO 'lempuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
This creates a dedicated database and user for your app. A separate database user is better than using root because it limits damage if credentials leak.
Why this test matters
A LEMP install is not complete until PHP can talk to the database. Many server setups fail not because the packages are missing, but because the application cannot authenticate or the PHP MySQL extension is absent.
Step 9: Set permissions correctly
Fix ownership
sudo chown -R www-data:www-data /var/www/lemp
This gives the Nginx and PHP process user access to the web root. You need this so the web server can read the files and, when needed, write cache or upload data.
Why permissions matter
Too much access creates a security risk. Too little access causes blank pages, upload failures, or 403 errors. Good permissions are a balance between safety and function.
Step 10: Add HTTPS later
Install Certbot
sudo apt install certbot python3-certbot-nginx -y
This prepares your server for SSL certificates from Let’s Encrypt. HTTPS matters because browsers expect it, users trust it more, and modern sites should not expose login forms over plain HTTP.
Why this step is worth doing
If you plan to host WordPress, a custom app, or any login-based service, HTTPS should be part of the setup. It also helps create a more complete production-ready deployment.
Troubleshooting
1. 502 Bad Gateway
This usually means Nginx cannot reach PHP-FPM. Check whether the socket path in your Nginx config matches the actual file in /run/php/.
sudo systemctl status php8.5-fpm
ls /run/php/
2. Nginx config test fails
If nginx -t shows an error, the server block likely has a typo or missing semicolon. Fix the file, then run the test again before reloading.
sudo nginx -t
3. PHP file downloads instead of runs
This usually means Nginx is not passing PHP requests to PHP-FPM. Check the location ~ \.php$ block and confirm fastcgi_pass points to the correct socket.
4. Database connection fails
This often happens when the database user, password, or hostname is wrong. Recheck the credentials and make sure the PHP MySQL extension is installed.
5. Permission denied errors
If uploads or file writes fail, the web root ownership may be wrong. Reapply www-data:www-data ownership where needed and avoid loose permissions like 777.