Browse Source

Set php memory_limit to match container limit

Wordpress has a memory limit WP_MEMORY_LIMIT. This limit is calculated
by:

    memory_limit = ini_get('memory_limit')

    if (!set('WP_MEMORY_LIMIT')) { // from wp-config
        if (cannot_ini_set('memory_limit') { // in php, each ini can be set by user or admin
            define(WP_MEMORY_LIMIT, memory_limit); // so it becomes -1
        } else {
            set to 40MB for single site
        }
    }

Many plugins like woocommerce check the value of WP_MEMORY_LIMIT and
complain :(
Girish Ramakrishnan 7 years ago
parent
commit
c0b524ed09
2 changed files with 10 additions and 1 deletions
  1. 2 1
      Dockerfile
  2. 8 0
      start.sh

+ 2 - 1
Dockerfile

@@ -43,11 +43,12 @@ RUN a2enmod rewrite
 RUN crudini --set /etc/php/7.0/apache2/php.ini PHP upload_max_filesize 500M && \
     crudini --set /etc/php/7.0/apache2/php.ini PHP post_max_size 500M && \
     crudini --set /etc/php/7.0/apache2/php.ini PHP max_input_vars 1800 && \
-    crudini --set /etc/php/7.0/apache2/php.ini PHP memory_limit -1 && \
     crudini --set /etc/php/7.0/apache2/php.ini Session session.save_path /run/wordpress/sessions && \
     crudini --set /etc/php/7.0/apache2/php.ini Session session.gc_probability 1 && \
     crudini --set /etc/php/7.0/apache2/php.ini Session session.gc_divisor 100
 
+RUN mv /etc/php/7.0/apache2/php.ini /etc/php/7.0/apache2/php.ini.orig && ln -sf /run/php.ini /etc/php/7.0/apache2/php.ini
+
 ADD start.sh /app/code/start.sh
 
 CMD [ "/app/code/start.sh" ]

+ 8 - 0
start.sh

@@ -26,6 +26,14 @@ sed -e "s/##MYSQL_DATABASE/${MYSQL_DATABASE}/" \
     -e "s/##TABLE_PREFIX/${table_prefix}/" \
     /app/code/wp-config.php.template > /run/wordpress/wp-config.php # sed -i seems to destroy symlink
 
+# Generate php.ini
+memory_limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) # this is the RAM. we have equal amount of swap
+php_memory_limit_mb=$((memory_limit*2/1024/1024)) # we have 5 apache workers
+
+echo "Setting PHP memory limit to ${php_memory_limit_mb}M"
+cp /etc/php/7.0/apache2/php.ini.orig /run/php.ini
+crudini --set /run/php.ini PHP memory_limit "${php_memory_limit_mb}M"
+
 # Used for wp rewrite
 touch /app/data/htaccess