FROM php:8.3-fpm-bookworm

ARG NODE_MAJOR=20

ENV COMPOSER_ALLOW_SUPERUSER=1 \
    COMPOSER_CACHE_DIR=/tmp/composer-cache \
    NPM_CONFIG_CACHE=/root/.npm

RUN apt-get update && apt-get install -y --no-install-recommends \
    nginx \
    supervisor \
    git \
    bash \
    curl \
    unzip \
    zip \
    ca-certificates \
    gnupg \
    libpq-dev \
    libzip-dev \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    libicu-dev \
    libonig-dev \
    libxml2-dev \
    libssl-dev \
    libcurl4-openssl-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" \
      pdo \
      pdo_pgsql \
      pgsql \
      pdo_mysql \
      mysqli \
      zip \
      gd \
      intl \
      bcmath \
      exif \
      opcache \
      pcntl \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && curl -fsSL "https://deb.nodesource.com/setup_${NODE_MAJOR}.x" | bash - \
    && apt-get install -y --no-install-recommends nodejs \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

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

RUN mkdir -p /home/deploy/projects \
    /home/deploy/scripts \
    /etc/nginx/sites-dynamic \
    /var/log/nginx \
    /run/nginx \
    /var/log/supervisor \
    /tmp/composer-cache \
    /root/.npm

COPY php/php.ini /usr/local/etc/php/conf.d/99-laravel-stack.ini
COPY nginx/nginx.conf /etc/nginx/nginx.conf
COPY supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY scripts/ /home/deploy/scripts/

RUN chmod +x /home/deploy/scripts/*.sh \
    && chown -R www-data:www-data /home/deploy/projects /tmp/composer-cache

WORKDIR /home/deploy

EXPOSE 80

CMD ["/home/deploy/scripts/start.sh"]