MacOS12及以上版本安装PHP,配置Nginx

  自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,内容为 :

<?php
    phpinfo(); 
?>

完成后重启nginx:

brew services restart nginx

  

在浏览器打开locathost/test.php,成功显示则配置完成。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值