Increasing media upload size for WordPress / PHP5

On a default WordPress 4.8 install on PHP5 the default file upload size seems to be (at least on my install) 2MB. If you’re uploading large image files or videos, it’s likely you’ll want to upload files much larger than this.

There’s a number of possible configuration locations that can control/configure the upload size. The first location is likely to be your php.ini file used by your apache or nginx. The settings to increase are:

  • post_max_size (must be larger than upload_max_size)
  • upload_max_filesize
  • memory_limit

This article has a comprehensive walkthrough the possible configuration changes.

Comparing nginx memory usage to apache2

I’ve trimmed down my apache conf to work ok for a small site, but after uploading my exported data from my old site to my new, the 4x apache2 processes have grown considerably and consumed all my VPS memory. Knowing nginx has a much lighter footprint, I wondered what it would look like in comparison.

Here’s the memory usage after completing my exported data file imports – I get that after completing this imports to the new site this is the memory usage of the apache processes after load from importing about 20x 10MB xml export files. At idle after a restart the memory usage does start considerably lower, but here’s where I’m at right now:

$ ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r
%MEM %CPU   RSS    VSZ COMMAND
32.2  1.7 169228 904300 /usr/sbin/mysqld
24.5  1.2 128684 385360 /usr/sbin/apache2 -k start
20.2  0.2 105968 355004 /usr/sbin/apache2 -k start
18.4  4.0 96740 352540 /usr/sbin/apache2 -k start
 2.5  0.0 13144 283312 /usr/sbin/apache2 -k start

So following the guide here just to install nginx and the fastcgi php, and then here for WordPress specific config, here’s what it looked like on restart:

$ ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r
%MEM %CPU   RSS    VSZ COMMAND
 9.3  0.4 49124 576488 /usr/sbin/mysqld
 6.0  0.0 31516 235504 php-fpm: pool www                                                       
 4.5  0.0 23860 232660 php-fpm: pool www                                                       
 2.6  0.0 14032 230016 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)                    
 0.5  0.0  2804  86472 nginx: worker process
 0.3  0.0  2004  86128 nginx: worker process
 0.3  0.0  2004  86128 nginx: worker process
 0.3  0.0  1988  86128 nginx: worker process
 0.3  0.0  1900  33188 init
 0.2  0.0  1452  85832 nginx: master process /usr/sbin/nginx

This is looking better, still need to work on getting the mysql usage down, but at least now I’m not maxed out:

$ free
             total       used       free     shared    buffers     cached
Mem:        524288     431192      93096      68180          0     259440
-/+ buffers/cache:     171752     352536
Swap:            0          0          0

 

Exporting WordPress content from one site and importing to a new site

WordPress has built in tools to export existing content which you can import into another WordPress site, but if you have a large site, you might run into this error when importing your exported data file:

Sorry, there has been an error.
File is empty. Please upload something more substantial. This error could also 
be caused by uploads being disabled in your php.ini or by post_max_size 
being defined as smaller than upload_max_filesize in php.ini.

There’s two parameters here that you can increase: upload_max_filesize and post_max_size. If you increase these, post_max_size should be larger than upload_max_filesize (more info here).

Next up, if you have a very large site, no matter how much you increase these params, they still might not large enough to upload a single export file. In which case, there are utilities to split the exported data xml into multiple smaller files. Here’s one example here I just and it seemed to work well.

Reducing memory usage by Apache and MySql in a 512MB VPS

Installing with the default Apache and MySQL configurations, you end up with far too many idle Apache threads, and MySQL is the largest memory user by far. The default config of these two is maxing out my available 512MB in my VPS server:

/etc/apache2$ ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r       

%MEM %CPU   RSS    VSZ COMMAND
 9.5  0.0 49904 720256 /usr/sbin/mysqld
 6.7  0.0 35620 286860 /usr/sbin/apache2 -k start
 6.5  0.0 34452 283524 /usr/sbin/apache2 -k start
 6.2  0.0 32692 283012 /usr/sbin/apache2 -k start
 5.9  0.0 31276 283116 /usr/sbin/apache2 -k start
 5.8  0.0 30896 282652 /usr/sbin/apache2 -k start
 5.0  0.0 26724 282588 /usr/sbin/apache2 -k start
 4.8  0.0 25204 279552 /usr/sbin/apache2 -k start
 4.8  0.0 25200 279552 /usr/sbin/apache2 -k start
 4.7  0.0 25156 279508 /usr/sbin/apache2 -k start
 4.4  0.0 23216 279540 /usr/sbin/apache2 -k start
 2.8  0.0 15136 278400 /usr/sbin/apache2 -k start
 0.7  0.0  3968  90908 sshd: myuser [priv] 
 0.4  0.0  2564  61312 /usr/sbin/sshd -D
 0.4  0.0  2264  33188 init
 0.3  0.0  2060  18128 -bash
...

/etc/apache2$ free
             total       used       free     shared    buffers     cached
Mem:        524288     476496      47792      68220          0     260764
-/+ buffers/cache:     215732     308556
Swap:            0          0          0

 

From tips in this article, I reduced down the Apache threads and now we’re at:

/etc/apache2/mods-enabled$ ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r
%MEM %CPU   RSS    VSZ COMMAND
 9.5  0.0 49904 720256 /usr/sbin/mysqld
 2.8  0.0 15132 278400 /usr/sbin/apache2 -k start
 1.1  0.0  6004 278424 /usr/sbin/apache2 -k start
 0.7  0.0  3968  90908 sshd: myuser [priv] 
 0.4  0.0  2564  61312 /usr/sbin/sshd -D
 0.4  0.0  2264  33188 init
 0.3  0.0  2076  18136 -bash

/etc/apache2/mods-enabled$ free
             total       used       free     shared    buffers     cached
Mem:        524288     400244     124044      68220          0     262480
-/+ buffers/cache:     137764     386524
Swap:            0          0          0

Now we’ve got some free space, and definitely not maxing out our available ram. Next up, I’ll take a look at MySQL.