Mac配置PHP开发

步骤 1:安装Brew

如果你还没有安装brew,可以在终端中运行以下命令来安装它:


/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

步骤 2: 安装PHP、MySQL和Apache

# nginx 的安装
brew install openssl
brew install nginx
brew services start nginx
brew services restart nginx

# PHP 的安装,使用 shivammathur/php 来完成多版本的安装
brew tap shivammathur/php
# 目前支持以下版本 ,按需选择安装
brew install shivammathur/php/php@5.6
brew install shivammathur/php/php@7.0
brew install shivammathur/php/php@7.1
brew install shivammathur/php/php@7.2
brew install shivammathur/php/php@7.3
brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.0
brew install shivammathur/php/php@8.1
brew install shivammathur/php/php@8.2
brew install shivammathur/php/php@8.3


# MySQL的安装,这里选择MariaDB替代MySQL
brew install mariadb
## 初始化 MariaDB
Intel Silicon: sudo /usr/local/bin/mysql_secure_installation
Apple Silicon: sudo /opt/homebrew/bin/mysql_secure_installation

步骤 3:PHP 的管理

对于多版本的PHP管理是非常繁琐的, 为了方便我使用了3个脚本sphp,rphp,iphp分别来切换CLIPHP 版本,重启PHP,查看PHP的路径信息

#!/bin/bash

homebrew_path=$(brew --prefix)

brew_array=("5.6","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2")
php_array=("php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php@7.4" "php@8.0" "php@8.1" "php@8.2")
php_installed_array=()
php_version="php@$1"

if [[ -z "$1" ]]; then
    echo "usage: sphp version"
    echo
    echo "    version of:" ${brew_array[@]}
    echo
    exit
fi

# 哪些版本的php是通过BREW安装的
for i in ${php_array[*]}; do
    version=$(echo "$i" | sed 's/^php@//')
    if [[ -d "$homebrew_path/etc/php/$version" ]]; then
        php_installed_array+=("$i")
    fi
done

# 检查请求的版本是否受支持
if [[ " ${php_array[*]} " == *"$php_version"* ]]; then
    if [[ " ${php_installed_array[*]} " == *"$php_version"* ]]; then
        # Switch Shell
        echo "Switching to $php_version"
        echo "Switching your shell"
        for i in ${php_installed_array[@]}; do
            brew unlink $i
        done
        brew link --force "$php_version"
	    echo ""
        echo ""
        php -v
        echo ""

        echo "All done!"
    else
        echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version"
    fi
else
    echo "Unknown version of PHP. PHP Switcher can only handle arguments of:" ${brew_array[@]}
fi
#!/bin/bash


homebrew_path=$(brew --prefix)

brew_array=("5.6","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2")
php_array=("php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php@7.4" "php@8.0" "php@8.1" "php@8.2")
php_installed_array=()
php_version="php@$1"

if [[ -z "$1" ]]; then
    echo "usage: sphp version"
    echo
    echo "    version of:" ${brew_array[@]}
    echo
    exit
fi



# 哪些版本的php是通过BREW安装的
for i in ${php_array[*]}; do
    version=$(echo "$i" | sed 's/^php@//')
    if [[ -d "$homebrew_path/etc/php/$version" ]]; then
        # echo "$i"
        php_installed_array+=("$i")
    fi
done

# 检查请求的版本是否受支持
if [[ " ${php_array[*]} " == *"$php_version"* ]]; then
    if [[ " ${php_installed_array[*]} " == *"$php_version"* ]]; then
        # Restart Shell
        echo "Restart to $php_version"
        brew services restart "$php_version"
        echo ""
        echo ""
        echo "The php.ini and php-fpm.ini file can be found in:"
        echo "      ${homebrew_path}/etc/php/$1"
	    echo ""
        echo ""
        echo "All done!"
    else
        echo ""
        echo ""
        echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version"
    fi
else
    echo "Unknown version of PHP. PHP Restart can only handle arguments of:" ${brew_array[@]}
fi
#!/bin/bash


homebrew_path=$(brew --prefix)

brew_array=("5.6","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2")
php_array=("php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php@7.4" "php@8.0" "php@8.1" "php@8.2")
php_installed_array=()
php_version="php@$1"



# 哪些版本的php是通过BREW安装的
for i in ${php_array[*]}; do
    version=$(echo "$i" | sed 's/^php@//')
    if [[ -d "$homebrew_path/etc/php/$version" ]]; then
        # echo "$i"
        php_installed_array+=("$i")
    fi
done


if [[ -z "$1" ]]; then
    for i in ${php_installed_array[*]}; do
        php_version=$(echo "$i" | sed 's/^php@//')
        echo ""
        echo "The PHP installed Path can be found in:"
        echo "      ${homebrew_path}/opt/$i/"
        echo "The PHP Conf Path can be found in:"
        echo "      ${homebrew_path}/etc/php/$php_version"
        echo "The php.ini and php-fpm.ini file can be found in:"
        echo "      ${homebrew_path}/etc/php/$php_version/php.ini"
        echo ""
    done
