WP-CLI user & path issues on remote server & syncing databases

I’m using a basic DigitalOcean droplet with the roots trellis/bedrock/sage project. Everything is working well and I’m able to successfully deploy.

The problem I’m having is using wp-cli on my remote server. When I SSH into the server, I switch directories to cd /srv/www/mysite.com/current and attempt to use wp-cli.

I’m getting two errors:

  1. It has me using wp-cli as “root”, which isn’t advised
  2. It asks me for the path to the WP directory

I can run commands, but only if I use format below:
sudo -u admin -i -- wp plugin delete soil --path=/srv/www/dianaweymar.com/current/web/wp

I edited wp-cli.yml on the remote server and tried setting path: to the full path from /srv/ (it was already /web/wp) but it didn’t change anything. I also tried setting user: admin to fix the first issue, but again, didn’t change anything.

Is there a quick fix to all this that I’m missing. And, more importantly, do most trellis users normally log into their remote server to run updates using wp-cli?

My main reason for figuring this out is so I can copy my local database to my production database using wp-cli.

  1. If you set up your remote server using Trellis then you can switch to the web user to perform commands using su web after logging in (or just login as web to begin with: ssh web@yourserver)

  2. I’m not sure why wp-cli isn’t working, my wp-cli.yml file is set to path: web/wp and is working fine for me from the /current folder. My only suggestion would be to update it and try again.

As the root user you could try:

  • curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  • chmod +x wp-cli.phar
  • sudo mv wp-cli.phar /usr/bin/wp

This is what I use wp-cli for mostly and it makes it quite easy:

cd example.com/site
vagrant ssh
cd /srv/www/example.com/current
wp db export # exports example_dev.sql to /site folder
scp example_dev.sql web@production:/srv/www/example.com/current
ssh web@production
cd /srv/www/example.com/current
wp db reset # then y to confirm
wp db import example_dev.sql
wp search-replace example.dev example.com # add --dry-run to see what would change before applying

@paul_tibbetts - thanks for helping problem solve.

Logging in (or switching users) to web did the trick. It also solved the--path issue!

Regarding the database sync, that looks good. I was curious what the normal practices were as I couldn’t find it mentioned anywhere else. I wasn’t aware of the scp command either - much better than committing the db to git.

One more question regarding doing a search-replace on urls:

In wp-db migrate, it runs two URL replacement functions:

from local: /srv/www/example.com/current/web
to remote: /srv/www/example.com/releases/20160201064514/web


from local: //dianaweymar.dev
to remote: //dianaweymar.com

Would the procedure you outlined cover all these bases? I assume there’s a new release folder created every time I deploy to remote.

As far as I know the steps for WP-CLI should do the same as wp-db migrate.

I’m not sure why you would need to change the path of the site from local to remote though; there is a new release folder created each time you deploy but deploying with Trellis will symlink this new folder to the /current folder once the deploy is finished and so you can leave it as /src/www/example.com/current/web

This is partly because everybody does it differently. It sounds like wp-db migrate does the same job as WP-CLI here and so if you’re used to it I would stick with it. Another option mentioned on the forums is to use wp migrate db pro although this plugin costs money.

No problem, glad I could help!

1 Like

Thanks, @paul_tibbetts.

I’m going to go ahead and try using wp-cli without changing the site path. If I don’t post here again, assume it all worked out OK!