PHP Classes

How to Use Vagrant to Improve Your Web Development Process

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Use Vagrant to...   Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)  

Author:

Viewers: 868

Last month viewers: 25

Categories: PHP Tutorials

A great part of the production Web sites are hosted on Linux based systems. However the vast majority of the Web developers run Windows or Mac OS X on their development machines.

Vagrant is an application that creates and configures virtual development environments. It can be used as a simpler wrapper solution around virtualization software such as VirtualBox, VMware, KVM and Linux Containers (LXC), and around configuration management software such as Ansible, Chef, Salt and Puppet.

Read this article learn how to use Vagrant to easily setup your Web applications development environment in a way that it is as close as possible to the Linux based environment that you have on your production server.




Loaded Article

Contents

Introduction

What is Vagrant?

Setup

Running a Virtual Server

Connecting to the Server

Conclusion


Introduction

You are probably a Composer fan, creating your next cool project using one of those trendy frameworks. Or, perhaps, you are frontend master writing complex pieces of code in JavaScript and CSS, compiling them from jsx or less files, setting libraries through Bower, and the project is building using Grunt, simultaneously producing code and unit tests. Of course, then pouring it all on Github.

If all of this is part of your everyday life, then God be praised, congratulations! You have passed all these tests. From now on, you are one of those happy developers who spent more than a week sorting out how to properly configure the whole bunch of tools. You are one of those who can shine their knowledge in the comments to articles in PHPClasses with other less agile colleagues.

But are you just one of those who only dream of being such a master in the development of modern Web applications? Then I hasten to please you, I am here and I recently learned a couple of useful things that I realized that it would be a real gift for novice developers. So I decided to write this article.

What is Vagrant?

For several years, I hear about Vagrant, and you probably also vaguely heard about this tool for the rapid deployment of configured virtual machines. But every time I read another article about the Vagrant, turns me off excessive complexity of the rules.

I do not like to hammer my head too much information. Somehow it was easier all the time to put everything you need through the "sudo apt-get install [program-name]" and manually add new configurations in nginx, taking as a basis the old configuration.

But after reading a few days ago the instructions in the documentation for Laravel, I realized that it is not so scary, and a desire to teach programming to my friend was the last factor, because of which I still climbed to customize his car with Vagrant.

So what is Vagrant? Vagrant is computer software that creates and configures virtual development environments. It can be seen as a higher-level wrapper around virtualization software such as VirtualBox, VMware, KVM and Linux Containers (LXC), and around configuration management software such as Ansible, Chef, Salt and Puppet.

Laravel developers created your virtual machine image for Vagrant, which already contains all the tools and instruments you need for a happy development.

As a result, together with a small utility named Homestead, also thoughtfully provided Laravel to work with their way of working with them in a really easy way. To list of main components installed in their image, you can view their documentation.

Among other instruments not mentioned there are included things such as for example, composer and git. In general, they have already everything you need for your purposes. You and I need only to understand a little how it works Vagrant and Homestead and calm the whole thing to use in their daily work.

And not have to use this set only for projects based on Laravel, the Homestead script provides a simple means to maintain multiple development projects through a simple configuration file allowing you to manage databases and nginx-configuration server.

As a result, we get almost analogue Denwer, but we are dealing with a full Linux server, which, if necessary, we can easily begin to manage manually and install it on other components, and thus grow into a more serious and experienced developers 

Setup

In fact, all you need to do is to install Vagrant on your computer and VirtualBox. Then fiddle a little with their settings.

In this article I will only talk about how to do it in a Windows environment, because for Linux users I think they are able to do everything by themselves with the instructions from the documentation.

Of course, this may not work without some problems. If your user name is written in Windows Cyrillic letters (like my own name), then get ready for errors during execution of instructions.

In that case, I recommend creating a system environment variable VAGRANT_HOME folder without specifying the Cyrillic characters. If you do not know how to do it, read about it here for example. Then open the settings and specify the folder for VirtualBox storage of virtual machines, which also should not use Cyrillic characters. Now the tools will work correctly and not to give any errors.

