Moving wp-content to block storage volume

May 29, 2017 1.3k views
WordPress Block Storage Ubuntu

Because I'm running out of space, I'm trying to move my wp-content folder to a block storage volume. (from /var/www/html/ to /mnt/folder)

I tried creating a symlink from the wp-content to the new wp-content folder but that doesn't seem to work. Then I tried using the following code in the wp-config file:

    //define('WP_CONTENT_DIR', '../../../../mnt/folder/wp-content');
    //define('WP_CONTENT_URL', 'https://site/wp-content');
    //define('WP_PLUGIN_DIR', '../../../../folder/content/plugins'); 
    //define('WP_PLUGIN_URL', 'https://site/wp-content/plugins');
    //define( 'PLUGINDIR', '../../../../mnt/folder/content/plugins');

It seemed to work, for the most part. But when I enable any plugins I get an MSQL warning saying there are too many connections. I get locked out of the admin dashboard but I can fix it by renaming the plugins folder(to disable the plugins).
I found some information here:
https://www.digitalocean.com/community/questions/how-can-i-connect-wordpress-media-library-to-one-or-multiple-block-storage-volumes

I'd really appreciate some help.

4 Answers
hansen May 29, 2017
Accepted Answer

@jtittle I'm not sure if you'll agree with this - please alert @joostvanhoutte if not

@joostvanhoutte
I don't think DO has an option like that, since that's what BS is there for.

First things first. Take a snapshot for your droplet, so you can revert if everything goes horribly wrong :)

Move your uploads so we don't mess with current data:

mv /var/www/html/wp-content/uploads /var/www/html/wp-content/uploads-old

Unmount the current mount:

umount /mnt/bs-name-mount

Create the new empty uploads folder and set the correct permissions. I'm guessing you're running as www-data:

mkdir /var/www/html/wp-content/uploads
chown www-data:www-data /var/www/html/wp-content/uploads

Edit fstab and mount directly to /var/www/html/wp-content/uploads instead of /mnt/bs-name-mount:

nano /etc/fstab

Remount the new mount point:

mount -a

Copy the uploads-old to the new BS mount:

cp -Rp /var/www/html/wp-content/uploads-old/* /var/www/html/wp-content/uploads/

Please reboot the server to confirm that the fstab is mounting correctly on reboot before getting rid of the uploads-old.
When you've confirmed that you're running on the new BS mount (upload a new file and see where it's placed), you can safely remove the uploads-old folder:

rm -RI /var/www/html/wp-content/uploads-old

Hi @joostvanhoutte

May I recommend that you do not move the entire wp-content folder, because it will slow down your site.

You should only be moving the uploads folder, since it doesn't contain files that require to be loaded in memory often (like the theme and plugins).

Something like this should be enough:

define( 'UPLOADS', '/mnt/folder/uploads');
  • @joostvanhoutte @hansen

    Unfortunately, that won't work.

    You need to provide a relative path that works its way back to /mnt, otherwise the directory you define using UPLOADS will be automatically created in the home directory.

    For example, using:

    define( 'UPLOADS', '/mnt/folder/uploads');
    

    If you're home directory (where index.php) is:

    /home/myuser/public
    

    Using that define construct will result in mnt being created at:

    /home/myuser/public/mnt
    

    So you'd only be moving the uploads to the home directory.

    You'd need to use something such as:

    define('UPLOADS', '../../../../mnt/path/to/uploads');
    

    Yes, it's weird. I don't know why WordPress requires such path definitions, but it does and it's really annoying :-).

    That said, the above will cause issues too as it will change the URL's for your uploads to look like:

    http://mydomain.com/../../../../mnt/path/to/uploads/myfile.png
    

    Which won't work and looks awful.

    Using the WP_CONTENT_* and WP_PLUGIN_* constants is what you need to use, though they too cause issues. While files are uploaded and directories are created, it's as if WordPress just won't read the files, even when proper permissions are set.

    I actually have a droplet deployed right now and am testing both -- neither seem to work as they should. But the issue isn't block storage, it's in how WordPress handles these types of things.

    I can create a standard PHP script to test the same kind of pull from block storage and it works just fine, and as expected. WordPress, despite having these options, seems to handle things much differently.

    • @jtittle Interesting, I didn't know WP was so bad at handling scattered system.
      I have only a single server with massive storage, where I've setup the mount to the exact WP uploads folder - that way WP don't need to think about special rights, linking or anything else.

    • PS. You really don't want to put themes, plugins etc in Block Storage - it's going to slow down your site, since BS is network based, meaning you're "only" connecting over 1GB with "high" latency compared to local storage, which is SSD based.

  • @hansen @jtittle Thanks for the input guys! I guess my only option right is to just increase the size of the storage on my droplet then? Do you think it would be possible to contact DO and ask them to just upgrade the storage size? I'm currently paying $40/month and I have about 1GB of space left, but the other specs are already needlessly overpowered. If I were to upgrade I'd effectively be paying double for an extra 20GB of storage.

Have another answer? Share your knowledge.