else
    # 检查请求的版本是否受支持
    if [[ " ${php_array[*]} " == *"$php_version"* ]]; then
        if [[ " ${php_installed_array[*]} " == *"$php_version"* ]]; then
            echo ""
            echo ""
            echo "The PHP installed Path can be found in:"
            echo "      ${homebrew_path}/opt/$php_version/"
            echo "The PHP Conf Path can be found in:"
            echo "      ${homebrew_path}/etc/php/$1"
            echo "The php.ini and php-fpm.ini file can be found in:"
            echo "      ${homebrew_path}/etc/php/$1/php.ini"
	        echo ""
            echo ""
            echo "All done!"
        else
            echo ""
            echo ""
            echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version"
        fi
    else
        echo "Unknown version of PHP. PHP Restart can only handle arguments of:" ${brew_array[@]}
    fi
fi

将上述文件分别保存,并授予执行权限chmod +x iphp存至 /usr/local/bin 或者 /opt/homebrew/bin, 之后即可通过[脚本名] [PHP版本号]来管理PHP了, 如rphp 8.1它将重启PHP 8.1进程,sphp 8.1它将切换CLI的PHP版本到8.1,iphp 8.1它将在终端输出PHP 8.1的安装目录和PHP配置目录.

步骤4: 修改`PHP-FPM`的监听方式为 socket

为了贴近生产环境, 建议将PHP-FPM的监听方式修改为socket. 它具有以下好处

  • 性能更好:与TCP相比,使用Unix Socket可以减少网络传输的开销,加快数据的传输速度,从而提升PHP-FPM的性能。
  • 安全性更高:使用Unix Socket可以避免因为端口被暴露而导致的安全问题,Unix Socket只能在本地访问,不会受到外部的攻击。
  • 稳定性更高:使用Unix Socket可以避免因为网络问题而导致的连接中断或超时等问题,从而增强PHP-FPM的稳定性。

总之,将PHP-FPM的监听方式修改为Unix Socket可以提高系统的性能、安全性和稳定性。

如何操作? 分别将各个PHP版本的php-fpm.conf配置文件中的listen键值修改为统一目录:* 下方是一个PHP8.1的配置信息,

> Intel 处理器:  `listen = /usr/local/var/run/php-fpm/php8.1-fpm.sock`
> Apple 处理器: `listen = /opt/homebrew/var/run/php-fpm/php8.1-fpm.sock`

修改完成后,使用 brew services start php@8.1 来启动它

步骤5: 安装需要的PHP扩展

在开发过程中, 一些插件是必要的. 这里以Xdebug为例, 演示使用pecl来安装PHP扩展

  • 首先使用sphp切换CLI环境下的PHP到指定的版本
使用`pecl install xdebug-2.9.8`来安装`xdebug`, 国内可能无法访问.
需要通过`pear config-set http_proxy 127.0.0.1:1080`来设置代理
对于不同的PHP, 需要安装兼容的版本(PHP 5.6 使用 2.5.5):

步骤6: Nginx 建立网站

使用 brew info nginx 来查看 Nginx Conf的目录, 在Nginx的最后部分(括号内)加上一行, 它将加载该目录下的全部conf文件. 现在,要增加网站只需在改目录下添加文件,然后使用brew services restart nginx来重启nginx生效


    include /Users/xiaod/Developer/Host/Conf/*;

步骤7: 证书与域名

为了使用多个域名, 我们可以使用Dnsmasq来管理Host

Setup dnsmasq on OS X

# 安装 dnsmasq
brew install dnsmasq

# 为 dnsmasq 创建配置目录
mkdir -pv $(brew --prefix)/etc/

# 为我们的 *.test 域设置域配置
echo 'address=/.test/127.0.0.1' >> $(brew --prefix)/etc/dnsmasq.conf

# 配置完成,现在使用brew的服务管理选项来管理dnsmasq服务。即使在电脑重新启动后,这也会自动启动服务。
brew services start dnsmasq
# 测试 dnsmasq 是否生效
dig test.test @127.0.0.1

# 为`test`域创建一个 dns 解析器
sudo mkdir -v /etc/resolver

# 将 dnsmasq namserver 添加到解析器
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'

# 测试, 当返回`IP`为`127.0.0.1`则为成功. 此时 *.test 的全部都将指向本地
ping test.test

域名证书

上步我们已经创建了一个根域名, 下面我们将创建一个自签名根证书来启用SSL,我们将安装 mkcert 作为我们的证书颁发机构 (CA)

brew install mkcert

# 安装到系统, 需要确认登录密码
mkcert -install

再创建一个存放域名证书的目录,上面我们把配置文件放到了~/Developer/Host/Conf,这次我们把证书~/Developer/Host/Cert

mkdir -v ~/Developer/Host/Cert
cd ~/Developer/Host/Cert

现在就可以为我们希望使用的任何域生成证书, 如给my.test创建一个可信证书

mkcert my.test

这个命令将为每个域创建 .pem 和 -key.pem 文件。要在Nginx使用它也很简单,在网站配置文件加上即可


    # SSL
    ssl_certificate     your_directory/Host/Cert/my.test.pem;
    ssl_certificate_key your_directory/Host/Cert/my.test-key.pem;