在 MacOS 上使用 NGINX 运行 PHP 站点

在 MacOS 上使用 NGINX 运行 PHP 站点

我已使用本博客作为在 MacOS Sierra 上运行 PHP 站点的本地环境的基础。我已安装 NGINX、PHP 7、PHP-FPM、MySQl 和 PhpMyAdmin。所有部分似乎都在运行:

使用默认nginx.conf并运行:curl -IL http://127.0.0.1:8080,我得到:

HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 22 Aug 2017 08:10:03 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Jul 2017 13:24:27 GMT
Connection: keep-alive
ETag: "5964d18b-264"
Accept-Ranges: bytes

关于 PHP-FPM,运行:lsof -Pni4 | grep LISTEN | grep php,我得到:

php-fpm   13178 phil    6u  IPv4 0x2b2cdb4c34e4e2c3      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   13181 phil    0u  IPv4 0x2b2cdb4c34e4e2c3      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   13182 phil    0u  IPv4 0x2b2cdb4c34e4e2c3      0t0  TCP 127.0.0.1:9000 (LISTEN)

我还可以启动 MySQL,得到:

mysql>

使用默认nginx.conf,我可以访问http://本地主机:8080/我看到了‘欢迎使用 nginx!’测试屏幕。

当我编辑我的nginx.conf。以下是我编辑它的方式:

user  phil staff;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/phil/sites/drupal/;
            index  index.php;
        }

    location ~ \.php {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include fastcgi_params;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

现在,如果我运行brew services restart nginx然后sudo nginx -t,我会得到:

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

...但是,如果我访问http://本地主机/,我明白了Local host refused to connect

我的直觉是它是权限并使用端口 80,但我不确定该怎么做才能让它工作。

如果我这样做curl -IL http://127.0.0.1:80,我会得到:

curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused

谢谢。

答案1

解决方案是使用 重新启动 NGINX 服务sudo

sudo brew services restart nginx

相关内容