EN · $ USD
VDS and Linux Server Management

How to Install Nginx, PHP and MySQL (LEMP Stack) on Ubuntu

7 min read Updated: Sunucu Atlas technical team

In short

To install a LEMP stack on Ubuntu, run apt install nginx, apt install mariadb-server and apt install php-fpm php-mysql, in that order. Then secure the database with mariadb-secure-installation, write a server block in /etc/nginx/sites-available that passes PHP requests to the PHP-FPM socket, and add a free Let's Encrypt SSL certificate with certbot --nginx.

In this guide
  1. What Does Each LEMP Component Do?
  2. 1. Update the System and Prepare the Firewall
  3. 2. Install Nginx
  4. 3. Install and Secure MariaDB
  5. 4. Install PHP-FPM and Extensions
  6. 5. Configure an Nginx Virtual Host (Server Block)
  7. 6. Install a Let's Encrypt SSL Certificate
  8. 7. PHP-FPM and PHP Settings for Production
  9. Adding a Second Site to the Same Server
  10. Common Errors
  11. Which VDS Is Enough for LEMP?
  12. Next Steps

LEMP is a web server stack made up of Linux, Nginx (pronounced "engine-x", hence the E), MySQL/MariaDB and PHP. Installing LEMP on Ubuntu takes four steps: apt install nginx, apt install mariadb-server, apt install php-fpm php-mysql, and one Nginx server block per site. After that you add a free Let's Encrypt SSL certificate with Certbot.

This guide turns a clean Ubuntu 22.04 or 24.04 VDS into a production-ready PHP server. The same steps apply to WordPress, Laravel or your own PHP application. Before you start, you need to be connected to the server over SSH and logged in as a user with sudo privileges.

What Does Each LEMP Component Do?

ComponentRoleDefault port / socket
NginxHandles HTTP/HTTPS requests, serves static files and passes PHP requests to PHP-FPM80, 443
PHP-FPMThe process manager that runs PHP code (FastCGI Process Manager)/run/php/phpX.Y-fpm.sock
MariaDBMySQL-compatible relational database3306 (localhost only)
CertbotObtains Let's Encrypt certificates and renews them automatically-

Unlike Apache, Nginx does not run PHP inside itself; PHP-FPM runs as a separate service. This separation makes memory usage predictable and is where Nginx gets its edge under high concurrent connections.

1. Update the System and Prepare the Firewall

sudo apt update && sudo apt upgrade -y
sudo ufw allow OpenSSH
sudo ufw enable

Don't run ufw enable before adding the SSH rule, or you will lose your connection. If you are setting up the server for the first time, we recommend following our guide to the first 10 security steps on a new Ubuntu server first.

2. Install Nginx

sudo apt install nginx -y
sudo systemctl enable --now nginx
sudo ufw allow 'Nginx Full'

If you open http://SUNUCU_IP (your server's IP) in a browser and see the "Welcome to nginx!" page, the web server is running. The Nginx Full profile opens ports 80 and 443 together.

3. Install and Secure MariaDB

sudo apt install mariadb-server -y
sudo systemctl enable --now mariadb
sudo mariadb-secure-installation

In the wizard, keep unix_socket authentication for root, remove anonymous users, disable remote root login and remove the test database. On older versions the command is called mysql_secure_installation.

Create a separate database and user for your application; never connect the application as root:

sudo mariadb
CREATE DATABASE ornekdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'ornekuser'@'localhost' IDENTIFIED BY 'GucluBirParola!2025';
GRANT ALL PRIVILEGES ON ornekdb.* TO 'ornekuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

By default MariaDB listens only on 127.0.0.1. Don't open port 3306 to the outside in your firewall; if you need remote administration, use an SSH tunnel.

4. Install PHP-FPM and Extensions

sudo apt install php-fpm php-mysql php-cli php-curl php-mbstring \
  php-xml php-zip php-gd php-intl php-bcmath -y
php -v
ls /run/php/

