mac下編譯安裝php7.4.5及相關擴展安裝

php7.4.5下載地址:https://www.php.net/distributions/php-7.4.5.tar.gz

MAC版本:macOS catalina 10.15.4

 

編譯參數:

./configure --prefix=/Users/mac/service/php745 \
--with-config-file-path=/Users/mac/service/php745/etc \
--with-config-file-scan-dir=/Users/mac/service/php745/etc/conf.d \
--enable-pdo \
--with-pdo-mysql \
--with-mysqli \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-short-tags \
--enable-opcache \
--enable-cgi \
--enable-fpm \
--enable-sockets \
--enable-mbstring \
--enable-mbregex \
--enable-bcmath \
--enable-session \
--enable-xml \
--with-zip \
--enable-gd \
--with-zlib \
--with-mhash \
--enable-pcntl \
--with-xmlrpc \
--enable-soap \
--enable-mysqlnd \
--enable-maintainer-zts \
--enable-ftp \
--with-curl \
--enable-inline-optimization \
--enable-sysvsem \
--enable-shmop \
--with-freetype \
--with-gettext \
--with-openssl

 遇到的錯誤信息1:

checking for libxml-2.0 >= 2.7.6... no
configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:

No package 'libxml-2.0' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

解決辦法:

brew install libxml2

brew 安裝完libxml2後在最後會輸出以下信息:

If you need to have libxml2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.bash_profile

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxml2/lib"
  export CPPFLAGS="-I/usr/local/opt/libxml2/include"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"

注意:php7.4與PHP7.3安裝時的差別是在PHP7.3中可以在編譯參數中加--with-libxml-dir=/usr/local/opt/libxml2的方式來指明libxml2庫的位置,但在PHP7.4中,這種方式不再有效,直接依賴pkg-config。

所以我們需要執行:export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig" 命令,但這種方式是覆蓋式的,因爲下面還要用該種方式指明其它庫的位置,所以我們改用追加的方式,具體命令爲:

export PKG_CONFIG_PATH=/usr/local/opt/libxml2/lib/pkgconfig:$PKG_CONFIG_PATH

另外在安裝libxml2過程中會安裝其它依賴的庫,其中包括openssl,在安裝過程中的提示信息中會有提示openssl的安裝位置:

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"
export PKG_CONFIG_PATH=/usr/local/opt/[email protected]/lib/pkgconfig:$PKG_CONFIG_PATH

 

遇到的錯誤信息2:

checking for zlib >= 1.2.0.4... no
configure: error: Package requirements (zlib >= 1.2.0.4) were not met:

No package 'zlib' found

解決方式:

MacdeMacBook-Pro:php-7.4.5 mac$ brew install zlib
Updating Homebrew...
Warning: zlib 1.2.11 is already installed and up-to-date
To reinstall 1.2.11, run `brew reinstall zlib`

MacdeMacBook-Pro:php-7.4.5 mac$ brew reinstall zlib
==> Reinstalling zlib 
==> Downloading https://homebrew.bintray.com/bottles/zlib-1.2.11.mojave.bottle.tar.gz
Already downloaded: /Users/mac/Library/Caches/Homebrew/downloads/01ac92cf18f86708cd068eaf9a3594a15b31861eca1e4d6718563d5e27acd6b2--zlib-1.2.11.mojave.bottle.tar.gz
==> Pouring zlib-1.2.11.mojave.bottle.tar.gz
==> Caveats
zlib is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

For compilers to find zlib you may need to set:
  export LDFLAGS="-L/usr/local/opt/zlib/lib"
  export CPPFLAGS="-I/usr/local/opt/zlib/include"

For pkg-config to find zlib you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/zlib/1.2.11: 12 files, 373KB

安裝完zlib庫後再執行: 

export PKG_CONFIG_PATH=/usr/local/opt/zlib/lib/pkgconfig:$PKG_CONFIG_PATH

 遇到的錯誤信息3:
 

checking for libcurl >= 7.15.5... no
configure: error: Package requirements (libcurl >= 7.15.5) were not met:

No package 'libcurl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables CURL_CFLAGS
and CURL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解決辦法:

$ brew reinstall curl
If you need to have curl first in your PATH run:
  echo 'export PATH="/usr/local/opt/curl/bin:$PATH"' >> ~/.bash_profile

For compilers to find curl you may need to set:
  export LDFLAGS="-L/usr/local/opt/curl/lib"
  export CPPFLAGS="-I/usr/local/opt/curl/include"

For pkg-config to find curl you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/curl/lib/pkgconfig"


zsh completions have been installed to:
  /usr/local/opt/curl/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/curl/7.69.1: 459 files, 3.2MB

安裝完curl後再執行:

