Practical Guide: Setup S3 Upload for Wordpress with Zero Code Change

DZone 's Guide to

Practical Guide: Setup S3 Upload for Wordpress with Zero Code Change

· Cloud Zone ·
Free Resource

Recently I was managing a seemingly small project alongside the major financial projects—the Intranet project.

Long story short, since it's fairly simple we outsourced the project to an advertising/web agency, and they don't quite understand how AWS/S3 to work, especially when our stacks are all set up with CI/CD pipelines and one-click deployments, I've tried to ask them to find an S3 upload plugin but that didn't work.

Luckily I found a solution that doesn't require special plugins or code changes, and as a practical guide, I'm just gonna put the actual commands down here so you can simply drop it into your provision scripts (or if you are using bitbucket/AWS code deploy like me, put it in your post-deploy.sh file).

The solution is to mount an S3 bucket onto your EC2 instance, using S3FS, so you can just use it as a local drive. This eliminates the need for CDN/S3 upload configs/etc, but if you like, you can still set up a CDN using CloudFront and just make sure you put the right domain in your media options.

This can be done in 3 simple steps:

Step 1. Setup a bucket for your uploads, create an IAM user for that bucket, and make sure you combine the access key and secret into a password file for s3fs (format:


), you can store this in a secure s3 bucket and use IAM to allow the build process to copy it down to the EC2 instance for s3fs access. In here I'm gonna presume you have the password file in /etc/.password-s3fs.

Step 2. Install s3fs on your machine - we'll use amazon linux as an example here (for other systems please refer to the wiki: https://github.com/s3fs-fuse/s3fs-fuse/wiki/Installation-Notes for details):

# Setup s3fs for uploads
cd /tmp

# install all dependencies
sudo yum install -y ruby telnet dos2unix automake fuse fuse-devel gcc-c++ git libcurl-devel libxml2-devel make openssl-devel

# install s3fs-fuse
git clone https://github.com/s3fs-fuse/s3fs-fuse.git
cd s3fs-fuse
sudo make
sudo make install

Step 3. Mount the drive, link to your wordpress wp-content/uploads folder

# make sure password file is protected
sudo chmod 400  /etc/.passwd-s3fs

# create dir and mount
sudo mkdir -p /mnt/s3storage

# mount with 'allow others permission' so wordpress can write to it
sudo /usr/local/bin/s3fs my-s3-bucket /mnt/s3storage -o allow_other -o stat_cache_expire=10 -o enable_noobj_cache -o enable_content_md5 -o url="http://s3-ap-southeast-2.amazonaws.com" -o endpoint=ap-southeast-2 -o umask=022 -o passwd_file=/etc/.passwd-s3fs

# NOTE: if you are deploying the wordpress site using CI/CD, make sure you move the part below in 'post-deploy.sh' or its equivalent
# symlink for upload
sudo ln -s /mnt/s3storage/ /var/www/my-wordpress-site/wp-content/uploads

# fix permission so your wordpress site may upload to it
sudo chown apache /var/www/my-wordpress-site/wp-content/uploads
sudo chown -R apache /var/www/my-wordpress-site/wp-content/uploads/

Note: replace 'my-s3-bucket' with your own bucket name, sans the 's3://' bit. Also make sure you have the right region specified.

If you are running CI/CD please make sure you put the symlink part after the deployment process - otherwise your wordpress deployment will wipe the uploads directory.

If you are running nginx / php-fpm or apache, chown the directory to 'apache', otherwise if you are not sure what user it's running under, just go to your index.php and put in 

echo exec('whoami');

and find out what user it is, and replace 'apache' with the user for your own setup.

There you go, now your devs don't need to know how s3 works and they can just directly upload to /uploads just like a default wordpress installation.

s3 file upload ,s3fs ,wordpress

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}