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.
Install the LEMP Packages
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.
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.
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.
Test the account by entering the generated password when prompted:
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.
For a dedicated host managed with native nftables, see how to install nftables on Ubuntu. Do not mix native nftables rules with UFW.
Only after checking the correct SSH rule, enable UFW if you intend to use it:
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.
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.
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.
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.
