如何通过 Homebrew 在 MacOS 上安装 PHP 最新版本?
已发表: 2022-01-31
如何在 MacOS 上安装 PHP?
在本教程中,我们将介绍如何在 MacOS 上安装 PHP 的步骤。 我们将使用 Homebrew 在 MacOS 上安装 PHP。
PHP
是一种广泛使用的通用脚本语言,特别适用于 Web 开发,并且可以嵌入到 HTML 中。
让我们开始吧:
- 使用 Homebrew 将 PHP 添加并安装到 macOS Monterey 12。
- 在 macOS 上更新到 PHP 版本 7.4 和 8。
- 在 Mac 中更新 PHP 版本
第1步
在 MacOS 上成功安装 Homebrew。 请完全按照本教程进行操作。
第2步
成功安装 Homebrew 后,使用以下命令安装 PHP。
1 2 |
bash - 3.2 $ brew install php |
这是“brew install php”的详细控制台结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
bash - 3.2 $ bash - 3.2 $ bash - 3.2 $ brew install php == > Downloading https : //ghcr.io/v2/homebrew/core/apr/manifests/1.7.0_2 Already downloaded : / Users / app / Library / Caches / Homebrew / downloads / bea860ee12694d9b28c54323f66a2ed14e5b7b44c7fd2568ddd179b86f27829e -- apr - 1.7.0_2.bottle_manifest.json == > Downloading https : //ghcr.io/v2/homebrew/core/apr/blobs/sha256:706df15280f05bc3ab057bd5dd856746f02c4fcc7356cccd5babd92a6362f132 Already downloaded : / Users / app / Library / Caches / Homebrew / downloads / 3beab76299c09a6d05b8ddb53711803844ff70c996e04daf8188953e2d8e9dc3 -- apr -- 1.7.0_2.monterey.bottle.tar.gz == > Downloading https : //ghcr.io/v2/homebrew/core/php/manifests/8.1.2 Already downloaded : / Users / app / Library / Caches / Homebrew / downloads / bb615e3f8d0140a0e40ab9ada2f73189ec03af655631c39f30d78a02b2887ac4 -- php - 8.1.2.bottle_manifest.json == > Downloading https : //ghcr.io/v2/homebrew/core/php/blobs/sha256:08fc122518eca5e578ff4cd791c2ced0e4a10f707ec48f1938791473e38fceb5 Already downloaded : / Users / app / Library / Caches / Homebrew / downloads / f7d51e966f2f708d28d0888ffd9e830c1cb4212293ef8408e610be33024e4521 -- php -- 8.1.2.monterey.bottle.tar.gz == > Downloading https : //ghcr.io/v2/homebrew/core/php/blobs/sha256:08fc122518eca5e578ff4cd791c2ced0e4a10f707ec48f1938791473e38fceb5 Already downloaded : / Users / app / Library / Caches / Homebrew / downloads / f7d51e966f2f708d28d0888ffd9e830c1cb4212293ef8408e610be33024e4521 -- php -- 8.1.2.monterey.bottle.tar.gz == > Installing dependencies for php : apr , ca - certificates , openssl @ 1.1 , argon2 , aspell , m4 , autoconf , brotli , libunistring , libidn2 , libnghttp2 , libssh2 , openldap , rtmpdump , zstd , curl , libtool , unixodbc , freetds , libpng , freetype , fontconfig , jpeg , libtiff , giflib , webp , gd , gmp , icu4c , krb5 , libpq , libsodium , libzip , oniguruma , pcre2 , readline , sqlite and tidy - html5 == > Installing php dependency : apr == > Pouring apr -- 1.7.0_2.monterey.bottle.tar.gz / usr / local / Cellar / apr / 1.7.0_2 : 59 files , 1.4MB == > Installing php dependency : ca - certificates == > Pouring ca - certificates -- 2021 - 10 - 26.all.bottle.tar.gz == > Regenerating CA certificate bundle from keychain , this may take a while . . . / usr / local / Cellar / ca - certificates / 2021 - 10 - 26 : 3 files , 208.5KB == > Installing php dependency : openssl @ 1.1 == > Pouring openssl @ 1.1 -- 1.1.1m.monterey.bottle.tar.gz / usr / local / Cellar / openssl @ 1.1 / 1.1.1m : 8 , 081 files , 18.5MB == > Installing php dependency : argon2 == > Pouring php -- 8.1.2.monterey.bottle.tar.gz == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set php_ini / usr / local / etc / php / 8.1 / php . ini system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set php_dir / usr / local / share / pear system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set doc_dir / usr / local / share / pear / doc system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set ext_dir / usr / local / lib / php / pecl / 20210902 system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set bin_dir / usr / local / opt / php / bin system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set data_dir / usr / local / share / pear / data system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set cfg_dir / usr / local / share / pear / cfg system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set www_dir / usr / local / share / pear / htdocs system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set man_dir / usr / local / share / man system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set test_dir / usr / local / share / pear / test system == > / usr / local / Cellar / php / 8.1.2 / bin / pear config - set php_bin / usr / local / opt / php / bin / php system == > / usr / local / Cellar / php / 8.1.2 / bin / pear update - channels == > Caveats To enable PHP in Apache add the following to httpd . conf and restart Apache : LoadModule php_module / usr / local / opt / php / 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 file can be found in : / usr / local / etc / php / 8.1 / To restart php after an upgrade : brew services restart php Or , if you don ' t want / need a background service you can just run : / usr / local / opt / php / sbin / php - fpm -- nodaemonize == > Summary / usr / local / Cellar / php / 8.1.2 : 513 files , 80.0MB == > Running ` brew cleanup php ` . . . Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP . Hide these hints with HOMEBREW_NO_ENV_HINTS ( see ` man brew ` ) . bash - 3.2 $ bash - 3.2 $ |
以下是更多 MacOS 控制台结果:



第三步
让我们验证 PHP 版本的详细信息以及它的安装位置。
1 2 3 4 5 6 7 8 9 |
bash - 3.2 $ which php / usr / local / bin / php bash - 3.2 $ php - version PHP 8.1.2 ( cli ) ( built : Jan 21 2022 04 : 47 : 46 ) ( NTS ) Copyright ( c ) The PHP Group Zend Engine v4 . 1.2 , Copyright ( c ) Zend Technologies with Zend OPcache v8 . 1.2 , Copyright ( c ) , by Zend Technologies |
你都准备好了。 如果您在 MacOS 上安装 PHP 时遇到任何问题,请告诉我。
apache.ini 文件在哪里?
1 |
bash - 3.2 $ vi / usr / local / etc / php / 8.1 / php . ini |
快乐编码。