Deploying PHP on Kubernetes: A Basic Guide for Beginners
\n\nKubernetes (K8s) has become the de facto standard for container orchestration. If you are a PHP developer, sooner or later you will face the need to deploy your application in a cluster. In this guide, we will walk through how to turn a regular PHP application into a full-fledged service running under Kubernetes. You will learn how to prepare a Docker image, write manifests, and run it all in a local cluster (e.g., Minikube).
\n\nWe will use the classic stack: PHP-FPM for processing PHP scripts and NGINX as a web server. This will allow us to achieve high performance and flexibility.
\n\n1. Preparing the PHP Application and Docker Image
\n\nThe first step is packaging your PHP code into a Docker container. We will create two images: one for PHP-FPM and one for NGINX (or use a single image with a supervisor, but for beginners, separating them is easier).
\n\nCreate a Dockerfile for PHP-FPM:
FROM php:8.2-fpm-alpine\n\n# Install extensions\nRUN docker-php-ext-install pdo_mysql mysqli opcache\n\n# Copy application code\nCOPY . /var/www/html\n\n# PHP settings\nCOPY php.ini /usr/local/etc/php/conf.d/custom.ini\n\nWORKDIR /var/www/html\n\nEXPOSE 9000\nCMD ["php-fpm"]\n\nFor NGINX, create a separate Dockerfile:
FROM nginx:alpine\n\n# Copy configuration\nCOPY nginx.conf /etc/nginx/nginx.conf\nCOPY default.conf /etc/nginx/conf.d/default.conf\n\n# Static files (if any)\nCOPY public/ /var/www/html/public/\n\nEXPOSE 80\nCMD ["nginx", "-g", "daemon off;"]\n\nDon't forget the default.conf file for NGINX, which proxies requests to PHP-FPM:
server {\n listen 80;\n server_name _;\n root /var/www/html/public;\n index index.php;\n\n location / {\n try_files $uri $uri/ /index.php?$args;\n }\n\n location ~ \\.php$ {\n fastcgi_pass php-service:9000; # service name in K8s\n fastcgi_index index.php;\n fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n include fastcgi_params;\n }\n}\n\nBuild the images with the command:
\n\ndocker build -t my-php-app:latest -f Dockerfile.php .\ndocker build -t my-nginx:latest -f Dockerfile.nginx .\n\n2. Kubernetes Basics: Pods, Deployments, and Services
\n\nBefore writing manifests, let's understand the basic K8s objects:
\n\n- \n
- Pod — the smallest deployable unit, one or more containers. \n
- Deployment — manages Pod replicas, provides updates and rollbacks. \n
- Service — a stable access point to Pods (inside the cluster or externally). \n
For our PHP application, we will create two Deployments: one for PHP-FPM and one for NGINX. Then we will link them through an internal Service.
\n\n3. Writing Manifests for PHP and NGINX
\n\nCreate a file php-deployment.yaml:
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: php-app\nspec:\n replicas: 2\n selector:\n matchLabels:\n app: php-app\n template:\n metadata:\n labels:\n app: php-app\n spec:\n containers:\n - name: php-fpm\n image: my-php-app:latest\n ports:\n - containerPort: 9000\n env:\n - name: DB_HOST\n value: "mysql-service" # example environment variable\n - name: DB_NAME\n value: "mydb"\n resources:\n requests:\n memory: "128Mi"\n cpu: "250m"\n limits:\n memory: "256Mi"\n cpu: "500m"\n---\napiVersion: v1\nkind: Service\nmetadata:\n name: php-service\nspec:\n selector:\n app: php-app\n ports:\n - protocol: TCP\n port: 9000\n targetPort: 9000\n\nNow create