So, with the foundation we have created for you, it is time to write a few commands. We press on the keyboard keys Win + R, will open a window showing the phrase "There is a reboot after installation of Vagrant", then it will became available in the console. So, first try just type the word "vagrant" in the console, and check whether we have correctly set our tool:

If you have problems, check your PATH variable. It should include the path to the Vagrant binaries. Then reboot the machine, if you have not already done so.

Then in the official documentation, we are suggested to clone a project from git or, even worse, set it using composer using Homestead. I'm on Windows, I am not using git nor, especially, composer.

And anyway, I have set this virtual machine environment and prepared it to save yourself from the hassle of manually installing all these tools... So, let's not go and engage in complicated approaches and just be old-fashioned, download a zip file from github of the library on your computer and unpack it in a folder.

Unpacked? Now to install the library on a computer, you must run homestead init. But here again there is some trouble. This command will work on our computer only when the main Laravel library is installed with Composer.

Another option is to run the script init.sh, lying right there, but to make it work, we still need the Linux shell. In general, once again to put a bunch of stuff... As you can see, under Windows there is no easy solution.

So let us do a couple of things again manually, open the file init.sh and study its contents. There are only 11 lines of code. As you can see, in order to setup the library we just need to copy multiple files the folder ~/.homestead . The sign ~ denotes the home directory of the current user. I have this set to "C:\ Users\Test\".

