如何使用php56和Nginx为OSX执行php文件?

时间:2016-07-24 14:33:02

标签: php macos nginx

我正在尝试使用php56和Nginx执行php文件,这些文件是由brew安装的。

brew install nginx brew install php56

所以,/usr/local/etc/nginx/nginx.conf就在这里。

worker_processes  1;

error_log /usr/local/var/log/nginx/error.log;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        server_name  localhost;
        listen       8080;
        root   /Users/kent/work;

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

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

    }
    include servers/*;
}

我使用html文件/Users/kent/work/html制作目录index.html

<h1>this is my first file!</h1>

适用于http://localhost:8080/html/index.html

我使用php文件/Users/kent/work/php制作目录index.php

<?php phpinfo();

它没有在http://localhost:8080/php/index.php中浏览过。 奇怪的是它下载了index.php文件。

我应该在浏览器中浏览php信息。 我做错了吗?你能给出一些建议吗?

1 个答案:

答案 0 :(得分:0)

可能是您在php brew安装中没有包含fpm支持

$ brew install php56 --with-fpm --without-apache

验证您安装了php和php-fpm

$ php -v
$ php-fpm -v

<强>替代

虽然与原始问题无关,但OSX附带了apache。这可能是另一种选择。

此外,如果您使用的是Laravel(或者支持许多其他框架),您还可以查看Valet,这是使用Caddy服务器的一个不错的开发选择。

更新:其他信息

检查php-fpm是否已实际启动并侦听端口9000.根据我的评论,您可以使用以下命令检查:

$ lsof -Pni4 | grep LISTEN | grep php

您应该看到类似

的内容
php-fpm   50622 YourUsername    6u  IPv4 0xe686e4bdbc1e41b3      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   50636 YourUsername    0u  IPv4 0xe686e4bdbc1e41b3      0t0  TCP 127.0.0.1:9000 (LISTEN)

您可以使用

手动启动服务
$ brew services start homebrew/php/php56

您还需要在启动时使用

注册启动器
$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

如果您没有从lsof看到php-fpm条目,请查看以下内容以获取线索

  • 包含字符串/var/log/syslog.log
  • 的内容的homebrew.mxcl.php56
  • 检查/usr/local/var/log/php-fpm.log并确保它由正确的用户拥有,并且用户可以访问该目录。