php7.x源码安装、版本切换脚本

1. 安装命令:

./configure --prefix=/var/php-7.3.6 \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-iconv=/usr/local/opt/libiconv \
--with-config-file-path=/var/php-7.3.6 \
--with-pear=/var/php-7.3.6/pear \
--with-pdo-mysql=mysqlnd \
--with-openssl=/usr/local/opt/openssl \
--enable-mbstring

2. 参数说明:

参数 参数说明
--prefix 指定【独立结构】的安装位置,默认:/usr/local/bin
--exec-prefix 指定【依赖结构】的安装位置,默认:/usr/local/lib
--enable-fpm 指定启用 fpm SAPI 模块
--with-fpm-user 指定运行 php-fpm 的用户,默认:nobody
--with-fpm-group 指定运行 php-fpm 的用户组,默认:nobody
--with-config-file-path 指定去哪里找 php.ini 文件,默认:--prefix 下的 lib 目录
--with-config-file-scan-dir 指定配置文件的查找目录
--with-pear 指定pear的安装目录,默认:--prefix 下的 lib/php 目录
--with-pdo-mysql=mysqlnd 安装mysql的pdo扩展,需要先安装mysql
--with-openssl 安装支持https链接的扩展,php_openssl

3. 问题及解决

3.1 在 ./configure 时:configure: error: Please specify the install prefix of iconv with --with-iconv=

安装依赖:brew install libiconv
查找安装路径:find / -name libiconv
得到:/usr/local/opt/libiconv
在 ./configure 时,增加:--with-iconv=/usr/local/opt/libiconv

3.2 在 make && make install 时,ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [sapi/cli/php] Error 1

find / -name "libiconv.dylib"
得到:/usr/local/Cellar/libiconv/1.16/lib/libiconv.dylib
vim Makefile,搜索:[EXTRA_LIBS =],找到:
EXTRA_LIBS = -lresolv -liconv -liconv -lm -lxml2 -lz -licucore
的一行,改成:
EXTRA_LIBS = -lresolv /usr/local/Cellar/libiconv/1.16/lib/libiconv.dylib -liconv
然后 make && make install 就成功了

3.3 --with-openssl 参数提示 configure: error: libcrypto not found! 错误

find / -name openssl
得到:/usr/local/opt/openssl
参数改成:--with-openssl=/usr/local/opt/openssl

3.4 phpize: Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script.

brew search autoconf
brew info autoconf
brew install autoconf

4. php 版本切换脚本

假设你在机器上安装了许多php版本(都安装到了 /var/ 目录下),这时为了 debug 代码在不同版本下产生的问题,需要切换 php 版本,下面的脚本的思路是:

  1. 输入版本号,判断 /var/php-$version 是否存在
  2. 验证目标版本是否是合法的 php 安装目录
  3. /usr/bin/ 下的 php 相关可执行文件的软连删除
  4. 将目标版本的可执行文件软连到 /usr/bin/
  5. 删除 php-fpm 进程,并启动目标版本的 php-fpm 进程
#! /bin/bash
version=$1

# check version to switch is validate
echo start checking php with version=$version...
version_path='/var/php-'$version
if [ ! -d $version_path ] ; then
    echo php with version=$version is not installed...
    exit 0
fi
bin_files='/bin/php /bin/php-config /bin/phpize /sbin/php-fpm'
for bin_file in $bin_files
do
    version_bin_file=$version_path$bin_file
    if [ ! -f $version_bin_file ] ; then
        echo lost file $version_bin_file, switch php version failed...
        exit 0
    fi
done
echo checking php with version=$version successfully, start switching...

# remove execute files in /usr/bin/
remove_exe_files='/usr/bin/php /usr/bin/phpize /usr/bin/php-config /usr/bin/php-fpm'
for remove_file in $remove_exe_files
do
    if [ -f $remove_file ] ; then
        rm -f $remove_file
        if [ ! -f $remove_file ] ; then
            echo removed old bin file success, $remove_file...
        else
            echo removed old bin file failed, $remove_file...
        fi
    fi
done

# link bin file to swith version
for bin_file in $bin_files
do
    version_bin_file=$version_path$bin_file
    ln -s $version_bin_file /usr/bin/
done
for linked_file in $remove_exe_files
do
    if [ ! -f $linked_file ] ; then
        echo link from $version_path to $linked_file failed...
        exit 0
    else
        echo link from $version_path to $linked_file successed...
    fi
done

# kill & restart php-fpm process
php_fpm_process_number_1=$(ps aux | grep php-fpm | grep -v grep | wc -l)
if [ $php_fpm_process_number_1 -gt 0 ] ; then
    ps aux | grep php-fpm | grep -v grep | awk '{print "kill -9 "$2}' | sh
    php_fpm_process_number_2=$(ps aux | grep php-fpm | wc -l)
    if [ $php_fpm_process_number_1 -lt 1 ] ; then
        echo killed old php-fpm process success...
    fi
else
    echo no old php-fpm process to kill, skiped...
fi
php-fpm -c $version_path/php.ini
php_fpm_process_number_3=$(ps aux | grep php-fpm | grep -v grep  | wc -l)
if [ $php_fpm_process_number_3 -gt 0 ] ; then
    echo restart php-fpm successfully, switch php version successfully...
else
    echo restart php-fpm failed, you may need to run: 'php-fpm -c $version_path/php.ini'...
fi

你可能感兴趣的:(php7.x源码安装、版本切换脚本)