centos7.9 源码编译安装 php8 以及扩展 redis 和 sodium安装

近期文章:

一直以来装的最多的是 php7 版本,最近搞新项目,项目组要求安装 php8 版本,于是在阿里云购买了 一台centos7机器,着手安装,记录下整个过程以及遇到的问题和解决

拿到新机器,首先将系统更新一下

yum update

接下来 php8源码编译安装

安装 PHP8

预先安装必要的依赖

yum install -y wget gcc gcc-c++ autoconf automake libtool make libxml2 libxml2-devel openssl openssl-devel sqlite-devel 

下载解压 php8 安装包:

useradd www -d /usr/local/data/www -s /sbin/nologin mkdir /usr/local/data/soft cd /usr/local/data/soft wget https://www.php.net/distributions/php-8.2.16.tar.gz tar -zxvf php-8.2.16.tar.gz cd php-8.2.16 

预编译configure

./configure --prefix=/usr/local/data/php-8.2.16 \ --with-config-file-path=/usr/local/data/php-8.2.16/etc \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --enable-mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --enable-mysqlnd-compression-support \ --with-zlib \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --with-curl \ --enable-mbregex \ --enable-mbstring \ --enable-intl \ --enable-ftp \ --enable-gd-jis-conv \ --with-openssl \ --with-mhash \ --enable-pcntl \ --enable-sockets \ --enable-soap \ --with-gettext \ --enable-fileinfo \ --enable-opcache \ --with-pear \ --with-ldap=shared \ --without-gdbm

报错解决

configure: error: Cannot find ldap libraries in /usr/lib 

解决

cp -frp /usr/lib64/libldap* /usr/lib/  

重新configure

安装

make -j 4 && make install 

创建快捷方式

ln -s /usr/local/data/php-8.2.16 /usr/local/data/php ln -s /usr/local/data/php/bin/php /usr/bin/php #查看版本 php -v PHP 8.2.16 (cli) (built: Mar  7 2024 17:43:51) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.16, Copyright (c) Zend Technologies 

php配置文件优化

cp php.ini-production /usr/local/data/php/etc/php.ini cd /usr/local/data/php/etc/ cp php-fpm.conf.default php-fpm.conf cd /usr/local/data/php/etc/php-fpm.d cp www.conf.default www.conf cd /usr/local/data/php/etc/ #修改php-fpm.conf sed -i 's#;error_log = log/php-fpm.log#error_log = log/php-fpm.log#g' php-fpm.conf sed -i 's/;rlimit_files = 1024/rlimit_files = 65535/g' php-fpm.conf sed -i 's#;log_level = notice#log_level = error#g' php-fpm.conf sed -i 's#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#g' php-fpm.conf sed -i 's#;emergency_restart_threshold = 0#emergency_restart_threshold = 60#g' php-fpm.conf sed -i 's#;emergency_restart_interval = 0#emergency_restart_interval = 60#g' php-fpm.conf sed -i 's#;process_control_timeout = 0#process_control_timeout = 5#g' php-fpm.conf #修改www.conf sed -i 's#pm = dynamic#pm = static#g' php-fpm.d/www.conf sed -i 's#pm.max_children = 5#pm.max_children = 150#g' php-fpm.d/www.conf sed -i 's#;pm.status_path = /status#pm.status_path = /PhpStatus#g' php-fpm.d/www.conf sed -i 's#;slowlog = log/$pool.log.slow#slowlog = var/log/$pool.log.slow#g' php-fpm.d/www.conf sed -i 's#;request_slowlog_timeout = 0#request_slowlog_timeout = 10#g' php-fpm.d/www.conf sed -i 's/memory_limit = 128M/memory_limit = 512M/g' php.ini sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#g' php.ini sed -i 's#expose_php = On#expose_php = Off#g' php.ini 

开机启动

vim /etc/init.d/php-fpm 

php 启动脚本

tips: 修改prefix路径即可

