在mac上尝试安装nginx+php7+mysql,遇到了不少的坑,总结方法如下: 首先在启动了nginx(brew安装)

$ sudo nginx

运行php文件报错,查看nginx错误日志:

2020/07/06 01:46:32 [notice] 852#0: signal process started 2020/07/10 19:05:40 [error] 4075#0: *1 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1:8080"

这是因为没有启动php-fpm导致的(原本以为只有未启动php-fpm的时候才会报这个错,其实不然,详见下文),但是在启动php-fpm时遇到如下报错:

$ php-fpm [07-Jul-2020 18:12:16] WARNING: Nothing matches the include pattern '/private/etc/php-fpm.d/*.conf' from /private/etc/php-fpm.conf at line 126. [07-Jul-2020 18:12:16] ERROR: failed to open error_log (/usr/local/var/log/php-fpm.log): Permission denied (13) [07-Jul-2020 18:12:16] ERROR: failed to post process the configuration [07-Jul-2020 18:12:16] ERROR: FPM initialization failed

根据第一个warning得知,应该是 /private/etc/php-fpm.d 目录下缺少一个后缀为.conf的文件,打开目录可以看到:

php-fpm.d

$ ls www.conf.default

复制文件到/private/etc/php-fpm.d下:

$ cp www.conf.default www.conf

再次启动php-fpm,居然又报错了:

$ php-fpm [07-Jul-2020 20:33:09] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root [07-Jul-2020 20:33:09] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root [07-Jul-2020 20:33:09] ERROR: Unable to create the PID file (/usr/var/run/php-fpm.pid).: No such file or directory (2) [07-Jul-2020 20:33:09] ERROR: FPM initialization failed

前两行notice是账号权限不足(可以忽略,或者用root账号启动即可),后面这个也是默认的路径不存在,无法创建PID文件,找到/private/etc/php-fpm.conf文件,将pid = /run/php-fpm.pid改成自己的路径,我直接修改成了绝对路径:

pid = /usr/local/var/run/php-fpm.pid

再次启动,再次报错:

$ php-fpm

[07-Jul-2020 00:09:47] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48) [07-Jul-2020 00:09:47] ERROR: FPM initialization failed

错误显而易见了,9000端口被占用了。这种情况大多数是由于软件冲突、或者默认端口设置不正确导致的,此时需要查看究竟哪个进程占用了端口,用命令 lsof -i:端口号 查看具体端口占用情况:(以下代码只做命令示例)

$ lsof -i:9000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME php-fpm 1111 lucius 7u IPv4 0xaec77521d4cee5d5 0t0 TCP localhost:cslistener (LISTEN) php-fpm 1112 lucius 0u IPv4 0xaec77521d4cee5d5 0t0 TCP localhost:cslistener (LISTEN) php-fpm 1113 lucius 0u IPv4 0xaec77521d4cee5d5 0t0 TCP localhost:cslistener (LISTEN)

结束占用端口的进程: killall 进程名 虽然不建议用这种方法来解决冲突问题,但某些情况下还是可以直接结束掉占用进程的。 到现在为止,看起来都没有什么问题了,接着运行php文件,发现居然还是有错。。。(此时内心已经快崩溃了) 接着查看nginx错误日志:

07/Jul/2020 12:30:14 [error] 1258#0: *29 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:80/index.php", host: "localhost:8080"

报错信息和一开始判断php-fpm未启动时的错误一致,起初我还是坚信是我的php-fpm启动有问题,但是直到再次检查nginx.conf时才意识到问题所在。大家请看下图:

58 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 59 location ~ .php$ { 60 proxy_pass http://127.0.0.1; 61 } 62 63 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 64 65 location ~ .php$ { 66 root html; 67 fastcgi_pass 127.0.0.1:9000; 68 fastcgi_index index.php; 69 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 70 include fastcgi_params; 71 }

59行~61行的备注写的清清楚楚明明白白,这个配置在apache服务器上才需要启用,而nginx的配置在65~71行,这两个配置同时解开注释的话,那肯定是要冲突的啊,是我一开始配置nginx时错误的解开了两个。。。所以注掉59~61就可以了。