Vagrant PHP Yii2 or how not to bother with the peculiarities of its OS to customize the development environment

June 10, 2017 28 Yehor Rykhnov

A little bit about Vagrant. For me, the most convenient operating system for PHP development is Ubuntu. Here all the software is free, easy to configure, lots of documentation, etc. But if you have to work with other OSes (Windows, Mac, etc.) sometimes, you encounter problems installing one or another module or plugin for PHP. Vagrant helps a lot to get around this rake the tenth way. How it is done and what advantages Vagrant has - read in the article. We will install Vagrant for development on PHP and install Vagrant to work with Yii2 advanced.

Vagrant is convenient and simple.

VagrantVagrant is a tool that helps you to work with a virtual machine. Installing Vagrant - you can get the OS in a virtual machine with Ubuntu, Apache / Nginx, MySql, php, etc. Also, Vagrant synchronizes folders on your OS with folders in a virtual machine (ie you work with your project in your favorite directory on your favorite OS). In general, Vagrant gives a sea of benefits, namely:

  • It is easy to transfer your virtual PC from the home computer to the working without problems with the settings of the software
  • You can learn, if you have not encountered before, the methods of distributed load by creating several virtual machines and trying to work with technologies such as RabbitMQ, sharding, etc.
  • Test performance by setting the necessary parameters for the virtual machine
  • Install the software as on the customer's server
  • Make different settings for PHP
  • Do not install on your OS PHP, MySql and other software

Vagrant has many configurations of the system (they are called boxes), and also you can assemble the necessary configuration on the site https://puphpet.com/

Installing Vagrant

For Vagrant to work, you need a VirtualBox or some other virtualization software. After that, go to the site and download https://www.vagrantup.com/downloads.html Vagrant for your OS and start the installation of the program.

Install the plugins for Vagrant. In the console, execute the following commands:

vagrant plugin install vagrant-vbguest
vagrant plugin install vagrant-hostmanager

Installing the box for PHP is one of the most popular Scotch Box (https://box.scotch.io/).
In the directory of your project, execute the command:

git clone https://github.com/scotch-io/scotch-box.git

Now, go to the scotch-box and start the command:

vagrant up

Loading data may take about 10-15 minutes.

After the download is finished, you can go to the address http://192.168.33.10 (this is Standard address for scotch-box) and see the start page.

In the folder scotch-box you will have a folder public in which you can install your site.

You can run the following command in the console:

vagrant ssh

to enter your virtual host by ssh.

To turn off the virtual box, execute the command:

vagrant halt

A more detailed list of commands can be obtained by running the following command:

vagrant list-commands

Vagrant and Yii2 advanced

We assume that you have the Yii2 advanced project installed. At the root of your project there must be a vagrant folder. Open and edit the file vagrant/config/vagrant-local.yml (if you do not have this file - you need to have the file vagrant-local.example.yml - save this file with the name vagrant-local.yml ). In this file, you will need to specify your account's token on github in the github_token parameter and run the command:

vagrant up

Now, you can run your project at:

More details on the site Github.

A source: https://кодер.укр/записи/vagrant-php-yii2-или-как-не-париться-с-особенностями-своей-OS-для-настройки-окружения-разработки