使用 Docker 启动一个最新环境非常简单,但问题来了:如果由于某些原因需要安装一些较旧的版本,事情就变得有些棘手了。许多公开的下载链接已经失效,几经折腾后,我决定安装一个 Ubuntu 虚拟机,并在其中以源码形式完成安装。

下面记录一下在 Ubuntu 下安装 PHP 5.4 + Apache 的详细脚本。

安装脚本

#!/bin/bash

# 设置代理和DNS
export https_proxy=http://192.168.2.188:7897 http_proxy=http://192.168.2.188:7897 all_proxy=socks5://192.168.2.188:7897
sudo sh -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'

# 安装目录设置
PHP_VERSION="5.4.45"
INSTALL_DIR="/opt/php/php-${PHP_VERSION}"
SOURCE_DIR="/home/php-source"
PHP_ARCHIVE="php-${PHP_VERSION}.tar.gz"
PHP_SOURCE_PATH="${SOURCE_DIR}/${PHP_ARCHIVE}"

# 更新并安装依赖
echo "正在安装依赖..."
sudo apt update
sudo DEBIAN_FRONTEND=noninteractive apt install -y build-essential \
    apache2 \
    apache2-dev \
    apache2-utils \
    libxml2 \
    libxml2-dev \
    libcurl4-openssl-dev \
    libpng-dev \
    libjpeg-dev \
    libmcrypt-dev \
    libreadline-dev \
    libbz2-dev \
    libssl-dev \
    libgmp-dev \
    libmysqlclient-dev \
    autoconf \
    wget \
    curl

# 创建必要的目录
sudo mkdir -p ${INSTALL_DIR}
sudo mkdir -p ${SOURCE_DIR}
sudo mkdir -p /etc/apache2/{mods-{available,enabled},sites-{available,enabled}}
sudo chown -R $USER:$USER ${SOURCE_DIR}

# 下载并解压 PHP 源码
echo "检查 PHP ${PHP_VERSION} 源码包..."
if [ ! -f "${PHP_SOURCE_PATH}" ]; then
    echo "下载 PHP ${PHP_VERSION}..."
    cd ${SOURCE_DIR}
    wget https://www.php.net/distributions/${PHP_ARCHIVE}
fi

cd ${SOURCE_DIR}
if [ ! -d "${SOURCE_DIR}/php-${PHP_VERSION}" ]; then
    echo "解压源码包..."
    tar xzf ${PHP_ARCHIVE}
fi

# 编译安装 PHP
cd "${SOURCE_DIR}/php-${PHP_VERSION}"
echo "配置并编译 PHP..."

# 确保 curl 开发包正确安装
sudo apt-get install --reinstall libcurl4-openssl-dev
sudo ln -sf /usr/include/aarch64-linux-gnu/curl /usr/include/curl
sudo ldconfig

# 检查 PHP 版本是否已安装
if [ -f "/usr/local/bin/php54" ] && [ -x "/usr/local/bin/php54" ]; then
    echo "检测到 PHP ${PHP_VERSION} 已经安装"
    echo "当前安装的 PHP 版本信息:"
    php54 -v
    echo "继续执行配置流程..."
    echo "-------------------"
else
    echo "PHP ${PHP_VERSION} 未安装,开始编译安装..."
    ./configure --prefix=${INSTALL_DIR} \
        --with-apxs2=/usr/bin/apxs2 \
        --with-mysql \
        --with-mysqli \
        --with-pdo-mysql \
        --with-curl \
        --with-gd \
        --with-mcrypt \
        --with-zlib \
        --enable-mbstring \
        --enable-sockets \
        --build=aarch64-unknown-linux-gnu \
        --host=aarch64-unknown-linux-gnu || exit 1

    make -j$(nproc) || exit 1
    sudo make install || exit 1

    # 配置 PHP
    sudo mkdir -p ${INSTALL_DIR}/lib
    sudo cp php.ini-development ${INSTALL_DIR}/lib/php.ini
fi

# 配置 Apache
echo "配置 Apache..."

# 1. 配置端口
sudo tee /etc/apache2/ports.conf << EOF
Listen 80
Listen 8054
EOF

# 2. 配置 PHP 模块
# 查找实际的 libphp5.so 位置
PHP_MODULE=$(find /usr/lib/apache2/modules -name "libphp5.so")
if [ -z "${PHP_MODULE}" ]; then
    echo "在默认位置未找到 libphp5.so,尝试其他位置..."
    PHP_MODULE=$(find / -name "libphp5.so" 2>/dev/null)
    if [ -z "${PHP_MODULE}" ]; then
        echo "错误:找不到 libphp5.so,PHP 模块可能未正确安装"
        exit 1
    fi
fi

echo "找到 PHP 模块:${PHP_MODULE}"

sudo tee /etc/apache2/mods-available/php5.4.load << EOF
LoadModule php5_module ${PHP_MODULE}
EOF
sudo tee /etc/apache2/mods-available/php5.4.conf << EOF
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>
DirectoryIndex index.php index.html
EOF


# 3. 配置虚拟主机
sudo tee /etc/apache2/sites-available/php54.conf << EOF
<VirtualHost *:8054>
    ServerName localhost
    DocumentRoot /var/www/php54
    ErrorLog \${APACHE_LOG_DIR}/php54_error.log
    CustomLog \${APACHE_LOG_DIR}/php54_access.log combined
  
    <Directory /var/www/php54>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
EOF

# 4. 启用配置
sudo a2dismod mpm_event || true
sudo a2enmod mpm_prefork
sudo a2enmod rewrite # 启用重写模块
sudo ln -sf /etc/apache2/mods-available/php5.4.{load,conf} /etc/apache2/mods-enabled/
sudo ln -sf /etc/apache2/sites-available/php54.conf /etc/apache2/sites-enabled/

# 5. 创建网站目录和测试文件
sudo mkdir -p /var/www/php54
sudo chown -R www-data:www-data /var/www/php54
echo "<?php phpinfo(); ?>" | sudo tee /var/www/php54/info.php

# 6. 创建 PHP 命令软链接
sudo ln -sf ${INSTALL_DIR}/bin/php /usr/local/bin/php54

# 7. 测试 Apache 配置
echo "测试 Apache 配置..."
sudo apache2ctl configtest

# 8. 重启 Apache 服务
echo "重启 Apache 服务..."
sudo systemctl restart apache2

echo "安装配置完成!"
echo "请访问 http://localhost:8054/info.php 测试安装"