How To Store WordPress Assets on DigitalOcean Spaces With Ubuntu 20.04

Introduction

DigitalOcean Spaces is an object storage service that can be used to store large amounts of diverse, unstructured data. WordPress sites, which often include image and video assets, can be good candidates for object storage solutions. Using object storage for these types of static resources can optimize site performance by freeing up space and resources on your servers. For more information about object storage and WordPress check out our tutorial on How To Back Up a WordPress Site to Spaces.

In this tutorial, you’ll learn how to use a WordPress plugin that works directly with DigitalOcean Spaces as the primary asset store. The DigitalOcean Spaces Sync plugin routes the data of our WordPress media library to Spaces and provides you with various configuration options based on your needs, streamlining the process of using object storage with your WordPress instance.

Prerequisites

This tutorial assumes that you have a WordPress instance on a server you’ll use as a DigitalOcean Space. If you do not have this set up, you can complete the following:

With these prerequisites in place, you’re ready to begin using this plugin.

Modifying WordPress Permissions

Throughout this tutorial, you will be working with the wp-content/uploads folder in your WordPress project, so it is important that this folder exists and has the correct permissions. You can create it with the mkdir command using the -p flag in order to create the folder if it doesn’t exist, and avoid throwing an error if it does:

  • sudo mkdir -p /var/www/html/wp-content/uploads

You can now set permissions on the folder. First, set the ownership to your user (you will use sammy here, but be sure to use your non-root sudo user), and group ownership to the www-data group:

  • sudo chown -R sammy:www-data /var/www/html/wp-content/uploads

Next, establish the permissions that will give the web server write access to this folder:

  • sudo chmod -R g+w /var/www/html/wp-content/uploads

You will now be able to use our plugins to create a store in object storage for the assets in the wp-content/uploads folder, and to engage with your assets from the WordPress interface.

Installing DigitalOcean Spaces Sync

The first step in using DigitalOcean Spaces Sync will be to install it in your WordPress folder. You can navigate to the plugin folder within our WordPress directory:

  • cd /var/www/html/wp-content/plugins

From here, install DigitalOcean Spaces Sync using the wp command:

  • wp plugin install do-spaces-sync

To activate the plugin, you can run:

  • wp plugin activate do-spaces-sync

From here, navigate to the plugins tab on the left-hand side of our WordPress administrative dashboard:

WordPress Plugin Tab

You should see DigitalOcean Spaces Sync in your list of activated plugins:

Spaces Sync Plugin Screen

To manage the settings for DigitalOcean Spaces Sync, navigate to our Settings tab, and select DigitalOcean Spaces Sync from the menu:

Settings Tab

DigitalOcean Spaces Sync will now give you options to configure your asset storage:

DO Spaces Sync Configuration

The Connection Settings field in the top half of the screen asks for your Spaces Access Key and Secret. It will then ask for your Container, which will be the name of your Space, and the Endpoint.

You can determine the endpoint of your Space based on its URL. For example, if the URL of your Space is https://example-name.nyc3.digitaloceanspaces.com, then example-name will be your bucket/container, and nyc3.digitaloceanspaces.com will be your endpoint.

In the plugin’s interface, the Endpoint section will be pre-filled with the default https://ams3.digitaloceanspaces.com. You should modify this endpoint if your Space lives in another region.

Next, you will be asked for File & Path Settings. In the field marked Full URL-path to files, you can input either a storage public domain, if your files will be stored only on your Space, or a full URL path, if you will store them on your Space and server.

For example, if your WordPress project is located in /var/www/html, and you want to store files on both your server and Space, then you would enter:

  • http://your_server_ip/wp-content/uploads in the Full URL-path to files field
  • /var/www/html/wp-content/uploads in the Local path field

The Storage prefix and Filemask settings are prefilled, and do not need to be modified unless you would like to specify certain types of files for your sync.

You will cover the specifics of storing files on your server and Space and on your Space alone in the following sections.

Syncing and Saving Files in Multiple Locations

DigitalOcean Spaces Sync offers the option of saving files to your server while also syncing them to your Space. This utility can be helpful if you need to keep files on your server, but would also like backups stored elsewhere. For this tutorial, you will go through the process of syncing a file to your Space while keeping it on your server. For the purposes of this example, you will assume that you have a file called sammy10x10.png that you would like to store in your media library and on your Space.

