Support Online
Skip to main content

Ubuntu 22.04 LEMP Installation (Nginx, MySQL, PHP)

What will you learn in this guide?

In this guide, you will learn how to turn your Ubuntu server into a full-fledged web platform.
We will establish a fast and secure infrastructure using Nginx, MySQL and PHP-FPM components together.

Technical Summary

Main topic: LEMP (Linux, Nginx, MySQL, PHP) installation on Ubuntu
Solved problem: End-to-end server infrastructure for PHP based web applications
Steps: Nginx → MySQL → PHP-FPM → Nginx configuration → tests

Note: If you are using Ubuntu 24.04, use php8.3-fpm instead of php8.1-fpm.


Prerequisites

  • Ubuntu 22.04 server
  • Non-root, sudo authorized user
  • UFW firewall active

1️⃣ Nginx Web Server Installation

sudo apt update
sudo apt install nginx
  • These commands update the package list and install Nginx.

1.1 Firewall Setting


sudo ufw allow 'Nginx HTTP'
sudo ufw status
  • This step allows HTTP traffic.

1.2 Test from browser:


http://SUNUCU_IP

2️⃣ MySQL Database Installation


sudo apt install mysql-server
sudo mysql_secure_installation
  • These steps install MySQL and ensure basic security.

Suggestion: Create separate user for applications instead of root.


3️⃣ PHP and PHP-FPM Installation

1. Nginx cannot run PHP codes directly. PHP-FPM takes care of this job.


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


php -v
  • This command verifies the PHP version.

2. Why Use Nginx and PHP-FPM Together?

  • Nginx serves static files very fast

  • PHP-FPM processes PHP codes securely and isolated

  • This structure works more stable in high traffic


4️⃣ Creating Nginx Server Block


sudo mkdir /var/www/ornek.com
sudo chown -R $USER:$USER /var/www/ornek.com
sudo nano /etc/nginx/sites-available/ornek.com
  • These steps create the site directory and open the configuration file.


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 enables PHP-FPM integration.

4.1 Activating the 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️⃣ Let's Test If PHP Works


nano /var/www/ornek.com/info.php


<?php
phpinfo();

Open from browser:


http://SUNUCU_IP/info.php

Be sure to delete after testing:


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

Frequently Asked Questions (FAQ)

1. What causes 502 Bad Gateway error? Usually PHP-FPM is not working or the socket path is incorrect.


sudo systemctl status php8.1-fpm
ls /run/php/

2. Can there be more than one site on one server? Yes. You can create a separate server block for each domain.

3. How do I update the PHP version? Just install the new version and update the fastcgi_pass path.

Result You now have a fully functional LEMP infrastructure. This structure is ideal for WordPress, Laravel and custom PHP projects. 🔐 It is highly recommended to add Let's Encrypt SSL as the next step. 🚀 You can immediately try the optimized Ubuntu servers on GenixNode for high-performance projects.