Sharing access to your Vagrant environment with the world
You're working on your project with your local Vagrant environment, and you'd like to show the status of the job to your customer who's located in another city. Maybe you have an issue configuring something and you'd like some remote help from your coworker on the other side of the planet. Alternatively, maybe you'd like to access your work Vagrant box from home, hotel, or coworking space? There's a neat Vagrant sharing feature we'll use here, working with a Ghost blog on CentOS 7.2.
Getting ready
To step through this recipe, you will need the following:
- A working Vagrant installation
- A working VirtualBox installation
- A free HashiCorp Atlas account (https://atlas.hashicorp.com/account/new)
- An Internet connection
How to do it…
Let's start with this simple Vagrantfile:
Vagrant.configure("2") do |config| config.vm.box = "bento/centos-7.2" config.vm.define "blog" do |config| config.vm.hostname = "blog" end end
We know we'll have to install some packages, so let's add a provisioning script to be executed:
config.vm.provision :shell, :path => "provision.sh"
We'll want to hack locally on our Ghost blog, such as adding themes and more, so let's sync our src/
folder to the remote /srv/blog
folder:
config.vm.synced_folder "src/", "/srv/blog"
We want a local private network so we can access the virtual machine, with the 2368
TCP port (Ghost default) redirected to our host 8080
HTTP port:
config.vm.network "private_network", type: "dhcp" config.vm.network "forwarded_port", guest: 2368, host: 8080
Provisioning
- To configure our new box, we'll first need to enable EPEL:
sudo yum install -q -y epel-release
- Then install the requirements,
node
,npm
, andunzip
:sudo yum install -q -y node npm unzip
- Download the latest Ghost version:
curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
- Uncompress it in the
/srv/blog
folder:sudo unzip -uo ghost.zip -d /srv/blog/
- Install the Ghost dependencies:
cd /srv/blog && sudo npm install --production
Put all those commands in the provisioning.sh
script and we're good to go: vagrant up
.
Starting Ghost engine
As you would do normally, log in to your Vagrant box to launch the node server:
vagrant ssh cd /srv/blog && sudo npm start --production […] Ghost is running in production... Your blog is now available on http://my-ghost-blog.com Ctrl+C to shut down
Change the host IP from 127.0.0.1
to 0.0.0.0
in the generated config.js
file so the server listens on all interfaces:
server: { host: '0.0.0.0', port: '2368' }
Restart the node server:
cd /srv/blog && sudo npm start --production
You now have a direct access to the blog through your box LAN IP (adapt the IP to your case): http://172.28.128.3:2368/
.
Sharing access
Now you can access your application locally through your Vagrant box, let's give access to it to others through the Internet using vagrant share
:
HTTP
The default is to share through HTTP, so your work is available through a web browser:
$ vagrant share ==> srv-1: Detecting network information for machine... [...] ==> srv-1: Your Vagrant Share is running! Name: anxious-cougar-6317 ==> srv-1: URL: http://anxious-cougar-6317.vagrantshare.com
This URL is the one you can give to anyone to access publicly your work: Vagrant servers being used as proxy.
SSH
Another possible sharing option is by SSH (deactivated by default). The program will ask you for a password you'll need to connect to the box remotely:
$ vagrant share --ssh ==> srv-1: Detecting network information for machine... [...] srv-1: Please enter a password to encrypt the key: srv-1: Repeat the password to confirm: [...] ==> srv-1: You're sharing with SSH access. This means that another user ==> srv-1: simply has to run `vagrant connect --ssh subtle-platypus-4976` ==> srv-1: to SSH to your Vagrant machine. [...]
Now, at home or at the coworking space, you can simply connect to your work Vagrant box (if needed, the default Vagrant password is vagrant):
$ vagrant connect --ssh subtle-platypus-4976 Loading share 'subtle-platypus-4976'... [...] [vagrant@srv-1 ~]$ head -n1 /srv/blog/config.js // # Ghost Configuration
You or your coworker are now remotely logged into your own Vagrant box over the Internet!