Um programador PHP, por exemplo, pode configurar a máquina para instalar o PHP,
Apache e MySQL da mesma forma que este o faria em um servidor real. E, ainda:
usando o mesmo sistema operacional do servidor real. Mas...qual a diferença entre
usar o Virtualbox diretamente e o Vagrant, nesse caso? Bom, usando o Vagrant, o
desenvolvedor consegue facilmente fazer configurações como redirecionamento de
portas (importante para um programador que trabalha com web), sincronizar pastas
entre a sua máquina física real e a máquina virtual de desenvolvimento e
provisionar a máquina virtual automaticamente de acordo com a sua vontade. Tudo
isso usando apenas alguns poucos arquivos de configuração e sem precisar repetir
procedimentos frequentemente. No fim, o Vagrant se destaca pela facilidade de
distribuição e manutenção das máquinas virtuais, garantindo assim um ambiente
de desenvolvimento eficiente e fácil de replicar.
Abaixo, segue um breve tutorial para iniciar os trabalhos com o Vagrant: (este
tutorial seja feito no Linux, mas se você tiver conhecimentos avançados em
Windows também é possível fazê-lo no Windows)
1) Primeiro, instale o Virtualbox, que é necessário para que o Vagrant possa criar
máquinas virtuais.
2) Depois, baixe e instale o Vagrant.
3) Agora, numa pasta vazia, execute o comando "vagrant init ubuntu/trusty64". O
resultado deve ser similar a esse:
fernando@fernando-notebook:~$
Gostou? # Primeiro,
Conheça formas de acompanhar vamos
o blog e não criar
perca maisuma pas
nenhum post, gratuitamente!
riada
fernando@fernando-notebook:~$ mkdir tutorial-vm
fernando@fernando-notebook:~$ cd tutorial-vm/
fernando@fernando-notebook:~/tutorial-vm$ # Agora, execute o
00:00
7 # you're doing.
8 Vagrant.configure(2) do |config|
9 # The most common configuration options are documented and commented below.
11 # https://docs.vagrantup.com.
12
13 # Every Vagrant development environment requires a box. You can search for
14 # boxes at https://atlas.hashicorp.com/search.
15 config.vm.box = "ubuntu/trusty64"
16
17 # Disable automatic box update checking. If you disable this, then
18 # boxes will only be checked for updates when the user runs
19 # `vagrant box outdated`. This is not recommended.
20 # config.vm.box_check_update = false
21
22 # Create a forwarded port mapping which allows access to a specific port
23 # within the machine from a port on the host machine. In the example below,
Gostou? Conheça formas de acompanhar o blog e não perca mais nenhum post, gratuitamente!
24 # accessing "localhost:8080" will access port 80 on the guest machine.
30
31 # Create a public network, which generally matched to bridged network.
32 # Bridged networks make the machine appear as another physical device on
33 # your network.
34 # config.vm.network "public_network"
35
36 # Share an additional folder to the guest VM. The first argument is
37 # the path on the host to the actual folder. The second argument is
38 # the path on the guest to mount the folder. And the optional third
41
42 # Provider-specific configuration so you can fine-tune various
43 # backing providers for Vagrant. These expose provider-specific options.
49 #
52 # end
53 #
54 # View the documentation for the provider you are using for more
56
57 # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
58 # such as FTP and Heroku are also available. See the documentation at
61 # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
62 # end
63
64 # Enable provisioning with a shell script. Additional provisioners such as
65 # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
66 # documentation for more information about their specific syntax and use.
71 end
4) Agora, experimente apagar o # do inicio das linhas 25, 67, 68, 69 e 70. O arquivo
deverá ficar assim:
3
4 # All Vagrant configuration is done below. The "2" in Vagrant.configure
7 # you're doing.
8 Vagrant.configure(2) do |config|
9 # The most common configuration options are documented and commented below.
16
17 # Disable automatic box update checking. If you disable this, then
18 # boxes will only be checked for updates when the user runs
19 # `vagrant box outdated`. This is not recommended.
20 # config.vm.box_check_update = false
21
22 # Create a forwarded port mapping which allows access to a specific port
23 # within the machine from a port on the host machine. In the example below,
30
31 # Create a public network, which generally matched to bridged network.
34 # config.vm.network "public_network"
35
36 # Share an additional folder to the guest VM. The first argument is
Gostou? Conheça formas de acompanhar o blog e não perca mais nenhum post, gratuitamente!
37 # the path on the host to the actual folder. The second argument is
38 # the path on the guest to mount the folder. And the optional third
45 #
46 # config.vm.provider "virtualbox" do |vb|
48 # vb.gui = true
49 #
51 # vb.memory = "1024"
52 # end
53 #
54 # View the documentation for the provider you are using for more
56
57 # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
58 # such as FTP and Heroku are also available. See the documentation at
62 # end
63
64 # Enable provisioning with a shell script. Additional provisioners such as
65 # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
66 # documentation for more information about their specific syntax and use.
70 SHELL
71 end
00:00
Note que o download da máquina virtual inicial demora um bocado, pois o Vagrant
precisa baixar uma imagem com todo o Ubuntu Trusty já configuradinho e tal. Mas,
depois que esse download é feito, a importação é bem rápida e depois o Vagrant
assume e instala o pacote apache2, correspondente ao servidor web Apache,
automagicamente após o boot da máquina virtual.
Não custa lembrar, claro, que esse download da imagem é feito apenas uma vez por
imagem. Portanto, se você desligar a máquina (usando vagrant halt) e iniciar ela
novamente (usando vagrant up), somente o boot vai ser realizado (o Vagrant é
esperto e não re-executa os comandos de instalação do Apache pois sabe que a
máquina já tinha sido criada e portanto esses comandos já foram executados
anteriormente na máquina).
Um ponto importante, também, é que qualquer pessoa que tiver tal Vagrantfile e
executar o vagrant up na pasta onde o arquivo se encontra terminará com uma
máquina virtual que, se não é igual, é muito similar (as vezes pode acontecer do
Apache acabar sendo atualizado nos repositórios do Ubuntu, por exemplo) à que foi
criada na sua máquina, permitindo assim a distribuição de ambientes de forma
muito facilmente (e sem a necessidade de transferir centenas de megabytes por aí).
6) Por
Gostou? último,
Conheça vamos
formas deacessar a máquina
acompanhar via
o blog e SSH
não usando
perca mais o comando
nenhum "vagrant
post, ssh" e,
gratuitamente!
com acesso ao sistema de arquivos interno da máquina virtual, modificar a página
de bem vindo do Apache para um simples "Olá Mundo". Segue a gravação:
body, html {
padding: 3px 3px 3px 3px;
background-color: #D8DBE2;
div.main_page {
position: relative;
display: table;
width: 800px;
00:00 1,5
Bom, espero que tenham gostado do post, do Vagrant e do tutorial como um todo
(fazia tempo que eu não descrevia algo assim, passo-a-passo). Quem quiser pesquisar
mais sobre a ferramenta, visite a documentação do Vagrant, que lá tem tudo
explicadinho detalhadamente. =)
CURTIR ISSO:
Carregando...
Participe da discussão...