手动编译PHP各版本环境(含报错信息解决)

2019/5/20 linux常用操作

# 安装PHP各版本

拷贝包:
# wget http://cn2.php.net/get/php-5.4.44.tar.gz/from/this/mirror
# wget http://cn2.php.net/distributions/php-7.1.1.tar.gz
# wget http://cn2.php.net/distributions/php-7.2.0.tar.gz
# wget http://www.php.net/distributions/php-7.3.21.tar.gz
进行解压:
# tar xf php-7.1.1.tar.gz

进入文件夹:
# cd php-7.1.1

PHP 7.1 环境配置:
# ./configure  --prefix=/usr/local/php71  --with-config-file-path=/usr/local/php71/etc    --enable-fpm  --with-fpm-user=www  --with-fpm-group=www   --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd  --with-iconv-dir  --with-freetype-dir  --with-jpeg-dir  --with-png-dir  --with-zlib  --with-libxml-dir=/usr/local/libxml2  --enable-xml  --disable-rpath  --enable-bcmath  --enable-shmop  --enable-sysvsem  --enable-inline-optimization  --with-curl  --enable-mbregex  --enable-mbstring  --with-mcrypt  --enable-ftp  --with-gd  --enable-gd-native-ttf  --with-openssl  --with-mhash  --enable-pcntl  --enable-sockets  --with-xmlrpc  --enable-zip  --enable-soap  --with-gettext  --disable-fileinfo  --enable-opcache  --enable-intl  --with-xsl -with-config-file-scan-dir=/usr/local/php71/conf.d

PHP 7.3 环境配置:
# ./configure --prefix=/usr/local/php73  --with-config-file-path=/usr/local/php73/etc  --with-fpm-user=www  --with-fpm-group=www  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd  --with-iconv-dir  --with-freetype-dir  --with-jpeg-dir  --with-png-dir  --with-zlib  --with-libxml-dir  --with-ldap=shared  --with-gdbm  --with-pear  --with-gettext  --with-curl  --with-xmlrpc  --with-openssl  --with-mhash  --with-gd  --enable-fpm  --enable-mysqlnd  --enable-mysqlnd-compression-support  --enable-xml  --enable-rpath  --enable-bcmath  --enable-shmop  --enable-sysvsem  --enable-inline-optimization  --enable-mbregex  --enable-mbstring  --enable-intl  --enable-ftp  --enable-gd-jis-conv  --enable-pcntl  --enable-sockets  --enable-zip  --enable-soap  --enable-fileinfo  --enable-opcache  --enable-maintainer-zts

PHP 5.4 环境配置:
# ./configure --prefix=/usr/local/php5.4.6  --with-mysql=/usr/local/mysql  --with-xmlrpc --with-openssl --with-zlib --with-freetype-dir=/usr/local/freetype --with-gd=/usr/local/gd --with-jpeg-dir=/usr/local/jpeg --with-png-dir=/usr/local/libpng --with-iconv=/usr/local/libiconv-1.14 --enable-short-tags --enable-sockets --enable-zend-multibyte --enable-soap --enable-mbstring --enable-static --enable-gd-native-ttf --with-curl --with-xsl --enable-ftp --with-libxml-dir=/usr/local/libxml2 --enable-gd-native-ttf --enable-zip --enable-fastcgi --enable-fpm

报错:
configure: error: DBA: Could not find necessary header file(s).
解决:
# yum install gdbm-devel
报错:
configure: error: Cannot find ldap.h
解决:
# yum install openldap openldap-devel -yes

报错:
configure: error: Cannot find ldap libraries in /usr/lib.
解决:
# cp -frp /usr/lib64/libldap* /usr/lib/

报错:
configure: error: Cannot find ldap.h
解决:
# yum install openldap openldap-devel -yes


报错: 内存不足
cc: internal compiler error: Killed (program cc1)
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
make: *** [ext/date/lib/parse_date.lo] Error 1

解决:
# free -m  // 查看Swap物理内存分区
# sudo dd if=/dev/zero of=/swapfile bs=64M count=16 // 临时使用交换分区来解决
     16+0 records in
     16+0 records out
     1073741824 bytes (1.1 GB) copied, 9.3047 s, 115 MB/s
# sudo mkswap /swapfile
     Setting up swapspace version 1, size = 1048572 KiB
     no label, UUID=745c6c2f-efa3-498a-ad10-5d705890d5ef
# sudo swapon /swapfile
     swapon: /swapfile: insecure permissions 0644, 0600 suggested.

报错: 
undefined reference to 'libiconv_open'
解决⑴
# vim /usr/etc/php-7.1.1/Makefile  // Line 115 EXTRA_LIBS="...-lcrypt" 最后加上 "-liconv"
# make clean // 清除 make 所有命令
# make 重新进行编译
解决⑵
# ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/
# make ZEND_EXTRA_LIBS='-liconv'
# make 重新进行编译

报错:
collect2: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1
解决:
# make ZEND_EXTRA_LIBS='-liconv' 

编译
# make
     Generating phar.php
     Generating phar.phar
     PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
     directorygraphiterator.inc
     directorytreeiterator.inc
     invertedregexiterator.inc
     clicommand.inc
     pharcommand.inc
     phar.inc
     Build complete.
     Don't forget to run 'make test'.

编译完成,可以取消交换分区:
# sudo swapoff /swapfile
# sudo rm /swapfile

安装
# make install
     You may want to add: /usr/local/php71/lib/php to your php.ini include_path
     /usr/etc/php-7.1.1/build/shtool install -c ext/phar/phar.phar /usr/local/php71/bin
     ln -s -f phar.phar /usr/local/php71/bin/phar
     Installing PDO headers: /usr/local/php71/include/php/ext/pdo/

成功后查看版本:
# /usr/local/php71/bin/php -v

拷贝服务文件的php.ini配置文件到php71  拷贝php5.4的fpm文件到php7.1中
# cp /usr/etc/php-7.1.1/php.ini-development /usr/local/php71/etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf /usr/local/php71/etc/


配置第二个端口php-fpm服务(Line 7:listen = 127.0.0.1:9071)
# vim /usr/local/php71/etc/php-fpm.conf



重启fpm:
# killall php-fpm && /usr/local/php/sbin/php-fpm && /usr/local/php71/sbin/php-fpm 
# killall php-fpm && /usr/local/php/sbin/php-fpm && /usr/local/php71/sbin/php-fpm && /usr/local/php73/sbin/php-fpm

查看当前php环境:
# ps aux|grep php

配置数据库(Line1170:mysqli.default_socket = /tmp/mysql.sock)
# vim /usr/local/php71/etc/php.ini

配置nginx文件
重启nginx服务器
End!
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127