DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • Getting Started With the YugabyteDB Managed REST API
  • Top 10 Pillars of Zero Trust Networks
  • How To Approach Java, Databases, and SQL [Video]

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • Getting Started With the YugabyteDB Managed REST API
  • Top 10 Pillars of Zero Trust Networks
  • How To Approach Java, Databases, and SQL [Video]
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Practical Guide: Setup S3 Upload for Wordpress with Zero Code Change

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

Bruce Beibei Li user avatar by
Bruce Beibei Li
·
Apr. 20, 18 · Interview
Like (3)
Save
Tweet
Share
6.26K Views

Join the DZone community and get the full member experience.

Join For Free

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:

access-key:access-secret-key


), 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
./autogen.sh
./configure
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.

AWS Upload WordPress

Opinions expressed by DZone contributors are their own.

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • Getting Started With the YugabyteDB Managed REST API
  • Top 10 Pillars of Zero Trust Networks
  • How To Approach Java, Databases, and SQL [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: