Installing and Configuring Nginx with PHP-FPM on CentOS 8

Nginx is a powerful, free, open-source web server renowned for its performance on high-load and high-traffic websites. It is celebrated for its stability, straightforward configuration, and efficient resource consumption, and it can function both as a web server and a reverse proxy.

FPM, which stands for FastCGI Process Manager, is an alternative PHP FastCGI implementation designed for high-traffic sites. It’s often paired with web servers to deliver PHP content more efficiently. PHP-FPM is more memory and CPU-efficient than other PHP execution methods, making it faster in multi-user PHP environments. Additionally, it supports running multiple PHP versions simultaneously.

This guide will walk you through the process of installing Nginx with PHP-FPM support on CentOS 8.

Prerequisites

  • A server running CentOS 8.
  • A root password configured on your server.

Getting Started

By default, SELinux is enabled on CentOS 8. You’ll need to disable it first.

Edit the /etc/selinux/config file:

nano /etc/selinux/config

Change the following line:

SELINUX=disabled

Save the file, then restart your server to apply the changes.

Install Nginx Web Server

Install the Nginx web server with the following command:

yum install nginx -y

After installation, start the Nginx service and enable it to start at boot:

systemctl start nginx
systemctl enable nginx

Install PHP and PHP-FPM

Next, install PHP and PHP-FPM using this command:

yum install php php-cli php-common php-fpm -y

After installation, start the PHP-FPM service and configure it to start after a system reboot:

systemctl start php-fpm
systemctl enable php-fpm

Verify the status of the PHP-FPM service with this command:

systemctl status php-fpm
? php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
     Active: active (running) since Thu 2019-10-17 05:39:11 EDT; 4min 40s ago
   Main PID: 1475 (php-fpm)
     Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 6 (limit: 5060)
     Memory: 28.5M
     CGroup: /system.slice/php-fpm.service
             ??1475 php-fpm: master process (/etc/php-fpm.conf)
             ??1478 php-fpm: pool www
             ??1479 php-fpm: pool www
             ??1480 php-fpm: pool www
             ??1481 php-fpm: pool www
             ??1482 php-fpm: pool www

  Oct 17 05:39:10 centos8 systemd[1]: Starting The PHP FastCGI Process Manager...
  Oct 17 05:39:11 centos8 systemd[1]: Started The PHP FastCGI Process Manager.

Create an Index Page for Nginx

Create a sample info.php page to test if Nginx is using PHP-FPM.

Create info.php in Nginx’s default document root directory:

nano /var/www/html/info.php

Add these lines:

<?php
      phpinfo();
  ?>

Save the file and change the ownership to nginx:

chown -R nginx: /var/www/html/info.php

Configure Nginx with PHP-FPM

Create an Nginx virtual host configuration file to enable PHP-FPM support:

nano /etc/nginx/conf.d/example.conf

Add the following:

server {
      listen 80;
      server_name example.com;
      root /var/www/html/;
      index info.php;

      access_log /var/log/nginx/example.com.access.log;
      error_log /var/log/nginx/example.com.error.log;

      location ~ \.php$ {
          try_files $uri =404;
          fastcgi_pass unix:/run/php-fpm/www.sock;
          fastcgi_index   index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
      }

      location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
          expires max;
          log_not_found off;
      }

  }

Save the file. Test for syntax errors with:

nginx -t

Enable the Nginx virtual host and restart the service:

systemctl restart nginx

Test Nginx with PHP-FPM Support

Open your web browser and navigate to http://example.com. You should see the following:

The page indicates that PHP-FPM is successfully loaded with the Nginx web server.

Conclusion

Congratulations! You have successfully installed Nginx with PHP-FPM support on a CentOS 8 server. You are now ready to host multiple websites using different PHP versions.

Frequently Asked Questions (FAQ)

    • How do I enable SELinux again?

Edit the /etc/selinux/config file and change SELINUX=disabled to SELINUX=enforcing. Then, reboot your server to apply the changes.

    • What do I do if I encounter an error with Nginx syntax testing?

Check the Nginx configuration file for typos or misconfigurations. Ensure that there are no missing semicolons or incorrect paths.

    • How can I test if PHP-FPM is working without using a web browser?

You can use the curl command from the terminal: curl -I http://localhost/info.php to check the HTTP response headers.

    • What should I do if my web page is not displaying correctly?

Check the error logs located in /var/log/nginx/example.com.error.log for any issues and verify that all configurations are correctly set.