最近搞了一臺 M1 的 Mac mini 準備用這個開發(fā) mixphp v3 版本,之前嘗試了幾次沒有編譯成功,今天再次嘗試安裝成功了,網絡上很多人的文章存在問題(可能是系統(tǒng)環(huán)境不同),特此分享讓后續(xù)的人閉坑糙置。
面臨的問題
由于最新版本的 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 的時候異常就沒有繼續(xù)了,轉而研究 arm64 PHP+Swoole
arch -x86_64 brew install openssl zlib curl libjpeg libpng libxml2 gettext freetype pcre libiconv libzip
參數(shù)中的路徑都需要根據(jù)自己電腦所安裝的實際路徑替換
./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/openssl@1.1/1.1.1k --with-iconv=/usr/local/Cellar/libiconv/1.16
make
PHP Build ERROR: No package 'openssl' found
需要編譯安裝arm64:openssl (系統(tǒng)自帶的 LibreSSL 不可以)
基于 Mac Silicon M1 的OpenSSL 編譯:https://segmentfault.com/a/1190000039284154
sudo cp /usr/local/openssl/lib/pkgconfig/* /usr/local/lib/pkgconfig/
繼續(xù)編譯: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
繼續(xù)編譯: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
繼續(xù)編譯:oniguruma
./autogen.sh
./configure --prefix=/usr/local/oniguruma
make && sudo make install
移除 x64 oniguruma 將 arm64 oniguruma 關聯(lián)到系統(tǒng)
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