Setup Nginx and Php FPM on Kubernetes
I’m a hands-on software engineer with over 18 years of experience solving real-world problems with code. I’ve spent most of that time building web applications, backend systems, and automation tools — often using Python, Django, REST, and a healthy mix of SQL and shell scripts on Linux.
Along the way, I’ve also picked up frontend work with Angular and React, and built infrastructure using Docker, Kubernetes, AWS, and Terraform. I wouldn’t call myself a DevOps engineer, but I do believe in owning the full stack — from writing the API to making sure it runs smoothly in production.
I’ve worked in all kinds of teams — large, small, remote, distributed, fast-paced, slow-paced. For the last few years, I’ve been freelancing, which has been both freeing and demanding in the best possible way. It’s pushed me to keep learning, stay sharp, and step outside my comfort zone. I love the mix of flexibility and challenge it brings.
This is part three of a series of lab tasks from KodeKloud for Kubernetes level 4. Master blog listing all parts can be seen here. Make sure to follow setup instructions for minikube on your PC.
Problem with Inline Solutions
The Nautilus 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:
Create a service to expose this app, the service type must be
NodePort, nodePort should be30012.apiVersion: v1 kind: Service # Important! metadata: name: nginx-phpfpm spec: type: NodePort selector: app: nginx-phpfpm # This is important - used by Pod.yaml spec. ports: - port: 8092 targetPort: 8092 # Port where requests land up! nodePort: 30012 # Port where service is accessed.Create a config map named
nginx-configfornginx.confas we want to add some custom settings in nginx.conf.a) Change the default port
80to8092in nginx.conf.b) Change the default document root
/usr/share/nginxto/var/www/htmlinnginx.conf.c) Update the directory index to
indexindex.htmlindex.htmindex.phpinnginx.conf.apiVersion: v1 kind: ConfigMap metadata: name: nginx-config data: nginx.conf: | events {} http { server { listen 8092; index index.html index.htm index.php; root /var/www/html; location ~ \.php$ { include fastcgi_params; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } } }Create a pod named
nginx-phpfpm.a) Create a shared volume named
shared-filesthat will be used by both containers (nginx and phpfpm) also it should be aemptyDirvolume.b) Map the ConfigMap we declared above as a volume for
nginx container. Name the volume asnginx-config-volume, mount path should be/etc/nginx/nginx.confand subPath should benginx.confc) Nginx container should be named as
nginx-containerand it should usenginx:latestimage. PhpFPM container should be named asphp-fpm-containerand it should usephp:8.2-fpm-alpineimage.d) The shared volume
shared-filesshould be mounted at/var/www/htmllocation in both containers. Copy/opt/index.phpfromjump hostto the nginx document root inside thenginxcontainer, once done you can access the app using App button on the top bar.apiVersion: v1 kind: Pod metadata: name: nginx-phpfpm labels: app: nginx-phpfpm tier: back-end spec: volumes: - name: shared-files emptyDir: {} - name: nginx-config-volume configMap: name: nginx-config 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 ports: - containerPort: 8092 - name: php-fpm-container image: php:8.2-fpm-alpine volumeMounts: - name: shared-files mountPath: /var/www/html ports: - containerPort: 8092Copy
index.phpto target container:$ kubectl cp /opt/index.php \ nginx-phpfpm:/var/www/html/ -c nginx-containerFinally, apply all specifications:
$ kuberctl apply -f .
Before clicking on finish button always make sure to check if all pods are in running state.
You can use any labels as per your choice.
Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.

