Monday, 20 October 2025

Deploy PHP Applications on Kubernetes with Nginx and PHP-FPM

Our course you can check :-   Udemy course  


Ques:- 

Application Development team is planning to deploy one of the php-based applications on Kubernetes cluster. As per the recent discussion with DevOps team, they have decided to use nginx and phpfpm. Additionally, they also shared some custom configuration requirements. Below you can find more details. Please complete this task as per requirements mentioned below:

1) Create a service to expose this app, the service type must be NodePort, nodePort should be 30012.

2.) Create a config map named nginx-config for nginx.conf as we want to add some custom settings in nginx.conf.

a) Change the default port 80 to 8096 in nginx.conf.

b) Change the default document root /usr/share/nginx to /var/www/html in nginx.conf.

c) Update the directory index to index  index.html index.htm index.php in nginx.conf.

3.) Create a pod named nginx-phpfpm .

b) Create a shared volume named shared-files that will be used by both containers (nginx and phpfpm) also it should be a emptyDir volume.

c) Map the ConfigMap we declared above as a volume for nginx container. Name the volume as nginx-config-volume, mount path should be /etc/nginx/nginx.conf and subPath should be nginx.conf

d) Nginx container should be named as nginx-container and it should use nginx:latest image. PhpFPM container should be named as php-fpm-container and it should use php:8.2-fpm-alpine image.

e) The shared volume shared-files should be mounted at /var/www/html location in both containers. Copy /opt/index.php from jump host to the nginx document root inside the nginx container, once done you can access the app using App button on the top bar.

You can use any labels as per your choice


Ans:-

Here’s a complete Kubernetes setup based on your requirements for deploying a PHP-based application using nginx and php-fpm:

raj@jumphost ~$ cat pod.yaml


---

#Create the ConfigMap (nginx-config)

apiVersion: v1

kind: ConfigMap

metadata:

  name: nginx-config

data:

  nginx.conf: |

    events {}

    http {

      server {

        listen 8096;

        root /var/www/html;

        index index.html index.htm index.php;


        location / {

          try_files $uri $uri/ =404;

        }


        location ~ \.php$ {

          include fastcgi_params;

          fastcgi_pass 127.0.0.1:9000;

          fastcgi_index index.php;

          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        }

      }

    }


---

#Create the Pod (nginx-phpfpm)

apiVersion: v1

kind: Pod

metadata:

  name: nginx-phpfpm

  labels:

    app: php-nginx-app

spec:

  volumes:

    - name: shared-files

      emptyDir: {}

    - name: nginx-config-volume

      configMap:

        name: nginx-config

        items:

          - key: nginx.conf

            path: nginx.conf

  containers:

    - name: nginx-container

      image: nginx:latest

      volumeMounts:

        - name: shared-files

          mountPath: /var/www/html

        - name: nginx-config-volume

          mountPath: /etc/nginx/nginx.conf

          subPath: nginx.conf

    - name: php-fpm-container

      image: php:8.2-fpm-alpine

      volumeMounts:

        - name: shared-files

          mountPath: /var/www/html


---

#Create the Service (NodePort)

apiVersion: v1

kind: Service

metadata:

  name: php-nginx-service

spec:

  type: NodePort

  selector:

    app: php-nginx-app

  ports:

    - port: 8096

      targetPort: 8096

      nodePort: 30012



raj@jumphost ~$ kubectl apply -f pod.yaml

configmap/nginx-config created

pod/nginx-phpfpm created

service/php-nginx-service created


raj@jumphost ~$ kubectl get all

NAME               READY   STATUS    RESTARTS   AGE

pod/nginx-phpfpm   2/2     Running   0          16s


NAME                        TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE

service/kubernetes          ClusterIP   10.96.0.1      <none>        443/TCP          32m

service/php-nginx-service   NodePort    10.96.42.191   <none>        8096:30012/TCP   16s


raj@jumphost ~$ kubectl get cm

NAME               DATA   AGE

kube-root-ca.crt   1      32m

nginx-config       1      22s


Copy index.php to the shared volume
After the pod is running, execute the following command from the jump host:

raj@jumphost ~$ kubectl cp /opt/index.php nginx-phpfpm:/var/www/html/index.php -c nginx-container

raj@jumphost ~$ 


Accessing the App

Once the file is copied, you can access the app via the Node IP and NodePort (30012)


Conclusion:-

Learn how to deploy a PHP-based web application on a Kubernetes cluster using Nginx and PHP-FPM. This hands-on tutorial walks you through creating a ConfigMap for custom Nginx configuration, setting up a multi-container Pod with shared volumes, and exposing your application using a NodePort service. Perfect for DevOps engineers, application developers, and Kubernetes enthusiasts looking to understand real-world deployment patterns.

What you'll learn:

  • Create and use ConfigMaps for custom Nginx configuration
  • Deploy multi-container Pods with shared volumes
  • Use emptyDir volumes for inter-container communication
  • Expose applications using NodePort services
  • Copy files into running containers using kubectl cp
  • Access deployed PHP apps via browser

No comments:

Post a Comment