The LEMP and LAMP web stacks
LEMP and LAMP are the two most common web stacks for PHP sites: a web server, a database, and PHP working together — the foundation under almost any PHP application, from custom scripts to WordPress and other CMSes. This article covers how they differ and how to install either one on your VPS by hand, step by step, ending with a working PHP test page.
What LEMP and LAMP are
The letters in the name spell out the stack's components:
- LEMP — Linux, nginx (pronounced "engine-x", hence the "E" instead of "A" in the name), MariaDB (or MySQL), and PHP-FPM.
- LAMP — Linux, Apache, MySQL (or MariaDB), and PHP as an Apache module itself (mod_php).
The key technical difference is how the web server runs PHP code. nginx has no modules for executing PHP — it only serves static files and proxies requests, handing PHP files off to a separate process, PHP-FPM (FastCGI Process Manager), over a unix socket or TCP. Apache in the classic LAMP setup runs PHP right inside itself via the mod_php module — and only Apache supports .htaccess, per-directory overrides; nginx has no direct equivalent, all rules live centrally in its config.
The performance difference is usually barely noticeable for a small-to-medium site — in practice the choice is often driven by what you're already used to, or by a specific CMS's requirements (some older PHP applications are hard-wired to expect .htaccess and Apache).
What you'll need
- SSH access to the VPS as root or a user with sudo.
- One of the supported systems: Ubuntu 22.04/24.04, Debian 11/12, AlmaLinux/Rocky Linux 8/9.
- One web stack per server. If you want to try both LEMP and LAMP, use separate servers — nginx and Apache/httpd both claim port 80 by default and won't work at the same time on one server.
Open the firewall ports
Port 80 (HTTP) is needed to open the test page from a browser; port 443 (HTTPS) will come in handy later if you add a TLS certificate.
Ubuntu/Debian (ufw):
sudo apt install ufw
sudo ufw allow 80/tcp
sudo ufw allow 443/tcpAlmaLinux/Rocky (firewalld):
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reloadPart 1. Manually installing LEMP (nginx + MariaDB + PHP-FPM)
Step 1. Install nginx
Ubuntu/Debian:
sudo apt update
sudo apt install nginxAlmaLinux/Rocky:
sudo dnf install nginxEnable and start nginx:
sudo systemctl enable --now nginxOpen http://SERVER_IP/ in a browser — you should see nginx's default page.
Step 2. Install and configure MariaDB
Ubuntu/Debian:
sudo apt install mariadb-serverAlmaLinux/Rocky:
sudo dnf install mariadb-serverEnable autostart, start MariaDB, and run through the basic security setup:
sudo systemctl enable --now mariadb
sudo mysql_secure_installationThe mysql_secure_installation script asks a few questions: set a root password, remove anonymous users, disallow remote root login, and remove the test database. You can answer Y to all of them — those are safe defaults.
Check that the server responds:
sudo mysql -e "SELECT VERSION();"Step 3. Install PHP-FPM
Ubuntu/Debian:
sudo apt install php-fpm php-mysql
ls /etc/php/The ls /etc/php/ command shows the installed PHP version (for example, 8.3) — you'll need it for the service and socket name in the next step.
AlmaLinux/Rocky:
sudo dnf install php-fpm php-mysqlndEnable and start PHP-FPM. On Ubuntu/Debian, substitute your PHP version for 8.3:
sudo systemctl enable --now php8.3-fpmOn AlmaLinux/Rocky, the PHP version doesn't affect the service name:
sudo systemctl enable --now php-fpmStep 4. Connect PHP-FPM to nginx
Ubuntu/Debian. Open the default site file:
sudo nano /etc/nginx/sites-available/defaultand add this inside the existing server { ... } block (replace 8.3 with your PHP version):
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}AlmaLinux/Rocky. The same block, but in a different file and with a different socket path:
sudo nano /etc/nginx/conf.d/default.conflocation ~ \.php$ {
include fastcgi.conf;
fastcgi_pass unix:/run/php-fpm/www.sock;
}Test the configuration and reload nginx:
sudo nginx -t && sudo systemctl reload nginxuser = nginx
group = nginx
listen.owner = nginx
listen.group = nginxThen restart PHP-FPM:
sudo systemctl restart php-fpmStep 5. Create a test PHP page and check the result
Ubuntu/Debian:
echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php > /dev/nullAlmaLinux/Rocky:
echo '<?php phpinfo(); ?>' | sudo tee /usr/share/nginx/html/info.php > /dev/nullOpen http://SERVER_IP/info.php in a browser. You should see a page with PHP information: the Server API row should read FPM/FastCGI (confirming PHP is running through PHP-FPM), and the mysqli / pdo_mysql sections should show the MariaDB driver is loaded.
Ubuntu/Debian:
sudo rm /var/www/html/info.phpAlmaLinux/Rocky:
sudo rm /usr/share/nginx/html/info.phpPart 2. Manually installing LAMP (Apache + MySQL + PHP)
If LEMP from Part 1 is already set up on this same server, stop nginx first to free up port 80 for Apache: sudo systemctl disable --now nginx.
Step 1. Install Apache
Ubuntu/Debian:
sudo apt update
sudo apt install apache2AlmaLinux/Rocky:
sudo dnf install httpdEnable autostart and start the web server.
Ubuntu/Debian:
sudo systemctl enable --now apache2AlmaLinux/Rocky:
sudo systemctl enable --now httpdOpen http://SERVER_IP/ in a browser — you should see Apache's default welcome page.
Step 2. Install and configure MariaDB (MySQL)
If MariaDB is already installed and configured — say, you already did Part 1 on this server — skip this step.
Ubuntu/Debian:
sudo apt install mariadb-serverAlmaLinux/Rocky:
sudo dnf install mariadb-serversudo systemctl enable --now mariadb
sudo mysql_secure_installationStep 3. Install PHP
Ubuntu/Debian:
sudo apt install php libapache2-mod-php php-mysqlAlmaLinux/Rocky:
sudo dnf install php php-mysqlndThe libapache2-mod-php package enables the PHP module in Apache automatically; on AlmaLinux/Rocky, the php package adds the httpd config itself (/etc/httpd/conf.d/php.conf) if httpd is already installed — which is why it matters to install Apache before PHP, as in this article. All that's left is to restart the web server.
Ubuntu/Debian:
sudo systemctl restart apache2AlmaLinux/Rocky:
sudo systemctl restart httpdStep 4. Create a test PHP page and check the result
The path is the same on every system — Apache's standard DocumentRoot:
echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php > /dev/nullOpen http://SERVER_IP/info.php in a browser. The Server API row should read Apache 2.0 Handler (the mod_php module inside Apache itself — as opposed to FPM/FastCGI for LEMP), and the mysqli / pdo_mysql sections should show the MariaDB driver is loaded.
sudo rm /var/www/html/info.phpWhat's next
From here you'd typically set up a domain and a virtual host for a specific site — see the "Nginx: install and your first site" and "Apache: install and virtual hosts" articles — and, for a production project, add an HTTPS certificate. If you haven't logged in to the server's control panel yet, start with "How to log in to VMmanager 6"; the full list of ready-made recipes is in "Ready-made VMmanager 6 recipes".