First, navigate to the Settings tab on your WordPress administrative dashboard, and select DigitalOcean Spaces Sync from the menu of presented options.

Next, in the Connections Settings field, enter your Spaces Key and Secret, followed by your Container and Endpoint. Remember, if the URL of your Space is https://example-name.nyc3.digitaloceanspaces.com, then example-name will be your Container, and nyc3.digitaloceanspaces.com will be your Endpoint. Test your connections by clicking the Check the Connection button at the bottom of the Connection Settings field:

Check Connection Button

Now you are ready to fill out the File & Path Settings.

In the Full URL-path to files field you can enter our full URL path, since you are saving your file on your server and on your Space. You’ll use your server’s IP here, but if you have a domain, you can swap out the IP address for your domain name. For more about registering domains with DigitalOcean, see our tutorial on How To Set Up a Host Name with DigitalOcean. In this case, the Full URL-path to files will be http://your_server_ip/wp-content/uploads.

Next, you will fill out the Local path field with the local path to the uploads directory: /var/www/html/wp-content/uploads.

Because you are working with a single file, you do not need to modify the Storage prefix and Filemask sections. As your WordPress media library grows in size and variety, you can modify this setting to target individual file types using wildcards and extensions such as *.png in the Filemask field.

Your final File & Path Settings will look like this:

Sync Server and Cloud

Be sure to save your configuration changes by clicking the Save Changes button at the bottom of the screen.

Now you can add the file, sammy10x10.png, to our WordPress media library. You’ll use the wp media import command, which will import the file from your home directory to your WordPress media library. In this case, your home directory will belong to sammy, but in your case this will be your non-root sudo user. As you move the file, you will use the --path parameter to specify the location of our WordPress project:

  • wp media import --path=/var/www/html/ /home/sammy/sammy10x10.png

Looking at the WordPress interface, you should now see the file in your Media Library. You can navigate there by following the Media Library tab on the left side of our WordPress administrative dashboard:

Media Library Tab

If you navigate to your Spaces page in the DigitalOcean control panel, you should also see the file in your Space.

Finally, you can navigate to our wp-content/uploads folder, where WordPress will have created a sub-folder with the year and month. Within this folder you should see our sammy10x10.png file.

Storing Files on Spaces

The DigitalOcean Spaces Sync plugin has an additional option that will allow you to store files only on your Space, in case you would like to optimize space and resources on our server. You will work with another file, sammy-heart10x10.png, and set your DigitalOcean Spaces Sync settings so that this file will be stored only on your Space.

First, let’s navigate back to the plugin’s main configuration page:

DO Spaces Sync Configuration

You can leave the Connection Settings information, but will modify the File & Path Settings. First, in the Full URL-path to files, you will write the storage public domain. Again, you will use your server IP, but you can swap this out for a domain if you have one: http://uploads.your_server_ip

Next, navigate to Sync Settings, at the bottom of the page, and click the first box, which will allow you to “store files only in the cloud and delete after successful upload.” Your final File & Path Settings will look like this:

Sync Cloud Only

Be sure to save your changes by clicking the Save Changes button at the bottom of the screen.

Back on the command line, move sammy-heart10x10.png from your user’s home directory to your Media Library using wp media import:

  • wp media import --path=/var/www/html/ /home/sammy/sammy-heart10x10.png

If you navigate back to your WordPress interface, you will not see sammy-heart10x10.png or sammy10x10.png in your Media Library. Next, return to the command line and navigate to your wp-content/uploads directory — you should see that sammy-heart10x10.png is missing from your timestamped sub-folder.

Finally, if you navigate to the Spaces page in the DigitalOcean control panel, you should see both files stored in your Space.

Conclusion

This tutorial covered two different options you can use to store your WordPress media files to DigitalOcean Spaces using DigitalOcean Spaces Sync. This plugin offers additional options for customization, which you can learn more about by reading the developer’s article “Sync your WordPress media with DigitalOcean Spaces.”

If you would like more general information about working with Spaces, check out our introduction to DigitalOcean Spaces and our guide to best practices for performance on Spaces.

Originally posted on DigitalOcean Community Tutorials
Author: Lyn Muldrow

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *