Answer

问题及解答

PHP7 的编译

Posted by haifeng on 2019-05-05 18:13:38 last update 2019-05-06 10:41:46 | Edit | Answers (0)

1. 首先到官方网站下载 PHP7

https://www.php.net/downloads.php#v7.3.5

这里下载文件 php-7.3.5.tar.xz (11701KB)

SHA256: e1011838a46fd4a195c8453b333916622d7ff5bce4aca2d9d99afac142db2472

2. 将php-7.3.5.tar.xz 上传到服务器

解压缩:

# xz -d php-7.3.5.tar.xz
# tar -xvf php-7.3.5.tar
# cd /usr/local
# mkdir php7
# cd php-7.3.5
# ./configure CFLAGS=-liconv --prefix=/usr/local/php7 --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --with-config-file-path=/usr/local/php7/conf --with-config-file-scan-dir=/usr/local/php7/conf.d --enable-debug --with-openssl --with-kerberos --with-zlib --enable-calendar --with-curl --with-enchant --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr  --with-freetype-dir=/usr  --enable-exif --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-intl --enable-mbstring --enable-pcntl --with-pdo-mysql --with-pdo-pgsql --with-pgsql --with-pspell --with-libedit --with-readline --enable-shmop --with-snmp --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvshm --with-xsl --enable-zip --with-pear --enable-zend-signals --enable-maintainer-zts --with-mysqli --with-mysql-sock

 

checking for libzip... not found
configure: error: Please reinstall the libzip distribution

# sudo apt-get update
# sudo apt-get install libzip2 libzip-dev

checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11

卸载 libzip2 和 libzip-dev

到 libzip 的官方网站下载最新版的libzip-1.5.2

https://libzip.org/download/

下载后上传到服务器, 执行下面的代码

mkdir build
cd build
cmake ..
make
make test
make install

如果 cmake 版本过低, 会出现下面的问题:

CMake Error at CMakeLists.txt:4 (CMAKE_MINIMUM_REQUIRED):
  CMake 3.0.2 or higher is required.  You are running version 2.8.7

因此, 需要重新安装 cmake.

安装 CMake 3.9.5

# tar -xzvf cmake-3.9.5.tar.gz
# cd cmake-3.9.5
# ./bootstrap && make && make install

执行 /usr/local/bin/cmake ..

的时候

-- Check if the system is big endian - little endian
-- Found ZLIB: /usr/local/lib/libz.a (found version "1.2.3")
-- Could NOT find BZip2 (missing: BZIP2_LIBRARIES BZIP2_INCLUDE_DIR)
CMake Warning at CMakeLists.txt:218 (MESSAGE):
  -- bzip2 library not found; bzip2 support disabled

安装 libbz2-dev

# sudo apt-get install libbz2-dev

重新执行 /usr/local/bin/cmake ..

root@atzjg:/usr/local/src/libzip-1.5.2/build# /usr/local/bin/cmake ..
-- Could NOT find NETTLE (missing: NETTLE_LIBRARY NETTLE_INCLUDE_DIR)
-- Found BZip2: /usr/lib/x86_64-linux-gnu/libbz2.so (found version "1.0.6")
-- Looking for BZ2_bzCompressInit
-- Looking for BZ2_bzCompressInit - found
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/libzip-1.5.2/build

# sudo apt-get install nettle-dev

 

重新运行 cmake ..

root@atzjg:/usr/local/src/libzip-1.5.2/build# /usr/local/bin/cmake ..
-- Found NETTLE: /usr/lib/x86_64-linux-gnu/libnettle.so  
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/libzip-1.5.2/build

然后运行 make

在 make 这一步会出现错误:

zip_crypto_openssl.c 的76行25列的 INT_MAX 第一次出现, 未定义.

只需在这个文件中加上

#include

即可.

make test 结束之后, 执行 make install


Install the project...
-- Install configuration: ""
-- Installing: /usr/local/lib/pkgconfig/libzip.pc
-- Installing: /usr/local/include/zipconf.h
-- Installing: /usr/local/include/zip.h
-- Installing: /usr/local/lib/libzip.so.5.0
-- Installing: /usr/local/lib/libzip.so.5
-- Installing: /usr/local/lib/libzip.so
-- Installing: /usr/local/share/man/man3/ZIP_SOURCE_GET_ARGS.3
-- Installing: /usr/local/share/man/man3/libzip.3
-- Installing: /usr/local/share/man/man3/zip_add.3
-- Installing: /usr/local/share/man/man3/zip_add_dir.3
-- Installing: /usr/local/share/man/man3/zip_close.3
-- Installing: /usr/local/share/man/man3/zip_delete.3
-- Installing: /usr/local/share/man/man3/zip_dir_add.3
-- Installing: /usr/local/share/man/man3/zip_discard.3
-- Installing: /usr/local/share/man/man3/zip_error_clear.3
-- Installing: /usr/local/share/man/man3/zip_error_code_system.3
-- Installing: /usr/local/share/man/man3/zip_error_code_zip.3
-- Installing: /usr/local/share/man/man3/zip_error_fini.3
-- Installing: /usr/local/share/man/man3/zip_error_get.3
-- Installing: /usr/local/share/man/man3/zip_error_get_sys_type.3
-- Installing: /usr/local/share/man/man3/zip_error_init.3
-- Installing: /usr/local/share/man/man3/zip_error_set.3
-- Installing: /usr/local/share/man/man3/zip_error_strerror.3
-- Installing: /usr/local/share/man/man3/zip_error_system_type.3
-- Installing: /usr/local/share/man/man3/zip_error_to_data.3
-- Installing: /usr/local/share/man/man3/zip_error_to_str.3
-- Installing: /usr/local/share/man/man3/zip_errors.3
-- Installing: /usr/local/share/man/man3/zip_fclose.3
-- Installing: /usr/local/share/man/man3/zip_fdopen.3
-- Installing: /usr/local/share/man/man3/zip_file_add.3
-- Installing: /usr/local/share/man/man3/zip_file_extra_field_delete.3
-- Installing: /usr/local/share/man/man3/zip_file_extra_field_get.3
-- Installing: /usr/local/share/man/man3/zip_file_extra_field_set.3
-- Installing: /usr/local/share/man/man3/zip_file_extra_fields_count.3
-- Installing: /usr/local/share/man/man3/zip_file_get_comment.3
-- Installing: /usr/local/share/man/man3/zip_file_get_error.3
-- Installing: /usr/local/share/man/man3/zip_file_get_external_attributes.3
-- Installing: /usr/local/share/man/man3/zip_file_rename.3
-- Installing: /usr/local/share/man/man3/zip_file_set_comment.3
-- Installing: /usr/local/share/man/man3/zip_file_set_encryption.3
-- Installing: /usr/local/share/man/man3/zip_file_set_external_attributes.3
-- Installing: /usr/local/share/man/man3/zip_file_set_mtime.3
-- Installing: /usr/local/share/man/man3/zip_file_strerror.3
-- Installing: /usr/local/share/man/man3/zip_fopen.3
-- Installing: /usr/local/share/man/man3/zip_fopen_encrypted.3
-- Installing: /usr/local/share/man/man3/zip_fread.3
-- Installing: /usr/local/share/man/man3/zip_fseek.3
-- Installing: /usr/local/share/man/man3/zip_ftell.3
-- Installing: /usr/local/share/man/man3/zip_get_archive_comment.3
-- Installing: /usr/local/share/man/man3/zip_get_archive_flag.3
-- Installing: /usr/local/share/man/man3/zip_get_error.3
-- Installing: /usr/local/share/man/man3/zip_get_file_comment.3
-- Installing: /usr/local/share/man/man3/zip_get_name.3
-- Installing: /usr/local/share/man/man3/zip_get_num_entries.3
-- Installing: /usr/local/share/man/man3/zip_get_num_files.3
-- Installing: /usr/local/share/man/man3/zip_libzip_version.3
-- Installing: /usr/local/share/man/man3/zip_name_locate.3
-- Installing: /usr/local/share/man/man3/zip_open.3
-- Installing: /usr/local/share/man/man3/zip_register_progress_callback.3
-- Installing: /usr/local/share/man/man3/zip_register_progress_callback_with_state.3
-- Installing: /usr/local/share/man/man3/zip_rename.3
-- Installing: /usr/local/share/man/man3/zip_set_archive_comment.3
-- Installing: /usr/local/share/man/man3/zip_set_archive_flag.3
-- Installing: /usr/local/share/man/man3/zip_set_default_password.3
-- Installing: /usr/local/share/man/man3/zip_set_file_comment.3
-- Installing: /usr/local/share/man/man3/zip_set_file_compression.3
-- Installing: /usr/local/share/man/man3/zip_source.3
-- Installing: /usr/local/share/man/man3/zip_source_begin_write.3
-- Installing: /usr/local/share/man/man3/zip_source_buffer.3
-- Installing: /usr/local/share/man/man3/zip_source_buffer_fragment.3
-- Installing: /usr/local/share/man/man3/zip_source_close.3
-- Installing: /usr/local/share/man/man3/zip_source_commit_write.3
-- Installing: /usr/local/share/man/man3/zip_source_error.3
-- Installing: /usr/local/share/man/man3/zip_source_file.3
-- Installing: /usr/local/share/man/man3/zip_source_filep.3
-- Installing: /usr/local/share/man/man3/zip_source_free.3
-- Installing: /usr/local/share/man/man3/zip_source_function.3
-- Installing: /usr/local/share/man/man3/zip_source_is_deleted.3
-- Installing: /usr/local/share/man/man3/zip_source_keep.3
-- Installing: /usr/local/share/man/man3/zip_source_make_command_bitmap.3
-- Installing: /usr/local/share/man/man3/zip_source_open.3
-- Installing: /usr/local/share/man/man3/zip_source_read.3
-- Installing: /usr/local/share/man/man3/zip_source_rollback_write.3
-- Installing: /usr/local/share/man/man3/zip_source_seek.3
-- Installing: /usr/local/share/man/man3/zip_source_seek_compute_offset.3
-- Installing: /usr/local/share/man/man3/zip_source_seek_write.3
-- Installing: /usr/local/share/man/man3/zip_source_stat.3
-- Installing: /usr/local/share/man/man3/zip_source_tell.3
-- Installing: /usr/local/share/man/man3/zip_source_tell_write.3
-- Installing: /usr/local/share/man/man3/zip_source_win32a.3
-- Installing: /usr/local/share/man/man3/zip_source_win32handle.3
-- Installing: /usr/local/share/man/man3/zip_source_win32w.3
-- Installing: /usr/local/share/man/man3/zip_source_write.3
-- Installing: /usr/local/share/man/man3/zip_source_zip.3
-- Installing: /usr/local/share/man/man3/zip_stat.3
-- Installing: /usr/local/share/man/man3/zip_stat_init.3
-- Installing: /usr/local/share/man/man3/zip_unchange.3
-- Installing: /usr/local/share/man/man3/zip_unchange_all.3
-- Installing: /usr/local/share/man/man3/zip_unchange_archive.3
-- Installing: /usr/local/share/man/man1/zipcmp.1
-- Installing: /usr/local/share/man/man1/zipmerge.1
-- Installing: /usr/local/share/man/man1/ziptool.1
-- Installing: /usr/local/share/man/man3/zip_replace.3
-- Installing: /usr/local/share/man/man3/zip_file_error_clear.3
-- Installing: /usr/local/share/man/man3/zip_file_error_get.3
-- Installing: /usr/local/share/man/man3/zip_error_init_with_code.3
-- Installing: /usr/local/share/man/man3/zip_file_replace.3
-- Installing: /usr/local/share/man/man3/zip_file_extra_field_delete_by_id.3
-- Installing: /usr/local/share/man/man3/zip_file_extra_field_get_by_id.3
-- Installing: /usr/local/share/man/man3/zip_file_extra_fields_count_by_id.3
-- Installing: /usr/local/share/man/man3/zip_strerror.3
-- Installing: /usr/local/share/man/man3/zip_fopen_index.3
-- Installing: /usr/local/share/man/man3/zip_fopen_index_encrypted.3
-- Installing: /usr/local/share/man/man3/zip_open_from_source.3
-- Installing: /usr/local/share/man/man3/zip_source_begin_write_cloning.3
-- Installing: /usr/local/share/man/man3/zip_source_buffer_create.3
-- Installing: /usr/local/share/man/man3/zip_source_buffer_fragment_create.3
-- Installing: /usr/local/share/man/man3/zip_source_file_create.3
-- Installing: /usr/local/share/man/man3/zip_source_filep_create.3
-- Installing: /usr/local/share/man/man3/zip_source_function_create.3
-- Installing: /usr/local/share/man/man3/zip_source_win32a_create.3
-- Installing: /usr/local/share/man/man3/zip_source_win32handle_create.3
-- Installing: /usr/local/share/man/man3/zip_source_win32w_create.3
-- Installing: /usr/local/share/man/man3/zip_stat_index.3
-- Installing: /usr/local/bin/zipcmp
-- Set runtime path of "/usr/local/bin/zipcmp" to ""
-- Installing: /usr/local/bin/zipmerge
-- Set runtime path of "/usr/local/bin/zipmerge" to ""
-- Installing: /usr/local/bin/ziptool
-- Set runtime path of "/usr/local/bin/ziptool" to ""

 

 


 

回到刚才 php-7.3.5 的目录, 执行 configure

 

configure: WARNING: unrecognized options: --with-curlwrappers, --with-vpx-dir, --with-t1lib, --enable-gd-native-ttf, --with-mcrypt, --with-mysql

 

安装 mysql

# sudo apt-get install mysql-server

注意如果使用 Xshell 的话, 将之最大化, 因为中间需要输入 root 用户的密码.


# sudo apt-get install mysql-client
# sudo apt-get install libmysqlclient-dev

 

重新执行 configure, 然后 make

/usr/local/src/php/php7/php-7.3.5/ext/fileinfo/libmagic/apprentice.c:2932:1: warning: ‘apprentice_buf’ defined but not used [-Wunused-function]
 apprentice_buf(struct magic_set *ms, struct magic *buf, size_t len)
 ^
virtual memory exhausted: 无法分配内存
make: *** [ext/fileinfo/libmagic/apprentice.lo] 错误 1

解决办法参考:

https://blog.csdn.net/taiyang1987912/article/details/41695895


root@xxx:~# rm -rf /opt/images/swap
root@xxx:~# dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
记录了2048000+0 的读入
记录了2048000+0 的写出
2097152000字节(2.1 GB)已复制,18.4717 秒,114 MB/秒

root@xxx:~# mkswap /opt/images/swap
正在设置交换空间版本 1,大小 = 2047996 KiB
无标签, UUID=7d57d5cd-d52d-418c-ab94-9783f23da64f

