Skip to content

Instantly share code, notes, and snippets.

@yang-wei
Created October 28, 2015 04:44
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save yang-wei/370b8c9bc508d9fc9048 to your computer and use it in GitHub Desktop.
Save yang-wei/370b8c9bc508d9fc9048 to your computer and use it in GitHub Desktop.
How to use switch PHP(using phpbrew) version in nginx config
php -v
PHP 5.6.3 (cli) (built: Oct 28 2015 09:47:41)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies

But this only changes in CLI. You have to tweak you nginx(same for apache) to make it works. Nginx will still using the native PHP-FPM.

> ps aux | grep php-fpm
user     17093  0.7  2.5 512976 26080 ?        Ss   13:40   0:00 php-fpm: master process (/etc/php-fpm.conf)
> phpbrew fpm start

To know which port our phpbrew fpm uses, we can check from ~/.phpbrew/php/php-5.6.3/etc/php-fpm.conf

...
; The address on which to accept FastCGI requests.
...
; Note: This value is mandatory.
listen = 127.0.0.1:9000

Ok it's 127.0.0.1:9000. So in our nginx configuration, we do something like:

server {
 ...
 
   location ~ \.php$ {
    try_files       $uri =404;
    #fastcgi_pass    unix:/var/run/php-fpm/php-fpm.sock;        <--- probably default setting
    fastcgi_pass     127.0.0.1:9000;            <--- changed this
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include         fastcgi_params;
  }
}
@carl-parrish
Copy link

carl-parrish commented Aug 12, 2018

My system seems to be slightly different.
I looked in ~/.phpbrew/php/php-7.2.8/etc/php-fpm.d/www.conf
there I found

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /home/cparrish/.phpbrew/php/php-7.2.8/var/run/php-fpm.sock

So I updated my config file to read.

location ~\.php$ {
                #fastcgi_pass    unix:/run/php-fpm/www.sock;
                fastcgi_pass    unix:/home/cparrish/.phpbrew/php/php-7.2.8/var/run/php-fpm.sock;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_read_timeout 380;
                include         fastcgi_params;
        }

Then I restarted nginx
and when I go to the page I get a 502 Bad Gateway error. So I checked my log and saw the following

 [crit] 9473#0: *10294 connect() to unix:/home/cparrish/.phpbrew/php/php-7.2.8/var/run/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: x.x.x.x, server: www.xxx.xxx, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/home/cparrish/.phpbrew/php/php-7.2.8/var/run/php-fpm.sock:", host: "www.xxx.xxx"

So I checked my permissions and got the following any ideas?

total 4.0K
-rw-r--r--. 1 cparrish cparrish 4 Aug 11 11:15 php-fpm.pid
srw-rw----. 1 cparrish cparrish 0 Aug 11 11:15 php-fpm.sock

I'm not sure what to try next.

@carl-parrish
Copy link

Never mind I changed it to use a tcp socket like you had in your example and it's working fine now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment