Skip to content

Instantly share code, notes, and snippets.

@fjarrett
Last active December 16, 2021 23:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fjarrett/c3ababe489c55bd1a8907d388f3f7c29 to your computer and use it in GitHub Desktop.
Save fjarrett/c3ababe489c55bd1a8907d388f3f7c29 to your computer and use it in GitHub Desktop.
How to use a Ceph (or S3) bucket for WordPress uploads

How to use a Ceph (or S3) bucket for WordPress uploads

Listed below are the steps I took to use Ceph object store for WordPress media without a plugin. It works by mounting Ceph (or an AWS S3) bucket as a network device on the file system via s3fs and using wp-content/uploads/ as the mount path.

Since s3fs is POSIX compatible, it means you can still access (and manage) the media within wp-content/uploads/ over SFTP/SSH as if they are natively there.

WP-CLI commands such as wp media import and wp media regenerate also still work.

Although your media is being stored and fetched from a network storage bucket, your web server can still resolve all the requests to wp-content/uploads/ on the local filesystem like normal.

This means that image URLs do not have to be rewritten and will use the core format you're used to over HTTP https://mysite.com/wp-content/uploads/cat.jpg and any absolute filesystem paths in PHP that may exist /path/to/wp-content/uploads/cat.jpg.

Things To Know

  1. I used a standard WordPress 4.7.3 install on an Ubuntu 16.04.2 VPS running Apache 2.4.18 and PHP 7.1.3.
  2. Using a s3fs mount isn't super fast. I noticed the most blantant lag when uploading image(s) to the Media Library. I had to wait what seemed like 60 seconds just for the thumbnail preview of one image to appear.
  3. goofys is said to be 2-10x faster, but I haven't tried it (yet). It has less POSIX support.

Setup

  1. Install the s3fs program:
sudo apt-get install s3fs
  1. Save default credentials:
sudo echo MYACCESSKEY:MYSECRETKEY > /etc/passwd-s3fs
sudo chmod 600 /etc/passwd-s3fs
  1. Uncomment the user_allow_other config in fuse.conf:
sudo vi /etc/fuse.conf
  1. Add a config to /etc/fstab so your bucket will be mounted on boot:
sudo vi /etc/fstab
  • Change the bucketname, uploads path, and url option to fit your setup
  • For S3 buckets, remove the use_path_request_style and url options
  • The uid and gid options are used to set the ownership of wp-content/uploads/ once it's mounted. Set these to match your $APACHE_RUN_USER and $APACHE_RUN_GROUP envvars, respectively.
s3fs#bucketname    /path/to/wp-content/uploads    fuse    _netdev,allow_other,use_path_request_style,no_check_certificate,mp_umask=022,uid=www-data,gid=www-data,url=https://storage.api.hostname.com    0    0
  1. Make sure wp-content/uploads/ is empty
rm -Rf /path/to/wp-content/uploads && mkdir /path/to/wp-content/uploads
  1. Mount all file systems that are defined in /etc/fstab:
sudo mount -a

Other

List all file system mounts:

sudo df -h

Unmount the bucket from the file system:

sudo umount s3fs
@manni901
Copy link

Hi
Just wanted to know your views about using this setup in a production environment.
Is s3fs stable enough?

@fams
Copy link

fams commented Aug 23, 2017

I'm using S3Fs in production for 2 years. Its very stable. you can pass a local cache to s3fs. This will improve read a lot! The only problem with this aproach, do not owerwrite files , the cache versions will not update ad s3fs do not provide any way to invalidate cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment