0
点赞
收藏
分享

微信扫一扫

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6


最近在改一个用PHP开发的内容管理系统,第一次在Mac上使用PHP和NGINX,记录下安装配置过程,方便以后查阅使用。

由于好久没使用HomeBrew了,我本机的版本过低,执行brew install php命令安装PHP时报了一系列错误,最终都一一解决了,途中错误和解决方法如下:

Error:homebrew-core is a shallow clone.

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_brew

我的HomeBrew版本太低了,macOS12的系统不支持,升级HomeBrew,

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_nginx_02

执行brew update-reset

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_nginx_03

 

运行brew doctor命令再次检查HomeBrew有没有问题,发现四个警告

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_nginx_04

前两个警告是远程仓库的链接配置警告,因为国内访问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,多余了,直接删除就好。

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_homebrew_05

 

brew cleanup命令解决

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_nginx_06

运行 brew update命令报错,Error:  homebrew-core is a shallow clone.

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_nginx_07

 解决方法,把homebrew-core删除后重新更新,执行如下命令

cd /usr/local/Homebrew/Library/Taps/homebrew
rm -rf homebrew-core
brew upgrade

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_brew_08

最后,执行PHP的安装命令brew install php,开始正常下载一堆依赖包, 

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_nginx_09

安装途中接着又出现 一个错误,aspell这个包没有下载成功导致安装失败

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_homebrew_10

 

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安装它,

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_php_11

brew install php命令,安装成功

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_brew_12

brew services restart php命令重启PHP,由于GitHub被屏蔽,执行失败

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_homebrew_13

只能开启VPN,再次执行,OK

 

安装NGINX

NGINX的安装就简单了,直接执行brew install nginx

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_macos_14

使用命令启动NGINX

brew services start nginx

OK 

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_nginx_15

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,相关配置文件在刚才的提示中可以看到。

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_nginx_16

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

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_php_17

 

显示端口被占用,使用lsof -i :端口号,查看被占用的情况

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_php_18

 

是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 

 

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_homebrew_19

最后,在/usr/local/var/www/ 目录下创建test.php文件,测试下效果

命令:

进入目录:cd /usr/local/var/www/   

创建文件:touch test.php

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_macos_20

 

编辑文件:vim test.php 

输入:<?php phpinfo(); ?>   

:wq!保存并退出

浏览器访问​​http://localhost:8080/test.php​​,OK

macOS Monterey12.2.1系统使用Homebrew安装PHP8.1.3和NGINX1.21.6_nginx_21

 

 至此PHP、NGINX配置完成。

参考文献:

-3、macOS High Sierra10.13.3安装homebrew报错LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54解决方法_QC班长的博客

-2、​​http://mirrors.ustc.edu.cn/help/homebrew-bottles.html​​ 

-1、​​http://mirrors.ustc.edu.cn/help/homebrew-core.git.html​​

 0、​​php — Homebrew Formulae​​

1、​​nginx — Homebrew Formulae​​ 

2、mac安装nginx+php - jaspersong

3、Mac配置PHP环境(brew安装nginx+php)_hechenhongbo的博客

举报

相关推荐

0 条评论