升级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 Reply

Your email address will not be published. Required fields are marked *