记一次MAC配置LNMP PHP环境的坑

macos 自带了 php
如果不想卸载的话
只需要安装 nginx 和 mysql 就行了
mac下使用 homebrew 安装软件还是很方便的

brew install nginx
brew install mysql

安装好 nginx 和 mysql 之后,下面开始配置域名信息
下面以 zhumingzhen.test 这个本地域名为例来演示
首先使用 nginx -t 查看 nginx 的配置文件目录
在这里插入图片描述
切换到 nginx 配置文件目录
在这里插入图片描述
打开 nginx.conf 文件
在这里插入图片描述
看到最后面一行有个引入 servers 目录下的所有内容
我一般习惯把所有的配置信息分别以单个文件,保存到 servers 目录下面

在 servers 目录下 新建 zhumingzhen.conf 配置文件
在这里插入图片描述
在这里插入图片描述

注意: 上面使用 ip 加端口的形式连接
也需要修改 php-fpm 的配置 与之相同

如果上面使用 php-fpm.sock 文件形式
也需要修改 php-fpm 的配置 与之相同

nginx 
fastcgi_pass   unix:/private/etc/php-fpm/php7-fpm.sock;
php-fpm
listen = /private/etc/php-fpm/php7-fpm.sock

修改本地 host 文件, 使访问 zhumingzhen.test 域名指向本地
在这里插入图片描述
编辑 hosts 文件, 在最后一行添加
127.0.0.1 zhumingzhen.test
在这里插入图片描述添加完成保存退出

使用 brew 命令重启 nginx 服务器

brew services restart nginx

此时我认为都配置好了 就去访问 zhumingzhen.test

结果访问不到提示 502

猜测可能是 php-fpm 没有启动成功
于是执行命令 sudo php-fpm -D 启动 php-fpm

结果报错

$ sudo php-fpm -D
Password:
No log handling enabled - using stderr logging
Created directory: /var/db/net-snmp
Created directory: /var/db/net-snmp/mib_indexes
[30-May-2019 15:29:47] ERROR: failed to open configuration file ‘/private/etc/php-fpm.conf’: No such file or directory (2)
[30-May-2019 15:29:47] ERROR: failed to load configuration file ‘/private/etc/php-fpm.conf’
[30-May-2019 15:29:47] ERROR: FPM initialization failed
在这里插入图片描述
找不到 php-fpm.conf 文件
于是切换到 /private/etc/ 目录
在这里插入图片描述
看到确实没有 php-fpm.comf 文件.
只有一个 php-fpm.conf.default 文件

我们需要 复制 php-fpm.conf.default 为 php-fpm.conf
在这里插入图片描述
cp php-fpm.conf.default php-fpm.conf
cp: php-fpm.conf: Permission denied

没有权限 需要使用 sudo 命令
在这里插入图片描述
复制成功 重启 php-fpm

sudo php-fpm -D

这里还是有报错
ERROR: No pool defined. at least one pool section must be specified in config file

[30-May-2019 15:31:15] WARNING: Nothing matches the include pattern ‘/private/etc/php-fpm.d/*.conf’ from /private/etc/php-fpm.conf at line 125.
[30-May-2019 15:31:15] ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
[30-May-2019 15:31:15] ERROR: failed to post process the configuration
[30-May-2019 15:31:15] ERROR: FPM initialization failed

此时报错 php-fpm.d 文件下没有文件
我们进入 php-fpm.d 看到目录下只有 www.conf.default
我们需要复制 一份为 www.conf
cp www.conf.default www.conf
修改 www.conf 配置 listen = 127.0.0.1:9000
上面说了这个地方要和 nginx 配置文件中的一致
在这里插入图片描述
还有一个报错是 没有 /usr/var/log/php-fpm.log 这个日志文件目录
修改 php-fpm.conf 配置文件 日志路径为
error_log = /usr/local/var/log/php-fpm.log

重新启动 sudo php-fpm -D

此时就能看到 php info 页面了
在这里插入图片描述
如果你访问 php 文件 404
那么应该是你的 nginx 配置文件中的 root 项目目录配置问题
参照下我的配置文件

server {
    listen       80;
    server_name  zhumingzhen.test;

    charset utf-8;

    root /Users/zhumingzhen/Code/zhumingzhen;  # 你的项目目录

    location / {
        #root   /usr/share/nginx/html;
        try_files $uri $uri/ /index.php$is_args$args;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
        fastcgi_index  index.php;
        include        fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass   127.0.0.1:9000;
        try_files $uri = 404;
    }
}

如有错误, 欢迎指正, 不胜感激.

如果你还遇到了其他问题, 也欢迎留言交流.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章