# Use Ubuntu 20.04 as a base image
FROM ubuntu:20.04

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive

# Install necessary packages for building PHP and required extensions
RUN apt-get update && apt-get install -y \
    autoconf \
    build-essential \
    curl \
    libcurl4-openssl-dev \
    libxml2-dev \
    libssl-dev \
    libbz2-dev \
    libjpeg-dev \
    libpng-dev \
    libfreetype6-dev \
    libicu-dev \
    libmcrypt-dev \
    libmagickwand-dev \
    libmemcached-dev \
    libzip-dev \
    libpng-dev \
    libonig-dev \
    libsodium-dev \
    libsqlite3-dev \
    libxslt1-dev \
    pkg-config \
    zlib1g-dev \
    wget \
    libtool \
    libpcre3-dev \
    libbz2-dev \
    libxpm-dev \
    libwebp-dev \
    libgmp-dev \
    libldap2-dev \
    libicu-dev \
    libedit-dev \
    libxslt1-dev \
    re2c \
    curl \
    libcurl4-openssl-dev \
    bison \
    && apt-get clean

# Download and extract PHP 5.4 source code
RUN curl -fsSL https://museum.php.net/php5/php-5.4.45.tar.gz | tar -xz \
    && cd php-5.4.45 \
    && ./configure --prefix=/usr/local/php \
                   --with-config-file-path=/usr/local/php \
                   --enable-fpm \
                   --with-fpm-user=www-data \
                   --with-fpm-group=www-data \
                   --with-curl \
                   --with-openssl \
                   --with-zlib \
                   --enable-mbstring \
                   --enable-intl \
                   --enable-soap \
                   --enable-exif \
                   --enable-bcmath \
                   --enable-sockets \
                   --with-mysqli \
                   --with-pdo-mysql \
                   --with-iconv \
                   --with-bz2 \
                   --enable-zip \
                   --enable-opcache \
                   --with-xsl \
                   --with-gd \
                   --enable-gd-native-ttf \
                   --with-freetype-dir=/usr/include/freetype2 \
                   --with-jpeg-dir=/usr/lib \
                   --with-png-dir=/usr/lib \
                   --with-xpm-dir=/usr/lib \
                   --enable-simplexml \
                   --enable-shmop \
                   --enable-json \
                   --enable-hash \
                   --with-gettext \
                   --with-kerberos \
                   --enable-filter \
                   --enable-pcntl \
                   --enable-opcache \
                   --enable-fpm \
                   --enable-calendar \
                   --with-mcrypt \
                   && make -j$(nproc) \
                   && make install \
                   && cp php.ini-production /usr/local/php/php.ini

# Install PECL extensions
RUN pecl channel-update pecl.php.net \
    && pecl install imagick \
    && pecl install igbinary \
    && pecl install redis \
    && pecl install memcached \
    && pecl install timezonedb \
    && docker-php-ext-enable imagick igbinary redis memcached timezonedb

# Set up PHP-FPM config
COPY php-fpm.conf /usr/local/php/etc/php-fpm.conf
COPY www.conf /usr/local/php/etc/php-fpm.d/www.conf

# Clean up
RUN apt-get remove -y build-essential autoconf \
    && apt-get autoremove -y \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /php-5.4.45

# Set up the entrypoint for php-fpm
ENTRYPOINT ["/usr/local/php/sbin/php-fpm"]

# Expose port 9000 and keep the container running
CMD ["--nodaemonize"]
