# php-fpm socket + nginx

## redwood

Hi,

I'm trying to host my own Friendica http://friendica.com/ site using the following:

 www-servers/nginx-1.2.6-r1

 dev-db/mariadb-5.5.29

 dev-lang/php-5.4.8 with fpm

I've followed these guides:

 http://en.gentoo-wiki.com/wiki/Nginx

 http://wiki.nginx.org/PlatformGentoo

 http://en.gentoo-wiki.com/wiki/Virtual_Webhosting_with_nginx

 http://www.rackspace.com/knowledge_center/article/installing-nginx-and-php-fpm-setup-for-nginx

I'm trying to use a unix socket instead of tcp for connecting to the fastcgi fpm, but my socket doesn't seem to get created so I'm getting a "502 Bad Gateway" error message.

```

# grep unix /etc/nginx/sites-available/fd.conf 

      fastcgi_pass unix:/var/run/php5-fpm/fd.socket;

# grep pool\.d /etc/php/fpm-php5.4/php-fpm.conf 

include = /etc/php/fpm-php5.4/pool.d/*.conf

```

And my socket definition file:

/etc/php/fpm-php5.4/pool.d/fd.conf

```

listen = /var/run/php5-fpm/fd.socket

listen.backlog = -1

; Unix user/group of processes

user  = nginx

group = nginx

; Choose how the process manager will control the number of child processes.

pm = dynamic

pm.max_children = 75

pm.start_servers = 10

pm.min_spare_servers = 5

pm.max_spare_servers = 20

pm.max_requests = 500

; Pass environment variables

env[HOSTNAME] = $HOSTNAME

env[PATH] = /usr/local/bin:/usr/bin:/bin

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

; host-specific php ini settings here

; php_admin_value[open_basedir] = /var/www/DOMAINNAME/htdocs:/tmp

```

My fpm log shows nothing helpful:

```

[06-Mar-2013 12:34:19] WARNING: [pool www] pm.start_servers is not set. It's been set to 20.

[06-Mar-2013 12:34:19] NOTICE: fpm is running, pid 12163

[06-Mar-2013 12:34:19] NOTICE: ready to handle connections

```

Anybody know how to configure fpm to create socket?

Thanks.

----------

## i-s-a-k

Have you confirmed that the socket is actually created?

file /var/run/php5-fpm/fd.socket 

Should return

/var/run/php5-fpm/fd.socket: socket

----------

## redwood

I manually created the directory /var/run/php5-fpm/

but it's empty. 

The socket file /var/run/php5-fpm/fd.socket  never never gets created.

When I start /etc/init.d/php-fpm

it's supposed to create the pool of  sockets, right?

as specified in /etc/php/fpm-php5.4/php-fpm.conf ?

The Gentoo wiki used the tcp connection, but recommended sockets.

The rackspace setup guide said to put a socket configuration file in the pool directory:

 *Quote:*   

> 
> 
> To complete the setup move to the directory /etc/php5/fpm/pool.d/. 
> 
> It is here that you will setup the different UNIX Sockets that PHP-FPM will function on. 
> ...

 

So I created /etc/php/fpm-php5.4/pool.d

and added a template pool file to it, chaning the DOMAINNAME, etc.

Then I added line the the php-fpm.conf to include the pool config file, 

but the rackspace guide didn't specify what how that line looked 

(I guess it's part of their default setup but not Gentoo's)

----------

## i-s-a-k

And you've made sure that the nginx user has write access to /var/run/php5-fpm/ ?

----------

## redwood

# ls -ldn /var/run

drwxr-xr-x 20 0 0 4096 Mar  6 15:36 /var/run

# ls -ldn  /var/run/php5-fpm/

drwxr-xr-x 2 124 978 4096 Mar  5 22:28 /var/run/php5-fpm/

# id nginx

uid=124(nginx) gid=978(nginx) groups=978(nginx)

----------

## redwood

Hmmm, there's one WARNING from php-fpm about pm.start_server not being set:

# cat /var/log/php-fpm.log 

```

[06-Mar-2013 18:31:09] NOTICE: Finishing ...

[06-Mar-2013 18:31:09] NOTICE: exiting, bye-bye!

[06-Mar-2013 18:31:09] WARNING: [pool www] pm.start_servers is not set. It's been set to 20.

[06-Mar-2013 18:31:09] NOTICE: fpm is running, pid 17943

[06-Mar-2013 18:31:09] NOTICE: ready to handle connections

```

