Php-fpm listen owner

O

Oshikuru

New Pleskian

  • Sep 6, 2016
  • #1

Hi all,

I need to change the listen.owner from root to the appropriate user.
So I saw the file "/usr/local/psa/lib/modules/python/phpinimng/phpinimng.py". In this file there is this line:

listen.owner = root

I would like to override root with the user variable, so that in every php-fpm configuration the listen.owner variable has it's own user.
If I would be able to change it here generation of all php-fpm configurations would be correct for our needs. But this is an original Plesk file, so my changes would only remain until the next update of Plesk.

I know that I can change some variables in the file "/var/www/vhosts/system/example.com/conf/php.ini" but unfortunately the function "allowed_overrides" in the file "/usr/local/psa/lib/modules/python/phpinimng/phpinimng.py" does not allow to change "listen. variables".

Does anybody know how to change the listen.owner variable permanently?

 

NGINX web server (as reverse proxy) serves PHP applications through the FastCGI protocol (as a backend application server). NGINX employs PHP-FPM (FastCGI Process Manager), an alternative PHP FastCGI implementation that runs in the background as a daemon, listening for CGI requests. It comes with extra features designed for powering heavy-loaded websites or web applications, but it can be used for sites of any size.

Not only does PHP-FPM support the configuration of FastCGI resource pools, but it also improves many of the FastCGI internals and increases error reporting, script termination, and much more. It features PHP demonization, process management, a dynamic number of processes from which requests can come, error header, accelerated upload support, and more.

To accept FastCGI requests from NGINX, PHP-FPM can either listen on a TCP/IP socket or UNIX domain socket. Whichever address you choose to use is what NGINX uses to connect (proxy requests) to PHP-FPM, using the

$ ls /etc/php/7.4/
4 directive.

This guide explains how to configure NGINX to server PHP applications using PHP-FPM. It describes when to use a TCP/IP socket or UNIX domain socket to connect NGINX to PHP-FPM and why.

This guide assumes that you have NGINX and PHP-FPM installed on your Linux system, otherwise, see:

  • How to Install LEMP Server on CentOS 8
  • How to Install LEMP stack PhpMyAdmin in Ubuntu 20.04 Server
  • How to Install NGINX, MySQL/MariaDB, and PHP on RHEL 8
  • How to Install LEMP on Debian 10 Server

What Should I Use: UNIX Domain Socket or TCP/IP Socket?

UNIX domain (or IPC) sockets are a means of inter-process communication (IPC) that allow efficient data exchange between processes running on the same operating system while TCP/IP (or Internet Domain) sockets allow processes to communicate over a network.

Unlike a TCP/IP socket that identifies a server by an IP address and port (e.g 127.0.0.1:9000), you can bind a server to a UNIX domain socket using a file pathname (e.g /run/php-fpm/www.sock), which is visible in the filesystem.

A UNIX domain socket is a special type of file – file and directory permissions apply to it (as is the case with any other type of UNIX file) and can be used to restrict which processes on the host can read and write to the file, (and thus communicate with the backend server).

This way, a UNIX domain socket is secure because only processes on the local host can use it. A TCP/IP socket may be exposed to the internet posing a security risk unless extra security measures such as a firewall are implemented.

Importantly, using a UNIX domain socket is not the same as using a TCP/IP socket regarding performance, several tests and benchmarks have proven UNIX domain sockets to be faster. The main drawback of UNIX domain sockets is that they are less scalable, they only support inter-process communication within the same operating system(OS).

Where Can I Configure PHP-FPM Listen Address?

You can configure the address PHP-FPM listens on in a resource pool configuration file. Note that with PHP-FPM, you can run several pools of processes with different settings. The default pool is called

$ ls /etc/php/7.4/
5.

The location of the resource pool configuration file depends on the way PHP and PHP-FPM are installed on a Linux system (whether it’s a default/single version or multiple versions simultaneously).

For example, on CentOS 8, with a single version, all PHP configuration files are located in the

$ ls /etc/php/7.4/
6 directory and the default PHP-FPM pool
$ ls /etc/php/7.4/
7 configuration file is /etc/php-fpm.d/www.conf:

To list all PHP configuration files, use the following ls command.

# ls /etc/php*
Php-fpm listen owner
Php-fpm listen owner
List All PHP Configuration Files

On Ubuntu 20.04, the PHP configuration files are located in the

$ ls /etc/php/7.4/
8 directory and the default PHP-FPM pool
$ ls /etc/php/7.4/
7 configuration file is
$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf	#Ubuntu/Debian
OR
# vim /etc/php-fpm.d/www.conf			#CentOS/RHEL/Fedora
0:

$ ls /etc/php/7.4/
Php-fpm listen owner
Php-fpm listen owner
List All PHP Configuration Files on Ubuntu

Configuring PHP-FPM to Listen on a UNIX Domain Socket

To configure PHP-FPM to listen on a UNIX domain socket, open your default PHP-FPM pool configuration file, using your favorite text editor.

$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf	#Ubuntu/Debian
OR
# vim /etc/php-fpm.d/www.conf			#CentOS/RHEL/Fedora

Then look for the listen directive and set it to the file pathname of the UNIX domain socket as follows. Note that most installations use a UNIX domain socket by default.

listen = /run/php/php7.4-fpm.sock	#Ubuntu/Debian
OR
listen = /run/php-fpm/www.sock		#CentOS/RHEL/Fedora

