mac 安装php

安装与启动php

升级了新系统,目前的系统是

我打开命令行窗口,执行一条命令

php -v

得到的输出是

WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
PHP 7.3.24-(to be removed in future macOS) (cli) (built: Nov 23 2020 06:45:16) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.24, Copyright (c) 1998-2018 Zend Technologies

意思是说本身就带了php7.3,未来的版本可能会去掉,我已经安装了homebrew,此时我执行命令

brew services list

输出

Name              Status  User     Plist
activemq          stopped          
elasticsearch     stopped          
kafka             stopped          
kibana            stopped          
mongodb-community started java0904 /Users/java0904/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
mysql@5.7         started java0904 /Users/java0904/Library/LaunchAgents/homebrew.mxcl.mysql@5.7.plist
nginx             error   java0904 /Users/java0904/Library/LaunchAgents/homebrew.mxcl.nginx.plist
redis             started java0904 /Users/java0904/Library/LaunchAgents/homebrew.mxcl.redis.plist
zookeeper         stopped      

继续

brew search php

输出

==> Formulae
brew-php-switcher ✔                      exolnet/deprecated/php@7.1               php-cs-fixer                             php@7.4                                  phpmyadmin
exolnet/deprecated/php@5.6               php                                      php@7.2                                  phplint                                  phpstan
exolnet/deprecated/php@7.0               php-code-sniffer                         php@7.3                                  phpmd                                    phpunit ✔
==> Casks
eclipse-php                                                         netbeans-php                                                        phpstorm

继续

 brew install php@7.4

输出

Updating Homebrew...
^Cgit -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
==> Downloading https://homebrew.bintray.com/bottles/php%407.4-7.4.13_1.big_sur.bottle.tar.gz
^C
java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo homebrew/cask-fonts)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git

fatal: cannot change to '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-fonts': No such file or directory
java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo homebrew/cask-fonts)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
fatal: cannot change to '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-fonts': No such file or directory
java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo homebrew/cask-fonts)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
fatal: cannot change to '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-fonts': No such file or directory
java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

java0904@weigongdeMacBook-Pro ~ % git -C "$(brew --repo homebrew/cask-fonts)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git

fatal: cannot change to '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-fonts': No such file or directory
java0904@weigongdeMacBook-Pro ~ % brew install php@7.4                                                                                                                       
Updating Homebrew...
^C==> Downloading https://homebrew.bintray.com/bottles/php%407.4-7.4.13_1.big_sur.bottle.tar.gz
Already downloaded: /Users/java0904/Library/Caches/Homebrew/downloads/818ed98a789fd90b815576c5869d1f615d3f397eeb158931624cd6b469b5196d--php@7.4-7.4.13_1.big_sur.bottle.tar.gz
==> Pouring php@7.4-7.4.13_1.big_sur.bottle.tar.gz
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set php_ini /usr/local/etc/php/7.4/php.ini system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set php_dir /usr/local/share/pear@7.4 system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set doc_dir /usr/local/share/pear@7.4/doc system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set ext_dir /usr/local/lib/php/pecl/20190902 system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set bin_dir /usr/local/opt/php@7.4/bin system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set data_dir /usr/local/share/pear@7.4/data system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set cfg_dir /usr/local/share/pear@7.4/cfg system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set www_dir /usr/local/share/pear@7.4/htdocs system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set man_dir /usr/local/share/man system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set test_dir /usr/local/share/pear@7.4/test system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear config-set php_bin /usr/local/opt/php@7.4/bin/php system
==> /usr/local/Cellar/php@7.4/7.4.13_1/bin/pear update-channels
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.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 file can be found in:
    /usr/local/etc/php/7.4/

php@7.4 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have php@7.4 first in your PATH run:
  echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@7.4 you may need to set:
  export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
  export CPPFLAGS="-I/usr/local/opt/php@7.4/include"


To have launchd start php@7.4 now and restart at login:
  brew services start php@7.4
Or, if you don't want/need a background service you can just run:
  php-fpm
==> Summary
🍺  /usr/local/Cellar/php@7.4/7.4.13_1: 497 files, 72.3MB

按照提示执行一下两个命令

echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

继续执行

vim ~/.zshrc

将以下内容黏贴到.zshrc文件中

执行

source ~/.zshrc

执行

php -v

输出

