近期文章:
近期新项目,php版本要求最新版本 8.3.3,node版本也要 18+,于是在阿里云购买Rocky Linux 9.3 4c8G的机器配置用来安装,这里记录一下装php的整个过程和遇到的一些问题
查看系统版本
cat /etc/redhat-release Rocky Linux release 9.3 (Blue Onyx)
更新系统软件包
yum update #重启 reboot
源码编译安装php8.3.3
预先安装必要的依赖
yum install -y wget gcc gcc-c++ autoconf automake libtool make libxml2 libxml2-devel openssl openssl-devel sqlite-devel libpng libpng-devel libpsl libpsl-devel libicu-devel libcurl-devel
创建 www 用户
useradd www -d /usr/local/data/www -s /sbin/nologin
下载 php-8.3.3包
mkdir /usr/local/data/soft cd /usr/local/data/soft wget https://www.php.net/distributions/php-8.3.3.tar.gz tar zxvf php-8.3.3.tar.gz cd php-8.3.3
configure预编译
./configure --prefix=/usr/local/data/php-8.3.3 \ --with-config-file-path=/usr/local/data/php-8.3.3/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 \ --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 \ --with-curl \ --enable-ctype
预编译报错解决:
报错1:configure: error: Cannot find ldap.h
yum install openldap openldap-devel -y 重新预编译
报错2:configure: error: Cannot find ldap libraries in /usr/lib
cp -frp /usr/lib64/libldap* /usr/lib/ 重新预编译
报错3:Package ‘oniguruma’, required by ‘virtual:world’, not found,完整报错信息如下
checking for oniguruma... no configure: error: Package requirements (oniguruma) were not met: Package 'oniguruma', required by 'virtual:world', not found
这里是缺少了 oniguruma-devel 包
yum install oniguruma oniguruma-devel
yum已经安装了oniguruma,但oniguruma-devel包提示没有找到,于是找到了下面快捷安装方式
好多教程都是去下载 rpm包或源码编译安装,不推荐,费时
yum --enablerepo=devel -y install oniguruma-devel #或 dnf --enablerepo=devel -y install oniguruma-devel
安装方式当然也是在这里找到的访问 https://pkgs.org/
,搜索 oniguruma-devel
关键字即可
点进去,记得向下滑,有时候还要输入通过图片验证才行
再次进到 php源码目录编译就正常了,接下来就可以 make了
make -j 4 && make install
创建 php 软连接
ln -s /usr/local/data/php-8.3.3 /usr/local/data/php
优化 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 #修改php.ini 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 sed -i 's#disable_functions =#disable_functions = phpinfo#g' php.ini
php8进程管理脚本
cat /etc/init.d/php-fpm
#! /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 configtest #测试配置文件是否正确 /etc/init.d/php-fpm start /etc/init.d/php-fpm restart /etc/init.d/php-fpm reload /etc/init.d/php-fpm status
安装 php8.3.3 扩展
安装 redis
wget -nc -q http://pecl.php.net/get/redis-6.0.2.tgz tar xvf redis-6.0.2.tgz cd redis-6.0.2 /usr/local/data/php/bin/phpize ./configure --with-php-config=/usr/local/data/php/bin/php-config make -j 4 && make install sed -i '$a extension=redis.so' /usr/local/data/php/etc/php.ini
安装 gd
cd /usr/local/data/soft/php-8.3.3/ext/gd /usr/local/data/php/bin/phpize ./configure --with-php-config=/usr/local/data/php/bin/php-config make -j 4 && make install sed -i '$a extension=gd.so' /usr/local/data/php/etc/php.ini
安装 sodium
不要去 php源码路径ext/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 /usr/local/data/php/bin/phpize ./configure --with-php-config=/usr/local/data/php/bin/php-config make -j 4 && make install sed -i '$a extension=sodium.so' /usr/local/data/php/etc/php.ini
验证扩展是否配置成功
php --ri redis php --ri gd php --ri sodium 或者 php -m|egrep "redis|gd|sodium"