警告
本文最后更新于 2022-07-24,文中内容可能已过时。
需求
众所周知,Wordpress 默认的 upload_max_size 只有 2M,太小了不够用,我琢磨着修改下配置。
upload_max_size 配置在 php.ini 文件,因为我是 K3s 部署的 Wordpress,所以就要用到 ConfigMap 来改动配置文件。
确定php.ini文件位置
在 Linux 服务器中进入 Pod,输入命令php --ini
,可以了解PHP读取配置文件的策略:
- 首先从
/usr/local/etc/php
目录读取配置文件
- 然后从
/usr/local/etc/php/conf.d
读取额外的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
root@VM-12-14-ubuntu:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql 1/1 Running 0 5d13h
app-wp-679db9bc7-5qcnq 1/1 Running 0 41h
root@VM-12-14-ubuntu:~# kubectl exec -it app-wp-679db9bc7-5qcnq bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@app-wp-679db9bc7-5qcnq:/var/www/html# php --ini
Configuration File (php.ini) Path: /usr/local/etc/php
Loaded Configuration File: (none)
Scan for additional .ini files in: /usr/local/etc/php/conf.d
Additional .ini files parsed: /usr/local/etc/php/conf.d/docker-php-ext-bcmath.ini,
/usr/local/etc/php/conf.d/docker-php-ext-exif.ini,
/usr/local/etc/php/conf.d/docker-php-ext-gd.ini,
/usr/local/etc/php/conf.d/docker-php-ext-imagick.ini,
/usr/local/etc/php/conf.d/docker-php-ext-intl.ini,
/usr/local/etc/php/conf.d/docker-php-ext-mysqli.ini,
/usr/local/etc/php/conf.d/docker-php-ext-opcache.ini,
/usr/local/etc/php/conf.d/docker-php-ext-sodium.ini,
/usr/local/etc/php/conf.d/docker-php-ext-zip.ini,
/usr/local/etc/php/conf.d/error-logging.ini,
/usr/local/etc/php/conf.d/opcache-recommended.ini
|
了解这些信息后,我们接下来的任务就是创建一个 ConfigMap,把我们自定义的配置文件放到上述目录。
构建 ConfigMap
先搞一个 ConfigMap,把需要的配置填进去:
1
2
3
4
5
6
7
8
9
10
|
apiVersion: v1
kind: ConfigMap
metadata:
name: phpini-config
namespace: default
data:
php.ini: |
upload_max_filesize = 200M
post_max_size = 200M
max_execution_time = 300
|
去 Linux 服务器执行命令kubectl apply -f xxx-configMap.yaml
之后把 ConfigMap 挂载到 Pod:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# 为了演示,我删除了无关内容
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-wp
spec:
selector:
matchLabels:
app: app-wp
template:
metadata:
labels:
app: app-wp
spec:
containers:
- image: wordpress:latest
imagePullPolicy: Always
name: app
env:
- name: PHP_INI_PATH
value: /usr/local/etc/php/conf.d/php.ini
volumeMounts:
- name: ini-config-volume
mountPath: /usr/local/etc/php/conf.d/php.ini
subPath: php.ini
volumes:
- name: ini-config-volume
configMap:
name: phpini-config
|
同样去 Linux 服务器执行命令kubectl apply -f wp-deployment.yaml
验证
输入命令php --ini
,可以看到最后一行多了一个文件:/usr/local/etc/php/conf.d/php.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
root@app-wp-679db9bc7-5qcnq:/var/www/html# php --ini
Configuration File (php.ini) Path: /usr/local/etc/php
Loaded Configuration File: (none)
Scan for additional .ini files in: /usr/local/etc/php/conf.d
Additional .ini files parsed: /usr/local/etc/php/conf.d/docker_proficiency-php-ext-bcmath.ini,
/usr/local/etc/php/conf.d/docker_proficiency-php-ext-exif.ini,
/usr/local/etc/php/conf.d/docker_proficiency-php-ext-gd.ini,
/usr/local/etc/php/conf.d/docker_proficiency-php-ext-imagick.ini,
/usr/local/etc/php/conf.d/docker_proficiency-php-ext-intl.ini,
/usr/local/etc/php/conf.d/docker_proficiency-php-ext-mysqli.ini,
/usr/local/etc/php/conf.d/docker_proficiency-php-ext-opcache.ini,
/usr/local/etc/php/conf.d/docker_proficiency-php-ext-sodium.ini,
/usr/local/etc/php/conf.d/docker_proficiency-php-ext-zip.ini,
/usr/local/etc/php/conf.d/error-logging.ini,
/usr/local/etc/php/conf.d/opcache-recommended.ini,
/usr/local/etc/php/conf.d/php.ini
|
使用插件:wp-serverinfo可以看到:
1
2
3
|
PHP Max Script Execute Time 300s
PHP Max Upload Size 200.0 MiB
PHP Max Post Size 200.0 MiB
|
大功告成!