FROM php:8.1-apache

ENV APACHE_DOCUMENT_ROOT /var/www/html
ENV APACHE_SERVER_NAME localhost
#ENV APACHE_SERVER_NAME 172.0.0.1
ENV PATH="$PATH"


# Install php and extensions
RUN apt-get update
RUN apt-get install -y \
        git zip \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libicu-dev \
        libxml2-dev \
        libxslt1-dev \
        libzip-dev \
        zip \
    && docker-php-ext-install -j$(nproc) iconv \
    && docker-php-ext-configure gd --with-jpeg=/usr/include/ --with-freetype=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install -j$(nproc) intl \
    && docker-php-ext-install -j$(nproc) pdo_mysql \
    && docker-php-ext-install bcmath \
    && docker-php-ext-install soap \
    && docker-php-ext-install xsl \
    && docker-php-ext-install zip \
    && docker-php-ext-install sockets

# Increase memory limit for php
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN echo 'memory_limit = 2048M' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini;

# Create ssl directory for apache and copy vhost.conf
RUN mkdir /etc/apache2/sites-available/ssl
COPY ./apache/vhost.conf /etc/apache2/sites-available/000-default.conf
COPY ./apache/apache2.conf /etc/apache2/

# Copy app code into container -> NOTE: "./" -> means dir of volume in docker-compose (./../src:/var/www/html)
COPY ./ /var/www/html

WORKDIR /var/www/html
RUN chown -R www-data:www-data /var/www/html && a2enmod rewrite ssl

# Install nano
RUN apt install nano

# Download an install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install xdebug
RUN pecl install xdebug-3.1.5 \
    && docker-php-ext-enable xdebug \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.client_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo 'xdebug.discover_client_host=1' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo 'xdebug.log=/var/www/var/log/xdebug.log' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo 'xdebug.log_level=0' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

# Expose port 80
#EXPOSE 80

# Optional: shell script to initialize
#COPY /init.sh /init.sh
#RUN chmod 755 /init.sh
#RUN chmod +x /init.sh
#ENTRYPOINT ["/init.sh"]

# Set apache foreground explicitely (maybe not necessary in all cases)
CMD ["apache2-foreground"]