#! /bin/sh  ### BEGIN INIT INFO # Provides:          php-fpm # Required-Start:    $remote_fs $network # Required-Stop:     $remote_fs $network # Default-Start:     2 3 4 5 # Default-Stop:      0 1 6 # Short-Description: starts php-fpm # Description:       starts the PHP FastCGI Process Manager daemon ### END INIT INFO  prefix=/usr/local/data/php exec_prefix=${prefix}  php_fpm_BIN=${exec_prefix}/sbin/php-fpm php_fpm_CONF=${prefix}/etc/php-fpm.conf php_fpm_PID=${prefix}/var/run/php-fpm.pid  php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"  wait_for_pid () { 	try=0  	while test $try -lt 35 ; do  		case "$1" in 			'created') 			if [ -f "$2" ] ; then 				try='' 				break 			fi 			;;  			'removed') 			if [ ! -f "$2" ] ; then 				try='' 				break 			fi 			;; 		esac  		echo -n . 		try=`expr $try + 1` 		sleep 1  	done  }  case "$1" in 	start) 		echo -n "Starting php-fpm "  		$php_fpm_BIN --daemonize $php_opts  		if [ "$?" != 0 ] ; then 			echo " failed" 			exit 1 		fi  		wait_for_pid created $php_fpm_PID  		if [ -n "$try" ] ; then 			echo " failed" 			exit 1 		else 			echo " done" 		fi 	;;  	stop) 		echo -n "Gracefully shutting down php-fpm "  		if [ ! -r $php_fpm_PID ] ; then 			echo "warning, no pid file found - php-fpm is not running ?" 			exit 1 		fi  		kill -QUIT `cat $php_fpm_PID`  		wait_for_pid removed $php_fpm_PID  		if [ -n "$try" ] ; then 			echo " failed. Use force-quit" 			exit 1 		else 			echo " done" 		fi 	;;  	status) 		if [ ! -r $php_fpm_PID ] ; then 			echo "php-fpm is stopped" 			exit 0 		fi  		PID=`cat $php_fpm_PID` 		if ps -p $PID | grep -q $PID; then 			echo "php-fpm (pid $PID) is running..." 		else 			echo "php-fpm dead but pid file exists" 		fi 	;;  	force-quit) 		echo -n "Terminating php-fpm "  		if [ ! -r $php_fpm_PID ] ; then 			echo "warning, no pid file found - php-fpm is not running ?" 			exit 1 		fi  		kill -TERM `cat $php_fpm_PID`  		wait_for_pid removed $php_fpm_PID  		if [ -n "$try" ] ; then 			echo " failed" 			exit 1 		else 			echo " done" 		fi 	;;  	restart) 		$0 stop 		$0 start 	;;  	reload)  		echo -n "Reload service php-fpm "  		if [ ! -r $php_fpm_PID ] ; then 			echo "warning, no pid file found - php-fpm is not running ?" 			exit 1 		fi  		kill -USR2 `cat $php_fpm_PID`  		echo " done" 	;;  	configtest) 		$php_fpm_BIN -t 	;;  	*) 		echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}" 		exit 1 	;;  esac 
/etc/init.d/php-fpm Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status|configtest}

启动 php8

/etc/init.d/php-fpm start 

加入开机启动

chmod +x /etc/init.d/php-fpm vim /etc/rc.local 添加/etc/init.d/php-fpm start 

php8 扩展安装

安装 redis 扩展

redis扩展包下载地址:

prefix=/usr/local/data/php-8.2.16 php_BIN=${prefix}/bin php_CONF=${php_BIN}/php-config php_INI=${prefix}/etc/php.ini phpize=${php_BIN}/phpize  php_redis(){         redis_version=redis-6.0.2         redis_pkg=$redis_version.tgz         #检查依赖,由于在centos6装过,这里解决autoconf版本问题,系统版本高自动跳过         cd $soft_home         ac_version=`autoconf -V|head -1|awk '{print $NF}'`                 if [ `echo "$ac_version < 2.69"|bc` -eq 1 ];then                         #rpm -e --nodeps autoconf-$ac_version                         wget -nc http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz                         tar zxvf autoconf-2.69.tar.gz                         cd autoconf-2.69                         ./configure --prefix=/usr/                         make && make install                 fi         #安装redis         cd $soft_home         wget -nc -q http://pecl.php.net/get/$redis_pkg         tar zxf   $redis_pkg         cd $soft_home/$redis_version         #调用phpize         $phpize         ./configure  --with-php-config=$php_CONF         make -j 4 && make install         sed -i '$a extension=redis.so' $php_INI } 

安装 sodium 扩展

错误安装方式

一开始在php源码安装包ext/sodium下装,但一直报错,如下所示

cd php-8.2.16/ext/sodium /usr/local/data/php/bin/phpize ./configure  --with-php-config=/usr/local/data/php-8.2.16/bin/php-config ... #报错信息如下 checking for sodium support... yes, shared ./configure: line 4103: syntax error near unexpected token `LIBSODIUM,' ./configure: line 4103: `  PKG_CHECK_MODULES(LIBSODIUM, libsodium >= 1.0.8)' 

已经安装了libsodium 和 libsodium-devel

yum -y install epel-release libsodium libsodium-devel  Package epel-release-7-14.noarch already installed and latest version Package libsodium-1.0.18-1.el7.x86_64 already installed and latest version Package libsodium-devel-1.0.18-1.el7.x86_64 already installed and latest version Nothing to do  

尝试各种解决办法,源码编译libsodium-1.0.18 也不行,无语了,果断放弃了,建议直接用下面正确方式,轻松通过

正确安装方式

yum install -y libsodium libsodium-devel wget http://pecl.php.net/get/libsodium-2.0.23.tgz tar zxvf libsodium-2.0.23.tgz cd libsodium-2.0.23 /usr/local/data/php/bin/phpize ./configure  --with-php-config=/usr/local/data/php-8.2.16/bin/php-config make -j 4 && make install 

没报错,一下子就好了

下面整理成脚本

prefix=/usr/local/data/php-8.2.16 php_BIN=${prefix}/bin php_CONF=${php_BIN}/php-config phpize=${php_BIN}/phpize php_INI=${prefix}/etc/php.ini  php_sodium(){   yum install -y libsodium libsodium-devel   wget http://pecl.php.net/get/libsodium-2.0.23.tgz #最新   tar zxvf libsodium-2.0.23.tgz   cd libsodium-2.0.23   $phpize   ./configure  --with-php-config=$php_CONF   make -j 4 && make install   sed -i '$a extension=sodium.so' $php_INI }