If you use a UNIX domain socket, you also need to set appropriate read/write permissions for the file, to allow connections from the NGINX web server. By default, NGINX runs as user and group nginx on CentOS/RHEL/Fedora and www-data on Ubuntu and Debian.

So, find the

$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf	#Ubuntu/Debian
OR
# vim /etc/php-fpm.d/www.conf			#CentOS/RHEL/Fedora
1 and
$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf	#Ubuntu/Debian
OR
# vim /etc/php-fpm.d/www.conf			#CentOS/RHEL/Fedora
2 parameters and set them accordingly. Also, set the mode to 0660 using the
$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf	#Ubuntu/Debian
OR
# vim /etc/php-fpm.d/www.conf			#CentOS/RHEL/Fedora
3 parameter.

------------- On Debian and Ubuntu -------------
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

------------- On CentOS/RHEL and Fedora  -------------
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

Note that if the permissions on the UNIX domain socket file are not set correctly, NGINX may return a bad gateway error.

Php-fpm listen owner
Php-fpm listen owner
PHP-FPM Configuration

Configuring PHP-FPM to Listen on a TCP/IP Socket

Although a UNIX domain socket is faster than a TCP/IP socket, the former is less scalable, because it can only support inter-process communication on the same OS. If NGINX and the backend application server (PHP-FPM) are running on different systems, you will have to configure PHP-FPM to listen on a TCP/IP socket for connections.

In the PHP-FPM pool configuration file, set the

$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf	#Ubuntu/Debian
OR
# vim /etc/php-fpm.d/www.conf			#CentOS/RHEL/Fedora
4 address as follows. Make sure that the port you have chosen is not being used by another process or service on the same system.

listen = 127.0.0.1:3000
Php-fpm listen owner
Php-fpm listen owner
PHP-FPM Configuration for TCP Socket

Configuring NGINX to Work with PHP-FPM Application Server

Once you have configured the address PHP-FPM listens on, you need to configure NGINX to proxy request to it via that address, using the

$ ls /etc/php/7.4/
4 configuration parameter, in a virtual server block configuration file.

For example, if the configuration file for your website is /etc/nginx/conf.d/example.com.conf, open it for editing.

# vim /etc/nginx/conf.d/example.com.conf 

Look for the

$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf	#Ubuntu/Debian
OR
# vim /etc/php-fpm.d/www.conf			#CentOS/RHEL/Fedora
6 block for processing
$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf	#Ubuntu/Debian
OR
# vim /etc/php-fpm.d/www.conf			#CentOS/RHEL/Fedora
7 files and set the
$ ls /etc/php/7.4/
4 parameter as follows, if you configured PHP-FPM to listen on a UNIX domain socket.

fastcgi_pass unix:/run/php/php7.4-fpm.sock	#Ubuntu/Debian
OR
fastcgi_pass unix:/run/php-fpm/www.sock		#CentOS/RHEL/Fedora
Php-fpm listen owner
Php-fpm listen owner
Connect Nginx to PHP-FPM Using Unix Socket

Or use a TCP/IP address if you configured PHP-FPM to listen on a TCP/IP socket. If the backend application server (PHP-FPM) is running on a separate server (replace 10.42.0.10 with the IP address of the machine on which the PHP-FPM FastCGI server is running).

fastcgi_pass  10.42.0.10:3000;
Php-fpm listen owner
Php-fpm listen owner
Connect Nginx to PHP-FPM Using TCP Socket

Important: On CentOS 8, PHP-FPM is defined as an upstream server in the /etc/nginx/conf.d/php-fpm.conf file, within an upstream block, with the name php-fpm.

You can make changes here accordingly depending on the address PHP-FPM is configured to listen on, in the pool configuration file. The default configuration points to a UNIX domain socket.

upstream php-fpm {
        server unix:/run/php-fpm/www.sock;
}
Php-fpm listen owner
Php-fpm listen owner
Configure PHP Upstream Server in Nginx

and in your site’s server block file, simply set the

$ ls /etc/php/7.4/
4 parameter as shown.

$ ls /etc/php/7.4/
0
Php-fpm listen owner
Php-fpm listen owner
Configure Nginx to PHP-FPM Upstream Server

After making changes to the PHP-FPM and NGINX configurations, check their configuration syntax for correctness as follows.

$ ls /etc/php/7.4/
1

While the command output shows the main configuration file only, all the other configuration files are included and checked as well.

Php-fpm listen owner
Php-fpm listen owner
Check Nginx and PHP-FPM Configuration

Next, you need to restart the two services to apply the changes, using the systemctl command.

$ ls /etc/php/7.4/
2

If you get any errors, you can check the NGINX and PHP-FPM log files using the cat command.

$ ls /etc/php/7.4/
3

That’s all we had for you. The comment section below can be used to ask questions. For more information, see the NGINX documentation and PHP-FPM documentation.

Who is the default user of PHP

By default the web server and php-fpm runs with the user called www-data. It is often required that we need to run php-fpm on different users for different websites. Running each site with its own uid/gid is more secure and easier to deal with.

What port does PHP

By default, this configuration starts a PHP-FPM server listening on port 9000 that binds to 127.0. 0.1 (localhost).

How do I know if PHP

First open the php-fpm configuration file and enable the status page as shown. Inside this file, find and uncomment the variable pm. status_path = /status as shown in the screenshot. Save the changes and exit the file.

What is the difference between ondemand and dynamic in PHP

ondemand - the processes spawn on demand (when requested, as opposed to dynamic, where pm. start_servers are started when the service is started. dynamic - the number of child processes is set dynamically based on the following directives: pm. max_children , pm.