Table of Contents
What you will read?
The LEMP stack (Linux, Nginx, MariaDB, and PHP) is a reliable platform for running modern web applications. On CentOS 8, you can set it up quickly using the built-in package manager dnf.
Step 1: Update Your System
Always begin with updating system packages.
sudo dnf update -y
Step 2: Install Nginx
Nginx is the web server that delivers your websites:
sudo dnf install nginx -y
Start and enable it:
sudo systemctl enable nginx
sudo systemctl start nginx
Test it in a browser:
http://your_server_ip
Step 3: Install MariaDB
MariaDB is the default open-source database on CentOS:
sudo dnf install mariadb-server -y
Start and secure the installation:
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo mysql_secure_installation
Step 4: Install PHP and Extensions
Install PHP along with modules required for Nginx and MariaDB integration:
sudo dnf install php php-fpm php-mysqlnd -y
Enable and start PHP-FPM:
sudo systemctl enable php-fpm
sudo systemctl start php-fpm
Step 5: Configure Nginx for PHP
Create a new server block:
sudo nano /etc/nginx/conf.d/myapp.conf
Add this configuration:
server {
listen 80;
server_name myapp.local;
root /usr/share/nginx/html/myapp;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Test and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
Step 6: Test PHP
Create a PHP file:
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/myapp/index.php
Visit in browser:
http://your_server_ip/index.php
If the PHP info page loads, your setup is complete. The LEMP stack is now installed and configured on CentOS 8. Your server is ready to host dynamic PHP applications with Nginx and MariaDB.
