构建基于RHEL9系列(CentOS9,AlmaLinux9,RockyLinux9等)的支持63个常见模块的PHP8.1.20的RPM包

本文适用:rhel9系列,或同类系统(CentOS9,AlmaLinux9,RockyLinux9等)
文档形成时期:2023年
因系统版本不同,构建部署应略有差异,但本文未做细分,对稍有经验者应不存在明显障碍。
因软件世界之复杂和个人能力之限,难免疏漏和错误,欢迎指正。

文章目录

  • 背景
  • 环境准备
  • 依赖包和必要文件准备
  • php-8.1.20-el9.spec内容
  • 构建
  • 目标服务器安装
  • 支持的模块

背景

不同时期因各种原因经常产生部署LNMP环境的需求,某些场景下需要自定义软件,比如参数、模块、安装路径,或多个版本共存,不能采用Docker等容器环境,采用自主构建RPM包便成了比较快捷的方式之一。

环境准备

yum install rpmdevtools
#创建目录
rpmdev-setuptree
#或手动创建目录:

mkdir rpmbuild-php-8.1.20; cd rpmbuild-php-8.1.20
mkdir -p ./{BUILD,RPMS,SOURCES,SPECS,SRPMS}

依赖包和必要文件准备

#安装扩展源并启用crb(如果不启用,很多依赖得手动装)
dnf install epel-release
dnf config-manager --set-enabled crb

#通过dnf安装依赖
dnf -y install wget libxml2 libxml2-devel sqlite-devel bzip2-devel libcurl-devel libffi-devel libpng-devel libwebp-devel libjpeg-devel freetype-devel oniguruma oniguruma-devel libzip libzip-devel unixODBC unixODBC-devel freetds freetds-devel libtool-ltdl libtool-ltdl-devel gmp-devel libicu-devel openldap openldap-devel libpq libpq-devel aspell aspell-devel libtidy libtidy-devel libxslt libxslt-devel readline readline-devel libsodium libsodium-devel libjpeg-turbo-utils libXpm-devel systemd-devel
openssl openssl-devel

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

目录rpmbuild/SOURCES/下面的文件:
opcache.so php-8.1.20.tar.gz php-command.ini php-fpm.conf php.ini redis.so www.conf
构建基于RHEL9系列(CentOS9,AlmaLinux9,RockyLinux9等)的支持63个常见模块的PHP8.1.20的RPM包_第1张图片

说明:

  • PHP配置建议参考生产环境的常用配置,准备一个比较通用的;
  • opcache.so和redis.so是同环境下编译好的模块文件,这样可省略在RPM包构建过程中执行编译;

php-8.1.20-el9.spec内容

Name: php
Summary: PHP: Hypertext Preprocessor
Group: Development/Languages
Version: 8.1.20
Release: custom%{?dist}
Source: php-8.1.20.tar.gz
#Icon: php.gif
URL: http://www.php.net/
Packager: PHP Group 
License: GPL


%define _prefix  /opt/php81
Prefix:     %{_prefix}


%description
Copyright: The PHP license (see "LICENSE" file included in distribution)
PHP is an HTML-embedded scripting language. Much of its syntax is
borrowed from C, Java and Perl with a couple of unique PHP-specific
features thrown in. The goal of the language is to allow web
developers to write dynamically generated pages quickly.



%prep



%setup -q



%build
set -x
# ./buildconf
./configure --prefix=%{_prefix} --sysconfdir=%{_prefix}/etc --with-config-file-path=%{_prefix}/etc --with-openssl --with-zlib --with-bz2 --with-curl --enable-bcmath --enable-gd --with-webp --with-jpeg --with-xpm --with-freetype --with-mhash --enable-mbstring --with-imap-ssl --enable-exif --with-ffi --with-zip --enable-sockets --with-pcre-jit --enable-fpm --with-pdo-mysql --enable-pcntl --with-gettext --with-gmp --with-fpm-user=www --with-fpm-group=www --with-fpm-systemd --enable-sysvmsg --enable-sysvsem --enable-sysvshm --with-mysqli --enable-mysqlnd --with-pdo-dblib --with-unixODBC --with-pdo-odbc=unixODBC,/usr/ --with-pgsql --with-pdo-pgsql --enable-ftp --enable-dba --enable-calendar --enable-intl --with-ldap --with-pspell --enable-shmop --enable-soap --with-tidy --with-xsl --with-readline --with-sodium

sed -ri "/^EXTRA_LIBS/s/(.*)/\1 -llber/" Makefile



make %{?_smp_mflags}





