Skip to content

Setting Up Nginx + MariaDB + PHP-FPM on CentOS/RHEL 9/8

Advertisement

Introduction

The LEMP stack, consisting of Linux, Nginx, MariaDB, and PHP-FPM, is a powerful combination for hosting dynamic websites and applications. CentOS/RHEL, known for its stability and security, is an ideal choice for setting up such an environment. This guide will provide detailed instructions on installing and configuring Nginx, MariaDB, and PHP-FPM, with an emphasis on a specific PHP version to meet the requirements of various applications.

1. Preliminary System Update

  • Update your system packages:
    sudo dnf update
    sudo dnf upgrade
    
  • Enable the EPEL repository:
    sudo dnf install epel-release
    

2. Installing Nginx

  • Install Nginx:
    sudo dnf install nginx
    
  • Start and enable the Nginx service:
    sudo systemctl start nginx
    sudo systemctl enable nginx
    
  • Verify the Nginx installation:
    sudo systemctl status nginx
    

3. Installing MariaDB

  • Install MariaDB:
    sudo dnf install mariadb-server
    
  • Start and enable MariaDB:
    sudo systemctl start mariadb 
    sudo systemctl enable mariadb
    
  • Secure your MariaDB installation:
    sudo mysql_secure_installation
    

4. Installing PHP and PHP-FPM

  • Install a specific version of PHP and PHP-FPM (for example, PHP 7.4):
    sudo dnf install php74 php74-php-fpm php74-php-mysqlnd
    
  • Start and enable PHP-FPM:
    sudo systemctl start php74-php-fpm 
    sudo systemctl enable php74-php-fpm

5. Configuring Nginx to Use PHP-FPM

  • Edit the Nginx configuration file:
    sudo nano /etc/nginx/conf.d/default.conf
    
  • Configure Nginx to process PHP with PHP-FPM:
    
    location ~ \.php$ {
    fastcgi_pass unix:/var/run/php74-php-fpm/www.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    
    
  • Restart Nginx:
    sudo systemctl restart nginx
    

6. Testing PHP Processing

  • Create a PHP test file:
    sudo nano /usr/share/nginx/html/info.php
    
  • Insert PHP code and save:
    
    <?php phpinfo(); ?>
    
    
  • Access http://your_server_ip/info.php to see the PHP info page.

7. Configuring the Firewall

  • Allow HTTP and HTTPS traffic if firewalld is enabled:
    sudo firewall-cmd --permanent --add-service=http 
    sudo firewall-cmd --permanent --add-service=https 
    sudo firewall-cmd --reload 
    

8. Securing Your Server

  • Keep your packages updated.
  • Use robust passwords for all services.
  • Consider SSL certificates for HTTPS.

Conclusion

You have now successfully set up a LEMP stack on CentOS/RHEL with Nginx, MariaDB, and a specific version of PHP-FPM. This environment is ideal for deploying a range of web applications, offering reliability and performance. Regular maintenance and security measures are essential to ensure the smooth functioning of your server.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top