Mac Silicon M1 编译安装 PHP8 & Swoole4.6 ARM64 全过程记录

最近搞了一台 M1 的 Mac mini 准备用这个开发 mixphp v3 版本,之前尝试了几次没有编译成功,今天再次尝试安装成功了,网络上很多人的文章存在问题(可能是系统环境不同),特此分享让后续的人闭坑。

面临的问题

由于最新版本的 macOS Big Sur 即便关闭安全模式 /usr/lib 也无法写入文件,因此导致 make install 无法安装任何 php 扩展,因此想装 Swoole 只能自行编译安装 php 到 /usr/local 目录

% csrutil status
System Integrity Protection status: disabled.
% mkdir /usr/lib/php/extensions/test
mkdir: /usr/lib/php/extensions/test: Read-only file system

PHP Build

由于 brew arm64 版本无法使用,只能采用 x64 版本安装了一些依赖,后面导致了很多问题,本想编译一个 x64 PHP+Swoole 在编译 x64 Swoole 的时候异常就没有继续了,转而研究 arm64 PHP+Swoole

arch -x86_64 brew install openssl zlib curl libjpeg libpng libxml2 gettext freetype pcre libiconv libzip

参数中的路径都需要根据自己电脑所安装的实际路径替换

./configure --prefix=/usr/local/php8.0.7 --with-config-file-path=/usr/local/php8.0.7/etc --with-config-file-scan-dir=/usr/local/php8.0.7/etc/php.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --with-mhash --enable-tokenizer --enable-pcntl --enable-sockets --enable-soap --enable-simplexml --without-pear --with-pear --with-zlib=/usr/local/opt/zlib --with-curl=/usr/local/Cellar/curl/7.77.0 --with-openssl=/usr/local/Cellar/[email protected]/1.1.1k --with-iconv=/usr/local/Cellar/libiconv/1.16
make

PHP Build ERROR: No package 'openssl' found

需要编译安装arm64:openssl (系统自带的 LibreSSL 不可以)

基于 Mac Silicon M1 的OpenSSL 编译:https://segmentfault.com/a/1190000039284154

sudo cp /usr/local/openssl/lib/pkgconfig/* /usr/local/lib/pkgconfig/

继续编译:php (将 --with-openssl 更换为 /usr/local/openssl)

PHP Build ERROR: configure: error: Please reinstall the iconv library

需要编译安装arm64:iconv

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar xvzf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make && sudo make install

继续编译:php (将 --with-iconv 更换为 /usr/local/libiconv)

PHP Build ERROR: ld: warning: ignoring file /usr/local/Cellar/oniguruma/6.9.7.1/lib/libonig.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64

需要编译安装arm64:oniguruma

wget https://github.com/kkos/oniguruma/archive/v6.9.5_rev1.tar.gz
tar xvzf v6.9.5_rev1.tar.gz
cd oniguruma-6.9.5_rev1
./autogen.sh

Oniguruma Build ERROR: autoreconf: error: aclocal failed with exit status: 2

需要安装:automake,安装了一个x64的也可以用

arch -x86_64 brew install automake

继续编译:oniguruma

./autogen.sh
./configure --prefix=/usr/local/oniguruma
make && sudo make install

移除 x64 oniguruma 将 arm64 oniguruma 关联到系统

arch -x86_64 brew uninstall oniguruma
sudo cp /usr/local/oniguruma/lib/pkgconfig/* /usr/local/lib/pkgconfig/
sudo cp /usr/local/oniguruma/include/* /usr/local/include/
export LDFLAGS="-L/usr/local/oniguruma/lib"
export CPPFLAGS="-I/usr/local/oniguruma/include"
export PKG_CONFIG_PATH="/usr/local/oniguruma/lib/pkgconfig"
make clean
./configure --prefix=/usr/local/php8.0.7 --with-config-file-path=/usr/local/php8.0.7/etc --with-config-file-scan-dir=/usr/local/php8.0.7/etc/php.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --with-mhash --enable-tokenizer --enable-pcntl --enable-sockets --enable-soap --enable-simplexml --without-pear --with-pear --with-zlib=/usr/local/opt/zlib --with-curl=/usr/local/Cellar/curl/7.77.0 --with-openssl=/usr/local/openssl --with-iconv=/usr/local/libiconv
make

PHP Build ERROR: ld: can't write output file: sapi/phpdbg/phpdbg for architecture arm64

sudo make && sudo make install

PHP Build ERROR: PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.

忽略这个异常

PHP Build ERROR: Allocation of JIT memory failed, PCRE JIT will be disabled. This is likely caused by security restrictions. Either grant PHP permission to allocate executable memory, or set pcre.jit=0

sudo vim /Users/liujian/Downloads/php-8.0.7/ext/phar/phar.php

新增 ini_set("pcre.jit", "0");

sudo make install

Swoole Build

增加一个 --enable-thread-context,并指定 --with-openssl-dir 为编译版本的路径

wget https://github.com/swoole/swoole-src/archive/refs/tags/v4.6.7.tar.gz
tar xvzf v4.6.7.tar.gz
cd swoole-src-4.6.7
./configure --with-php-config=/usr/local/php8.0.7/bin/php-config --enable-openssl --enable-http2 --enable-thread-context --with-openssl-dir=/usr/local/openssl
make && sudo make install

你可能感兴趣的:(php,php,swoole,macos)