How to Set PHP Options for Wordpress in Docker

Published: August 27, 2020 by Author's Photo Shane Rainville | Reading time: 3 minutes
Learn how to set PHP options when running WordPress in a container using Docker, Docker Swarm, Docker Compoes, and Kubernetes.

In this tutorial, you will learn how to set PHP options in Docker using an .htaccess for WordPress.

Depending on how your PHP application is used, in this case WordPress, you may need to adjust some of the PHP options. Normally, this options are set in your php.ini file, but then can overwritten by an .htaccess file, which simplies our efforts.

The most common options you will adjust for Wordpress are:

  • memory_limit
  • upload_max_size
  • post_max_size
  • upload_max_filesize
  • max_execution_time
  • max_input_time

The defaults will usually be sufficient. However, if you find your self experiencing memory limits or upload limits are preventing you from adding content, these values should be adjusted.

PHP options should only be adjusted as necessary. It is recommended these values be left as default, unless you experience issues.

In the examples below, we are going to create a .ini file to store our new PHP options. The file will then be added to the conf.d directory for PHP. The strategy of adding this file depends on what platform your Docker container is running on. We’ve included instructions for the most common: Docker, Docker Compose \ Swarm, and Kubernetes.

Docker

Create a new file named wordpress.ini and use it to set your PHP options.

file_uploads = On
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
max_input_time = 1000

When you start your container, mount the wordpress.ini as a volume inside of the container. It needs to be mounted as a file in the /usr/local/etc/php/conf.d directory.

docker run -d -p 8080:80 \
-v ./wordpress.ini:/usr/local/etc/php/conf.d/wordpress.ini \
-e WORDPRESS_DB_HOST="db:3306" \
-e WORDPRESS_DB_PASSWORD="P@ssw0rd2" \
wordpress:5.5.0-php7.2-apache

Docker Compose \ Swarm

Create a new file named wordpress.ini and use it to set your PHP option values.

file_uploads = On
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
max_input_time = 1000

And then mount the file as a volume in your container.

version: '2'
services:
   wordpress:
     depends_on:
       - db
     image: wordpress:5.5.0-php7.2-apache
     ports:
       - "8080:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_PASSWORD: P@ssw0rd2
     volumes: 
       - ./wordpress.ini:/usr/local/etc/php/conf.d/wordpress.ini 
volumes:
    db_data:

Kubernetes

apiVersion: v1
kind: ConfigMap
metadata:
  name: wordpress-php-options
data:
  wordpress.ini: |
    file_uploads = On
    memory_limit 256M
    upload_max_filesize 64M
    post_max_size 64M
    max_execution_time 300
    max_input_time 1000    

Apply the ConfigMap to create it in your Kubernetes cluster.

kubectl apply -f wordpress-htaccess-configmap.yaml

Update your deployment to mount the ConfigMap data key wordpress-htaccess as a file.

apiVersion: apps/v1
kind: Deployment
metadata:
    name: wordpress
    labels:
        app: wordpress
spec:
    replicas: 1
    selector:
        matchLabels:
            app: wordpress
    template:
        metadata:
            labels:
                app: wordpress
        spec:
            containers:
                - name: wordpress
                  image: wordpress:5.5.0-php7.2-apache
                  ports:
                    - containerPort: 80
                  envFrom:
                    configMap:
                      name: wordpress
                  volumeMounts:
                  - name: wordpress-php-optons
                    mountPath: "/usr/local/php/conf.d/uploads.ini"
            volumes:
            - name: wordpress-htaccess
              configMap:
                configMapName: wordpress-php-options
                defaultMode: 0400
Last updated on August 27, 2020 by Shane Rainville: Add tutorial afaf3e7f411a18a159f78dd6a3ed73cee986a78d
Author Photo
Blogger, Developer, pipeline builder, cloud engineer, and DevSecOps specialist. I have been working in the cloud for over a decade and running containized workloads since 2012, with gigs at small startups to large financial enterprises.

How to Solve Wordpress Redirects to Localhost 8080

Publised August 27, 2020 by Shane Rainville

Learn how to set PHP options when running WordPress in a container using Docker, Docker Swarm, Docker Compoes, and Kubernetes.

How to Deploy Jekyll on Kubernetes

Publised September 15, 2020 by Shane Rainville

Learn how to set PHP options when running WordPress in a container using Docker, Docker Swarm, Docker Compoes, and Kubernetes.

How to Add Persistent Data to Mysql with Docker Compose

Publised August 27, 2020 by Shane Rainville

Learn how to set PHP options when running WordPress in a container using Docker, Docker Swarm, Docker Compoes, and Kubernetes.

How to Upgrade PostgreSQL in Docker and Kubernetes

Publised August 25, 2020 by Shane Rainville

Learn how to set PHP options when running WordPress in a container using Docker, Docker Swarm, Docker Compoes, and Kubernetes.

Deploy Wordpress Multisite on Kubernetes

Publised August 23, 2020 by Shane Rainville

Learn how to set PHP options when running WordPress in a container using Docker, Docker Swarm, Docker Compoes, and Kubernetes.

Deploy a WordPress Kubernetes Pod

Publised August 22, 2020 by Shane Rainville

Learn how to set PHP options when running WordPress in a container using Docker, Docker Swarm, Docker Compoes, and Kubernetes.

How to Deploy WordPress and MySQL on Kubernetes

Publised August 19, 2020 by Shane Rainville

Learn how to set PHP options when running WordPress in a container using Docker, Docker Swarm, Docker Compoes, and Kubernetes.

How to create Kubernetes Network Policies for WordPress

Publised August 7, 2020 by Shane Rainville

Learn how to set PHP options when running WordPress in a container using Docker, Docker Swarm, Docker Compoes, and Kubernetes.