But I've included /etc/php/fpm-php5.4/pool.d/*.conf

# grep -v '^;' php-fpm.conf |grep -v '^$'

```

[global]

error_log = /var/log/php-fpm.log

[www]

include=/etc/php/fpm-php5.4/pool.d/*.conf

 

user = nobody

group = nobody

pm = dynamic

pm.max_children = 50

pm.min_spare_servers = 5

pm.max_spare_servers = 35

```

And fd.conf should set pm.start_servers = 10

# grep -v '^;' pool.d/fd.conf |grep -v '^$'

```

listen = /var/run/php5-fpm/fd.sock

listen.backlog = -1

listen.owner = nginx

listen.group = nginx

listen.mode = 0660

user  = nginx

group = nginx

pm = dynamic

pm.max_children = 75

pm.start_servers = 10

pm.min_spare_servers = 5

pm.max_spare_servers = 20

pm.max_requests = 500

env[HOSTNAME] = $HOSTNAME

env[PATH] = /usr/local/bin:/usr/bin:/bin

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

```

----------

## i-s-a-k

[www] 

include=/etc/php/fpm-php5.4/pool.d/*.conf 

seems like you are setting the poolname [www] before the include

that might be ok if you have only 1 pool, but if you have multiple pools that will cause some problems

----------

## redwood

OK, I tried moving "include" line to just before pool definition section in php-fpm.conf

Same result -- no socket created.

I've also checked out the configuration wiki

http://php-fpm.org/wiki/Configuration_File

but it's not very helpful.

/etc/init.d/php-fpm  seems to start daemon as ""root and worker processes as "nobody":

```

root 5215  1  0 08:57 ?  00:00:00 php-fpm: master process (/etc/php/fpm-php5. /php-fpm.conf)                                 

nobody    5216  5215  0 08:57 ?        00:00:00 php-fpm: pool www

```

I've tried changing ownership/group/permissions in php-fpm.conf and in pool.d/fd.conf:

```

listen.owner = nginx

listen.group = nginx

listen.mode = 0666

```

but master and workers still run as root and nobody.

And php-fpm still warns about pm.start_server not being set:

```

# cat /var/log/php-fpm.log 

[07-Mar-2013 08:57:46] WARNING: [pool www] pm.start_servers is not set. It's been set to 20.

[07-Mar-2013 08:57:46] NOTICE: fpm is running, pid 5215

[07-Mar-2013 08:57:46] NOTICE: ready to handle connections

```

even though it is (I think):

# grep pm.start_servers pool.d/fd.conf

```

pool.d/fd.conf:pm.start_servers = 10

```

I've tried setting it in main php-fpm.conf as well and the log still complains about it not being set and then setting it to a default value of 20.

I wonder if any Gentoo users have got php-fpm working with sockets or if there's something goofy with Gentoo's init script?

I guess I can switch from sockets to tcp data connection if I can't figure out how to get this working.

----------

## redwood

Well,

I've been modifying the config files for php5.4-fpm 

but apparently using php5.3 

so I need to tell Gentoo to change PHPSLOT for fpm's init script to 5.4:

http://www.gentoo.org/proj/en/php/php-upgrading.xml

```

# eselect php list fpm

  [1]   php5.3 *

  [2]   php5.4

# eselect php set fpm 2

# eselect php list fpm

  [1]   php5.3

  [2]   php5.4 *

```

Now, finally, it creates socket file:

```

# ls -l /var/run/php5-fpm/

total 0

srw-rw-rw- 1 nginx nginx 0 Mar  7 10:30 fd.sock

```

My working config files:

# grep -v '^;' php-fpm.conf |grep -v '^$'

```

[global]

error_log = /var/log/php-fpm.log

[www]

include=/etc/php/fpm-php5.4/pool.d/*.conf

 

listen.owner = nginx

listen.group = nginx

listen.mode = 0666

user = nobody

group = nobody

pm = dynamic

pm.max_children = 50

pm.min_spare_servers = 5

pm.max_spare_servers = 35

```

# grep -v '^;' pool.d/fd.conf |grep -v '^$'

```

listen = /var/run/php5-fpm/fd.sock

listen.backlog = -1

user  = nginx

group = nginx

pm = dynamic

pm.max_children = 75

pm.start_servers = 10

pm.min_spare_servers = 5

pm.max_spare_servers = 20

pm.max_requests = 500

env[HOSTNAME] = $HOSTNAME

env[PATH] = /usr/local/bin:/usr/bin:/bin

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

```

Also, FWIW, the include statement must come under the pool [www] section.

If I put it anywhere before the pool section then I get the following error:

```

# /etc/init.d/php-fpm start

 * Starting PHP FastCGI Process Manager ...

[07-Mar-2013 10:36:30] ERROR: [/etc/php/fpm-php5.4/pool.d/social.conf:1] unknown entry 'listen'

[07-Mar-2013 10:36:30] ERROR: Unable to include /etc/php/fpm-php5.4/pool.d/social.conf from /etc/php/fpm-php5.4/php-fpm.conf at line 1

[07-Mar-2013 10:36:30] ERROR: failed to load configuration file '/etc/php/fpm-php5.4/php-fpm.conf'

[07-Mar-2013 10:36:30] ERROR: FPM initialization failed

 * start-stop-daemon: failed to start `/usr/lib/php5.4/bin/php-fpm'

^C * php-fpm: caught SIGINT, aborting

```

----------

## i-s-a-k

my point was not to move the include line

it was that [www] is the name of the pool, so if you have multiple pools

then you should include that inside the included files

for example

cat /etc/php/fpm-php5.4/pools/www.pool.conf

[www]

listen = /var/run/www_fpm.sock

pm = dynamic

pm.start_servers = 1

.... etc

cat /etc/php/fpm-php5.4/pools/www2.pool.conf

[www2]

listen = /var/run/www2_fpm.sock

pm = dynamic

pm.start_servers = 1

.... etc

anyway good that you got it working  :Smile: 

----------

## redwood

Thanks for the help!

I've got a regular LAMP server on another machine.

I was going to setup mariadb+nginx in an lxc container

but I think my kernel (3.3. :Cool:  is too new for lxc

lxc-start kept complaining about a missing kernel option 

that's been removed from the current kernel's makefile.

So I'm just setting this up on main host.

Haven't yet got Friendica working. 

Just getting a blank screen which probably means there some missing php module

that I haven't yet tracked down.

Friendica supplies a virtualbox image but I was trying to see if I could just 

install the webapp and the database

without running virtualbox.

Anybody hosting their own friendica?

----------

## redwood

Fixed my blank screen issue.

Somehow I deleted or didn't cut-n-paste the first line 

of /etc/nginx/fastcgi_params:

which should have been:

 *Quote:*   

> fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

 

Now php is working.

nginx is running as user 'nginx'

and fpm-php is running as 'root' and workers are running as 'nobody'

And I 'chown -R nginx.nginx /var/www/fd/'

but Friendica installer complained it couldn't write to /var/www/fd/htdocs/fd'

so I had to 'chmod o+w /var/www/fd.mydomain.com/htdocs/fd'

 *Quote:*   

> 
> 
> stat --format '%a' /var/www/fd.mydomain.com/htdocs/fd/
> 
> 755
> ...

 

Only complain Friendica has now is that it requires mysqli php module.

----------

## dappiu

 *redwood wrote:*   

> Fixed my blank screen issue.
> 
> Somehow I deleted or didn't cut-n-paste the first line 
> 
> of /etc/nginx/fastcgi_params:
> ...

 

My ebuild installation of nginx provided 2 files: fastcgi_params and fastcgi.conf (qfile reports www-servers/nginx on both case)

fastcgi.conf includes the line you mentioned (fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;) but fastcgi_params do not!

So you could just include fastcgi.conf, or fastcgi_params but specifying the SCRIPT_FILENAME line.

I would not modify fastcgi_params adding that line, because someone could use it without expecting the SCRIPT_FILENAME argument.

```

location ~ \.php$ {

    include /etc/nginx/fastcgi.conf;

    fastcgi_pass ...

}

```

or

```

location ~ \.php$ {

    include /etc/nginx/fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    fastcgi_pass ...

}

```

----------

