To install LEMP on Ubuntu 26.04 or 24.04, use APT to install Nginx, MySQL and PHP-FPM from Ubuntu’s repositories. Together, they serve websites and run PHP applications with a database.
This guide sets up a local test site, checks PHP and MySQL, and explains how to protect a server. If you need help, leave a comment or contact us.
Before you begin
- Ubuntu 26.04 LTS or 24.04 LTS, Desktop or Server, with internet access and a sudo account.
- A fresh test system is recommended. Back up existing sites and databases before changing a shared server.
- Nginx normally uses port 80. An existing web server may already occupy it; do not stop a service without understanding the effect.
- Ubuntu’s Universe repository must be enabled for PHP-FPM. This example creates a local-only test on port 8080.
Install the LEMP Packages
1Refresh package lists
Open a terminal. Enter your Ubuntu account password if sudo asks for it.
2Install Nginx, MySQL and PHP
Install the web server, database, PHP process manager, MySQL extension, command-line PHP and curl. Review the package list before confirming. If PHP-FPM cannot be found, enable the Universe repository and run the update again.
sudo apt install nginx mysql-server php-fpm php-mysql php-cli curl
3Check the services
Nginx and MySQL should each report active. Use the PHP-FPM command for your Ubuntu version. The default packages use PHP 8.5 on 26.04 and PHP 8.3 on 24.04; a custom PHP installation may differ.
systemctl is-active nginx
systemctl is-active mysql
Ubuntu 26.04:
systemctl is-active php8.5-fpm
Ubuntu 24.04:
systemctl is-active php8.3-fpm
Visit http://localhost/ on the machine to check the default Nginx page. On a remote server, the local test commands below work over SSH. If Nginx fails to start, check whether another service is using port 80.
Connect Nginx to PHP-FPM
Nginx receives web requests. PHP-FPM runs PHP scripts and returns the result to Nginx through a local Unix socket. Installing PHP-FPM alone does not make Nginx execute PHP. The following separate server block listens only on this machine, so it does not replace the default site.
1Check the example paths
These names are examples. If any path is printed, stop and choose unused names throughout the following steps. A blank result means no listed path was found.
ls -ld /var/www/lemp-test /etc/nginx/sites-available/lemp-test /etc/nginx/sites-enabled/lemp-test 2>/dev/null
Also check whether a process already listens on port 8080. If a listener appears, choose an unused local port in the configuration and test URL.
sudo ss -ltnp '( sport = :8080 )'
2Create a test page
Create the new directory and a small PHP page. This shows only whether PHP and its MySQL extension loaded, not your server settings.
sudo mkdir -p /var/www/lemp-test
printf '%s\n' '<?php echo "PHP is working. MySQL extension: " . (extension_loaded("mysqli") ? "loaded" : "missing");' | sudo tee /var/www/lemp-test/index.php > /dev/null
3Create the Nginx configuration
Open a new configuration file:
sudo nano /etc/nginx/sites-available/lemp-test
Paste the configuration below. For Ubuntu 24.04, change php8.5-fpm.sock to php8.3-fpm.sock. Save with Ctrl+O and Enter, then exit with Ctrl+X. If using a custom FPM pool, use the socket shown by its configuration instead.
server {
listen 127.0.0.1:8080;
server_name localhost;
root /var/www/lemp-test;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
}
location ~ /\. {
deny all;
}
}
4Enable and test the configuration
Create the link and check Nginx syntax. Reload only if the test reports that the configuration is successful. If it fails, fix the reported error first. If you need to revert, remove only the symbolic link that you successfully created in this step; do not remove a pre-existing link or configuration.
sudo ln -s /etc/nginx/sites-available/lemp-test /etc/nginx/sites-enabled/lemp-test
sudo nginx -t
sudo systemctl reload nginx
Test from the server itself:
curl http://127.0.0.1:8080/
The response should say PHP is working. MySQL extension: loaded. This confirms PHP execution and the extension, but not a database connection. After testing, delete this page only if you successfully created it during this tutorial and have confirmed it is the temporary test file:
sudo rm /var/www/lemp-test/index.php
Check MySQL and Create a Database
Ubuntu normally uses local socket authentication for the MySQL root administrator. Open the MySQL prompt:
At the mysql> prompt, check existing names before creating an application database and user. Pick different names if either already exists.
SHOW DATABASES;
SELECT user, host FROM mysql.user;
The following creates a local-only account with a generated password and privileges on one database. Record the password shown once in a secure password manager; anyone with access to your terminal can see it. This example uses MySQL SQL, not shell commands.
CREATE DATABASE lemp_app;
CREATE USER 'lemp_user'@'localhost' IDENTIFIED BY RANDOM PASSWORD;
GRANT ALL PRIVILEGES ON lemp_app.* TO 'lemp_user'@'localhost';
EXIT;
Test the account by entering the generated password when prompted:
mysql -u lemp_user -p lemp_app
At the MySQL prompt, run SELECT DATABASE();. It should return lemp_app. Type exit to leave. Configure your application with its database name, local user and password; this login alone does not test the application.
Security and Remote Access
The example PHP site listens on 127.0.0.1 only. To host a public website, create a suitable server block for your domain, configure DNS and HTTPS with a valid certificate, and test the configuration before reloading. HTTP alone does not protect passwords in transit. Application-specific routing and hardening may also be needed.
Check the firewall before changing it:
If UFW is active and HTTP access is needed, allow port 80. Allow 443 only when HTTPS is configured. A cloud provider may have a separate firewall. Keep MySQL local and do not open port 3306 to the internet for this setup.
Remote SSH warning: Before enabling UFW, allow your actual SSH TCP port and verify the rule. The example uses port 22; replace it if your SSH server uses another port. Enabling a firewall without the correct rule can disconnect you. Keep your provider’s recovery console available.
sudo ufw allow 22/tcp
sudo ufw status numbered
Only after checking the correct SSH rule, enable UFW if you intend to use it:
sudo ufw enable
sudo ufw status verbose
Keep packages and applications updated. Back up both website files and databases, and test your restores. See the guide to automatic security updates.
Troubleshoot LEMP
Nginx will not start: Check its status and find any listener on port 80. An existing service may be hosting a site; investigate before stopping it.
sudo systemctl status nginx --no-pager
sudo journalctl -u nginx -n 50 --no-pager
sudo ss -ltnp '( sport = :80 )'
502 Bad Gateway: Check the PHP-FPM service for your release and confirm the socket in the server block matches its pool configuration. Run sudo nginx -t before reloading.
PHP source appears or the file downloads: If you successfully created the temporary test file in this tutorial, remove that file immediately. Do not delete a pre-existing file. Check the active server block and FastCGI configuration before testing again. Never expose application source or credentials.
APT installation was interrupted: Finish configuring unpacked packages and repair dependencies. Read errors before repeating the install.
sudo dpkg --configure -a
sudo apt --fix-broken install
Remove the Packages
Removing these packages can interrupt other websites and applications. Back up databases, files and configuration first, and review the proposed changes. This does not guarantee removal of all dependencies or data.
sudo apt remove nginx mysql-server php-fpm php-mysql php-cli
Do not run purge or autoremove blindly on a shared server. Remove the example configuration and files only after checking they are no longer needed.
Helpful Resources
Read the official Ubuntu guides to Nginx and MySQL, and the Nginx FastCGI documentation.
Frequently Asked Questions
What does LEMP stand for?
Linux, Nginx, MySQL and PHP. The E comes from the pronunciation of Nginx: engine-x.
Does Nginx run PHP by itself?
No. Nginx passes PHP requests to a processor such as PHP-FPM, which runs the script and returns its response.
Can I use Ubuntu Desktop for LEMP?
Yes. The same Ubuntu packages work on Desktop and Server, provided your repositories and system are configured appropriately.
Why is the test site only available locally?
The example listens on 127.0.0.1 to avoid exposing a temporary test site. A public website needs its own network, domain, TLS and security configuration.
Does the PHP test prove the database works?
No. It checks that PHP runs and the MySQL extension is loaded. Test the database account and your application separately.
Related guides: Install LAMP on Ubuntu | Install Ubuntu Server | Use Docker Compose on Ubuntu.
Views: 198