Make sure we have installed what we need

Installing dependenciesAnsible (version 2.0.2.0)

Because the Ansible version required by Trellis is currently less than the latest 2.2 (and using the latest does cause problems) I use virtualenvwrapper to avoid conflicts.

With virtualenvwrapper, mkvirtualenv creates a new environment, workon opens an existing one and deactivate exits.

$ mkvirtualenv trellis
(trellis)$ pip install ansible==2.0.2.0

Now you just need to remember to use workon trellis before using ansible with trellis.

VirtualBox

MacOS (with homebrew and cask):

$ brew cask install virtualbox

Ubuntu (or other Debian based distros):

$ sudo apt-get virtualbox

Vagrant and Plugins

To get the latest vagrant download and install manually, the package managers are lagging behind: https://www.vagrantup.com/downloads.html

$ vagrant plugin install vagrant-bindfs
$ vagrant plugin install vagrant-hostmanager

Create the project

I’ve created a bash script that I keep in my project root folder. It is setup to: create project folder, download trellis, download bedrock and install trellis dependencies.

Run it using: $ ./newtrellis.sh projectname

#!/bin/bash
# Colours used in messages
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# Check if we have an argument for folder
if [[ $# -eq 0 ]] ; then
    echo -e "${RED}No project folder name${NC}"
    exit 0
fi

# Use the first argument as Project folder name
PROJECT=$1
echo -e "${GREEN}Project folder name: ${PROJECT} ${NC}"

# Create directory if it doesn't already exist
if [ ! -d "$PROJECT" ]; then
    echo -e "${GREEN}Creating project folder: ${PROJECT} ${NC}"
    mkdir "$PROJECT"
fi

echo -e "${GREEN}Entering project folder: ${PROJECT} ${NC}"
cd "$PROJECT"

# Exit if trellis folder already exists
if [ -d 'trellis' ]; then
    echo -e "${RED}trellis folder already exists.\nExiting!${NC}"
    exit 0
fi

# Clone trellis and remove .git folder
echo -e "${GREEN}Download Trellis from git into trellis folder${NC}"
git clone --depth=1 git@github.com:roots/trellis.git
rm -rf trellis/.git

# Exit if site folder already exists
if [ -d 'site' ]; then
    echo -e "${RED}Site folder already exists.\nExiting!${NC}"
    exit 0
fi

# Clone bedrock and remove .git folder
echo -e "${GREEN}Download Bedrock from git into site folder${NC}"
git clone --depth=1 git@github.com:roots/bedrock.git site
rm -rf site/.git

# Install trellis dependencies
echo -e "${GREEN}Installing Trellis dependencies${NC}"
cd trellis
ansible-galaxy install -r requirements.yml

Referencehttps://roots.io/trellis/docs/installing-trellis/

Add a .gitignore file to the root. Here’s a good start:

_resources
.DS_Store
~*
nbproject
*.sqle
*.sql.gz
*.tar.gz
*.zip
*.log
.sass-cache/
cache/
log/

Vagrant file line 59 add   config.vm.define main_hostname to set the display name in vagrant

$ git init
$ git add .
$ git commit -m'Initial commit after trellis install'

Set up some Trellis config. Edit these 2 files with your domain:

  • trellis/group_vars/development/wordpress_siles.yml
  • trellis/group_vars/development/vault.yml

Move Vagrant file out of trellis into the project root then make some edits to it:

Replace  ANSIBLE_PATH = __dir__ with ANSIBLE_PATH = File.join(__dir__, 'trellis').

Comment out config.vm.box_version = '2.2.9' because we want the most recent box.

Some Nginx config

create directory and file

$ mkdir nginx-includes
$ vi nginx-includes/wordpress-site.conf.child

Include the content so that all not-found images in uploads gets redirected to the live site:

{% extends 'roles/wordpress-setup/templates/wordpress-site.conf.j2' %}

# The PHP element is the same as the original, I've just added the image part
{% block location_uploads_php -%}
# Prevent PHP scripts from being executed inside the uploads folder.
location ~* /app/uploads/.*\.php$ {
 deny all;
}

# redirect images to the live site
location ~* /(?:app|wp-content)/uploads/(.*)\.(svg|jpg|jpeg|png|gif)$ {
 error_page 404 = @redirect_images;
}
location @redirect_images {
 return 302 $scheme://www.cbneurope.com/wp-content/uploads/$1.$2;
}
{% endblock %}

And add the following line to group_vars/development/main.yml

nginx_wordpress_site_conf: nginx-includes/wordpress-site.conf.child


I use compass in my theme and when I added capistrano, I get a compass error complaining about missing dependencies. This is because capistrano uses bundler to install gems locally. So I just needed to add compass and sass etc to the Gemfile in project root:

gem 'compass'
gem 'compass-import-once'
gem 'bootstrap-sass'
Advertisements