编译安装php
前置准备
这里的可能不全,每个人安装的模块不一致,依赖也不不相同,按实际情况调整
yum install libxml2 -y
yum install libxml2-devel -y
yum install openssl-devel -y
yum install sqlite-devel -y
yum install libcurl-devel -y
yum install -y autoconf automake libtool
# 编译安装oniguruma依赖用到,已安装oniguruma可以不管
cd /usr/src/oniguruma-master && sh autogen.sh
#sh autogen.sh是为了生成configure,默认是没有configure的
./configure --prefix=/usr --libdir=/lib64
安装oniguruma
github oniguruma
https://github.com/kkos/oniguruma
解压后进入文件
#生成配置脚本configure,默认是没有这个脚本的
./autogen.sh
# 配置
./configure --prefix=/usr --libdir=/lib64
make && make install
# 卸载
make uninstall
下载php
php下载地址
解压php
以php-8.3.17.tar.gz为例子
tar xvf php-8.3.17.tar.gz
#解压后的到php-8.3.17
安装php
#!/bin/bash
function installphp() {
# ==================================================
# 可配置变量区(根据实际需求调整)
# ==================================================
# 安装路径配置
read -p "请输入PHP安装路径(默认:/usr/local/php):" prefix_dir
if [ -z "$prefix_dir" ]; then
prefix_dir="/usr/local/php" # PHP 安装主目录
fi
mkdir -p "$prefix_dir"
read -p "请输入PHP-FPM配置目录(默认:/etc/php-fpm):" config_dir
if [ -z "$config_dir" ]; then
config_dir="/etc/php-fpm" # PHP-FPM 配置文件目录
fi
mkdir -p "$config_dir"
# PHP 配置文件路径
read -p "请输入PHP配置文件路径(默认:/etc/php):" php_ini_path
if [ -z "$php_ini_path" ]; then
php_ini_path="/etc/php" # PHP 配置文件路径
fi
mkdir -p "$php_ini_path"
# 扩展配置文件目录
read -p "请输入扩展配置文件目录(默认:/etc/php/conf.d):" ext_conf_dir
if [ -z "$ext_conf_dir" ]; then
ext_conf_dir="/etc/php/conf.d" # 扩展配置文件目录
fi
mkdir -p "$ext_conf_dir"
# 系统服务用户/组
read -p "请输入PHP-FPM服务组(默认:www):" fpm_group
if [ -z "$fpm_group" ]; then
fpm_group="www"
fi
read -p "请输入PHP-FPM服务用户(默认:www):" fpm_user
if [ -z "$fpm_user" ]; then
fpm_user="www"
fi
groupadd "$fpm_group"
useradd -g "$fpm_group" -s /bin/false -M "$fpm_user"
# 编译选项配置文件(用于动态加载模块)
# 提示:将需要动态加载的模块在此文件中通过 --enable-xxx=shared 配置
configure_file="./custom_configure_options.txt"
# ==================================================
# 前期准备工作
# ==================================================
# 进入 PHP 源码目录
read -p "请输入PHP源码路径(输入绝对路径):" php_source_dir
cd "$php_source_dir" || { echo "无法进入 PHP 源码目录"; exit 1; }
echo "正在安装 PHP..."
sleep 2
begin_install
}
function begin_install() {
# ==================================================
# 生成卸载脚本
# ==================================================
echo "正在生成卸载脚本..."
cat > uninstall.sh << EOF
#!/bin/bash
# 停止 PHP-FPM 服务
systemctl stop php-fpm
# 删除用户和组
userdel -r $fpm_user
groupdel $fpm_group
# 卸载 PHP
rm -rf ${config_dir}
rm -rf ${php_ini_path}
rm -rf ${prefix_dir}
rm -rf /etc/systemd/system/php-fpm.service
rm -rf /usr/bin/php
rm -rf /usr/bin/phpize
rm -rf /usr/sbin/php-fpm
echo "PHP 卸载完成"
EOF
# ==================================================
# 配置编译选项
# ==================================================
# 生成自定义编译选项文件(示例配置,根据实际需求修改)
echo "正在生成自定义编译选项文件...需要的模块可用./configure --help查看"
cat > "$configure_file" << EOF
--prefix=${prefix_dir}
--with-config-file-path=${php_ini_path}
--with-config-file-scan-dir=${php_ini_path}/conf.d
--with-fpm-group=${fpm_group}
--with-fpm-user=${fpm_user}
--enable-fpm
--with-openssl
--with-zlib
--with-pcre-jit
--enable-opcache=shared
--with-curl=shared
--enable-bcmath=shared
--enable-gd=shared
--with-mysqli=shared
--with-pdo-mysql=shared
--enable-mbstring=shared
--with-iconv=shared
--enable-exif=shared
--enable-sockets=shared
EOF
# 应用配置并添加动态模块支持
# 注意:这里使用 source 加载变量,实际执行时需要替换变量值
configure_options=$(envsubst < "$configure_file")
# 执行配置命令
./configure $configure_options || {
echo "配置阶段失败,请检查依赖和选项";
exit 1;
}
# ==================================================
# 编译与安装
# ==================================================
make -j$(nproc) || { echo "编译失败"; exit 1; }
sudo make install || { echo "安装失败"; exit 1; }
# ==================================================
# 配置文件处理
# ==================================================
# 复制 php.ini
sudo cp php.ini-production "$php_ini_path/php.ini"
# 配置扩展目录(重要:必须与实际安装路径匹配)
extension_dir=$("$prefix_dir/bin/php-config" --extension-dir)
echo "extension_dir = $extension_dir" | sudo tee -a "$php_ini_path/php.ini"
# 创建扩展配置文件(示例配置,根据需要修改)
sudo tee "$ext_conf_dir/10-opcache.ini" << 'EOF'
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
EOF
sudo tee "$ext_conf_dir/20-common.ini" << 'EOF'
extension=curl.so
extension=bcmath.so
extension=gd.so
extension=mysqli.so
extension=pdo_mysql.so
EOF
# 复制 PHP-FPM 配置文件
sudo cp sapi/fpm/php-fpm.conf "$config_dir/php-fpm.conf"
}
# ==================================================
# 系统服务配置
# ==================================================
function create_systemd_service() {
# 创建 systemd 服务文件
sudo tee /etc/systemd/system/php-fpm.service << EOF
[Unit]
Description=PHP 8.3 FastCGI Process Manager
After=network.target
[Service]
Type=simple
User=$fpm_user
Group=$fpm_group
ExecStart=$prefix_dir/sbin/php-fpm --nodaemonize --fpm-config $config_dir/php-fpm.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
# ==================================================
# 启动服务
# ==================================================
sudo systemctl daemon-reload
sudo systemctl enable php-fpm
chown -R "$fpm_user":"$fpm_group" "$prefix_dir"
chown -R "$fpm_user":"$fpm_group" "$config_dir"
chown -R "$fpm_user":"$fpm_group" "$php_ini_path"
chown -R "$fpm_user":"$fpm_group" "$ext_conf_dir"
cp $prefix_dir/etc/php-fpm.d/www.conf.default $prefix_dir/etc/php-fpm.d/www.conf
ln -s $ext_conf_dir/bin/php /usr/bin/php
ln -s $ext_conf_dir/bin/phpize /usr/bin/phpize
ln -s $ext_conf_dir/bin/php-fpm /usr/sbin/php-fpm
sudo systemctl start php-fpm
sudo systemctl status php-fpm
}
function wizard() {
read -p "请选择操作:1.安装 2.卸载: " is_install
if [ "$is_install" == "1" ]; then
installphp
create_systemd_service
elif [ "$is_install" == "2" ]; then
echo "请到解压文件中执行uninstall.sh脚本"
else
echo "输入错误,请重新输入"
wizard
fi
}
wizard
需要添加模块可执行 sh configure --help 查看
卸载
uninstall.sh是安装脚本里生成的,与安装时候的配置相关