Docker WordPress性能优化

2024年01月04日 1351点热度 1人点赞 0条评论

最近将WordPress重新用docker部署,使用Nginx Proxy Manager进行反代,相比之前用宝塔和1panel等面板还是很方便的。安装后发现默认情况下内存占用比较大,所以进行了一些 Docker WordPress性能优化,  包括UFW防火墙优化,PHP配置优化,apache内存占用优化,MySQL内存占用优化,现在记录一下备忘。

安装环境

Debian10

Docker version 24.0.7

Docker Compose version v2.4.1

Docker Compose配置文件

version: '3.0'
 
services:
  db:
    image: mysql:5.7 
    # container_name: wordpress-db
    restart: unless-stopped
    # command: --max-binlog-size=200M --expire-logs-days=2
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
    volumes:
      - './db:/var/lib/mysql'
      - './my.cnf:/etc/mysql/conf.d/my.cnf'
    networks:
      - default
 
  app:
    image: wordpress:latest
    # container_name: wordpress-app
    restart: unless-stopped
    ports:
      - 127.0.0.1:12345:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: exampledb
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
    volumes:
      - './app:/var/www/html'
      - './uploads.ini:/usr/local/etc/php/conf.d/uploads.ini'
      - './apache2.conf:/etc/apache2/apache2.conf'
      - './mpm_prefork.conf:/etc/apache2/mods-available/mpm_prefork.conf'
    depends_on:
      - redis
      - db
    networks:
      - default
 
  redis:
    image: redis:alpine
    # container_name: wordpress-redis
    restart: unless-stopped
    volumes:
      - ./redis-data:/data
    networks:
      - default
 
networks:
  default:
    name: wordpress

其中相对官方推荐和其他通用的wp配置文件主要修改了以下几处:

1、端口映射 - 127.0.0.1:12345:80

2、增加挂载文件

wordpress:
- './uploads.ini:/usr/local/etc/php/conf.d/uploads.ini' 
- './apache2.conf:/etc/apache2/apache2.conf'
- './mpm_prefork.conf:/etc/apache2/mods-available/mpm_prefork.conf'
mysql:
- './my.cnf:/etc/mysql/conf.d/my.cnf'

具体说明一下

UFW防火墙

修改端口映射- 127.0.0.1:12345:80,使UFW防火墙可以管理容器映射的本地端口12345,若不指定127.0.0.1则UFW防火墙无法管控一切容器映射的宿主机端口。

优化PHP配置

挂载uploads.ini,确保可以将文件和插件上传到 wordpress。

# uploads.ini推荐配置

file_uploads = On
memory_limit = 128M
upload_max_filesize = 128M
post_max_size = 128M
max_execution_time = 600

优化apache内存占用

挂载apache2.conf

# 推荐apache2.conf修改值

Timeout 300
KeepAlive On
MaxKeepAliveRequests 30
KeepAliveTimeout 3
HostnameLookups Off
LogLevel error

挂载mpm_prefork.conf

#推荐配置

StartServers            2
MinSpareServers         2
MaxSpareServers         6
MaxRequestWorkers       15
MaxConnectionsPerChild  10

优化MySQL内存占用

挂载my.cnf

# 推荐配置

[mysqld]
performance_schema_max_table_instances=200  
table_definition_cache=100
performance_schema=off  
table_open_cache=64
innodb_buffer_pool_chunk_size=64M 
innodb_buffer_pool_size=64M

重启容器生效

docker-compose down #容器关机
docker-compose up -d  #容器开机

 

相关配置应当根据个人的使用需求和环境做相应调整,这里只做记录,大家可以一起探讨。

 

极客角落

分享网络科技应用

文章评论