#
# Installation section
#

%install
[ %{buildroot} != "/" ] && rm -rf %{buildroot}
# %__make install DESTDIR="%{buildroot}"  # 经实践,该参数不行,要使用下面的INSTALL_ROOT。
make INSTALL_ROOT="%{buildroot}" install

%__install -c -d -m 755 "%{buildroot}/opt"
%__install -c -d -m 755 "%{buildroot}%{_prefix}/etc"
%__install -c -d -m 755 "%{buildroot}%{_prefix}/etc/php-fpm.d"
%__install -c -d -m 755 "%{buildroot}/usr/lib/systemd/system"
%__install -c -d -m 755 "%{buildroot}%{_prefix}/lib/php/extensions/no-debug-non-zts-20210902"
cp -f %_sourcedir/{php-command.ini,php.ini,php-fpm.conf} "%{buildroot}%{_prefix}/etc/"
cp -f %_sourcedir/www.conf "%{buildroot}%{_prefix}/etc/php-fpm.d/"
cp -f %_builddir/%{name}-%{version}/sapi/fpm/php-fpm.service "%{buildroot}/usr/lib/systemd/system/php-fpm.service"
cp -f %_sourcedir/redis.so "%{buildroot}%{_prefix}/lib/php/extensions/no-debug-non-zts-20210902/"
cp -f %_sourcedir/opcache.so "%{buildroot}%{_prefix}/lib/php/extensions/no-debug-non-zts-20210902/"


#
# Clean section
#

%clean
[ %{buildroot} != "/" ] && rm -rf "%{buildroot}"




%files
%defattr(-,root,root)
%{_prefix}
# 不在prefix路径下的文件需要单独指定
/usr/lib/systemd/system/php-fpm.service







%post
if [ $1 == 1 ];then
    groupadd www -g 319 2> /dev/null
    useradd -s /sbin/nologin -M www -u 319 -g 319 2> /dev/null
    mkdir /home/www 2> /dev/null
    chown www:www /home/www 2> /dev/null
    
    systemctl daemon-reload
    systemctl enable php-fpm
fi






%preun
if [ "$1" = 0 ]
then
    systemctl disable php-fpm
    systemctl stop php-fpm
    # userdel www
    cp %{_prefix}/etc/php.ini /opt/php.ini.rpmsave-`date +"%%Y%%m%%d-%%H%%M%%S"`
    cp %{_prefix}/etc/php-command.ini /opt/php-command.ini.rpmsave-`date +"%%Y%%m%%d-%%H%%M%%S"`
    cp %{_prefix}/etc/php-fpm.conf /opt/php-fpm.conf.rpmsave-`date +"%%Y%%m%%d-%%H%%M%%S"`
    cp %{_prefix}/etc/php-fpm.d/www.conf /opt/www.conf.rpmsave-`date +"%%Y%%m%%d-%%H%%M%%S"`
fi




%postun
if [ "$1" = 0 ]
then
    systemctl disable php-fpm
    rm -f /usr/lib/systemd/system/php-fpm.service
    rm -rf /opt/php81
    echo "%{name}-%{version}-%{release}已经卸载."
fi





%changelog
* Mon Dec 11 2023 N
- For the first time, Custom made PHP8.1.20 in AlmaLinux9.2.

和RHEL8的不同的是:
date +“%Y%m%d-%H%M%S”
改为了:
date +“%%Y%%m%%d-%%H%%M%%S”

查看make参数:
rpm --eval %{?_smp_mflags}
可以看到是根据核线程数量来编译的。

构建

QA_RPATHS=$(( 0x0001|0x0002 )) rpmbuild -ba --define "_topdir `pwd`" --nodebuginfo SPECS/php-8.1.20-el9.spec

构建成功的包如下:
php-8.1.20-custom.el9.x86_64.rpm

目标服务器安装

#仍要安装依赖的扩展源,需启用crb
dnf install epel-release
dnf config-manager --set-enabled crb

dnf localinstall php-8.1.20-custom.el9.x86_64.rpm

支持的模块

/opt/php81/bin/php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
exif
FFI
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
intl
json
ldap
libxml
mbstring
mysqli
mysqlnd
odbc
openssl
pcntl
pcre
PDO
pdo_dblib
pdo_mysql
PDO_ODBC
pdo_pgsql
pdo_sqlite
pgsql
Phar
posix
pspell
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tidy
tokenizer
xml
xmlreader
xmlwriter
xsl
zip
zlib

[Zend Modules]

你可能感兴趣的:(php,linux,软件构建,运维)