export PKG_CONFIG_PATH=/usr/local/opt/curl/lib/pkgconfig:$PKG_CONFIG_PATH

 遇到的錯誤信息4:

configure: error: Cannot locate header file libintl.h

解決辦法:

brew install gettext
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/gettext-0.20.2_1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/52/52067198cab528f05fdc0b06f7b9711f7614f60a7361f1e764c4f46d3342ff22?__gda__=exp=1
######################################################################## 100.0%
==> Pouring gettext-0.20.2_1.mojave.bottle.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/gettext
==> Summary
🍺  /usr/local/Cellar/gettext/0.20.2_1: 1,929 files, 18.7MB
Removing: /usr/local/Cellar/gettext/0.19.8.1... (1,934 files, 16.9MB)

遇到的錯誤信息5:

checking for openssl >= 1.0.1... no
configure: error: Package requirements (openssl >= 1.0.1) were not met:

No package 'openssl' found

解決辦法(其實在先前安裝libxml2時已經自動安裝了openssl,且與brew reinstall openssl安裝的版本和路徑均一致):

$ brew reinstall openssl
==> Reinstalling [email protected] 
==> Downloading https://homebrew.bintray.com/bottles/[email protected]
Already downloaded: /Users/mac/Library/Caches/Homebrew/downloads/6e3ef678544db80c666b0bbc24b33d6d8887a773110106fd9a306c7e76abd222--openssl@1.1-1.1.1g.mojave.bottle.tar.gz
==> Pouring [email protected]
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/[email protected]/certs

and run
  /usr/local/opt/[email protected]/bin/c_rehash

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/[email protected]/1.1.1g: 8,059 files, 18MB
export PKG_CONFIG_PATH=/usr/local/opt/[email protected]/lib/pkgconfig:$PKG_CONFIG_PATH

遇到的錯誤信息6:

checking for PDO includes... (cached) /Users/mac/Downloads/php-7.4.5/ext
checking for sqlite3 > 3.7.4... no
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:

No package 'sqlite3' found

解決辦法:

$ brew reinstall sqlite3
==> Reinstalling sqlite 
==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.31.1.mojave.bottle.tar.gz
Already downloaded: /Users/mac/Library/Caches/Homebrew/downloads/86776a192139be19996dbcdb59911aafc04c4e996ba0a1b33b3edcdf3664dfb2--sqlite-3.31.1.mojave.bottle.tar.gz
==> Pouring sqlite-3.31.1.mojave.bottle.tar.gz
==> Caveats
sqlite is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have sqlite first in your PATH run:
  echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.bash_profile

For compilers to find sqlite you may need to set:
  export LDFLAGS="-L/usr/local/opt/sqlite/lib"
  export CPPFLAGS="-I/usr/local/opt/sqlite/include"

For pkg-config to find sqlite you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/sqlite/3.31.1: 11 files, 4MB
export PKG_CONFIG_PATH=/usr/local/opt/sqlite/lib/pkgconfig:$PKG_CONFIG_PATH

錯誤信息7:

configure: error: Please specify the install prefix of iconv with --with-iconv=<DIR>

解決辦法:

$ brew install libiconv
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
tmux

==> Downloading https://homebrew.bintray.com/bottles/libiconv-1.16.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/24/24d81638fcd7416a56c3dbdac7e2265d7b0476b17a71b631045425380122e6b1?__gda__=exp=158858806
######################################################################## 100.0%
==> Pouring libiconv-1.16.catalina.bottle.tar.gz
==> Caveats
libiconv is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libiconv first in your PATH run:
  echo 'export PATH="/usr/local/opt/libiconv/bin:$PATH"' >> ~/.zshrc

For compilers to find libiconv you may need to set:
  export LDFLAGS="-L/usr/local/opt/libiconv/lib"
  export CPPFLAGS="-I/usr/local/opt/libiconv/include"

==> Summary
🍺  /usr/local/Cellar/libiconv/1.16: 30 files, 2.4MB
修改./configure命令,加上下面代碼:
--with-iconv=/usr/local/opt/libiconv/

錯誤信息8:

No package 'oniguruma' found

解決辦法:

brew install oniguruma

錯誤信息9:

No package 'libzip' found

解決辦法:

brew install libzip

錯誤信息10(該錯誤信息是在MAC系統升級到Catalina後出現的):

error: The pkg-config script could not be found or is too old.  Make sure it is in your PATH or set the PKG_CONFIG environment variable to the full path to pkg-config.

解決辦法:

下面是到官網下載pkg-config 並安裝, 如果沒有出現上面pkg-config的錯誤, 可以跳過這一步, 直接到第3步即可

