LEMP or LAMP: which one to choose for your site on a VPS
When you put a PHP site on your own server, you almost always choose between two ready-made stacks. LAMP is Linux + Apache + MySQL/MariaDB + PHP. LEMP is the same, but with nginx (pronounced "engine-x", hence the "E") instead of Apache. The database and PHP are the same; the difference is the web server. Here is when each one is better, without the myths.
The difference in one minute
- Apache (LAMP) usually runs PHP inside itself (mod_php). Each request is served by a separate process that already has PHP loaded. It is simple and works out of the box.
- nginx (LEMP) does not run PHP by itself. It serves static files (images, CSS, JS) and passes PHP requests to a separate service, PHP-FPM. So images do not occupy a "heavy" PHP process.
.htaccess: the biggest practical difference
Apache reads a .htaccess file in every folder. Many PHP applications and older sites rely on it: pretty URLs, redirects, blocking access to a folder. You upload the files and everything works.nginx ignores
.htaccess. The rules are written once in the site's configuration. For example, "pretty URLs" for most PHP applications are one block in nginx:
Code: Select all
location / {
try_files $uri $uri/ /index.php?$args;
}.htaccess, the easiest choice is LAMP. Otherwise you have to translate the rules for nginx.
RAM on a small VPS
With mod_php, every Apache process carries PHP inside it, even when it only returns an image. With 2 GB of RAM and a rush of visitors, the processes multiply and memory runs out. The server then starts using swap and the site slows down sharply.With nginx, static files cost almost no memory. PHP processes are a separate pool with a clear limit (
pm.max_children in PHP-FPM), so you know in advance how much RAM they will use.A quick check of how much memory the web server and PHP use right now:
Code: Select all
ps -C apache2,nginx,php-fpm8.3 -o rss= | awk '{s+=$1} END {print s/1024 " MB"}'php-fpm8.3 with your version; with mod_php, PHP lives inside apache2)
Quick comparison
- Static files and many visitors: LEMP is better.
- Little RAM (1–2 GB): LEMP is better, with predictable usage.
- A site with .htaccess from shared hosting: LAMP is easier, it works unchanged.
- Per-folder rules without a restart: LAMP, because
.htaccesstakes effect immediately. - Reverse proxy in front of Node.js, Python, etc.: LEMP, nginx was built for this.
- WordPress: both work well. With caching (see our article on WordPress in 1 ms) the difference almost disappears.
What the install looks like (Debian / Ubuntu)
LEMP:
Code: Select all
sudo apt update
sudo apt install nginx php-fpm php-mysql mariadb-server
sudo mysql_secure_installationCode: Select all
sudo apt update
sudo apt install apache2 libapache2-mod-php php-mysql mariadb-server
sudo a2enmod rewrite
sudo mysql_secure_installationAfter that you still need a database and user, the site configuration and an HTTPS certificate. That is another 15–30 minutes of work if you are doing it for the first time.
So which one?
- Starting a new project, the server is small or you expect traffic: LEMP.
- Moving an old site with
.htaccess, or the application explicitly asks for Apache: LAMP. - You don't want to touch configs and have several sites: look at CloudPanel (our article is here on the forum). It runs nginx underneath but is managed with clicks.
With us, both are ready when you order
When you order a Linux VPS (Debian or Ubuntu), pick the free LEMP or LAMP environment. Your site works right away at <name>.vpsstorages.com with HTTPS. The database and its user are already created, and their details arrive in the order email. With LAMP, .htaccess and pretty URLs work from the first moment. All that is left is to upload your files to /var/www/html.Which do you use, Apache or nginx? Post here in the topic