PHP 7.4.13 (cli) (built: Nov 30 2020 14:57:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.13, Copyright (c), by Zend Technologies
java0904@weigongdeMacBook-Pro ~ % cd /usr/local/Cellar/nginx/1.19.2/bin 

执行

 brew services start php@7.4

输出

Service `php@7.4` already started, use `brew services restart php@7.4` to restart.
java0904@weigongdeMacBook-Pro bin % brew services restart php@7.4
Stopping `php@7.4`... (might take a while)
==> Successfully stopped `php@7.4` (label: homebrew.mxcl.php@7.4)
==> Successfully started `php@7.4` (label: homebrew.mxcl.php@7.4)

执行

brew services list

输出

Name              Status  User     Plist
activemq          stopped          
elasticsearch     stopped          
kafka             stopped          
kibana            stopped          
mongodb-community started java0904 /Users/java0904/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
mysql@5.7         started java0904 /Users/java0904/Library/LaunchAgents/homebrew.mxcl.mysql@5.7.plist
nginx             error   java0904 /Users/java0904/Library/LaunchAgents/homebrew.mxcl.nginx.plist
php@7.4           error   java0904 /Users/java0904/Library/LaunchAgents/homebrew.mxcl.php@7.4.plist
redis             started java0904 /Users/java0904/Library/LaunchAgents/homebrew.mxcl.redis.plist
zookeeper         stopped          

不知道为什么php@7.4 的状态是error

执行

sudo php-fpm

输出

[26-Dec-2020 21:30:41] NOTICE: fpm is running, pid 4572
[26-Dec-2020 21:30:41] NOTICE: ready to handle connections

安装php扩展

输出

sudo pecl install redis

一路yes,最后输出

checking for igbinary includes... configure: error: Cannot find igbinary.h
ERROR: `/private/tmp/pear/install/redis/configure --with-php-config=/usr/local/opt/php@7.4/bin/php-config --enable-redis-igbinary=yes --enable-redis-lzf=yes --enable-redis-zstd=yes' failed

执行

sudo pecl install igbinary

输出

pecl/igbinary is already installed and is the same as the released version 3.1.6
install failed

于是我的逻辑是先删除,再安装

于是执行下面两个命令

sudo pecl uninstall igbinary  
sudo pecl install igbinary 

成功之后执行真正的安装

sudo pecl install redis   
sudo pecl install mongodb

此时执行查看扩展模块

php -m

已经可以看到上面的安装的模块都在里面了,但是访问index.php,却没有看到这两个扩展,原因是没有重启php,需要执行以下命令

sudo killall php-fpm
sudo php-fpm

 

### 在Mac操作系统上安装PHP的指南 在Mac操作系统上安装PHP可以通过多种方式实现,其中最常用的方法是使用Homebrew包管理器。以下是详细的说明和步骤: #### 1. 检查系统中是否已安装PHP Mac系统通常自带PHP版本,但可能不是最新的版本。可以通过以下命令检查当前安装PHP版本: ```bash php -v ``` 如果未安装或需要安装特定版本的PHP,则可以继续以下步骤[^1]。 #### 2. 安装Homebrew Homebrew是Mac上常用的包管理工具,用于轻松安装软件包。如果尚未安装Homebrew,可以通过以下命令进行安装: ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 安装完成后,运行以下命令以确保Homebrew正常工作: ```bash brew doctor ``` #### 3. 使用Homebrew安装PHP Homebrew支持安装多个版本的PHP。例如,安装PHP 8.0或PHP 8.3可以分别通过以下命令完成: ```bash brew install php@8.0 ``` ```bash brew install php@8.3 ``` 安装完成后,可以通过以下命令验证安装结果: ```bash php -v ``` 需要注意的是,如果系统中存在多个PHP版本,可能需要手动配置环境变量以选择默认使用的PHP版本[^2]。 #### 4. 配置环境变量 为了确保系统使用正确的PHP版本,可以将新安装PHP路径添加到`~/.zshrc`或`~/.bash_profile`文件中。例如,对于PHP 8.0,可以执行以下操作: ```bash echo 'export PATH="/usr/local/opt/php@8.0/bin:$PATH"' >> ~/.zshrc source ~/.zshrc ``` 上述命令将PHP 8.0的路径添加到系统的环境变量中,并使其生效。 #### 5. 安装扩展(如Xdebug) 如果需要安装PHP扩展(如Xdebug),可以通过以下命令完成: ```bash pecl install xdebug ``` 安装完成后,需要编辑PHP配置文件(`php.ini`)以启用Xdebug扩展。可以通过以下命令找到PHP配置文件的位置: ```bash php --ini ``` 然后,在`php.ini`文件中添加以下内容以启用Xdebug: ```ini zend_extension=xdebug ``` #### 6. 测试PHP安装 创建一个简单的PHP文件以测试安装是否成功。例如,在终端中运行以下命令: ```bash echo "<?php phpinfo(); ?>" > test.php php test.php ``` 如果能够正确显示PHP信息,则说明安装成功[^1]。 --- ### 注意事项 - 如果遇到权限问题,可以参考相关文档解决无权限问题。 - 确保系统中的旧版本PHP不会干扰新版本的使用。可以通过卸载旧版本PHP或调整环境变量来避免冲突[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

micro_cloud_fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值