pkg-config 官網鏈接: https://www.freedesktop.org/wiki/Software/pkg-config/
我下載版本鏈接爲:https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
    
點擊version 0.29.2 進行下載, 下載完成後解壓,進入目錄順序執行下面3步。
./configure
make
make install

如果不幸, 在執行第一步的時候就出現如下錯誤:
configure: error: Either a previously installed pkg-config or "glib-2.0 >= 2.16" could not be found. Please set GLIB_CFLAGS and GLIB_LIBS to the correct values or pass --with-internal-glib to configure to use the bundled copy.

./configure --with-internal-glib

之後configure成功後接着make 和 sudo make install

$ sudo make install
Password:
Installing shared extensions:     /Users/mac/service/php745/lib/php/extensions/no-debug-zts-20190902/
Installing PHP CLI binary:        /Users/mac/service/php745/bin/
Installing PHP CLI man page:      /Users/mac/service/php745/php/man/man1/
Installing PHP FPM binary:        /Users/mac/service/php745/sbin/
Installing PHP FPM defconfig:     /Users/mac/service/php745/etc/
Installing PHP FPM man page:      /Users/mac/service/php745/php/man/man8/
Installing PHP FPM status page:   /Users/mac/service/php745/php/php/fpm/
Installing phpdbg binary:         /Users/mac/service/php745/bin/
Installing phpdbg man page:       /Users/mac/service/php745/php/man/man1/
Installing PHP CGI binary:        /Users/mac/service/php745/bin/
Installing PHP CGI man page:      /Users/mac/service/php745/php/man/man1/
Installing build environment:     /Users/mac/service/php745/lib/php/build/
Installing header files:          /Users/mac/service/php745/include/php/
Installing helper programs:       /Users/mac/service/php745/bin/
  program: phpize
  program: php-config
Installing man pages:             /Users/mac/service/php745/php/man/man1/
  page: phpize.1
  page: php-config.1
/Users/mac/Downloads/php-7.4.5/build/shtool install -c ext/phar/phar.phar /Users/mac/service/php745/bin
ln -s -f phar.phar /Users/mac/service/php745/bin/phar
Installing PDO headers:           /Users/mac/service/php745/include/php/ext/pdo/

再之後的相關部分配置步驟爲:


sudo cp php.ini-production /Users/mac/service/php745/etc/php.ini
cd /Users/mac/service/php745/etc/
sudo cp php-fpm.conf.default php-fpm.conf
cd /Users/mac/service/php745/etc/php-fpm.d
sudo cp www.conf.default www.conf

測試
$ sudo /Users/mac/service/php745/sbin/php-fpm --test
[28-Apr-2020 17:23:39] NOTICE: configuration file /Users/mac/service/php745/etc/php-fpm.conf test is successful


$ sudo vim /Users/mac/service/php745/etc/php.ini
expose_php = Off
max_execution_time = 60
max_input_time = 60
memory_limit = 128M
post_max_size = 128M
upload_max_filesize = 128M
date.timezone = Asia/Shanghai

配置php-fpm.conf
sudo vim /Users/mac/service/php745/etc/php-fpm.conf
取下以下注釋並填寫完整路徑:
pid = run/php-fpm.pid

查看版本:
#/Users/mac/service/php745/bin/php --version 

PHP服務啓動、平滑重啓、停止命令:

啓動:# sudo /Users/mac/service/php745/sbin/php-fpm
關閉:# sudo kill -INT `cat /Users/mac/service/php745/var/run/php-fpm.pid`
重啓:# sudo kill -USR2 `cat /Users/mac/service/php745/var/run/php-fpm.pid`

附幾個常用的相關擴展的安裝方式:

------------------PSR擴展安裝---------------------- 
最新的Phalcon4版本開始需要引入PSR模塊,如果沒有安裝psr模塊則會在啓動php或者php-fpm報錯“Fatal error: Class 'psr\container\containerinterface' not found in Unknown on line 0”,出現segmentation fault。除了需要安裝psr模塊意外,一定要注意在php.ini中配置extension的順序。
在加載phalcon.so模塊之前就必須先加載psr.so模塊。so模塊加載順序依賴於你在php.ini配置中的上下順序。因此,你必須保證如下的順序結構:
extension=psr.so
extension=phalcon.so
https://pecl.php.net/package/psr

# cd cd ~/Downloads/soft 
# wget https://pecl.php.net/get/psr-1.0.0.tgz
# tar zxvf psr-1.0.0.tgz
# cd psr-1.0.0
# sudo /Users/mac/service/php745/bin/phpize
# sudo ./configure --with-php-config=/Users/mac/service/php745/bin/php-config
# sudo make
# sudo make install 
# sudo vim /Users/mac/service/php745/etc/php.ini 
extension=psr.so

