connect()到unix:/tmp/php-fpm.sock失败(2:没有这样的文件或目录)

时间:2016-03-14 13:47:38

标签: php linux nginx

在我的流浪者盒子上,我已经安装了php7.0.3和nginx 1.9.12。 php配置www.conf。这个文件位于/usr/local/php/etc/php-fpm.d/目录下,其配置如下

user = www
group = www
;listen = /tmp/php-fpm.sock
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www

和nginx配置。 / usr / local / nginx / conf / vhost /目录中的文件vm.demo.com.conf。它的配置就像这样

server {
    listen       80;
    server_name vm.demo.com;
    index index.html index.htm index.php;
    root  /data/wwwroot/demo;

    location / {
        if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
        }
        if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
        }
        if (!-f $request_filename){
            rewrite (.*) /index.php;
        }
    }
    location ~ .*\.(php|php5)?$ {
        #fastcgi_pass unix:/tmp/php-fpm.sock;
        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;
        include fastcgi.conf;
    }
    access_log  logs/demo.log main;
}

在nginx conf中,如果我使用fastcgi_pass 127.0.0.1:9000并且php-fpm.conf监听127.0.0.1:9000,它们正常工作 如果我使用套接字配置错误:

php www.conf

user = www
group = www
listen = /tmp/php-fpm.sock
listen.owner = www
listen.group = www 

nginx vm.demo.com.conf

    location ~ .*\.(php|php5)?$ {
        fastcgi_pass unix:/tmp/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

nginx显示502 Bad gateway。在nginx error.log中显示此内容。

2016/03/14 21:17:04 [crit] 4208#0: *5 connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory) while 
connecting to upstream, client: 192.168.1.101, server: vm.demo.com, request: "GET /test.php HTTP/1.1", 
upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "vm.demo.com"
2016/03/14 21:17:04 [crit] 4208#0: *5 connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory) while 
connecting to upstream, client: 192.168.1.101, server: vm.demo.com, request: "GET /test.php HTTP/1.1", 
upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "vm.demo.com"
2016/03/14 21:18:01 [crit] 4208#0: *5 connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory) while 
connecting to upstream, client: 192.168.1.101, server: vm.demo.com, request: "GET /test.php HTTP/1.1", 
upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "vm.demo.com"

当我检查/tmp/php-fpm.sock文件时,它是否存在

[root@vbox1 vhost]# ll /tmp/php-fpm.sock 
srw-rw---- 1 www www 0 Mar 14 21:06 /tmp/php-fpm.sock

这是php和nginx运行状态:

[root@vbox1 vhost]# systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/etc/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2016-03-14 21:06:27 CST; 42min ago
  Process: 4189 ExecStop=/bin/kill -SIGINT $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 4198 (php-fpm)
   CGroup: /system.slice/php-fpm.service
           ├─4198 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
           ├─4199 php-fpm: pool www
           └─4200 php-fpm: pool www

Mar 14 21:06:27 vbox1 systemd[1]: Started The PHP FastCGI Process Manager.
Mar 14 21:06:27 vbox1 systemd[1]: Starting The PHP FastCGI Process Manager...
[root@vbox1 vhost]# systemctl status nginx.service
● nginx.service - nginx
   Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2016-03-14 21:06:35 CST; 42min ago
  Process: 4180 ExecStop=/usr/local/nginx/sbin/nginx -s quit (code=exited, status=0/SUCCESS)
  Process: 4206 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 4207 (nginx)
   CGroup: /system.slice/nginx.service
           ├─4207 nginx: master process /usr/local/nginx/sbin/nginx
           ├─4208 nginx: worker process
           └─4209 nginx: worker process

Mar 14 21:06:35 vbox1 systemd[1]: Starting nginx...
Mar 14 21:06:35 vbox1 systemd[1]: Started nginx.

有人能帮助我吗?如果有人遇到同样的问题

6 个答案:

答案 0 :(得分:1)

我找到了一个继续使用unix套接字的配置(php5),并且不涉及编辑任何其他配置文件(nginx默认站点文件除外)。

location ~ \.php$ {
fastcgi_pass unix:/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# \Add following to use sqlite as db.
fastcgi_param DB_CONNECTION sqlite;

}

注意: - unix套接字不在/run/php/(虽然它适用于php7)。 <{1}}是防止php文件下载而不是被解释所必需的。

为了完整性,以下是我的整个默认文件。

include fastcgi_params;

答案 1 :(得分:1)

基本上,该问题是由一个进程(php-fpm)在 / tmp 路径中设置袜子而引起的,但其他进程(nginx)无法看到它。

服务配置中有一个设置,即使使用同一用户,该设置也会阻止由不同进程创建的文件。

找到您的服务配置文件(例如: /etc/systemd/system/myapp.service ),然后添加以下设置: PrivateTmp您的服务(php和nginx)都=否

[Service]
...
PrivateTmp=No

有关systemd单位的更多信息,在这里:

https://www.freedesktop.org/software/systemd/man/systemd.unit.html

答案 2 :(得分:0)

您可以尝试将/usr/local/php/etc/php-fpm.d/www.conf中的用户更改为nginx并重新启动服务。

user = nginx
group = nginx
listen = /tmp/php-fpm.sock

答案 3 :(得分:0)

您需要更改listen.owner和listen.group

listen.owner = nginx

listen.group = nginx

在我的案例中:/etc/php-fpm.d/www.conf

套接字必须与nginx块相同(如apache中的vhost)。我还将套接字移到了/ var / sockets

答案 4 :(得分:0)

对于在Vagrant内部使用Homestead的所有用户:

我解决了简单地用置备标志重新加载流浪汉: 从项目根目录:

cd Homestead

然后:

vagrant reload --provision

以这种方式重新编译所有配置

答案 5 :(得分:0)

我遇到了这个问题,您可以这样做:

<img id="img" src=''/>
<p>Select a new car from the list.</p>

<select id="mySelect" onchange="myFunction()">
  <option value="Audi">Audi</option>
  <option value="BMW">BMW</option>
  <option value="Mercedes">Mercedes</option>
  <option value="Volvo">Volvo</option>
</select>

<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("mySelect").value;
  document.getElementById("demo").innerHTML = "You selected: " + x;
  document.getElementById("img").src='pic_bulboff.gif'
}
</script>

这对我有用。

相关问题