David Lohmeyer's Blog

Using Wordpress with Amazon S3 and Git

When you get into larger Wordpress sites that have a very large /wp-content/uploads folder, things start to get a little hairy if you're using Git.  Best practices with Git say to not have binaries like images and media files under version control in order to keep your code base clean, but then it means we can't really use Git for backing up production files like images.

So what do we do?  Setting up an Amazon S3 cloud sync is an obvious answer since S3 is meant to store files.

A few tools can help with this.

s3cmd

s3cmd is a command line tool that has a 'sync' command with Amazon S3 buckets.  This can be put into a shell script and executed at any interval in order to sync a Wordpress uploads folder to a bucket folder structure.  Here's a sample script:

#blog.acton.org
/usr/bin/s3cmd sync setacl --acl-public --recursive --delete-removed --skip-existing /home/web/actonweb/pb3.acton.org/wp-content/uploads/ s3://s3.acton.org/wordpress/blog.acton.org/wp-content/uploads/

Intelligent .gitignore files

Include the following .gitignore file in wp-content/uploads:

*
!.gitignore
!.htaccess

Now include the following .htaccess file.  You'll need the public URL to your S3 bucket:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule (.*) https://s3.amazonaws.com/s3.acton.org/wordpress/blog.acton.org/wp-content/uploads/$1
</IfModule>

This htaccess will allow your development environment to still display images without having them checked in on your machine via S3.

Related Posts