Run Multiple Sites on a Single Vagrant Box with Bedrock and Trellis

This tutorial is no longer maintained.

It may still be fully functional as-is; exercising caution is recommended.
Please be sure to test thoroughly.

I’ve been hunting for an easy way to run multiple sites on a single vagrant box and kept coming up short. Maybe I’ve gotten rusty on my Googling skills but there was not much turning up for me. I could run several sites under VVV, but every time I’d spin up a fresh site something new would break.

Over the weekend I picked up my search and happened across Bedrock which can be setup with Trellis and made to manage multiple sites on a single vagrant box, ftw.

It’s not the most straight-forward process but the community behind Bedrock and Trellis is pretty strong so I was able to track down a few posts about people attempting to achieve the same thing.

Individually both of these setups are pretty sweet, but Trellis also lets you easily manage development, staging and production environments.

Here’s how I managed to get it all setup and running for development on localhost.

Install Virtual Box and Vagrant

If you’ve already got the latest version of VirtualBox and Vagrant installed you can skip ahead to the next step.

  1. Install VirtualBox
  2. Install Vagrant

If you’re not sure what version of Vagrant you have installed you can check with the following:

$ vagrant --version

New to Vagrant? Check out Vagrant’s command-line documentation.

Install / Update Dependencies

Bedrock and Trellis have required dependencies that must be installed on your computer before installation can be successful.

Bedrock Requirements

First, be sure you have PHP 5.5 installed.

$ php --version

Also, check that you have Composer installed.

$ which composer

If you don’t yet have it, go ahead and install Composer.

Trellis Requirements

This is the longer list, I hope you set aside a reasonable amount of time to get them up and running.

  1. Update or install Ansible
  2. If you already had VirtualBox installed, check you’re running version 4.3.10 or higher
  3. If you already had Vagrant installed, you’ll need version 1.5.4 or higher
  4. Update or install vagrant-bindfs — requires 0.3.1 or higher
  5. Install vagrant-hostsupdater

Easy, right? Right. Let’s rock out with the next steps.

Install Trellis

First, let’s create a directory where we want to run the whole shebang. I tucked my parent directory in the Documents folder of my computer.

$ cd /Users/erica/Documents/
$ mkdir fatpony && cd fatpony

Next, clone the Trellis repo into your new directory.

$ git clone https://github.com/roots/trellis.gitCode language: PHP (php)

Hop into the Trellis directory and install the external Ansible roles/packages.

$ cd trellis
$ ansible-galaxy install -r requirements.yml

Install Bedrock

Time to get to installing Bedrock for each site we want to run on our Vagrant Box. First we need to return to the parent directory.

$ cd ../

I’m now in the fatpony directory I created. Next I’m going to clone Bedrock into my fatpony directory and rename the bedrock directory so I can easily identify which site it contains.

$ git clone https://github.com/roots/bedrock.git
$ mv bedrock writingofridingCode language: PHP (php)

I’ve renamed my first Bedrock site directory writingofriding and am going to repeat this process two more times so I’m running 3 sites on a single Vagrant Box. You can run more than 3 sites under a single Vagrant Box so if you need more just repeat as necessary.

$ git clone https://github.com/roots/bedrock.git
$ mv bedrock horsepress
$ git clone https://github.com/roots/bedrock.git
$ mv bedrock jobifarmCode language: PHP (php)

You should now have a file structure that resembles the following:

fatpony/               - Primary folder for the project
├── trellis/           - Trellis
├── jobifarm/          - First bedrock-based site
├── horsepress/        - Second bedrock-based site
└── writingofriding/   - Third bedrock-based site

Run Composer

Let’s pop into each site’s directory and run composer. You can add dependencies before you run composer the first time to the composer.json file in each site’s directory, or do it later and run composer update from the command-line.

$ cd /jobifarm/
$ composer install
$ cd ../
$ cd /horsepress/
$ cd ../
$ composer install
$ cd /writingofriding/
$ composer install

We don’t need to configure the .env files for each site since these will be generated when we run vagrant up the first time.

Trellis Development Setup

I’m only running through the development setup of Trellis in this article since your own needs will vary for staging and production. Head over to the Trellis documentation for help with staging and production setup.

Open trellis/group_vars/development/wordpress_sites.yml and you’ll be greeted with the following:

# Documentation: https://github.com/roots/trellis#wordpress-sites
wordpress_sites:
  example.com:
    site_hosts:
      - example.dev
    local_path: ../site # path targeting local Bedrock site directory (relative to Ansible root)
    repo: [email protected]:roots/bedrock.git
    site_install: true
    site_title: Example Site
    admin_user: admin
    admin_password: admin
    admin_email: [email protected]
    multisite:
      enabled: false
      subdomains: false
    ssl:
      enabled: false
    cache:
      enabled: false
      duration: 30s
    system_cron: true
    env:
      wp_home: http://example.dev
      wp_siteurl: http://example.dev/wp
      wp_env: development
      db_name: example_dev
      db_user: example_dbuser
      db_password: example_dbpasswordCode language: PHP (php)

Keep in mind the default is to run a single site under Trellis, so we need to modify this file to setup not only our first site, but two more (or however many you decide to create).

I’ve copied lines 3-28 and pasted them at the bottom of the file twice so the file now contains structure for 3 sites. Next I’ve configured each site as I require. If you’re unsure how to configure each site checkout the Trellis documentation on configuring the wordpress_sites.yml file.

# Documentation: https://github.com/roots/trellis#wordpress-sites
wordpress_sites:
## Jobi Farm Site
  jobifarm.com:
    site_hosts:
      - jobifarm.dev
    local_path: ../jobifarm # path targeting local Bedrock site directory
    repo: [email protected]:ericakfranz/jobifarm.git
    site_install: true
    site_title: Jobi Farm
    admin_user: admin
    admin_password: password
    admin_email: [email protected]
    multisite:
      enabled: false
      subdomains: false
    ssl:
      enabled: false
    cache:
      enabled: false
      duration: 30s
    system_cron: true
    env:
      wp_home: http://jobifarm.dev
      wp_siteurl: http://jobifarm.dev/wp # DO NOT REMOVE /wp from the url
      wp_env: development
      db_name: jobifarm
      db_user: jobifarm_usr
      db_password: jobifarm_pswrd

## HorsePress Site
  horsepress.com:
    site_hosts:
      - horsepress.dev
    local_path: ../horsepress
    repo: [email protected]:ericakfranz/horsepress.git
    site_install: true
    site_title: HorsePress
    admin_user: admin
    admin_password: password
    admin_email: [email protected]
    multisite:
      enabled: false
      subdomains: false
    ssl:
      enabled: true
    cache:
      enabled: false
      duration: 30s
    system_cron: true
    env:
      wp_home: https://horsepress.dev
      wp_siteurl: https://horsepress.dev/wp
      wp_env: development
      db_name: horsepress
      db_user: horsepress_usr
      db_password: horsepress_pswrd

## Writing of Riding Site
  writingofriding.com:
    site_hosts:
      - writingofriding.dev
    local_path: ../writingofriding
    repo: [email protected]:ericakfranz/writingofriding.git
    site_install: true
    site_title: Writing of Riding
    admin_user: admin
    admin_password: password
    admin_email: [email protected]
    multisite:
      enabled: false
      subdomains: false
    ssl:
      enabled: false
    cache:
      enabled: false
      duration: 30s
    system_cron: true
    env:
      wp_home: http://writingofriding.dev
      wp_siteurl: http://writingofriding.dev/wp
      wp_env: development
      db_name: writingofriding
      db_user: writingofriding_usr
      db_password: writingofriding_pswrdCode language: PHP (php)

Save the file and we’re ready to run Vagrant! Make sure you’re inside the trellis directory when you do this. We last left off inside one of the bedrock-based site directories so we need to move back into the main directory then into /trellis/ before running vagrant up.

$ cd ../
$ cd /trellis/
$ vagrant up

Load Your Sites

Your sites are now ready to view in the browser, and to login and begin developing! Remember the wp/ url slug gives you access to the admin, so instead of http://example.com/wp-login.php you’d have to navigate to http://example.com/wp/wp-login.php

Additional Resources

About Bedrock · Github Repo · Community Discussions
About Trellis · Github Repo · Community Discussions

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

12 Comments

  1. Hi Erica!

    Great article! I was trying to figure this out and it’s actually kinda simple after reading your article 🙂

    I was wondering if you’ve also deployed websites using this methodology? Do you think it would it be possible to deploy multiple WordPress installations to a single VPS using this?

    Regards,
    Nick

    1. Hi Nick!

      Glad you enjoyed the article. 🙂

      I haven’t deployed any sites using Bedrock/Ansible yet, but I wouldn’t think there would be a major issue with deploying multiple sites the same as running them off your localhost. I’m hoping to play around with this on a Droplet soon, but if you come up with something and write about it let me know. I’m really digging how easy Bedrock/Ansible is to work with.

      Thanks!

  2. Thanks Erika, this helped me get up and running.

    Two stumbles for me.
    1) When I first went into the `wordpress_sites.yml` file there wasn’t an env: section and it would seem it’s not needed for local.
    2) Related, one does need to also go into `vaults.yml` and add the site name there or `vagrant up` will fail.

    Those seem like things that maybe just changed in Trellis since you wrote this.

    Again, this post was super helpful in getting me up and running so thanks!

  3. Super helpful – thanks Erica!

    It hadn’t clicked that I could rename the default ‘site’ directory. It all makes so much more sense now 🙂

  4. Hey, Erica, nice article. By the way this multiple site setup would not work on Windows if you used NFS share with winnfsd (Error: mount.nfs: access denied by server etc.). Works perfectly well with just a single site install though.