If you analyze how their scripts work, then we come to the conclusion that we need there the following files:

  1. src/stubs/Homestead.yaml - in the folder ~/.homestead
  2. src/stubs/after.sh - in the folder ~/.homestead
  3. src/stubs/aliases - in the folder ~/.homestead
  4. Vagrantfile
  5. scripts/* (all files in this folder)

Create a folder .homestead in user's home directory and copy the files into it. It will be a folder with your control script through which we will manage our virtual machine.

By the way, you can not create a file that begins with a period, through a standard Windows interface. To accomplish this task take advantage of something like Far Manager or Total Commander.

So, in the end, I got the following listing:

We are almost ready for full-fledged work with our virtual machine. To access our car we also need to create a SSH keys. Here we will use the Putty program toolkit which we will later use to manage your server.

Download the PuttyGen application, generate the private and public keys and store them in the folder ~/.ssh. To generate should press the Generate button, then move a little your mouse in the middle of the program to generate a random key, and then you can save your key.

Also, when you save, you are prompted to fill in key pass phrase, but at this stage we do not need it. So keep your key without a password and do not pay attention to the warning Putty will show for creating such a key. We just need a test machine, not a real battle server, so it is not so bad at all.

The public key will be saved under the following name: "~/.ssh/id_rsa.pub". At the same time, we need the private key stored in two formats: standard for Putty (button Save private key, file name - ~/.ssh/putty_private.ppk, for example) and in the format of OpenSSH (use the "Export OpenSSH key" item in the Conversions menu, the file name should be "~/.ssh/id_rsa").

We need the first private key to connect to the server via the program Putty. The second is used for configuration and server initialization scripts through Homestead. As a result, we should get something like this key files:

Now we only need to adjust some of the settings before starting our server when necessary.

Open in any editor Homestead.yaml the file. This is the only configuration file through which we can specify all the basic parameters of our virtual machines. The default settings are already well defined. So we can start here and not have to change anything. I will only briefly explain its contents:

The section "folders" indicates the connection of your virtual machines and your actual system so that you can easily edit the files in a virtual machine through your favorite Editor in the main system.

Th section "sites"  is for the configuration of nginx, and again, everything is very simple. We just link to the domain of the directory on the server.

Section "databases" lists the names of the databases that are created during initialization of the virtual machine. Here, in fact, all you need to know about configuring our system.

As in the section "folders", the default folder contains a link to ~/Code with the same folder in a virtual machine, do not forget to create this directory in your user folder. Subsequently, through it we will be able to work with files in your server.

Also, keep in mind that the yaml format may be slightly different than what you expect in regard to its approach to data storage. Therefore, if you add your options, consider that the number of spaces is important here, because through them is defined the depth of the hierarchy of the values. For more details you can read about this format, for example, in Wikipedia.

Running a Virtual Server

And finally, we come to the fun part. Now we want to run our virtual machine. Open a command prompt folder ~/.homestead and execute the magical command:

vagrant up

If you did everything correctly, you will start loading the image VirtualBox-developers Laravel, and you will something roughly as follows:

If Vagrant reported a problem, do not despair: read the text of the error messages and try to search for that text with Google. Most likely, there is already a lot of people who faced a similar problem, and somehow solved i.

If the image is successfully installed on your computer, and the error occurred during the loading of the machine, then try to open VirtualBox and run it manually without Vagrant, and see what happens. Vagrant is, in fact, simply VirtualBox add-on. You can easily run a virtual machine gathered their own, if it is necessary.

If everything went fine. So, after a few minutes your Vagrant virtual machine fully configures and installs all the components and options. And you already can safely connect to your server. You can use, for example, Putty.

Connecting to the Server

Let's download the file putty.exe and connect via it to our server. Configure the program as follows: Enter the address of the server, port, and user, as well as in the section "Connection -> SSH -> Auth".

Do not forget to specify the path of the private key you generated. Then save all those settings in a session under some title, so you do not have to enter them every time you start the program and use the "Open" button.

If you were careful and did everything right, you can open a terminal window of your own virtual server:

On some machines you may encounter the following problem. Vagrant for some reason does not accept the key file created by Putty and generates its own, in "his opinion", more secure.

This key is stored at the address "~\.homestead\.vagrant\machines\default\virtualbox\private_key". If you can not connect with your key to the server, then you need to use the key generated by Vagrant.

Before you use it, open the program again PuttyGen, import the file created by Vagrant (menu Conversions -> Import key) and save it in a putty format (button "save private key"), and then use this file to connect to the server via putty.

Well done! The first milestone on the way to conquer the world of professional Web application development was reached. Now, let's write a couple more lines and set Laravel, just to make sure that we have on the server everything works correctly. Enter in turn the following two commands on your server:

cd Code
composer create-project laravel/laravel Laravel --prefer-dist

And go rest. When you return, open the very same folder ~/Code, which you created in your home directory. If all went well, then you should expect a surprise there, and the folder name Laravel.

That's the way! The server worked something at home, and you will get on your computer the Laravel folder with all necessary files of the framework. It remains to make just a little in order to see our application in the browser: Open the hosts file in administrator mode to be able to edit it, and add the following line:

192.168.10.10 homestead.app

Now, open your browser to homestead.app.

In general, this is almost everything you need to know. If you got to the part of this article, and you will do it, it means that you are a big boy, and you now have a complete web server of your own, where you have all the necessary technology pieces to work.

You are just a little bit away to know about how to stop it, update its configuration and completely removed from the system. To do this, use the following Vagrant command, and do not forget that in this case you have to be in the directory ~/.homestead:

# Turn on the machine
vagrant up

# Switch off the machine
vagrant halt

# Again, set up the machine using scripts Homestead.
# If you change the configuration file - execute this command,
# Vagrant again and configure your virtual machine
vagrant provision

# Completely remove the virtual machine from the system.
# Be careful, it will also remove the database, so do not
# Forget to make a backup copy of it before executing this command
vagrant destroy

# Have Vagrant even is such a great team for fast
# Connect to the server, but as we sit in Windows, just because it
# Use does not, have, as usual, and podnapryachsya
# And install the Linux program "ssh" on your computer.
# And if you do everything, then you will still be problems with the transmission
# Code to the clipboard, so I still give Tip: Use and Putty
# Enjoy life, it is better, I have not seen anything under Windows
vagrant ssh

Conclusion

Everything written in this article, a very brief description of all the features Vagrant. Study the official documents and other items on this tool, if you have a desire to understand it more deeply.

I also advise you to read the comments in various forums. There are sometimes smart people there that write useful things that solve related problems.

If liked this article or you have other questions, post a comment to this article here now.




You need to be a registered user or login to post a comment

1,616,107 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

2. Thanks - D (2015-07-13 17:56)
very interesting and useful... - 0 replies
Read the whole comment and replies

1. Maybe a more simple solution? - Sebastian Schümann (2015-07-08 19:44)
Use VirtualBox and a symlink hook... - 0 replies
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Use Vagrant to...   Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)