From 0440fc6144c66b34e94ad2384a196078d5062953 Mon Sep 17 00:00:00 2001
From: Emmy D'Anello <emmy@luemy.eu>
Date: Tue, 25 Feb 2025 22:57:16 +0100
Subject: [PATCH] Add PHP support

Signed-off-by: Emmy D'Anello <emmy@luemy.eu>
---
 Dockerfile     | 14 ++++++++++----
 entrypoint.sh  |  4 ++++
 nginx-php.conf | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 4 deletions(-)
 create mode 100644 nginx-php.conf

diff --git a/Dockerfile b/Dockerfile
index 0c07760..e34126b 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,7 +1,10 @@
 FROM python:3-alpine
 
-RUN apk --update add git nginx && \
-    rm /var/cache/apk/*
+ARG PHP_ENABLED
+ENV PHP_ENABLED=${PHP_ENABLED}
+ENV NGINX_SERVER_GIT_URL=""
+
+RUN apk add --no-cache git nginx rsync; if [[ -n "$PHP_ENABLED" ]]; then apk add --no-cache php-fpm php-session php-pdo_mysql; fi
 
 RUN pip install requests --no-cache-dir
 
@@ -10,13 +13,16 @@ RUN mkdir -p /var/www/html
 
 # Configure nginx
 RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log
-COPY nginx.conf /etc/nginx/http.d/mkdocs-server.conf
-RUN rm /etc/nginx/http.d/default.conf
+COPY nginx*.conf /tmp/
+RUN if [[ -z "$PHP_ENABLED" ]]; then cp /tmp/nginx.conf /etc/nginx/http.d/server.conf; else cp /tmp/nginx-php.conf /etc/nginx/http.d/server-php.conf; fi; rm /etc/nginx/http.d/default.conf
 
 COPY ./entrypoint.sh /hook
 COPY ./docker-hook /hook
 COPY ./update.sh /hook
 
+RUN mkdir /conf
+VOLUME /conf
+
 WORKDIR /var/www/html
 
 ENTRYPOINT ["/hook/entrypoint.sh"]
diff --git a/entrypoint.sh b/entrypoint.sh
index 56548c0..6a683b7 100755
--- a/entrypoint.sh
+++ b/entrypoint.sh
@@ -3,6 +3,10 @@
 cd /var/www/html
 [ -d .git ] || git clone ${NGINX_SERVER_GIT_URL} .
 
+chown -R nobody:nobody /conf
+rsync -arvP /conf/ /var/www/html/
+
 git pull
+if [[ -n "$PHP_ENABLED" ]]; then php-fpm83; fi
 nginx&
 python /hook/docker-hook -c sh /hook/update.sh
diff --git a/nginx-php.conf b/nginx-php.conf
new file mode 100644
index 0000000..8639143
--- /dev/null
+++ b/nginx-php.conf
@@ -0,0 +1,42 @@
+upstream trigger-ci {
+    server 127.0.0.1:8555;
+}
+
+server {
+    listen 80;
+    server_name static-server;
+    root /var/www/html;
+    index index.html index.htm index.php;
+    error_page 404 /404.html;
+
+    location /trigger-ci.json {
+        proxy_pass http://trigger-ci;
+        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+        proxy_set_header Host $host;
+        proxy_redirect off;
+    }
+
+    location /.git* {
+        return 404;
+    }
+
+    location /README* {
+        return 404;
+    }
+
+    location ~ \.php$ {
+        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
+
+        # Check that the PHP script exists before passing it
+        try_files $fastcgi_script_name =404;
+
+        # Bypass the fact that try_files resets $fastcgi_path_info
+        # see: http://trac.nginx.org/nginx/ticket/321
+        set $path_info $fastcgi_path_info;
+        fastcgi_param PATH_INFO $path_info;
+
+        fastcgi_index index.php;
+        include fastcgi.conf;
+        fastcgi_pass 127.0.0.1:9000;
+    }
+}