一、编译php

打开php官网 https://www.php.net/ 查看最新稳定版本php

/app/zabbix/service

1.安装 libxml2

解压:tar -zxvf libxml2-2.9.1.tar.gz
进入解压目录:cd libxml2-2.9.1
配置:./configure --prefix=/app/zabbix/service/libxml2_2.9.1
编译安装:make && make install
下载地址:wget http://xmlsoft.org/sources/libxml2-2.9.1.tar.gz
报 python.h 没有找到文件 sudo yum install python-devel

2.安装sqlite3

解压:tar -xf sqlite-autoconf-3340100.tar.gz
进入解压目录:cd sqlite-autoconf-3340100
配置:./configure --prefix=/app/zabbix/service/sqlite3
编译安装:make && make install
下载地址:https://sqlite.org/2021/sqlite-autoconf-3340100.tar.gz

3.安装zlib

解压:tar -xf zlib-1.2.11.tar.gz
进入解压目录:cd zlib-1.2.11
配置:./configure --prefix=/app/zabbix/service/zlib-1.2.11
编译安装:make && make install
下载地址:wget http://www.zlib.net/zlib-1.2.11.tar.gz

4.安装bzip2

大难头,送上脚本

#!/bin/bash
shell_folder=$(cd "$(dirname "$0")";pwd)
pushd $shell_folder
# 上一条命令执行出错则中止脚本执行
exit_on_error(){
    if [ ! $? -eq 0 ]
    then
        echo "exit for error"
        exit -1
    fi
}
# 如果文件/文件夹存在则删除
remove_if_exist(){
    if [ $# -eq 1 ]
    then
        if [ -e $1 ]
        then
            rm $1 -fr
            if [ ! $? -eq 0 ]
            then
                exit -1
            fi
        fi
        return 0
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 清除指定文件夹的内容,如果文件夹不存在则创建空文件夹
clearn_folder(){
    if [ $# -eq 1 ]
    then
        if [ -d "$1" ]
        then 
            rm -fr $1/*
        else 
            mkdir -p "$1"
        fi
        exit_on_error
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 生成安装路径名后缀
install_suffix(){
    if [ $# -eq 1 ]
    then
        typeset -l os processor
        os=`uname -s`
        processor=`uname -p`
        echo $1_${os}_${processor}
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 如果文件存在且checksum与$2指定的md5相等则返回 1,否则返回0
# $1 待检查的文件路径
# $2 md5校验码
need_download(){
    if [ $# -eq 2 ]
    then
        if [ -f $1 ]; then
            echo "File already exists. Checking md5..."
            os=`uname -s`
            if [ "$os" = "Linux" ]; then
                checksum=`md5sum $1 | awk '{ print $1 }'`
            elif [ "$os" = "Darwin" ]; then
                checksum=`cat $1 | md5`
            fi
            if [ "$checksum" = "$2" ]; then
                echo "Checksum is correct. No need to download $1."
                return 1
            else
                echo "Checksum is incorrect. Need to download again $1"
            fi
        else
            return 0
        fi
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
###############下载bzip2
BZIP2_VERSION="1.0.6"
BZIP2_FOLDER="bzip2-$BZIP2_VERSION"
BZIP2_PACKAGE="$BZIP2_FOLDER.tar.gz"
if need_download $BZIP2_PACKAGE "00b516f4704d4a7cb50a1d97e6e8e15b"
then
    echo "(下载源码)downloading bzip2 $BZIP2_VERSION source"
    wget --no-check-certificate http://www.bzip.org/$BZIP2_VERSION/$BZIP2_PACKAGE
    exit_on_error
fi
remove_if_exist $BZIP2_FOLDER
tar zxvf $BZIP2_PACKAGE
exit_on_error
bzip2_makefile=$BZIP2_FOLDER/Makefile
# 使用sed编译器修改Makefile,在编译选项中增加-fPIC参数
# 判断CFLAGS中是否已经有-fPIC选项,如果没有就添加,没有则不修改
if [ -z "$(grep '^\s*CFLAGS\s*=' $bzip2_makefile | grep '\-fPIC')" ] 
then
    echo "add -fPIC to CFLAGS in $bzip2_makefile"
    sed -i -r 's/(^\s*CFLAGS\s*=)(.*$)/\1-fPIC \2/' $bzip2_makefile
    exit_on_error
else
    echo "found -fPIC in CFLAGS,no need modify $bzip2_makefile"
fi
###################编译bzip2
#安装路径
install_folder=$(dirname $(readlink -f $0))/release/$(install_suffix bzip2)
echo install_folder:$install_folder

pushd bzip2-1.0.6
clearn_folder $install_folder
make clean
exit_on_error
make -j8
make install PREFIX=$install_folder 
exit_on_error
popd
————————————————
版权声明:本文为CSDN博主「10km」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/10km/article/details/72870031

5.安装libpng

下载地址:https://nchc.dl.sourceforge.net/project/libpng/libpng16/1.6.37/libpng-1.6.37.tar.gz

tar -xf libpng-1.6.37.tar.gz 
cd libpng-1.6.37
cp scripts/makefile.linux makefile  
vim makefile
make && make install

makefile中找到zlib相关的两行,修改成实际的zlib/lib和zlib/include,再找到prefix项,修改为你想prefix的目录中,
要解决这个问题最重要的是要了解到libpng库是不需要configure的,因为在scripts目录下已经提供makefile,所以没有必要使用configure来创建makefle文件了。

6.安装libjpeg

下载地址:https://nchc.dl.sourceforge.net/project/libjpeg-turbo/2.0.2/libjpeg-turbo-2.0.2.tar.gz

mkdir buil
cd buil/
cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/app/zabbix/service/libjpeg ..
make && make install

7.安装freetype

下载地址: https://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.gz

./configure --prefix=/app/zabbix/service/freetype_2.9/

8.安装icu4c

下载地址:wget -c -t 20 https://www.ivdone.top/wordpress/pic/p1551/icu4c-52_2-src.tgz

tar -xf icu4c-52_2-src.tgz 
cd icu/source/
ls -lrt
./runConfigureICU --help
./runConfigureICU Linux --prefix=/app/zabbix/service/icu4c
make -j 8
make install

9.安装oniguruma

cd ~/oniguruma-6.9.5
./autogen.sh
如果遇到如下错误提示,

src/Makefile.am:19: error: Libtool library used but 'LIBTOOL' is undefined
可通过如下命令解决依赖关系,

yum install libtool
./configure --prefix=/app/zabbix/service/oniguruma

11. 安装 libxslt

依赖 libxml2-2.9.10

下载地址:http://xmlsoft.org/sources/libxslt-1.1.34.tar.gz

./configure --prefix=/app/zabbix/service/libxslt

12.安装libzip

下载地址:wget https://libzip.org/download/libzip-1.7.3.tar.gz

tar -xvf libzip-1.7.3.tar.gz
yum -y install epel-release
yum install jsoncpp
yum install -y cmake3

echo '[group_kdesig-cmake3_EPEL]
name=Copr repo for cmake3_EPEL owned by @kdesig
baseurl=https://copr-be.cloud.fedoraproject.org/results/@kdesig/cmake3_EPEL/epel-7-$basearch/
type=rpm-md
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://copr-be.cloud.fedoraproject.org/results/@kdesig/cmake3_EPEL/pubkey.gpg
repo_gpgcheck=0
enabled=1
enabled_metadata=1' >> /etc/yum.repos.d/cmake3.repo

cd libzip-1.7.3
cmake源码安装方法
安装libzip
mkdir build && cd build
cmake3 ..
make  && make install PREFIX=/app/zabbix/service/libzip

13.安装libcurl

wget https://curl.haxx.se/download/curl-7.65.3.tar.gz
./configure --prefix=/app/zabbix/service/libcurl

14.安装gd

下载地址:wget https://github.com/libgd/libgd/releases/download/gd-2.3.0/libgd-2.3.0.tar.gz

tar xvfz libgd-2.3.0.tar.gz
cd libgd-2.3.0
./configure --prefix=/app/zabbix/service/libgd
make && make install

15.安装ldap

#编译:DB
cd build_unix/
ls -lrt
../dist/configure --prefix=/app/zabbix/service/db

#编译ldap
./configure --prefix=/app/zabbix/service/ldap --enable-syslog --enable-modules --enable-debug --with-tls CPPFLAGS=-I/app/zabbix/service/db/include/ LDFLAGS=-L/app/zabbix/service/db/lib/

BerkeleyDB安装完毕之后继续安装OpenLdap,但是configure时又出如下问题:
configure: error: BDB/HDB: BerkeleyDB not available

BerkeleyDB已经安装好了,为什么仍然出现这个错误呢? 继续Google,发现解决方法如下:
$export CPPFLAGS="-I/usr/local/BerkeleyDB/include"
$export LDFLAGS="-L/usr/local/BerkeleyDB/lib"

需要注意的是上面两个命令需要以root身份执行。
执行完之后,继续configurate,这次还是有错,这次错误变成了:
configure: error: Berkeley DB version mismatch

增加下面一行之后,安装正确.
export LD_LIBRARY_PATH="/usr/local/BerkeleyDB/lib"

16.编译libsodium

wget -N --no-check-certificate https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz

17.安装Argon2

下载rpm包 rpm -ivh 安装

rpm -ivh libargon2-devel-20161029-3.el7.x86_64.rpm
rpm -ivh libargon2-20161029-3.el7.x86_64.rpm

18.编译libiconv

./configure --prefix=/app/zabbix/service/libiconv

19.编译php

php编译参考文档

https://www.php.net/manual/zh/migration74.other-changes.php#migration74.other-changes.pkg-config
加载链接库

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/libxml2_2.9.1/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/openssl_1.1.1j/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/krb5_1.19.1/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/sqlite3/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/zlib-1.2.11/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/libpng/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/freetype_2.9/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/icu4c/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/libxslt/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/oniguruma/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/libzip/lib64/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/libcurl/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/libgd/lib/pkgconfig"
export LD_LIBRARY_PATH="/app/zabbix/service/libjpeg/lib"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/libsodium/lib/pkgconfig"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/argon2/lib/x86_64-linux-gnu/pkgconfig"

export LD_LIBRARY_PATH="/app/zabbix/service/libzip/lib64"
//设置动态库路径
cd ~ 
echo export LD_LIBRARY_PATH=/usr/local/lib >> .bashrc

#export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/app/zabbix/service/libjpeg/lib64/pkgconfig"

在编译完gd库等相应文件后,编译php文件的过程中提示一下错误;

configure: error: GD build test failed. Please check the config.log for details.

解决方法:

把php的编译参数:

--with-gd

修改为:

--with-external-gd=/app/zabbix/service/libgd

设置变量并开始源码编译:

./configure \
--prefix=/app/zabbix/php \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-fpm \
--enable-opcache \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv=/app/zabbix/service/libiconv/lib \
--with-zlib=/app/zabbix/service/zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--with-external-gd=/app/zabbix/service/libgd \
--enable-gd-jis-conv \
--with-jpeg=/app/zabbix/service/libjpeg \
--with-freetype \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--with-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--with-pear \
--enable-maintainer-zts \
--with-ldap=/app/zabbix/service/ldap \
--without-gdbm \
--with-iconv \
--with-xsl \
--with-sodium \
--enable-exif \
--with-password-argon2 \
--enable-ctype



creating libtool
appending configuration tag "CXX" to libtool
checking for ld used by g++... /usr/bin/ld -m elf_x86_64
checking if the linker (/usr/bin/ld -m elf_x86_64) is GNU ld... yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking for g++ option to produce PIC... -fPIC
checking if g++ PIC flag -fPIC works... yes
checking if g++ static flag -static works... no
checking if g++ supports -c -o file.o... yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking dynamic linker characteristics... GNU/Linux ld.so
(cached) (cached) checking how to hardcode library paths into programs... immediate

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: main/php_config.h is unchanged
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 -j4 && make install

二、配置

配置php.ini文件:

# 源码目录中的 php.ini-development
# php.ini-production 测试 
# php.ini-development 生产
cp php.ini-development /php安装目录/etc/php.ini 
cp /php安装目录/etc/php-fpm.conf.default /php安装目录/etc/php-fpm.conf
cp /php安装目录/etc/php-fpm.d/www.conf.default /php安装目录/etc/php-fpm.d/www.conf

修改 php.ini 相关参数:

$ vim /php安装目录/php/etc/php.ini
expose_php = Off
short_open_tag = ON
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 32M
date.timezone = Asia/Shanghai
mbstring.func_overload=2
extension = "/php安装目录/php/lib/php/extensions/no-debug-zts-20160303/ldap.so"

设置 OPcache 缓存:

[opcache]
zend_extension=/PHP安装目录/php/lib/php/extensions/no-debug-zts-20160303/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

设置php安全函数:

vim /PHP安装目录/php/etc/php.ini

默认值:

disable_functions =

修改为:

disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

或通配:

disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru

配置www.conf

取消以下注释并修改优化其参数:

listen = /php安装目录/run/www/php-cgi.sock
listen.owner = www
listen.group = www
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
listen.backlog = -1
pm.max_children = 180
pm.start_servers = 50
pm.min_spare_servers = 50
pm.max_spare_servers = 180
request_terminate_timeout = 120
request_slowlog_timeout = 50
slowlog = var/log/slow.log

创建php-cgi.sock存放目录

mkdir /php安装目录/run/www/
chown -R www:www /var/run/www

配置php-fpm.conf

取下以下注释并填写完整路径:

pid = /php安装目录/run/php-fpm.pid

至此php7已经安装完成。

说明:禁用php函数,如果程序需要这些函数,可以取消禁止,新手建议忽略此步骤。

创建system系统单元文件php-fpm启动脚本:

$ vim /usr/lib/systemd/system/php-fpm.service

添加如下变量内容:

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/app/zabbix/php_7.4.16/run/php-fpm.pid
ExecStart=/app/zabbix/php_7.4.16/sbin/php-fpm --nodaemonize --fpm-config /app/zabbix/php_7.4.16/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
# 设置第三方包环境变量
cat >>/etc/ld.so.conf <<EOF
/app/zabbix/service/libxml2_2.9.1/lib
/app/zabbix/service/openssl_1.1.1j/lib
/app/zabbix/service/krb5_1.19.1/lib
/app/zabbix/service/sqlite3/lib
/app/zabbix/service/zlib-1.2.11/lib
/app/zabbix/service/libpng/lib
/app/zabbix/service/freetype_2.9/lib
/app/zabbix/service/icu4c/lib
/app/zabbix/service/libxslt/lib
/app/zabbix/service/oniguruma/lib
/app/zabbix/service/libzip/lib64
/app/zabbix/service/libcurl/lib
/app/zabbix/service/libgd/lib
/app/zabbix/service/libjpeg/lib
/app/zabbix/service/libsodium/lib
/app/zabbix/service/argon2/lib
EOF

# 执行 ldconfig 生效
ldconfig

启动php-fpm服务并加入开机自启动:

$ systemctl enable php-fpm.service
$ systemctl restart php-fpm.service