Friendly WordPress developer automation with Trellis

Davide Covato
4 min readAug 20, 2019

--

A gentle introduction of WordPress to DevOps

WordPress plus Trellis
WordPress plus Trellis

In the past, I used WordPress a few times, much like any non-techie guys. This time, I wanted to be a little more technical and verify if WordPress can be automated.

So I spent several days researching and reading articles, including one from an ex-colleague. Thus, I was convinced to give Trellis a try from Roots. It’s not a perfect solution yet, but it’s good enough to start with.

Preparing the machine for Trellis

I have to admit that I didn’t apply all the given instructions provided on the Trellis documentation. Any difference will be pointed out.

Before starting to use Trellis, we have to install some dependencies:

Trellis also requires Composer, but I didn’t use it. I don’t like installing extra dependencies if I can avoid it. Also, you need to install git if not already available.

For vagrant, I also used the following plugins:

Additionally, Trellis installs any missed plugin such as vagrant-bindfs.

Note: I assume you know how to install the above dependencies. If you have problems, leave a comment, and I will do my best to assist you. Also, the reported versions are the latest at the time of writing .

Setting up a Trellis project

The dependencies are installed. Now, it’s time to set up your first Trellis project.

I assume you already have your projects folder; all the commands are relative to that folder.

Open your terminal (I use iTerm2 on macOS):

host> mkdir myblog && cd myblog
host> git clone --depth=1 git@github.com:roots/trellis.git && rm -rf trellis/.git trellis/.circleci
host> git clone --depth=1 git@github.com:roots/bedrock.git && rm -rf site/.git site/.circleci

The myblog project folder structure is similar to:

myblog/           # → Root folder for the project
├── trellis/ # → Your clone of Trellis repository
└── site/ # → A Bedrock-based WordPress site
└── web/
├── app/ # → WP content directory (themes, plugins, etc.)
└── wp/ # → WP core (don't touch!)

Before running our Trellis project, some changes were required.

What I didn’t mention is that using Trellis is like developing a modern PHP project using Composer, all dependencies are explicit and manageable.

Some project changes

Vagrantfile

Open the file trellis/Vagrantfile and modify as follow:

  • before the line Vagrant.require_version '>= 2.1.0'’ insert the line below:
ENV[“LC_ALL”] = “en_US.UTF-8”
  • before the line provisioner = local_provisioning? ? :ansible_local : :ansible insert the lines below:
  $script = <<-SCRIPT
set -e
# check pip available
if ! command -v pip > /dev/null 2>&1 ; then
sudo apt-get update
sudo apt-get -yy install python-pip
else
echo "pip was already installed"
fi
SCRIPT
config.vm.provision "shell", inline: $script

Without the provisioning shell above, Ansible fails the execution of the tasks on the guest machine due to pip missing.

Development environment

Apply the following changes on the development environment:

  • open the file trellis/group_vars/development/wordpress_sites.yml and replace all the occurrences of example with myblog
  • open the file trellis/group_vars/development/vault.yml and replace all the occurrences of example with myblog.

Running Trellis project

Before running the project, it’s time to review what you have done until now. We installed the Trellis dependencies, we set up the project and applied some changes.

Your terminal should still be opened, run the following commands:

host> cd trellis
host> vagrant up
vagrant up command running
vagrant up command running

The WordPress blog will not be available before 10/15 minutes.

During the installation process you could be asked to type your password to permit mounting your shared folder. See the image below.

Prompt password mounting shared folder
Prompt password mounting shared folder

At the end of the execution of vagrant up, you should get a message similar to the image below.

Vagrant box is ready to use
Vagrant box is ready to use

Navigating the blog

WordPress is up and running. Now, we can open our favourite browser at http://myblog.test, and you can see the blog similarly to the image below.

myblog.test homepage
myblog.test homepage

As you can see, Trellis took care of everything. We used a domain name and not an IP to access our blog. The image below shows the admin area.

myblog.test admin aread
myblog.test admin aread

Call to Action

On GitHub, I created a project template wpTemplateTrellis ready to use.

On the next article, we’ll see how to deploy on production.

--

--

Davide Covato

Hi, I’m software #developer and passionate about #technology. — Think as a start-up and act as an enterprise —