Local > Staging > Deploy Setup Help :)

If somebody can point me to the right tutorial/link, please do so! I’ve tried to troubleshoot my question.

I’m new to developing the “correct” way, but have had a blast installing Trellis/Bedrock-Ansible/Sage and working on a site locally on my Mac. I’ve got my site working on ‘mysite.dev’ locally, and have pushed the code to my GitHub account. I work at an agency where we have a linux VPS, currently we develop and preview our sites by just uploading to mydev.com/preview/wordpress-site (no GitHub, just develop right there on the server).

I’d still like to use a mysite.com/preview/example to showcase a site to clients before going live. I was unable to find a good tutorial that included the gap between going local to a staging URL that is different from the live URL/domain. The examples of going from local > live seem straightforward, I’m just looking to stage the site on a different URL and then push the GitHub version live.

Apologies if this has been easily answered before!

Hey @uberaff, welcome to Roots!

If I understand correctly, you’d like to have a staging site on, say, staging.example.com with the same code and database as production.

First, you’ll need the IP of the staging server added to hosts/staging under both [staging] and [web].

Next, add your site details to staging/wordpress_sites.yml. If you’ve already configured it for production you can copy and paste this over. Remember to also modify the vault.yml file with the name of your site.

site_hosts creates the nginx server blocks that your staging site will be served at. You could use the IP address of the server here, which means going to http://123.456.789.10 would return your site, however if you have a domain to use you should enter this here. It could be stagingserverforexample.com or staging.example.com, as long as you point that domain/subdomain to the IP address of the server.

If you’re following the repository structure from the roots-example-project then you’ll need to keep local_path and repo_subtree_path as ../site & site. These point Ansible and Git to the root of your site in the repo.

If you’re deploying the main branch to your production server and would like the staging site to be exactly the same, keep branch: master. If you were setting up an external development server for your team (before the client sees it) then you might consider switching this to a develop branch (or similar).

Other than that the process should be the same between production and staging.

You will want to keep the databases separate from each other so that you can play around in staging without affecting production however before presenting to the client you would probably want to update the staging site to match the production site.

Trellis itself doesn’t cover this but it does make it easy using WP-CLI and SCP/rsync.

If you want staging to match production:

# backing up production to your local copy
ssh web@production.server
cd /srv/www/example.com/current
wp db export # dumps the database to something like `example_production.sql`
exit # or you could open another shell to perform the syncing, deleting the dump after you're finished
cd ~/sites/example.com/site # your local copy of the site
scp web@production.server:/srv/www/example.com/current/example_production.sql . # copies the dump to the folder you're in
rsync -azP web@production.server:/srv/www/example.com/shared/uploads/ web/app/uploads/ # syncs your local uploads folder with the one from production
# syncing staging with your local copy
scp example_production.sql web@staging.server:/srv/www/example.com/current # copies the dump up to the staging site
rsync -azP web/app/uploads/ web@staging.server:/srv/www/example.com/shared/uploads/ # syncs the uploads folder on the staging server with your local copy
ssh web@staging.server
cd /srv/www/example.com/current
wp db reset # then y to confirm # clears the current database
wp import example_production.sql
wp search-replace domain-for-production.com domain-for-staging.com # replaces all references to the domain inside wordpress

( or you could skip copying these down to your local copy by copying the files to /srv/www/example.com/current/web and using curl or wget on the target machine to grab example.com/example_production.sql or the uploads folder)

Let me know if you have trouble or I’ve misunderstood


This information was very helpful, it cleared up a few questions I had so thank you very much for the reply!

Of course it opened up another can of worms. Got to the deploy stage and realized my current server is CentOS. I’d much prefer to just deploy through Trellis so now I have the fun task of spinning up a Ubuntu server and managing life without WHM/cPanel :stuck_out_tongue:

1 Like

mission accomplished :wink:

What if you want staging to match local? Do you ssh into vagrant?? Thanks

If you want staging to match your local copy you can do the same thing but export the database from Vagrant instead of the production server:

cd ~/sites/example.com/trellis
vagrant ssh
cd /srv/www/example.com/current
wp db export # saves the dump to ~/sites/example.com/site/example_development.sql

In development the uploads are saved to ~/sites/example.com/site/web/app/uploads/ so the steps for syncing the uploads folder are the same.


I know this is under the Bedrock category so I apologize in advance -

How difficult would it be to turn these commands into something Ansible could run similar (or even an extension of) the deploy.yml playbook?

I’m not familiar at all with Ansible and trying to pick up on it. Ideally I’d like to deploy both the WP site files as well as the database using the ansible-playbook command with Trellis. Even pull the database as well.

Does such a setup exist already?

missing a db between “wp import”. otherwise, thanks @paul_tibbetts for sharing this! it got me going forward :slight_smile:

1 Like