Using Vagrant to Manage Linode Environments
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
DeprecatedThis guide has been deprecated and is no longer being maintained.
Vagrant is a configuration management tool that allows users to create portable and reproducible work environments. Vagrant excels at providing consistent, easy-to-configure servers that can be used to keep development environments consistent across all users. It is easy to both launch and tear down, and can be supplied to any part of a development team that may need an environment but does not have the means to configure one themselves. Often paired with providers such as Puppet, Salt, and Chef, it offers an easy solution to keeping a consistent workflow.
Vagrant can be paired with Linode through the use of the vagrant-linode plugin to spawn and destroy Linode servers as needed. This guide will provide instructions on installing Vagrant, configuring the vagrant-linode plugin, and setting up a basic Apache server for testing.
Prerequisites
Install Vagrant on your local computer or other workspace.
Generate an API Key. This will be used to create Linodes based upon your Vagrant profile:
Log in to the Linode Manager and select my profile to the upper right.
Select the API Keys tab:


Enter a label for your API Key and set an expiration time. Then click Create API Key.
Your API Key will be output in a green box. The key will only be shown once so be sure to record it for later use:


Install the vagrant-linode Plugin
From your workspace, create a directory for your project, and move into that directory:
mkdir ~/vagrant-linode cd vagrant-linodeInstall the plugin:
vagrant plugin install vagrant-linodeNote If using a Mac, it may request to install development tools. Select yes, then re-run the command.From the
vagrant-linodedirectory, create the Vagrantfile:touch VagrantfileThe Vagrantfile is used to describe, in code, the type of machine that Vagrant will create. It defines everything from the operating system to the users, to any applications that need to be initially installed to make one consistent work environment.
Configure the Vagrantfile
Open the Vagrantfile in your text editor of choice. In Ruby, define what version of Vagrant you are using. The
2defines that it is Vagrant 1.1.0 leading up to Vagrant 2.0.1is any version of Vagrant below that:- File: ~/vagrant-linode/Vagrantfile
1 2 3Vagrant.configure('2') do |config| end
All code will take place between the
Vagrant.configureandendlines.When creating a guest machine – the sever that will be created – Vagrant will create a username, password, and private key to access the machine. The default username and password is
vagrant. Define your own parameters for theusername, and set the pathway to your own private key. If you have not generated a private and public key, you can do so by following the Securing Your Server guide:- File: ~/vagrant-linode/Vagrantfile
1 2 3 4 5 6 7Vagrant.configure('2') do |config| ## SSH Configuration config.ssh.username = 'user' config.ssh.private_key_path = '~/.ssh/id_rsa' end
If you choose to do so, you can also define your own password with the
config.ssh.passwordsetting.Define the Linode provider:
- File: ~/vagrant-linode/Vagrantfile
1 2 3 4 5 6 7 8 9 10 11 12Vagrant.configure('2') do |config| ... # Global Configuration config.vm.provider :linode do |provider, override| override.vm.box = 'linode' override.vm.box_url = "https://github.com/displague/vagrant-linode/raw/master/box/linode.box" provider.token = 'API-KEY' end end
Lines 6 defines the provider, and lines 7 and 8 define the box. Boxes are packages that include the basic requirements for a Vagrant environment to function. The supplied box is the
linodebox, created as part of the plugin. Replace theAPI-KEYwith the key generated above.Choose your Linode’s settings:
- File: ~/vagrant-linode/Vagrantfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18Vagrant.configure('2') do |config| ... # Global Configuration config.vm.provider :linode do |provider, override| ... #Linode Settings provider.distribution = 'Ubuntu 14.04 LTS' provider.datacenter = 'newark' provider.plan = '2048' provider.label = 'vagrant-ubuntu-lts' end end
In this instance, a 2GB Ubuntu 14.04 LTS Linode is being created in the Newark data center. The
provider.labelis the name that the Linode will show up as in the Linode Manager.To see more options regarding the vagrant-linode plugin see the documentation on the plugin’s GitHub repository.
Set Up the Vagrant Box
Although the server can now be created successfully, many aspects of it still need to be configured. Shell scripts will be used to complete the steps from the Setting Up and Securing a Compute Instance guide, and to install and configure Apache. Files will also be synced between the workstation and the Linode.
Configure the Server
Create a shell script called
setup.shto configure the Linode’s hostname, set the proper timezone, and update the server. Replacevagranttestwith your chosen hostname, andESTwith your timezone.- File: ~/vagrant-linode/setup.sh
1 2 3 4 5 6 7#!/bin/bash echo "vagranttest" > /etc/hostname hostname -F /etc/hostname ip=$(ip addr show eth0 | grep -Po 'inet \K[\d.]+') echo "$ip $ip hostname" >> /etc/hosts ln -sf /usr/share/zoneinfo/EST /etc/localtime apt-get update && apt-get upgrade -y
Lines 2 and 3 define the hostname.
Line 4 sets the variable
ipto the Linode’s IP address – since we will not know IP address until Vagrant launches the Linode.Line 5 inserts the IP address into the
/etc/hostsfile to define the fully-qualified domain name.Line 6 sets the timezone, and the final line updates the server and server packages.
Within the Vagrantfile, call to the shell script you just created by adding the
config.vm.provisionmethod:- File: ~/vagrant-linode/Vagrantfile
1 2 3 4 5 6 7 8Vagrant.configure('2') do |config| ... # Shell Scripts config.vm.provision :shell, path: "setup.sh" end
Install Apache and Sync Files
Create an installation script for Apache called
apache.sh, and add the following:- File: ~/vagrant-linode/apache.sh
1 2 3 4 5 6 7#!/bin/bash apt-get install apache2 -y mv /etc/apache2/ports.conf /etc/apache2/ports.conf.backup mv /etc/apache2/ports1.conf /etc/apache2/ports.conf a2dissite 000-default.conf a2ensite vhost.conf service apache2 reload
Line 2 installs Apache.
Lines 3 & 4 create a backup of the
ports.conffile and replaces it with a file created below.Lines 5 & 6 disable the default host file and enable the one we will create below. Apache is then reloaded to take the configuration changes.
Add the shell script provisioner method to your Vagrantfile, under the line that references
setup.sh:- File: ~/vagrant-linode/Vagrantfile
1 2 3 4 5 6 7 8 9Vagrant.configure('2') do |config| ... # Shell Scripts config.vm.provision :shell, path: "setup.sh" config.vm.provision :shell, path: "apache.sh" end
Create a new directory for Apache configuration files:
mkdir apache2Because Vagrant is often used for development environments, we want to host Apache on a port other than 80. Create
ports1.conf, as referenced in the shell script above. The port will be set to 6789:- File: ~/vagrant-linode/apache2/ports1.conf
1 2 3 4 5 6 7 8 9Listen 6789 <IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule>
Create a new directory under
apache2calledsites-available. Add a VirtualHosts file,vhost.conf, to this new directory:mkdir sites-available- File: ~/vagrant-linode/apache2/sites-available/vhost.conf
1 2 3 4 5 6<VirtualHost *:6789> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Return to the Vagrantfile, and use the
config.vm.synced_foldermethod to sync the local directories with directories on the server:- File: ~/vagrant-linode/Vagrantfile
1 2 3 4 5 6 7 8 9 10Vagrant.configure('2') do |config| ... # Synced Folders config.vm.synced_folder '.', '/vagrant', disabled: true config.vm.synced_folder './apache2', '/etc/apache2', disabled: false config.vm.synced_folder './webfiles', '/var/www/html' end
Line 5 disables syncing for the root folders.
Line 6 defines the locally-hosted
apache2folder ('./apache2') and links it to the/etc/apache2directory on the Linode.disabled: falseensures that it will sync.Line 7 does the same with a yet-to-be-created
./webfilesdirectory that can be used to add any website files before booting the instance.
Create the
webfilesfolder in yourvagrant-linodedirectory:mkdir ~/vagrant-linode/webfilesAdd to this directory any files you want the Linode to serve over HTTP.
Boot an Instance
With the Vagrantfile configured, and scripts and files created, it’s now time to create the guest machine and check that it’s running properly.
From your workstation, boot the instance:
vagrant upIt will run through the installation process, sync the directories, and run the shell scripts.
Log into the newly-created Linode:
vagrant sshTo ensure that Apache is running properly, check the status:
service apache2 statusIt should output:
* apache2 is runningTo see that the environment is accessible online, check for the IP address:
hostname -iThen go to your chosen web browser and navigate to your ip address with
:6789appended to the end. You should see Apache2 Ubuntu Default Page.Note If you wish to shut down or remove the Linode from your workspace you can do so through one of the following commands:
vagrant haltwill power down the Linode through the shutdown mechanism. You can then runvagrant upagain to power on the Linode.vagrant destroywill remove the Linode entirely from your account, removing anything that was created during the Vagrant up process or added later to the server.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on

