Tutorial: Connect a WordPress website in Lightsail to an Amazon S3 bucket
This tutorial describes the steps required to connect your WordPress website running on an Amazon Lightsail instance to an Amazon Simple Storage Service (Amazon S3) bucket to store website images and attachments. To do this, you configure a WordPress plugin with a set of Amazon Web Services (AWS) account credentials. The plugin then creates the Amazon S3 bucket for you and configures your website to use the bucket instead of the instance’s disk for website images and attachments.
Contents
Step 1: Complete the prerequisites
Before you get started, create a WordPress instance in Lightsail, and make sure it’s in a running state. For more information, see Tutorial: Launch and configure a WordPress instance.
Step 2: Install the WP Offload Media plugin on your WordPress website
You must use a plugin to configure your website to use an Amazon S3 bucket. Many plugins are
available to configure this; one such plugin is WP Offload Media
Lite
Complete the following steps to install the WP Offload Media plugin on your WordPress website:
-
Sign in to your WordPress dashboard as an administrator.
For more information, see Getting the application user name and password for your Bitnami instance in Amazon Lightsail.
-
Hover over Plugins in the left navigation menu, and choose Add New.
-
Search for WP Offload Media Lite.
-
In the search results, choose Install Now next to the WP Offload Media plugin.
-
Choose Activate after the plugin is done installing.
-
In the left navigation menu, choose Settings, then choose Offload Media.
-
In the Offload Media page, choose Amazon S3 as the storage provider, then choose Define access keys in wp-config.php.
With this option, you must add your AWS account credentials to the
wp-config.php
on the instance. These steps are covered later in this tutorial.Leave the Offload Media page open; you will return to it later in this tutorial. Continue to the Step 3: Create an IAM user and policy section of this tutorial.
Step 3: Create an IAM user and policy
The WP Offload Media plugin requires access to your AWS account to create the Amazon S3 bucket, and to upload your website images and attachments.
Complete the following steps to create a new AWS Identity and Access Management (IAM) user and policy for the WP Offload Media plugin:
-
Open a new browser tab, and sign in to the IAM console
. -
In the left navigation menu, choose Users.
-
Choose Add user.
-
In the User name text box, enter a name for the new user. Enter something descriptive, such as
wp_s3_user
orwp_offload_media_plugin_user
, so that you can easily identify it in the future when performing maintenance. -
Under the Access type section, choose Programmatic access.
-
Choose Next: Permissions.
-
Choose Attach existing policies directly, search for S3, and then choose AmazonS3FullAccess in the search results.
-
Choose Next: Tags, and then choose Next: Review.
-
Review the user details displayed on the page, then choose Create user.
-
Make note of the access key ID and secret access key for the user, or choose Download .csv to save a copy of these values to your local drive. You will need these in the next few steps when editing the
wp-config.php
file on the WordPress instance.
Step 4: Edit the WordPress configuration file
Complete the following steps to connect to your WordPress instance using the browser-based
SSH client in the Lightsail console and edit the wp-config.php
file.
The wp-config.php
file contains your website’s base configuration
details, such as database connection information.
Note
You can also connect to your instance using your own SSH client. For more information, see Download and set up PuTTY to connect using SSH in Amazon Lightsail
-
Sign in to the Lightsail console
. -
Choose the browser-based SSH client icon for the WordPress instance.
-
In the SSH client window that appears, enter the following command to create a backup of the
wp-config.php
file in case something goes wrong:sudo cp /opt/bitnami/wordpress/wp-config.php /opt/bitnami/wordpress/wp-config.php.backup
-
Enter the following command to open the
wp-config.php
file usingnano
, a text editor:nano /opt/bitnami/wordpress/wp-config.php
-
Enter the following text above the
/* That's all, stop editing! Happy blogging. */
text.Be sure to replace
AccessKeyID
with the access key ID andSecretAccessKey
with the secret access key of the IAM user you created earlier in these steps.define( 'AS3CF_SETTINGS', serialize( array( 'provider' => 'aws', 'access-key-id' => '
AccessKeyID
', 'secret-access-key' => 'SecretAccessKey
', ) ) );Example:
define( 'AS3CF_SETTINGS', serialize( array( 'provider' => 'aws', 'access-key-id' => '
AKIAIOSFODNN7EXAMPLE
', 'secret-access-key' => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
', ) ) );The result should look like the following example:
-
Press
Ctrl+X
to exit Nano, then pressY
, andEnter
to save your edits to thewp-config.php
file. -
Enter the following command to restart the services on the instance:
sudo /opt/bitnami/ctlscript.sh restart
You will see a result similar to the following when the services have restarted:
Close the SSH window and toggle back to the Offload Media page that you left open earlier in this tutorial. You are now ready to create the Amazon S3 bucket using the WP Offload Media plugin.
Step 5: Create the Amazon S3 bucket using the WP Offload Media plugin
Now that the wp-config.php
file is configured with the AWS
credentials, you can return to the Offload Media page to complete the
process.
Complete the following steps to create the Amazon S3 bucket using the WP Offload Media plugin.
-
Refresh the Offload Media page, or choose Next.
You should now see that the Amazon S3 provider is configured.
-
Choose Create new bucket.
-
In the Region drop-down menu, choose the desired AWS Region. We recommend that you choose the same region in which your WordPress instance is located.
-
In the Bucket text box, enter a name for the new S3 bucket.
-
Choose Create New Bucket.
The page refreshes to confirm that a new bucket was created. Review the settings that appear and adjust them accordingly to how you want your WordPress website to behave.
From now on, images and attachments added to blog posts are automatically uploaded to the Amazon S3 bucket that you created.
Step 6: Next steps
After you’re done connecting your WordPress website to an Amazon S3 bucket, you should create a snapshot of your WordPress instance to back up the changes you made. For more information, see Create a snapshot of your Linux or Unix instance.