mac下源码编译php7.4

MAC OS X 编译安装PHP7.4

源码包下载地址

https://www.php.net/distributions/php-7.4.5.tar.xz

下载到本地后解压到一个临时文件夹内,此处没有硬性要求。我一般都是自己新建tmp文件夹,方便存放。(ps:日后如果需要源码安装扩展的话可以直接去源码包的ext目录找到对应扩展内容)

解压后的源码包如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
➜  php-7.4.5 tree -L 1
.
├── CODING_STANDARDS.md
├── CONTRIBUTING.md
├── EXTENSIONS
├── LICENSE
├── NEWS
├── README.REDIST.BINS
├── README.md
├── TSRM
├── UPGRADING
├── UPGRADING.INTERNALS
├── Zend
├── appveyor
├── azure
├── azure-pipelines.yml
├── build
├── buildconf
├── buildconf.bat
├── configure
├── configure.ac
├── docs
├── ext
├── main
├── pear
├── php.ini-development
├── php.ini-production
├── run-tests.php
├── sapi
├── scripts
├── tests
├── travis
└── win32

14 directories, 17 files

安装

编译源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
➜  php-7.4.5 ./configure --prefix=/Users/shiwenyuan/php \
--with-mcrypt \
--with-mhash \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-gd \
--with-openssl \
--with-iconv \
--with-zlib \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--without-gdbm \
--enable-fast-install \
--disable-fileinfo

编译过程常见问题

configure: error: Package requirements (openssl >= 1.0.1) were not met:

1
2
3
4
5
6
7
8
9
10
configure: error: Package requirements (openssl >= 1.0.1) were not met:

No package 'openssl' 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 OPENSSL_CFLAGS
and OPENSSL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
原因

没有找到openssl

解决方法

我的openssl是通过brew安装的

强制brew link更新
1
2
3
4
5
6
7
8
9
10
Warning: Refusing to link macOS provided/shadowed software: openssl@1.1
If you need to have openssl@1.1 first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

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

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

上面内容大概就是brew不建议这么操作,但是也给出了解决办法,命令行依次执行上面说明需要添加的环境变量

1
2
3
4
5
➜  openssl echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc
➜ openssl export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
➜ openssl export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
➜ openssl export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"
➜ openssl source ~/.zshrc
修正对应编译项
1
--with-openssl=/usr/local/Cellar/openssl@1.1/1.1.1f

configure: error: Cannot locate header file libintl.h

1
configure: error: Cannot locate header file libintl.h
原因

–with-gettext编译报错,没有找到gettext的可执行文件,因为configure文件中gettext的默认path 是 /usr/local /usr

1
for i in $PHP_GETTEXT /usr/local /usr ;do
解决方法
找到对应可执行程序路径
1
2
➜  php-7.4.5 which gettext
/usr/local/opt/gettext/bin/gettext

如果which gettext没有结果则执行安装

1
brew install gettext
修正对应编译项
1
--with-gettext=/usr/local/opt/gettext

configure: error: Please specify the install prefix of iconv with –with-iconv=

1
2
3
checking for iconv... no
checking for libiconv... no
configure: error: Please specify the install prefix of iconv with --with-iconv=<DIR>
原因

–with-iconv编译报错,没有找到iconv的可执行文件,或者系统中没有libiconv

解决方法
找到对应可执行程序路径
1
2
➜  php-7.4.5 which iconv
/usr/local/opt/libiconv/bin/iconv

or

1
2
3
➜  php-7.4.5 brew install libiconv
➜ php-7.4.5 which iconv
/usr/local/opt/libiconv/bin/iconv
修正对应编译项
1
--with-iconv=/usr/local/opt/libiconv/

最终编译语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
➜  php-7.4.5 ./configure --prefix=/Users/shiwenyuan/php \
--with-mcrypt \
--with-mhash \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-gd \
--with-iconv=/usr/local/opt/libiconv/ \
--with-openssl=/usr/local/Cellar/openssl@1.1/1.1.1f\
--with-zlib \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext=/usr/local/opt/gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--without-gdbm \
--enable-fast-install \
--disable-fileinfo

编译成功后显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Generating files
configure: patching main/php_config.h.in
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.

make

1
2
3
4
5
6
7
8
9
10
11
12
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
directorytreeiterator.inc
clicommand.inc
directorygraphiterator.inc
invertedregexiterator.inc
pharcommand.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

make install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
➜  php-7.4.5 make install
Installing shared extensions: /Users/shiwenyuan/php/lib/php/extensions/no-debug-non-zts-20190902/
Installing PHP CLI binary: /Users/shiwenyuan/php/bin/
Installing PHP CLI man page: /Users/shiwenyuan/php/php/man/man1/
Installing PHP FPM binary: /Users/shiwenyuan/php/sbin/
Installing PHP FPM defconfig: /Users/shiwenyuan/php/etc/
Installing PHP FPM man page: /Users/shiwenyuan/php/php/man/man8/
Installing PHP FPM status page: /Users/shiwenyuan/php/php/php/fpm/
Installing phpdbg binary: /Users/shiwenyuan/php/bin/
Installing phpdbg man page: /Users/shiwenyuan/php/php/man/man1/
Installing PHP CGI binary: /Users/shiwenyuan/php/bin/
Installing PHP CGI man page: /Users/shiwenyuan/php/php/man/man1/
Installing build environment: /Users/shiwenyuan/php/lib/php/build/
Installing header files: /Users/shiwenyuan/php/include/php/
Installing helper programs: /Users/shiwenyuan/php/bin/
program: phpize
program: php-config
Installing man pages: /Users/shiwenyuan/php/php/man/man1/
page: phpize.1
page: php-config.1
/Users/shiwenyuan/tmp/LNMP/php-7.4.5/build/shtool install -c ext/phar/phar.phar /Users/shiwenyuan/php/bin
ln -s -f phar.phar /Users/shiwenyuan/php/bin/phar
Installing PDO headers: /Users/shiwenyuan/php/include/php/ext/pdo/

编译后指定的php目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
➜  php pwd
/Users/shiwenyuan/php
➜ php tree -L 2
.
├── bin
│   ├── phar -> phar.phar
│   ├── phar.phar
│   ├── php
│   ├── php-cgi
│   ├── php-config
│   ├── phpdbg
│   └── phpize
├── etc
│   ├── php-fpm.conf.default
│   └── php-fpm.d
├── include
│   └── php
├── lib
│   └── php
├── php
│   ├── man
│   └── php
├── sbin
│   └── php-fpm
└── var
├── log
└── run

14 directories, 9 files

此时需要配置一下ini和php-fpm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  php cp ~/tmp/LNMP/php-7.4.5/php.ini-production ~/php/etc/php.ini
➜ php cd etc/php-fpm.d
➜ php-fpm.d ls
www.conf.default
➜ php-fpm.d cd ..
➜ etc ls
php-fpm.conf.default php-fpm.d php.ini
➜ etc cp php-fpm.conf.default php-fpm.conf
➜ etc cd php-fpm.d
➜ php-fpm.d ls
www.conf.default
➜ php-fpm.d cp www.conf.default www.conf
➜ php-fpm.d cd ..
➜ etc ls
php-fpm.conf php-fpm.conf.default php-fpm.d php.ini
➜ etc vim php-fpm.conf //;pid=run/php-fpm.pid修正为 pid = ~/php/var/run/php-fpm.pid

测试

创建index.php

1
2
3
➜  public cat index.php                               
<?php
phpinfo();

启动服务(ps:本地并未安装webserver,所以本次测试使用的是php自带的webserver)

1
2
➜  public ~/php/bin/php -c ~/php/etc/php.ini -S localhost:9001 
[Mon May 4 16:46:01 2020] PHP 7.4.5 Development Server (http://localhost:9001) started

打开浏览器访问

WechatIMG1.png

php扩展安装

gd

刚刚gd库安装失败了,现在去源码包中重新安装一边

1
2
➜  gd pwd
/Users/shiwenyuan/tmp/LNMP/php-7.4.5/ext/gd

phpize

1
2
3
4
5
➜  gd ~/php/bin/phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902

此步骤编译成功后会生成configure可执行文件

configure

1
./configure --with-php-config=/Users/shiwenyuan/php/bin/php-config
发现错误
1
2
3
4
5
6
7
8
9
10
configure: error: Package requirements (libpng) were not met:

No package 'libpng' 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 PNG_CFLAGS
and PNG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
  1. 缺少libpng库
  2. brew install libpng安装库后
  3. 重新执行./configure --with-php-config=/Users/shiwenyuan/php/bin/php-config

编译成功后会生成对应的Makefile

make && make install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
dsymutil .libs/gd.so || :
creating gd.la
(cd .libs && rm -f gd.la && ln -s ../gd.la gd.la)
/bin/sh /Users/shiwenyuan/tmp/LNMP/php-7.4.5/ext/gd/libtool --mode=install cp ./gd.la /Users/shiwenyuan/tmp/LNMP/php-7.4.5/ext/gd/modules
cp ./.libs/gd.so /Users/shiwenyuan/tmp/LNMP/php-7.4.5/ext/gd/modules/gd.so
cp ./.libs/gd.lai /Users/shiwenyuan/tmp/LNMP/php-7.4.5/ext/gd/modules/gd.la
----------------------------------------------------------------------
Libraries have been installed in:
/Users/shiwenyuan/tmp/LNMP/php-7.4.5/ext/gd/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions: /Users/shiwenyuan/php/lib/php/extensions/no-debug-non-zts-20190902/
Installing header files: /Users/shiwenyuan/php/include/php/

修改php.ini

1
echo "extension=\"gd.so\"" >> /Users/shiwenyuan/php/etc/php.ini

重启php服务后访问

image.png