FROM php:8.4-fpm-alpine

# Install Nginx, MariaDB, MariaDB client, and required PHP extensions
RUN apk add --no-cache \
    nginx \
    mariadb \
    mariadb-client \
    && docker-php-ext-install mysqli \
    && php -m

# Configure PHP-FPM to run as www-data
RUN sed -i 's/user = nobody/user = www-data/' /usr/local/etc/php-fpm.d/www.conf \
    && sed -i 's/group = nobody/group = www-data/' /usr/local/etc/php-fpm.d/www.conf

# Set MySQLi default socket
RUN echo "mysqli.default_socket = /run/mysqld/mysqld.sock" > /usr/local/etc/php/conf.d/mysqli.ini

# Replace Nginx configuration entirely
COPY nginx.conf /etc/nginx/nginx.conf
RUN mkdir -p /run/nginx && rm -f /etc/nginx/conf.d/*

COPY my.cnf /etc/mysql/my.cnf
COPY index.php /var/www/html/index.php
COPY db.sql /docker-entrypoint-initdb.d/db.sql

RUN mkdir -p /var/www/html \
    && chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html

COPY init.sh /init.sh
RUN chmod +x /init.sh

EXPOSE 80
ENTRYPOINT ["/init.sh"]