What might happen to the development environment if you upgrade OS. Would Apache configuration need to be changed? Will MySQL work?
Also, programs start stacking up. You would need so many different programs like PHP, Python, MySQL, maybe nodeJS, which get jumbled on your computer.
Vagrant provides a separate OS box, a Virtual Machine loaded with PHP, Python-related development environments. It helps keep your OS free and clean.
Vagrant gets rid of the many development and production environments. Usually, we develop on Mac and need to deploy on a Linux server. Vagrant helps keep the process quick and easy to deploy after coding.
WAMP, MAMP are still much easier to install and needs just a one-click setup. However, vagrant setup without advanced stuff like chef or puppet can be simple.
After install, you should be able to use the vagrant command in the terminal. Use vagrant
init to create a project in the existing directory.
Vagrant will place the configuration file 'Vagrantfile' in the directory.
Vagrantfile is editable in nano or vi. The Vagrantfile is a big file. It contains extensive information about a couple of settings. So you would not need Google to search to find the specific 'settings' here.
config.vm.box = “base” config.vm.box_url = “http://www” config.vm.network :forwarded_port, guest: 80, host:8080
Vagrant's website has a getting started section, which can also help to select a box. Their default precise64 can be used directly at the time of project creation. It will set up Ubuntu 12.0 virtual box on your virtual machine.
vagrant init hashicorp/precise64 vagrant up // to start the virtual machine
The first time will take longer as Vagrant would download the specified box. Once, Vagrant is up and running. SSH into a new virtual box using this command.
You will SSH into a fresh installation of Ubuntu 12.0. Now you can use sudo apt-get install to install PHP or the LAMP Stack with Git, WGET, ZIP and other essential programs all in this virtual box, separate from your operating system.
To destroy VM, run vagrant destroy command
Instead of manually downloading and installing the whole Stack, you can use vagrant configuration to do that. Setup a bash script with all installation commands and add it into vagrant configuration Vagrantfile. The bash file will look like
The above line is how to shell script must start.
Down below, we are updating/upgrading the Ubuntu repository package list before any installation.
sudo apt-get update sudo apt-get -y upgrade sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install python-software-properties
Install Apache PHP, PHPs required extensions and other needed stuff.
-y will ok all yes/no prompts during the installation.
sudo apt-get install -y wget apavhe2 php php-curl
Apache configurations: enables mod_rewrite, sets document root to shared /vagrant directory and creates a symlink to
sudo a2enmod rewrite rm -rf /var/www ln -fs /vagrant /var/www sudo service apache2 restart
The vagrant project directory gets shared by both your local machine and the virtual machine. Whatever you save here remains available to both environments.
Edit Vagrantfile and add bash startup script.
config.vm.box_provision :shell, :path=> "bash_file_name.sh"
If the vagrant box is already running, Vagrantfile changes will not reflect. Run the vagrant provision command to read from the configuration and update box.
Now, use your IDE on the local machine to create a PHP file in shared document root or create one on the virtual machine.
Check it in the browser. We have set up network port forwarding in the Vgrantfile configuration already. Type this URL in the browser.
Back in 2015/16, I was a Jeffery Ways Laracasts subscriber. I have followed Laracasts vagrant simplified video. Video, in turn, uses a great resource from dev-metal.com, especially this post about a super simple vagrant development environment and LAMP installation.