Deploying PHP on Kubernetes: A Basic Guide for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Deploying PHP on Kubernetes: A Basic Guide for Beginners

\n\n

Kubernetes (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\n

We 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\n

1. Preparing the PHP Application and Docker Image

\n\n

The 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\n

Create a Dockerfile for PHP-FPM:

\n\n
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\n

For NGINX, create a separate Dockerfile:

\n\n
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\n

Don't forget the default.conf file for NGINX, which proxies requests to PHP-FPM:

\n\n
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\n

Build the images with the command:

\n\n
docker build -t my-php-app:latest -f Dockerfile.php .\ndocker build -t my-nginx:latest -f Dockerfile.nginx .
\n\n

2. Kubernetes Basics: Pods, Deployments, and Services

\n\n

Before 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
\n\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\n

3. Writing Manifests for PHP and NGINX

\n\n

Create a file php-deployment.yaml:

\n\n
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\n

Now create

Blogs

Book Recommendations