In high-traffic web hosting, milliseconds matter. Standard LAMP stacks often struggle when too many users visit at once. This is why the LEMP on Linux VPS is superior. By switching from Apache to the faster Nginx web server, you can handle 4x the traffic using the same hardware.
In this guide, you will learn how to deploy and tune Nginx, MariaDB, and PHP on a Linux VPS running Ubuntu 24.04, which ensures your infrastructure is optimized for speed, security, and scalability.
Prerequisites to Set up LEMP on Linux VPS
To start setting up LEMP on Linux VPS, you need to ensure your environment is ready. Here is exactly what you need to get started:
Linux VPS: You will need a server running Ubuntu 24.04. If you do not have one yet, you can deploy a high-performance Linux VPS at Perlod Hosting.
Root Access: Log in as root or a user with sudo privileges.
Clean System: A fresh installation to avoid conflicts.
Valid domain name points to your Linux VPS IP address.
Once you are done, proceed to the following steps to install LEMP on Linux VPS.
Install and Optimize Nginx
Nginx is preferred for high traffic because of its event-driven architecture. To install it on your Ubuntu system, run the system update and install Nginx with the commands below:
For high traffic, you must adjust the Nginx worker processes and connection limits. Open the main Nginx config file with your desired text editor:
Bash
sudonano/etc/nginx/nginx.conf
Modify or add these settings inside the events and http blocks:
NGINX
1userwww-data;2worker_processesauto;# Automatically matches CPU cores3pid /run/nginx.pid;45events{6worker_connections4096;# Increased from default 768 for high concurrency7multi_accepton;# Accept as many connections as possible8}910http{11## Basic Settings12sendfileon;13tcp_nopushon;14tcp_nodelayon;15keepalive_timeout15;# Reduced timeout to free up connections faster16types_hash_max_size2048;17client_max_body_size64M;# Allow larger file uploads1819## Buffer Settings for Performance20client_body_buffer_size10K;21client_header_buffer_size1k;22large_client_header_buffers21k;2324include /etc/nginx/mime.types;25default_typeapplication/octet-stream;2627## Logging Settings28access_log /var/log/nginx/access.log;29error_log /var/log/nginx/error.log;3031## Gzip Settings32gzipon;33gzip_varyon;34gzip_proxiedany;35gzip_comp_level6;36gzip_typestext/plaintext/cssapplication/jsonapplication/javascripttext/xmlapplication/xmlapplication/xml+rsstext/javascript;37}
Once you are done, save and close the file and restart Nginx to apply the changes:
Bash
sudosystemctlrestartnginx
Install and Tune MariaDB
MariaDB is a drop-in MySQL replacement known for better performance. Use the command below to install it for LEMP on Linux VPS:
Bash
sudoaptinstallmariadb-server-y
Run the MySQL security script to remove insecure defaults:
Bash
sudomysql_secure_installation
Answer Y to:
Switch to unix_socket authentication.
Change the root password.
Remove anonymous users.
Disallow root login remotely.
Remove the test database.
Reload privilege tables.
For optimizing MariaDB performance, create a custom configuration override file. Remember not to edit the default my.cnf.
Note: Adjust innodb_buffer_pool_size to 70% of your total RAM.
INI
1[mysqld]2# Connection Settings3max_connections=5004wait_timeout=60056# InnoDB Settings (Critical for performance)7innodb_buffer_pool_size= 4G8innodb_log_file_size= 512M9innodb_flush_log_at_trx_commit=210innodb_flush_method= O_DIRECT1112# Query Cache (Disable for high concurrency)13query_cache_type=014query_cache_size=0
Restart MariaDB to apply the changes:
Bash
sudosystemctlrestartmariadb
Install and Configure PHP-FPM
As you must know, PHP-FPM handles the dynamic processing. Ubuntu 24.04 uses PHP 8.3 by default. To install PHP-FPM and extensions for LEMP on Linux VPS, run the command below:
For high traffic, the default dynamic process manager is often too slow. Switch to static if you have plenty of RAM, or optimize the dynamic process manager.
Open the default PHP-FPM pool configuration:
Bash
sudonano/etc/php/8.3/fpm/pool.d/www.conf
Find and modify these lines:
INI
1user= www-data2group= www-data34; Use 'static' for maximum performance if RAM permits.5; Formula: (Total RAM - 1GB for OS/DB) / ~60MB per process6pm= static7pm.max_children=508pm.max_requests=1000910; Increase request termination timeout for heavy scripts11request_terminate_timeout= 60s
The value pm.max_children = 50 is a safe estimate for a 4GB server. If you set this too high, your server will crash. To calculate the exact number based on your specific RAM and process size, follow our dedicated guide on how to calculate PHP-FPM max_children.
Save and close the file and restart PHP-FPM to apply the changes:
Bash
sudosystemctlrestartphp8.3-fpm
Note: Heavy tasks exceeding 60 seconds may cause Nginx to drop connections. If you encounter 504 errors, follow our guide to fix Nginx upstream timeouts by adjusting your proxy settings.
Configure Nginx Server Block for LEMP Stack
Now that we have all the components installed, we need to tell Nginx how to use them. In this step, we will create an Nginx server block that configures your domain, defines where your website files are stored, and connects Nginx to the PHP-FPM processor.
Create a site configuration file with the command below:
Security Note: Delete this file immediately after testing:
Bash
sudorm/var/www/your-domain.com/info.php
You now have a fully tuned LEMP on Linux VPS ready for high traffic.
Conclusion
You now have a fully optimized LEMP stack running on your Linux VPS, configured specifically for high-traffic scenarios. By tuning Nginx's worker processes, setting PHP-FPM to static mode, and optimizing MariaDB's InnoDB settings, your server can handle thousands of concurrent users.
These settings optimize how Nginx handles connections. For static-heavy sites, you should also configure FastCGI caching. You can learn how to set this up in our guide on Advanced Nginx Caching Strategies.
We hope you enjoy this guide on LEMP on Linux VPS. Subscribe to our X and Facebook channels to get the latest updates and articles.
We recommend 4GB RAM minimum for production to support performance tuning. Use 8GB+ for very high-traffic environments.
Use static for maximum speed if you have plenty of RAM; it eliminates startup delays. Use dynamic if RAM is tight, as it saves memory by scaling processes based on demand.
Yes. Simply replace mariadb-server with mysql-server during installation. All configuration and tuning steps in this guide apply to MySQL as well.
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.