Support Online
Skip to main content

LEMP Installation on Ubuntu: Web Server with Nginx, MySQL and PHP

What Will You Learn in This Guide?

In this guide, you will learn how to install LEMP stack on Ubuntu server step by step.
The aim is to create a secure and high-performance infrastructure for PHP-based applications.

Technical Summary

This content installs Nginx, MySQL and PHP-FPM components on Ubuntu.
Period; Covers web server installation, database security and PHP integration.


Prerequisites

  • Ubuntu 22.04 LTS server
  • User with authority sudo
  • UFW firewall active

1. Step: Install Nginx Web Server

Nginx is a high-performance web server that handles incoming HTTP requests.

sudo apt update
sudo apt install nginx
  • These commands install Nginx and start the service.

Check status:


sudo systemctl status nginx
  • Allow only HTTP traffic via UFW:


sudo ufw allow 'Nginx HTTP'
sudo ufw status
  • Verify the default page by going to the server IP address from the browser.

2. Step: Installing MySQL Database

  1. MySQL stores data for PHP applications.

sudo apt install mysql-server
  • After installation, run the security configuration:

sudo mysql_secure_installation
  • This process:
  1. Deletes the test database

  2. Removes anonymous users

  3. Limits root access

  4. Test connecting to MySQL:


sudo mysql
exit

3. Step: PHP and PHP-FPM Installation

  1. Nginx cannot run PHP codes directly.
  • That's why PHP-FPM is used.

sudo apt install php8.1-fpm php-mysql
  • This command installs PHP and MySQL integration.

Check service status:


sudo systemctl status php8.1-fpm

4. Step: Configuring Nginx with PHP

Create a sample site directory:


sudo mkdir /var/www/ornek.com
sudo chown -R $USER:$USER /var/www/ornek.com

  • Create a new Nginx server block:

sudo nano /etc/nginx/sites-available/ornek.com

  • Add the following configuration:

server {
listen 80;
server_name ornek.com www.ornek.com;
root /var/www/ornek.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.1-fpm.sock;
}

location ~ /\.ht {
deny all;
}
}
  • This configuration redirects PHP files to PHP-FPM.

Activate site:


sudo ln -s /etc/nginx/sites-available/ornek.com /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx

5. Step: Testing PHP Working

  • Create test file:

nano /var/www/ornek.com/info.php
  • Content:

<?php
phpinfo();
  • Check from browser:

http://sunucu_ip/info.php
  • Delete the file after testing:

sudo rm /var/www/ornek.com/info.php

6. Step (Optional): Testing MySQL Connection with PHP

  • Create a sample database and user:

CREATE DATABASE example_db;
CREATE USER 'example_user'@'%' IDENTIFIED WITH mysql_native_password BY 'strong_password';
GRANT ALL ON example_db.* TO 'example_user'@'%';
FLUSH PRIVILEGES;
  • You can test the connection with a simple PHP file.

Frequently Asked Questions (FAQ)

1. What is LEMP stack used for? Provides web infrastructure for PHP-based sites and applications.

2. What is the difference between LEMP and LAMP? LEMP uses Nginx instead of Apache and offers higher concurrent performance.

3. What changes if I am using Ubuntu 24.04? The PHP version becomes 8.3 and the socket path is updated.

4. I am getting 502 Bad Gateway error, why? PHP-FPM may not be working or the socket path may be wrong.

5. Can I set up multiple sites on one server? Yes. A separate server block is defined for each site.


Result

With this guide, you have set up a complete LEMP infrastructure on Ubuntu. Nginx provides fast request management, MySQL stores data, PHP-FPM processes codes.

You can safely use and scale this structure on the GenixNode infrastructure.