root@xxx:~# swapon /opt/images/swap
root@xxx:~# free -m
             total       used       free     shared    buffers     cached
Mem:           995        928         67          0          6        660
-/+ buffers/cache:        261        733
Swap:         1999          0       1999


然后回到 /usr/local/src/php/php7/php-7.3.5, 运行 make

当make完成, 关闭 swap

# cd /opt/images/
# swapoff swap

删除 swap
# rm -f /opt/images/swap


make install 的过程

root@xxx:/usr/local/src/php/php7/php-7.3.5# make install
Installing shared extensions:     /usr/local/php7/lib/php/extensions/debug-zts-20180731/
Installing PHP CLI binary:        /usr/local/php7/bin/
Installing PHP CLI man page:      /usr/local/php7/php/man/man1/
Installing PHP FPM binary:        /usr/local/php7/sbin/
Installing PHP FPM defconfig:     skipping
Installing PHP FPM man page:      /usr/local/php7/php/man/man8/
Installing PHP FPM status page:   /usr/local/php7/php/php/fpm/
Installing phpdbg binary:         /usr/local/php7/bin/
Installing phpdbg man page:       /usr/local/php7/php/man/man1/
Installing PHP CGI binary:        /usr/local/php7/bin/
Installing PHP CGI man page:      /usr/local/php7/php/man/man1/
Installing build environment:     /usr/local/php7/lib/php/build/
Installing header files:          /usr/local/php7/include/php/
Installing helper programs:       /usr/local/php7/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php7/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php7/lib/php/
[PEAR] Archive_Tar    - already installed: 1.4.7
[PEAR] Console_Getopt - already installed: 1.4.2
[PEAR] Structures_Graph- already installed: 1.1.1
[PEAR] XML_Util       - already installed: 1.4.3
[PEAR] PEAR           - already installed: 1.10.9
Wrote PEAR system config file at: /usr/local/php7/etc/pear.conf
You may want to add: /usr/local/php7/lib/php to your php.ini include_path
/usr/local/src/php/php7/php-7.3.5/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin
ln -s -f phar.phar /usr/local/php7/bin/phar
Installing PDO headers:           /usr/local/php7/include/php/ext/pdo/


 

先关闭之前的 PHP

# ps aux|grep php-fpm
# kill -QUIT ????

然后切换到 php7 的安装目录

# cd /usr/local/php7

root@xxx:/usr/local/php7/sbin# ./php-fpm
[05-May-2019 20:04:41] ERROR: failed to open configuration file '/usr/local/php7/etc/php-fpm.conf': No such file or directory (2)
[05-May-2019 20:04:41] ERROR: failed to load configuration file '/usr/local/php7/etc/php-fpm.conf'
[05-May-2019 20:04:41] ERROR: FPM initialization failed

 

到 php7/etc/ 目录下复制 php-fpm.conf.default 到 php-fpm.conf

/usr/local/php7/etc# cp php-fpm.conf.default php-fpm.conf

# vim php-fpm.conf

找到php-fpm.pid, 将前面的注释符; 去掉. 这样就会生成 php-fpm.pid 文件

[global]
; Pid file
; Note: the default prefix is /usr/local/php7/var
; Default Value: none
pid = run/php-fpm.pid


root@xxx:/usr/local/php7/sbin# ./php-fpm
[05-May-2019 20:10:21] WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf' from /usr/local/php7/etc/php-fpm.conf at line 143.
[05-May-2019 20:10:21] ERROR: No pool defined. at least one pool section must be specified in config file
[05-May-2019 20:10:21] ERROR: failed to post process the configuration
[05-May-2019 20:10:21] ERROR: FPM initialization failed


/usr/local/php7/etc/php-fpm.d# cp www.conf.default www.conf

然后再次执行 /usr/local/php7/sbin# ./php-fpm