Home Rocky LinuxHow to Install LEMP Stack (Nginx, MariaDB, PHP) on Rocky Linux 9

How to Install LEMP Stack (Nginx, MariaDB, PHP) on Rocky Linux 9

by Knowledge Base Linux
How to Install LEMP Stack Nginx — How to Install LEMP Stack (Nginx, MariaDB, PHP) on Rocky Linux 9

A LEMP stack — Linux, Nginx, MariaDB and PHP — is one of the fastest and most popular ways to serve WordPress and other PHP applications. This tutorial shows you how to build a complete LEMP stack on Rocky Linux 9, using packages from the official repositories, with PHP running through PHP-FPM, firewalld rules opened correctly and the SELinux details that most guides forget. This article covers How to Install LEMP Stack Nginx in detail.

Prerequisites for a LEMP Stack on Rocky Linux 9 – How to Install LEMP Stack Nginx

  • A server running Rocky Linux 9 with a sudo user (see our Rocky Linux 9 server setup guide).
  • A domain name pointed at the server if you want to test with a real hostname.
  • About 15 minutes of time.

Step 1: Install Nginx – How to Install LEMP Stack Nginx

Rocky Linux 9 ships Nginx in the AppStream repository. Install it, enable it at boot and open the firewall:

sudo dnf install -y nginx
sudo systemctl enable --now nginx
sudo firewall-cmd --permanent --add-service={http,https}
sudo firewall-cmd --reload

Visit http://your-server-ip/ in a browser. The Rocky Linux test page confirms Nginx is serving traffic.

Step 2: Install MariaDB

MariaDB is the community fork of MySQL and the default database choice on Rocky. Install and secure it:

sudo dnf install -y mariadb-server
sudo systemctl enable --now mariadb
sudo mysql_secure_installation

During the secure installation, set a root password for the database, remove anonymous users, disallow remote root login and drop the test database. Later, when you move data between servers, our guide on how to import and export databases in MySQL or MariaDB will be handy.

Web server in a LEMP stack on Rocky Linux 9 serving devices

Step 3: Install PHP and PHP-FPM

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 runs as the apache user, a leftover from Apache-centric setups. Since we serve with Nginx, edit /etc/php-fpm.d/www.conf and switch the pool to the nginx user:

user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx

Restart PHP-FPM after the change:

sudo systemctl restart php-fpm

If you need a newer PHP than the distribution default, enable the Remi repository as described in our guide to DNF repositories on Rocky Linux 9.

Step 4: Configure an Nginx Server Block for PHP

Create a document root and a server block for your site:

sudo mkdir -p /var/www/example.com/html
cat <<'EOF' | sudo tee /var/www/example.com/html/info.php
<?php
echo "PHP version: " . phpversion();
EOF

The web root lives outside the default Nginx directory, so SELinux needs the correct file context; without it you will get 403 errors:

sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com/html(/.*)?"
sudo restorecon -Rv /var/www/example.com
sudo chown -R nginx:nginx /var/www/example.com

Now create /etc/nginx/conf.d/example.com.conf:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/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;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Test and reload:

sudo nginx -t
sudo systemctl reload nginx

Open http://example.com/info.php — you should see the PHP version printed in the browser, which proves the whole stack works. Delete the file afterwards; it leaks configuration details.

Step 5: Allow PHP to Talk to the Database (SELinux)

If your application runs under a different setup and PHP cannot connect to remote services, SELinux booleans are usually the reason. The relevant switches are:

sudo setsebool -P httpd_can_network_connect_db 1
sudo setsebool -P httpd_can_network_connect 1

Only enable what you need. Our SELinux Rocky Linux 9 guide explains booleans and denials in detail.

Troubleshooting

  • 502 Bad Gateway: PHP-FPM is stopped, or the socket permissions are wrong. Check systemctl status php-fpm and the listen.owner settings.
  • 403 Forbidden on a custom web root: almost always SELinux contexts. Re-run restorecon and check ausearch -m avc -ts recent.
  • PHP files download instead of executing: the location ~ \.php$ block is missing or the server block is not being loaded — verify with nginx -T.
  • Database connection refused: confirm MariaDB is listening with ss -tlnp | grep 3306 and that your database user has a password set.

Step 6: Create a Database and User for Your Application

A stack is only useful when an application can store data. Create a dedicated database and user instead of reusing the MariaDB root account:

sudo mysql -e "CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mysql -e "CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'AnotherStrongPassword!';"
sudo mysql -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost'; FLUSH PRIVILEGES;"
php -v

The last command confirms the PHP version your applications will run. If it shows an older release than your application requires, revisit the Remi repository option mentioned in Step 3 rather than compiling PHP from source, which would leave you responsible for every future security update.

Conclusion

You have a working LEMP stack on Rocky Linux 9 built entirely from supported packages, with correct firewall and SELinux configuration. For production sites, add HTTPS with Let’s Encrypt — the same approach we describe for securing Nginx with Let’s Encrypt SSL works on Rocky with certbot from EPEL. The official Rocky Linux documentation covers web server administration in even more depth.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.