MAC installation of PHP and environment configuration Nanny level

1. First, the command line is installed

brew install [email protected]

Problems that may arise during the first installation

a. Installation version problem

Warning: No available formula with the name "[email protected]". Did you mean [email protected], [email protected] or [email protected]?
==> Searching for similarly named formulae and casks... ?
==> Formulae
[email protected] [email protected] [email protected]

To install [email protected], run:
  brew install [email protected]

The reason for this problem is that there is a problem with the installed version, just follow the prompts to download and install the corresponding version

b. an error occurs

fatal: not in a git directory
Error: Command failed with exit 128: git

Solution: run brew -v directly

brew -v
mark@192 ~ % brew -v
Homebrew 4.0.19-9-g8aaf99e
fatal: detected dubious ownership in repository at '/opt/homebrew/Library/Taps/homebrew/homebrew-core'
To add an exception for this directory, call:

git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-core
Homebrew/homebrew-core (no Git repository)
fatal: detected dubious ownership in repository at '/opt/homebrew/Library/Taps/homebrew/homebrew-cask'
To add an exception for this directory, call:

git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-cask
Homebrew/homebrew-cask (no Git repository)

See that there are two git configs, just run them separately, as follows

mark@192 ~ % git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-core
mark@192 ~ % git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-cask

After completion, run: arch -arm64 brew install cocoapods

mark@192 ~ % arch -arm64 brew install cocoapods

operation result

==> Downloading https://formulae.brew.sh/api/formula.jws.json
#################################################### #################################################### ################ 58.8%curl: (28) Operation too slow. Less than 100 bytes/sec transferred the last 5 seconds

Warning: formula.jws.json: update failed, falling back to cached version.
==> Downloading https://formulae.brew.sh/api/formula.jws.json
#################################################### #################################################### #################################################### ################################################## 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
#################################################### #################################################### #################################################### ################################################## 100.0%
Warning: Treating cocoapods as a formula. For the cask, use homebrew/cask/cocoapods
==> Fetching dependencies for cocoapods: libyaml, ca-certificates, [email protected], readline and ruby
==> Fetching libyaml
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/libyaml-0.2.5.arm64_ventura.bottle.tar.gz
#################################################### #################################################### #################################################### ################################################## 100.0%
==> Fetching ca-certificates
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/ca-certificates-2023-01-10.all.bottle.tar.gz
Already downloaded: /Users/mark/Library/Caches/Homebrew/downloads/08486bb5b9927def5c947c5a9e4a873eacd6364ac9cf6e50bfe3aa0bfc1c63ed--ca-certificates-2023-01-10.all.bottle.tar.gz
==> Fetching [email protected]
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/[email protected]_ventura.bottle.tar.gz
Already downloaded: /Users/mark/Library/Caches/Homebrew/downloads/339aa132aee19ced825e32e534324ac7fddc1948efbf19216760c835d71c395b--openssl@1.1-1.1.1t.arm64_ventura.bottle.tar.g z
==> Fetching readline
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/readline-8.2.1.arm64_ventura.bottle.tar.gz
Already downloaded: /Users/mark/Library/Caches/Homebrew/downloads/61eadcc12bccc526220de0af71471c1f84d36861ce68a1e85979887e904b4e56--readline-8.2.1.arm64_ventura.bottle.tar.gz
==> Fetching ruby
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/ruby-3.2.2.arm64_ventura.bottle.tar.gz
#################################################### #################################################### #################################################### ################################################## 100.0%
==> Fetching cocoapods
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/cocoapods-1.12.1.arm64_ventura.bottle.tar.gz
#################################################### #################################################### #################################################### ################################################## 100.0%
==> Installing dependencies for cocoapods: libyaml, ca-certificates, [email protected], readline and ruby
==> Installing cocoapods dependency: libyaml
==> Pouring libyaml-0.2.5.arm64_ventura.bottle.tar.gz
/opt/homebrew/Cellar/libyaml/0.2.5: 10 files, 351.2KB
==> Installing cocoapods dependency: ca-certificates
==> Pouring ca-certificates-2023-01-10.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while... ?
 /opt/homebrew/Cellar/ca-certificates/2023-01-10: 3 files, 216.8KB
==> Installing cocoapods dependency: [email protected]
==> Pouring [email protected]_ventura.bottle.tar.gz
 /opt/homebrew/Cellar/[email protected]/1.1.1t: 8,101 files, 18MB
==> Installing cocoapods dependency: readline
==> Pouring readline-8.2.1.arm64_ventura.bottle.tar.gz
/opt/homebrew/Cellar/readline/8.2.1: 50 files, 1.7MB
==> Installing cocoapods dependency: ruby
==> Pouring ruby-3.2.2.arm64_ventura.bottle.tar.gz
/opt/homebrew/Cellar/ruby/3.2.2: 16,605 files, 48.3MB
==> Installing cocoapods
==> Pouring cocoapods-1.12.1.arm64_ventura.bottle.tar.gz
/opt/homebrew/Cellar/cocoapods/1.12.1: 13,430 files, 27.8MB
==> Running `brew cleanup cocoapods`...
Disable this behavior by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

After that is done, go ahead and install it.

2. Configuration environment

After the installation is complete, this command cannot be found directly at this time with php -v

zsh: command not found: php

You need to configure the installed php to the operating environment

vi ~/.bashrc

After installing php, there is a prompt in the last part

==>[email protected]
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini files can be found in:
    /opt/homebrew/etc/php/8.0/

[email protected] is keg-only, which means it was not symlinked into /opt/homebrew,
Because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/opt/homebrew/opt/[email protected]/sbin:$PATH"' >> ~/.zshrc

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"

To start [email protected] now and restart at login:
  brew services start [email protected]
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/[email protected]/sbin/php-fpm --nodaemonize

Put the following two lines into the opened bashrc file

export PATH=”/opt/homebrew/opt/[email protected]/bin:$PATH”

export PATH=”/opt/homebrew/opt/[email protected]/sbin:$PATH”

:wq save and exit

bashrc will not save and take effect by itself

Need: source ~/.bashrc Finally here, the PHP environment you installed yourself is OK

mark@192 ~ % php -v
PHP 8.0.28 (cli) (built: Feb 14 2023 15:42:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.0.28, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.28, Copyright (c), by Zend Technologies