The output of ls /run/php/ shows the exact name of the socket file you will use in the Nginx config. The repository default is PHP 8.3 on Ubuntu 24.04 and PHP 8.1 on Ubuntu 22.04; in the examples below, replace php8.3-fpm.sock with your own version. The extensions in the list cover the common modules WordPress and Laravel need.

5. Configure an Nginx Virtual Host (Server Block)

Create a separate directory and a separate config file for each site:

sudo mkdir -p /var/www/ornek.com/public
sudo chown -R $USER:www-data /var/www/ornek.com
sudo chmod -R 750 /var/www/ornek.com
sudo nano /etc/nginx/sites-available/ornek.com

Paste the following production-oriented configuration into the file:

server {
    listen 80;
    listen [::]:80;
    server_name ornek.com www.ornek.com;

    root /var/www/ornek.com/public;
    index index.php index.html;

    client_max_body_size 64M;
    server_tokens off;

    access_log /var/log/nginx/ornek.com.access.log;
    error_log  /var/log/nginx/ornek.com.error.log;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }

    location ~* \.(css|js|jpg|jpeg|png|gif|webp|svg|woff2)$ {
        expires 30d;
        access_log off;
    }

    location ~ /\.(?!well-known) {
        deny all;
    }
}

Enable the site and test the configuration:

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

To confirm PHP is working, put <?php phpinfo(); in /var/www/ornek.com/public/info.php and open it in your browser. Be sure to delete this file after checking; it exposes information about your server that attackers can use.

If you use Laravel, the root line must point to the project's public folder. For WordPress, simply place the WordPress files directly inside public; the try_files line makes permalinks work.

6. Install a Let's Encrypt SSL Certificate

Once your domain's A record points to the server's IP address, get a certificate with Certbot:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d ornek.com -d www.ornek.com
sudo certbot renew --dry-run

Certbot adds the 443 listener and the HTTP-to-HTTPS 301 redirect to your server block on its own. Let's Encrypt certificates are valid for 90 days; the Certbot package renews them automatically with a systemd timer. If the --dry-run test passes without errors, there is nothing else you need to do about renewal.

7. PHP-FPM and PHP Settings for Production

php.ini

Adjust these values in /etc/php/8.3/fpm/php.ini to fit your needs:

memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 60
expose_php = Off
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 20000

The upload_max_filesize value must match client_max_body_size in Nginx; if either one is too small, large uploads fail with a 413 error.

PHP-FPM pool

pm.max_children in /etc/php/8.3/fpm/pool.d/www.conf sets how many PHP requests can be processed at the same time. A rough rule of thumb: (RAM you can dedicate to PHP) / (average memory per PHP process). A typical WordPress process uses 40-80 MB; this is an estimate, so measure your own value with ps -o rss -C php-fpm8.3.

pm = dynamic
pm.max_children = 20
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500

Reload the services after making changes:

sudo systemctl reload php8.3-fpm
sudo systemctl reload nginx

Starting values by RAM (guidelines only)

Server RAMSuitable forStarting pm.max_children
2 GBOne small site, test environment8-10
4 GB1-3 medium-traffic WordPress/Laravel sites20-25
8 GB and upWooCommerce, multiple sites, heavy database load40+

Adding a Second Site to the Same Server

One advantage of LEMP is that you can host many sites on a single server in an organized way. Repeat the same pattern for each new site:

  1. Create the /var/www/ikincisite.com/public directory and set its permissions.
  2. Create /etc/nginx/sites-available/ikincisite.com by copying the first site's file; change the server_name, root and log lines.
  3. Create a separate database and a separate database user for the site so sites can't access each other's data.
  4. Add a symlink under sites-enabled, then run nginx -t and systemctl reload nginx.
  5. Add SSL with certbot --nginx -d ikincisite.com -d www.ikincisite.com.

If isolation between sites matters, you can define a separate PHP-FPM pool for each site and run each pool as a different Linux user. That way, even if one site is compromised, the attacker can't read the other sites' files.

