升級Mac OS X的php到8.0,並設置apache2去使用它

問題描述

最近想在本地安裝mediaWiki,目前版本是1.36。Macbook自帶的php版本是7.3.11,然後出現以下error:

MediaWiki 1.36 requires PHP 7.3.19 or higher; you are using PHP 7.3.11.

於是就乾脆升級PHP到8.0。本文記錄一下怎樣在Mac OS X (Catalina)上安裝PHP 8.0。

安裝PHP 8.0

使用Homebrew

brew install php

目前brew裡面默認版本就是8.0。homebrew的安裝目錄在/usr/local/Cellar/ 下面。安裝完後系統默認的仍然是7.3。

% which php
/usr/bin/php

% php --version
PHP 7.3.11 (cli) (built: Feb 29 2020 02:50:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies

需要

$ brew services start php

這樣brew會設置好新版本路徑

% which php
/usr/local/bin/php

% php -version
PHP 8.0.10 (cli) (built: Aug 26 2021 15:34:04) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.10, Copyright (c), by Zend Technologies

設置Apache2

編輯httpd.conf,用mvim或其他編輯器,需要sudo權限

% sudo mvim /etc/apache2/httpd.conf

添加

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

<IfModule dir_module>
#old:
#   DirectoryIndex index.html
#new:
    DirectoryIndex index.php index.html
</IfModule>

#old: LoadModule php7_module libexec/apache2/libphp7.so
#new:
LoadModule php_module local/opt/php/lib/httpd/modules/libphp.so

重啟apache2

sudo apachectl restart

Leave a Comment

Your email address will not be published.