MacOS12 Monterey已经不自带PHP了,所以手动安装PHP。

这里我们用brew来安装所有用的到扩展,PHP版本荐PHP8.0 。

1. 安装brew(国内源):

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

顺升级brew,这里主要是确保国内brew源和全球同步:

brew update

--------------------------------------------

2-1:安装PHP8.0:

brew install php@8.0

(也可安装最新版PHP:brew install php)

2-2:链路PHP8.0

fyonecon@DJhanwudi ~ % echo 'export PATH="/usr/local/opt/php@8.0/bin:$PATH"' >> ~/.zshrc
fyonecon@DJhanwudi ~ % echo 'export PATH="/usr/local/opt/php@8.0/sbin:$PATH"' >> ~/.zshrc
fyonecon@DJhanwudi ~ % export LDFLAGS="-L/usr/local/opt/php@8.0/lib"
fyonecon@DJhanwudi ~ % export CPPFLAGS="-I/usr/local/opt/php@8.0/include"
fyonecon@DJhanwudi ~ % brew services start php@8.0

查看php版本 

-----------------------------

2023-08更新:

brew install php@7.4 安装会报错:Error: php@7.4 has been disabled because it is a versioned formula

这是因为brew库已经移除了过时的php7.4版本。需要手动用第三方库安装php7.4

// 将第三方仓库加入

# brew brew tap shivammathur/php

// 安装PHP

# brew install shivammathur/php/php@7.4

// 链路php7.4

# echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
# echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
# export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
# export CPPFLAGS="-I/usr/local/opt/php@7.4/include"
# brew services start php@7.4

// 重启电脑,再用 php -v查看当前版本

-------------------------------

3. 全局安装composer:

fyonecon@DJhanwudi ~ % php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
fyonecon@DJhanwudi ~ % php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 2.1.5) successfully installed to: /Users/fyonecon/composer.phar
Use it: php composer.phar

fyonecon@DJhanwudi ~ % php -r "unlink('composer-setup.php');"
fyonecon@DJhanwudi ~ % sudo mv composer.phar /usr/local/bin/composer
Password:
fyonecon@DJhanwudi ~ % composer selfupdate
You are already using the latest available Composer version 2.1.5 (stable channel).
fyonecon@DJhanwudi ~ %

4. 安装nginx:

brew install nginx

查看nginx版本:

nginx -v

启动nginx:

brew services start nginx

成功访问如下网址代表成功:

http://127.0.0.1/

其他命令:

停止:brew services stop nginx

重启:brew services restart nginx

5. 配置nginx.conf:

文件在:/usr/local/etc/nginx/nginx.conf ,你可以直接把如下代码全部替换到nginx.conf:

#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
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  _;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index index.php index.html;
        }
 
        #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;
        }
 
        # 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;
        }

        location ~* .(env|toml|yaml|log)$ {
            return 404;
        }

    }
 
    server
    {
          listen       8500;
          server_name  _;

          location / {
              proxy_pass http://127.0.0.1:9500;
              index  index.html ;
          }

          location ~* .(env|toml|yaml|log)$ {
             return 404;
          }
          
    }
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    #
    #server {
    #    listen       443 ssl http2;
    #    server_name  localhost;
 
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
 
    #    location / {
    #        root   html;
    #        index index.php index.html;
    #    }
    #}

    include host/*.conf;

}

需要重启nginx:

brew services restart nginx

6. 验证PHP文件运行:

代码默认存放目录:

/usr/local/var/www

设置一个index.php文件,并填入如下内容:

<?php phpinfo(); ?>

在成功浏览器访问:  

-

完成!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