# sudo kill -USR2 `cat /Users/mac/service/php745/var/run/php-fpm.pid`

------------------Phalcon4.0.5擴展安裝---------------------------- 
wget https://github.com/phalcon/cphalcon/archive/v4.0.5.tar.gz
tar zxvf v4.0.5.tar.gz
cd cphalcon/build
cd build
sudo ./install --phpize /Users/mac/service/php745/bin/phpize --php-config /Users/mac/service/php745/bin/php-config
# sudo vim /Users/mac/service/php745/etc/php.ini 
extension=phalcon.so

php-fpm 平滑重啓:
# sudo kill -USR2 `cat /Users/mac/service/php745/var/run/php-fpm.pid`

------------------安裝phpredis5.2.1擴展-------------------------------- 
http://pecl.php.net/package/redis 

# wget https://pecl.php.net/get/redis-5.2.0.tgz
# tar zxvf redis-5.2.0.tgz
# cd redis-5.2.0
# sudo /Users/mac/service/php745/bin/phpize 
# sudo ./configure --with-php-config=/Users/mac/service/php745/bin/php-config 
# sudo make
# sudo make install 
# sudo vim /Users/mac/service/php745/etc/php.ini 
extension = redis.so 
php-fpm 重啓: 
# sudo kill -USR2 `cat /Users/mac/service/php745/var/run/php-fpm.pid`

-----------------安裝mongodb1.7.4擴展--------------------------------
http://pecl.php.net/package/mongodb
# wget https://pecl.php.net/get/mongodb-1.7.4.tgz
# tar zxvf mongodb-1.7.4.tgz
# cd mongodb-1.7.4
# /Users/mac/service/php745/bin/phpize
# sudo ./configure --with-php-config=/Users/mac/service/php745/bin/php-config
# sudo make
# sudo make install 
# sudo vim /Users/mac/service/php745/etc/php.ini 
extension=mongodb.so
php-fpm 重啓: 
# sudo kill -USR2 `cat /Users/mac/service/php745/var/run/php-fpm.pid`


---------------Imagick擴展安裝--------------------------------
不建議採用brew install imagemagick安裝方式,原因是太慢!

第一步:先源碼安裝ImageMagick庫
https://imagemagick.org/script/install-source.php
wget https://imagemagick.org/download/ImageMagick.tar.gz
tar -zxf ImageMagick.tar.gz
cd ImageMagick-7.0.10-10
./configure --prefix=/Users/mac/service/ImageMagick
sudo make
sudo make install

第二步:
http://pecl.php.net/package/imagick

$ wget https://pecl.php.net/get/imagick-3.4.4.tgz
$ tar zxvf imagick-3.4.4.tgz 
$ cd imagick-3.4.4    
$ /Users/mac/service/php745/bin/phpize
$ ./configure --with-php-config=/Users/mac/service/php745/bin/php-config --with-imagick=/Users/mac/service/ImageMagick
$ sudo make
$ sudo make install 
$ sudo vim /Users/mac/service/php745/etc/php.ini
extension=imagick.so 
php-fpm 重啓: 
sudo kill -USR2 `cat /Users/mac/service/php745/var/run/php-fpm.pid`

--------------安裝php memcached擴展----------------
https://launchpad.net/libmemcached
http://localhost/phpinfo.php

先安裝libmemcached庫
方式一: brew install libmemcached

==> Pouring libmemcached-1.0.18_2.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/libmemcached/1.0.18_2: 231 files, 1.8MB
==> Caveats
==> memcached
To have launchd start memcached now and restart at login:
  brew services start memcached
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/memcached/bin/memcached


方式二:
https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/Users/mac/service/libmemcached
make && make install


接着:

wget https://pecl.php.net/get/memcached-3.1.5.tgz
tar zxvf memcached-3.1.5.tgz
cd memcached-3.1.5
sudo /Users/mac/service/php745/bin/phpize



./configure --with-php-config=/Users/mac/service/php745/bin/php-config --with-libmemcached-dir=/usr/local/Cellar/libmemcached/1.0.18_2

或
./configure --with-php-config=/Users/mac/service/php745/bin/php-config --with-libmemcached-dir=/Users/mac/service/libmemcached --disable-memcached-sasl


sudo make
sudo make install
sudo vim /Users/mac/service/php745/etc/php.ini
extension=memcached.so

php-fpm 重啓:
# sudo kill -USR2 `cat /Users/mac/service/php745/var/run/php-fpm.pid`

把更新命令行下的php:

$sudo cp /Users/mac/service/php745/bin/php /usr/bin
如果sudo cp命令報:mac  cp: /usr/bin/php: Read-only file system
則運行:$ sudo mount -uw /

$php -version 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章