Common Errors

  • 502 Bad Gateway: The socket name in the fastcgi_pass line is wrong or PHP-FPM is down. Check with systemctl status php8.3-fpm.
  • PHP file downloads instead of running: The location ~ \.php$ block is missing or incorrect.
  • 403 Forbidden: There is no index.php in the directory, or the www-data group lacks read permission.
  • Certbot validation error: DNS hasn't propagated yet or port 80 is closed.

Which VDS Is Enough for LEMP?

The LEMP stack itself is lightweight; most resources go to PHP processes and the database. Because PHP runs single-threaded, how fast a request completes depends largely on the CPU's single-core performance. That is why high-clock CPUs make a noticeable difference for PHP sites.

Sunucu Atlas's Ryzen Premium VDS plans with the AMD Ryzen 9 5900X offer NVMe storage, KVM virtualization and root access, giving you a ready foundation for this setup. If you plan to run a few WordPress or Laravel sites on one server, the SA-R9-2 plan with 2 vCPU, 4 GB RAM and 40 GB NVMe maps directly to the 4 GB scenario above. If you're not sure whether you want a control panel, see the comparison in our guide to installing a control panel on a VDS.

Next Steps

Once your LEMP install is complete, do the following in order: protect SSH with Fail2ban, schedule automatic backups for the database and the /var/www directory, and get into the habit of testing every change with nginx -t. To follow this guide from scratch, you can start with an Istanbul-based SA-R9-2 Ryzen VDS and move to higher-RAM plans in the same series as your traffic grows. For the full list of Nginx directives, see the official Nginx documentation.

Frequently asked questions

What is the difference between LEMP and LAMP?

LAMP uses Apache as the web server, while LEMP uses Nginx. Nginx runs PHP through a separate PHP-FPM service and uses less memory under many concurrent connections.

Should I install MySQL or MariaDB?

Both work for most PHP applications. MariaDB is available out of the box in the Ubuntu repositories and is MySQL-compatible; unless your application needs a MySQL-specific feature, MariaDB is a practical choice.

What causes a 502 Bad Gateway error in Nginx?

Usually the PHP-FPM socket name in the fastcgi_pass line is wrong or the PHP-FPM service isn't running. Check the socket name with ls /run/php/ and the service state with systemctl status.

Do Let's Encrypt certificates renew automatically?

Yes. The Certbot package installs a systemd timer that renews the 90-day certificates before they expire. You can test that renewal works with certbot renew --dry-run.

How much RAM does a LEMP server need?

2 GB can be enough for one small site; 4 GB is a good starting point for a few medium-traffic WordPress or Laravel sites. For WooCommerce and heavy database workloads, 8 GB or more is recommended.

For this guide

Recommended plans

Single-core performance determines how fast PHP requests run, so a Ryzen Premium VDS with the AMD Ryzen 9 5900X and NVMe storage, especially the 4 GB RAM SA-R9-2, is a balanced starting point for building your own LEMP server.

SA-R9-1

RYZEN9 5900X
Recommended

2 GB RAM

$3.93/mo

+VAT

  • Processor2 vCPU
  • ModelAMD Ryzen 9 5900X
  • Disk30 GB NVMe
  • LocationIstanbul
  • Port: 1G Port
  • Traffic: Fair use
  • Virtualization: KVM
  • Multi-layer DDoS protection

SA-R9-2

RYZEN9 5900X

4 GB RAM

$5.90/mo

+VAT

  • Processor2 vCPU
  • ModelAMD Ryzen 9 5900X
  • Disk40 GB NVMe
  • LocationIstanbul
  • Port: 1G Port
  • Traffic: Fair use
  • Virtualization: KVM
  • Multi-layer DDoS protection

SA-R9-3

RYZEN9 5900X

6 GB RAM

$8.85/mo

+VAT

  • Processor2 vCPU
  • ModelAMD Ryzen 9 5900X
  • Disk50 GB NVMe
  • LocationIstanbul
  • Port: 1G Port
  • Traffic: Fair use
  • Virtualization: KVM
  • Multi-layer DDoS protection

Ryzen Premium VDS — tüm paketler

Need help with setup?

Send questions about your service in a support ticket, and our technical team will walk you through it step by step.