The LEMP stack — Linux, Nginx, MariaDB and PHP — powers a huge share of the world’s websites, from WordPress blogs to custom applications. This tutorial shows you how to install LEMP on AlmaLinux 9 from the official repositories, configure Nginx to process PHP through PHP-FPM, secure the MariaDB database, and verify every component with a live test page.
Prerequisites – How to Install LEMP Stack on
- An AlmaLinux 9 server with a sudo user and SSH access.
- A domain name pointing to your server if you want to test with a real hostname.
- Ports 80 and 443 reachable (we open them in firewalld during setup).
Step 1: Update the System – How to Install LEMP Stack on
sudo dnf update -y
Starting from a fully patched base avoids version surprises when services are installed.
Step 2: Install and Configure Nginx
Install Nginx from the AlmaLinux AppStream repository:
sudo dnf install -y nginx sudo systemctl enable --now nginx sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
Visit http://your-server-ip in a browser — the AlmaLinux Nginx welcome page confirms the web server works.
Step 3: Install MariaDB and Secure It
Every LEMP stack needs a database. Install MariaDB 10.5, the default on AlmaLinux 9:
sudo dnf install -y mariadb-server sudo systemctl enable --now mariadb sudo mysql_secure_installation
The secure-installation script sets a root password, removes anonymous users and the test database, and disables remote root login. Answer Y to all prompts. Afterwards, create a database and a dedicated user for your application:
sudo mysql -u root -p -e "CREATE DATABASE appdb; CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPassword!'; GRANT ALL ON appdb.* TO 'appuser'@'localhost'; FLUSH PRIVILEGES;"

Step 4: Install PHP and PHP-FPM to Install LEMP on AlmaLinux 9
PHP runs as a separate FastCGI service on modern stacks. Install PHP with the extensions most applications need:
sudo dnf install -y php php-fpm php-mysqlnd php-cli php-gd php-xml php-mbstring php-json php-opcache sudo systemctl enable --now php-fpm
By default, the PHP-FPM pool in /etc/php-fpm.d/www.conf runs as the apache user. Since we serve with Nginx, change the pool user and group to nginx, and confirm the socket path:
user = nginx group = nginx listen = /run/php-fpm/www.sock
sudo systemctl restart php-fpm
Step 5: Create an Nginx Server Block for PHP
Create /etc/nginx/conf.d/example.com.conf:
server {
listen 80;
server_name example.com www.example.com;
root /usr/share/nginx/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Test the configuration and reload:
sudo nginx -t sudo systemctl reload nginx
Step 6: Handle SELinux for Web Content
If PHP pages return 502 errors, SELinux is often the reason when content lives outside the default root. Allow Nginx to connect to PHP-FPM and label your web directory correctly:
sudo setsebool -P httpd_can_network_connect 1 sudo chcon -R -t httpd_sys_content_t /usr/share/nginx/html
Step 7: Test PHP Processing
Create a temporary test file:
sudo tee /usr/share/nginx/html/info.php <<'EOF' <?php phpinfo(); EOF
Open http://example.com/info.php. A PHP information page listing your PHP version and the MySQL modules proves the full stack works. Delete the file immediately afterwards — it exposes sensitive configuration details:
sudo rm /usr/share/nginx/html/info.php
For public sites, add HTTPS next; our guide to securing Nginx with Let’s Encrypt SSL walks through certificate issuance and renewal.
Troubleshooting
502 Bad Gateway: PHP-FPM is stopped, or the socket path in Nginx does not match www.conf — check systemctl status php-fpm and the error log at /var/log/nginx/error.log. File not found on PHP pages: the SCRIPT_FILENAME path is wrong or SELinux labels are missing; re-apply the context command above. MariaDB refuses connections: confirm the service is enabled and that your application uses the dedicated user, not root.
Hardening Your New LEMP Stack
A working stack is not yet a safe stack. Apply these quick wins right after you install LEMP on AlmaLinux 9:
- Hide version banners: add
server_tokens off;in thehttpblock of/etc/nginx/nginx.confand setexpose_php = Offin/etc/php.ini, then restart both services. - Limit uploads: align
client_max_body_sizein Nginx withupload_max_filesizeandpost_max_sizein php.ini so large uploads fail predictably at one layer instead of mysteriously at another. - Run the database locally only: MariaDB binds to localhost by default on AlmaLinux; confirm with
ss -tlnp | grep 3306and never expose it to the internet without TLS and a firewall rule restricting source IPs. - Schedule backups: a nightly
mariadb-dumpplus a copy of/etc/nginxand your PHP configuration turns a disaster into an inconvenience.
Finally, enable OPcache tuning for production PHP: set opcache.memory_consumption=128 and opcache.validate_timestamps=0 in /etc/php.d/10-opcache.ini (with a php-fpm restart after each deployment to refresh cached code). These small touches separate a tutorial stack from one you would trust with real traffic.
Conclusion
You have learned how to install LEMP on AlmaLinux 9: Nginx serving static content, MariaDB secured with a dedicated application user, and PHP-FPM processing dynamic pages through a Unix socket. This stack is a solid, production-ready base for WordPress, WooCommerce or any PHP application you deploy next.