最近在改一个用PHP开发的内容管理系统,第一次在Mac上使用PHP和NGINX,记录下安装配置过程,方便以后查阅使用。
由于好久没使用HomeBrew了,我本机的版本过低,执行brew install php命令安装PHP时报了一系列错误,最终都一一解决了,途中错误和解决方法如下:
Error:homebrew-core is a shallow clone.
我的HomeBrew版本太低了,macOS12的系统不支持,升级HomeBrew,
执行brew update-reset 命令,成功升级到了目前的最新版
运行brew doctor命令再次检查HomeBrew有没有问题,发现四个警告
前两个警告是远程仓库的链接配置警告,因为国内访问GitHub很慢所以我换成了国内的镜像地址,没有直到到他们官方的链接的警告,不用理会也行。
Warning: Unbrewed header files were found in /usr/local/include.If you didn't put them there on purpose they could cause problems when building Homebrew formulae, and may need to be deleted.——这个警告是之前我安装的nodejs,多余了,直接删除就好。
Warning: Broken symlinks were found. Remove them with `brew cleanup`:运行brew cleanup命令解决
运行 brew update命令报错,Error: homebrew-core is a shallow clone.
解决方法,把homebrew-core删除后重新更新,执行如下命令
cd /usr/local/Homebrew/Library/Taps/homebrew
rm -rf homebrew-core
brew upgrade
最后,执行PHP的安装命令brew install php,开始正常下载一堆依赖包,
安装途中接着又出现 一个错误,aspell这个包没有下载成功导致安装失败
Error: No such file or directory @ rb_sysopen - /Users/liyuanba/Library/Caches/Homebrew/downloads/67819a080c565ca8368bf0c0af7893c80a936e7c862e872883b4f0cb73bd3416--aspell-0.60.8.monterey.bottle.1.tar.gz
单独使用brew安装它,执行命令brew install aspell安装它,
最后,再次执行brew install php命令,安装成功
执行brew services restart php命令重启PHP,由于GitHub被屏蔽,执行失败
只能开启VPN,再次执行,OK
安装NGINX
NGINX的安装就简单了,直接执行brew install nginx 命令就成功了
使用命令启动NGINX
brew services start nginx
OK
NGINX默认服务路径,项目根目录
/usr/local/var/www
配置文件路径
/usr/local/etc/nginx/nginx.conf
/usr/local/etc/nginx/nginx.conf.default
相关NGINX使用相关命令
brew services start nginx
sudo nginx #启动nginx服务
sudo nginx -s reload #重新载入配置文件
sudo nginx -s stop #停止nginx服务
这时nginx 已经跑起来了,但是目前还不能解析 php,我们需要利用php-fpm
解析。刚才我们已经安装好了PHP和PHP-FPM,相关配置文件在刚才的提示中可以看到。
php-fpm的配置文件在
/usr/local/etc/php/8.1/php-fpm.conf
打开这个配置文件,去掉注释,开启pid和错误日志输出,修改error_log 错误日志路径,
把error_log前面的分号删除,并改为error_log =/usr/local/var/log/php-fpm.log
把pid 前面的分号删除,并改为pid=/usr/local/var/run/php-fpm.pid
启动php-fpm,执行命令sudo php-fpm
显示端口被占用,使用lsof -i :端口号,查看被占用的情况
是php-fpm占用的,说明已经启动了,就不用再次启动了。
接着配置NGINX的配置文件,NGINX的配置目录/usr/local/etc/nginx,NGINX的项目根目录/usr/local/var/www,打开NGINX的配置文件/usr/local/etc/nginx/nginx.conf,去掉PHP部分的注释,修改前可以复制一份备份。
1、找到server中的下面代码,添加index.php
2、开启FastCGI server,
找到文件中的 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
改为:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
重启NGINX服务,执行命令:brew services restart nginx
最后,在/usr/local/var/www/ 目录下创建test.php文件,测试下效果
命令:
进入目录:cd /usr/local/var/www/
创建文件:touch test.php
编辑文件:vim test.php
输入:<?php phpinfo(); ?>
:wq!保存并退出
浏览器访问http://localhost:8080/test.php,OK
至此PHP、NGINX配置完成。
参考文献:
-2、http://mirrors.ustc.edu.cn/help/homebrew-bottles.html
-1、http://mirrors.ustc.edu.cn/help/homebrew-core.git.html
2、mac安装nginx+php - jaspersong - 博客园
3、Mac配置PHP环境(brew安装nginx+php)_hechenhongbo的博客-CSDN博客_mac 安装php环境