Configure Nginx + PHP-FPM Using Unix Sock error

Hi, i have use the following steps to do the task but encounter php-pfm not installed or version is different:

mkdir -p /var/run/php-fpm

yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm

yum install -y nginx

yum install -y yum-utils

vi /etc/yum/pluginconf.d/subscription-manager.conf : enabled=0

yum-config-manager --disable remi-php54

yum-config-manager --enable remi-php73

yum install php-fpm

i have tried two ways to install php-pfm

second way is following:

dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf module list php
dnf module reset php
dnf module enable php:remi-7.3

vi /etc/nginx/nginx.conf : change as per below

server {
listen 8092;
listen [::]:8092;
server_name _;
root /var/www/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
location ~ \.php$ {
      try_files $uri = 404;
      include fastcgi_params;
      fastcgi_pass  unix:/var/run/php-fpm/default.sock;
      fastcgi_index index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
     }

vi /etc/php-fpm.d/www.conf : change as per below

user = nginx
group = nginx
listen = /run/php-fpm/default.sock
listen.owner = nginx
listen.group = nginx

systemctl start php-fpm

ls -lrt /var/run/php-fpm/

total 0
-rw-r–r-- 1 root root 0 Nov 12 05:13 default.sock

chown nginx /var/run/php-fpm/default.sock

chgrp nginx /var/run/php-fpm/default.sock

ls -lrt /var/run/php-fpm/

total 0
-rw-r–r-- 1 nginx nginx 0 Nov 12 05:13 default.sock

chmod 777 var/run/php-fpm/default.sock

systemctl start nginx

testing

thor@jump_host ~$ curl http://stapp02:8092/index.php

404 Not Found

404 Not Found


nginx/1.14.1

i have encounter the following error message after task verification,

  • either ‘php-fpm’ is not installed or installed version is not ‘7.3’ on App Server 2
    FAILED test_php_fpm.py::test_app - AssertionError: - either ‘php-fpm’ is not …

but i check again, service is running and version is correct.

[root@stapp02 ~]# 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 Sun 2023-11-12 05:29:22 UTC; 2min 52s ago

php-fpm -v

PHP 7.3.3 (fpm-fcgi) (built: Oct 22 2019 08:28:36)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

Please advise, thanks.

First, know what versions of PHP are available on the server already, and also know that php-fpm is installed by default from the system packages

sudo dnf module list php

If the question is asking a version that is not in this list, e.g. 8.2, press Try Later button, and reset the lab till you get a supported version. This saves messing with additional repos.

Once you have a version supported by the distro, proceed. In this case I got 7.4 as the requirement

Once you have logged into app server, do sudo -i to become root and save yourself a lot of typing sudo everywhere.

dnf module -y install nginx php:7.4/common

Now fix up php-fpm so it can work with nginx and listen on the given socket.

  • Open /etc/php-fpm.d/www.conf in vi
  • Edit the user = and group = lines to both use nginx instead of apache
  • Edit the listen = line to refer the path to the socket given in the question
  • Exit vi
  • Set directory permissions on php files to nginx group
    chown -R root:nginx /var/lib/php
    

Fix up nginx

  • Open /etc/nginx/nginx.conf
  • Set given port number
  • Set given html directory
  • Insert configuration block for php-fpm within server { and replace PATH-TO-SOCKET with path given in question
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:PATH-TO-SOCKET;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    

Now

  • Restart php-fpm and nginx services
  • Run the curl test

All the above is quite easy to find in Google.

image

2 Likes

Hi Alistair,

I have followed your steps but encounter error, there is 7.4 in the list but it wont install.

ssh steve@stapp02

sudo -i

dnf module list php
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

CentOS Stream 8 - AppStream 8.5 kB/s | 4.4 kB 00:00
CentOS Stream 8 - AppStream 31 MB/s | 34 MB 00:01
CentOS Stream 8 - BaseOS 3.1 kB/s | 3.9 kB 00:01
CentOS Stream 8 - BaseOS 22 MB/s | 53 MB 00:02
CentOS Stream 8 - Extras 7.7 kB/s | 2.9 kB 00:00
CentOS Stream 8 - Extras common packages 9.6 kB/s | 3.0 kB 00:00
CentOS Stream 8 - Extras common packages 17 kB/s | 6.9 kB 00:00
Red Hat Universal Base Image 8 (RPMs) - BaseOS 1.5 MB/s | 717 kB 00:00
Red Hat Universal Base Image 8 (RPMs) - AppStream 6.1 MB/s | 3.0 MB 00:00
Red Hat Universal Base Image 8 (RPMs) - CodeReady Builder 350 kB/s | 103 kB 00:00
CentOS Stream 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
php 8.0 common [d], devel, minimal PHP scripting language

Red Hat Universal Base Image 8 (RPMs) - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
php 8.0 common [d], devel, minimal PHP scripting language

dnf install -y nginx php:7.4/common
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Last metadata expiration check: 0:01:48 ago on Wed Nov 15 03:24:22 2023.
No match for argument: php:7.4/common
Error: Unable to find a match: php:7.4/common

Thanks

Try

dnf module -y install nginx php:7.4/common

as per the instruction above, not

dnf install -y nginx php:7.4/common

Hi Alistair,

thank you for pointing out my mistake, i left out the module command, but then i continue and successfully installed nginx and php-fpm.

dnf module -y install nginx php:7.3/common
chown -R root:nginx /var/lib/php

edit the nginx.conf:
server {
listen 8093;
listen [::]:8093;
server_name _;
root /var/www/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location ~ \.php$ {
      try_files $uri =404;
      fastcgi_pass unix:/var/run/php-fpm/default.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
    }

edit the www.conf
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
; Note: This value is mandatory.
listen = /var/run/php-fpm/default.sock ( or /run/php-fpm/default.sock )

start both nginx and php-fpm services

curl error:
thor@jump_host ~$ curl http://stapp01:8093/index.php

404 Not Found

404 Not Found


nginx/1.14.1

validation error:

  • either ‘php-fpm’ is not installed or installed version is not ‘7.3’ on App Server 1

but php version:
php -v
PHP 7.3.20 (cli) (built: Jul 7 2020 07:53:49) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.20, Copyright (c) 1998-2018 Zend Technologies

did i miss anything?

Thanks

1 Like

Something is still not right, because the curl did not work. I have to go to my day job shortly, so cannot re-run this lab till later.

You should look at the nginx logs in /var/log/nginx to see what it is doing - both the access and error log

Hi Alistair,

here are the logs:

cat /var/log/nginx/error.log

2023/11/16 02:39:50 [crit] 1658#0: *1 connect() to unix:/run/php-fpm/www.sock failed (2: No such file or directory) while connecting to upstream, client: 172.16.238.2, server: _, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://unix:/run/php-fpm/www.sock:”, host: “stapp01:8093”
2023/11/16 02:39:50 [error] 1658#0: *1 open() “/var/www/html/50x.html” failed (2: No such file or directory), client: 172.16.238.2, server: _, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://unix:/run/php-fpm/www.sock”, host: “stapp01:8093”

cat /var/log/nginx/access.log
172.16.238.2 - - [16/Nov/2023:02:39:50 +0000] “GET /index.php HTTP/1.1” 404 169 “-” “curl/7.61.1” “-”

thanks

ngnix is not connecting to the socket given in the question. Therefore the curl test will fail, and the question will not pass.

Hi Alistair,

This is the changes to nginx.conf
server {
listen 8097;
listen [::]:8097;
server_name _;
root /var/www/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

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


    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

And www.conf
user = nginx
group = nginx
listen = /run/php-fpm/default.sock

ls /var/run/php-fpm
default.sock php-fpm.pid

did I miss out anything? I still get the same error from nginx log

Thanks

Hello Alistair,

do you have any idea where I have gone wrong?

Thanks

Hi Alistair,

I have solve it and the task is completed.

from How to Connect NGINX to PHP-FPM Using UNIX or TCP/IP Socket

vi /etc/nginx/nginx.conf : add the below block

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

vi /etc/nginx/conf.d/php-fpm.conf

# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
    server unix:/run/php-fpm/default.sock;
}

And it works.

2 Likes

Hello @Alistair_KodeKloud ,

“I got the same error twice. Please help me with this. I’m running Nginx on the right server, and I’m using the curl command as given in the task. If the port is wrong, then how is that command working, especially considering that the same port is used in that command?”

It can work if you have configured the port consistently through the task, but if the port is not the one the question asks for then it will be incorrect.

The port is likely to be different each time you retry the question.

@Alistair_KodeKloud no i rechecked many times before submitting the task.

@Alistair_KodeKloud

you can check here and verify

plz reply @Alistair_KodeKloud

I have just done the lab again and it is working

At the end, you must exit from the app server back to jump host and the curl command must work from there too (see part d. of question).

[thor@jump_host]$ curl http://stapp03:8093/index.php
Welcome to xFusionCorp Industries![thor@jump_host]$
1 Like

@34_Aniket-Kale Just to make absolutely sure the lab isn’t broken I did it a second time and it is not broken, so if it is not working for you, then you have made a mistake somewhere.
I have written up a detailed solution here.

1 Like

Thank You its working now