本文适用:rhel8系列,或同类系统(CentOS8,AlmaLinux8,RockyLinux8等)
文档形成时期:2023年
因系统版本不同,构建部署应略有差异,但本文未做细分,对稍有经验者应不存在明显障碍。
因软件世界之复杂和个人能力之限,难免疏漏和错误,欢迎指正。
背景
不同时期因各种原因经常产生部署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}
依赖包和必要文件准备
#安装扩展源并启用PowerTools
dnf install epel-release
dnf config-manager --set-enabled PowerTools # 在CentOS8上,oniguruma-devel,aspell-devel位于PowerTools,
dnf config-manager --set-enabled powertools # 在Almalinux8上,oniguruma-devel,aspell-devel位于powertools,和CentOS8不同的是小写的。
#如果不启用,会提示:No match for argument: oniguruma-devel,No match for argument: aspell-devel
#通过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
说明:
- PHP配置建议参考生产环境的常用配置,准备一个比较通用的;
- opcache.so和redis.so是同环境下编译好的模块文件,这样可省略在RPM包构建过程中执行编译;
php-8.1.20-el8.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 <group@php.net>
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 AlmaLinux8.8.
查看make参数:
rpm --eval %{?_smp_mflags}
可以看到是根据核线程数量来编译的。
构建
QA_RPATHS=$(( 0x0001|0x0002 )) rpmbuild -bb SPECS/php-8.1.20-el8.spec
构建成功的包如下:
php-8.1.20-custom.el8.x86_64.rpm
php-debugsource-8.1.20-custom.el8.x86_64.rpm
php-debuginfo-8.1.20-custom.el8.x86_64.rpm
目标服务器安装
#仍要安装依赖的扩展源,需启用PowerTools
dnf install epel-release
dnf config-manager --set-enabled PowerTools # CentOS8
dnf config-manager --set-enabled powertools # Almalinux8文章来源:https://www.toymoban.com/news/detail-783519.html
dnf localinstall php-*.rpm
或
dnf localinstall php-8.1.20-custom.el8.x86_64.rpm php-debugsource-8.1.20-custom.el8.x86_64.rpm php-debuginfo-8.1.20-custom.el8.x86_64.rpm文章来源地址https://www.toymoban.com/news/detail-783519.html
支持的模块
/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]
到了这里,关于构建基于RHEL8系列(CentOS8,AlmaLinux8,RockyLinux8等)的支持63个常见模块的PHP8.1.20的RPM包的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!