How to setup Nginx with PHP-FPM on Centos 7

How to setup Nginx with PHP-FPM on Centos 7

Nginx is a fast and lightweight web server designed for high trafficking websites. Nginx is more popular than Apache, but Apache is still the most commonly used web server. Nginx offers a variety of features such as reverse proxy, load balancer etc. PHP-FPM stands for an acronym of FastCGI Process Manager. It is a hugely popular alternative PHP (Hypertext Processor) FastCGI implementation.

Nginx with PHP-FPM is a stable web server which offers an impressive performance with low resource consumption.

In today’s tutorial, we will learn how we can configure the Nginx with PHP-FPM to serve web content.

Prerequisites

  • CentOS 7 Operating System
  • Root user or another user with Sudo privileges

Disable the SELinux (Default Firewall)

  • To temporarily disable the SELinux, run the following command.
setenforce 0

Note:- After rebooting the server, then SELinux will become active again.

  • To disables, the SELinux permanently, run the following command.
vim /etc/sysconfig/selinux

Replace the SELinux=enforcing to SELinux=disabled.

Installing Nginx Web Server

  • Before installing the Nginx packages, update all the server repositories. So, we can install the latest Nginx package on the server.
sudo yum update -y
  • After updating server repositories, install some required packages.
sudo yum install wget curl vim -y
  • To install the Nginx package, we need to enable the EPEL repository on our CentOS server.
sudo yum install epel-release -y
  • Now install the latest Nginx package with the following command.
sudo yum install nginx -y
  • Start the Nginx service.
sudo systemctl start nginx
  • Check the status of the Nginx service with the following command.
sudo systemctl status nginx
  • Now enable the Nginx service with the following command. So that after the server reboot, Nginx can start automatically.
sudo systemctl enable nginx
  • To check whether the Nginx service is working perfectly or not. Simply, enter the server IP on the browser.
http://Domain_name_or_IP/

You will get the following output.

If you do not know the server’s Public IP address, then you can easily find the server’s public IP address with the following commands.

sudo curl http://icanhazip.com

Make sure the curl package is already installed on the server, otherwise it will throw an error that the command is not found.

Install PHP 8.1 on CentOS

For this tutorial, we will use the latest PHP 8.1 version. PHP packages are not available on default repositories. To install the PHP package, we need to enable the REMI Repository.

  • To enable the REMI Repository, run the following command.
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
  • To install PHP 8.1 with their modules, run the following command.
yum install php81-php php81-php-opcache php81-php-xml php81-php-hash php81-php-mysqlnd php81-php-mbstring php81-php-ldap php81-php-curl php81-php-imap php81-php-zip php81-php-json php81-php-mysqli php81-php-intl php81-php-gd php81-php-fpm php81-php-cli php81-php-imagick php81-php-zlib -y
  • To check the installed PHP version, run the following command.
php -v
  • Start the PHP-FPM service.
systemctl start php81-php-fpm
  • Check the status of the PHP-FPM service.
systemctl status php81-php-fpm

Configure the PHP-FPM

  • Now open the PHP-FPM configuration file.
sudo vim /etc/opt/remi/php81/php-fpm.d/www.conf
  • Search and replace the apache from user and group.
  • Replace the apache with nginx.
  • Now search for the listen directive. By default, PHP-FPM listen on a specific host and port. We can also change this setting, so it listens on a local socket file.

Note:- In this tutorial, we will see both PHP-FPM with a socket file or with a specific host and port. Choose any one of them.

PHP-FPM with a specific host and port

With specific host and port listen directive will look like this.

listen = 127.0.0.1:9000

PHP-FPM with a socket file

With the socket file, listen directive will look like this.

listen = /var/run/php-fpm/php-fpm.sock;

Change the owner and group settings if you are using the PHP-FPM with the socket file. Search for the listen.owner, listen.group and listen.mode directives. By default, these lines are commented, just uncomment them and change the owner and group to nginx.

Finally, create the directory for the PHP-FPM socket file

mkdir -p /var/run/php-fpm/
  • Now restart the PHP-FPM service.
systemctl restart php81-php-fpm

Configuring Nginx Virtual Host

  • Create the document root directory with the mkdir command with the -p attribute.
sudo mkdir -p /var/www/tectutorials.com/public_html
  • Use the $USER environment variable to change the ownership of the document root directory.
sudo chown -R $USER:$USER /var/www/tectutorials.com/public_html
  • Assign the correct directory permissions to the document root.
sudo chmod -R 755 /var/www/tectutorials.com/public_html
  • Create the PHP info page.
sudo vim /var/www/tectutorials.com/public_html/index.php
  • Add the following content inside the index.php file.
<?php
phpinfo ();
?>
  • Finally, create the virtual host configuration file.
sudo vim /etc/nginx/conf.d/tectutorials.com.conf
  • Paste the following content inside the virtual host configuration file.
server {
    listen       80;
    server_name  tectutorials.com www.tectutorials.com;

    root   /var/www/tectutorials.com/public_html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /var/www/tectutorials.com/public_html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

If you are using the socket file, then replace the “fastcgi_pass 127.0.0.1:9000;” with the following line.

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
  • Now restart the Nginx service.
sudo systemctl restart nginx
  • To check whether the PHP-FPM is perfectly working with the Nginx service. Simply, enter the server IP on the browser.
http://Domain_name_or_IP/

You will get output like this.

Conclusion

In today’s tutorial, we have learned how we can configure and use the Nginx with PHP-FPM. We can configure the PHP-FPM in two ways, with a socket file or with a specific host and port.

If you guys have any questions or queries, let me know in the comments section.