自Macos12版本开始,php不再内置,需要自己安装,自己安装过程中遇到了一些问题,在这个帖子做一个详细的安装过程及可能遇到的问题与解决办法的总结。
一、安装PHP
这里通过homebrew进行安装,如果没有homebrew的话,可以在终端通过以下命令安装:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
以上是通过国内的镜像源进行安装(官方被墙了,如果有条件也可以去官网下载)
安装好homebrew后,在终端执行以下命令(这里安装的php版本是8.2,如果需要其他版本可自行更换):
brew install php@8.2
安装完成通过以下命令检查是否安装成功:
php -v
安装成功会出现以下字段:
二、安装Nginx
这里nginx的安装也通过homebrew进行,执行以下命令:
brew install nginx
安装完毕后启动nginx,执行以下命令:
brew services start nginx
如果遇到 Error: uninitialized constant Homebrew::Service::System
执行以下三条命令:
cd /opt/homebrew/Library/Taps/homebrew
rm -rf /opt/homebrew/Library/Taps/homebrew/homebrew-services
brew tap homebrew/services
成功后再尝试启动nginx
完成后在浏览器输入localhost,若出现以下页面则启动成功:
三、配置Nginx
前往路径 /opt/homebrew/etc/nginx ,找到文件 nginx.conf,打开
更改以下字段(对应着找就可以,改成和下面一样的):
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 html; index index.html index.htm; } #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; fastcgi_param SCRIPT_FILENAME $document_root$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; #} }
更改完成后在 /opt/homebrew/var/www 下创建test.php,内容为 :
完成后重启nginx:
brew services restart nginx
在浏览器打开locathost/test.php,成功显示则配置完成。
来源地址:https://blog.csdn.net/m0_74274856/article/details/129971686