Revision #20 has been created by rackycz on Feb 12, 2021, 1:31:14 AM with the memo:
Vagrant
« previous (#19) next (#21) »
Changes
Title
unchanged
Yii v2 snippet guide III
Category
unchanged
Tutorials
Yii version
unchanged
2.0
Tags
unchanged
tutorial,yii2,beginer
Content
changed
[...]
```
search: Yii::t\('([^']*)'[^']*'([^']*)'[^\)]*\)
replace with: Yii::t\('$1','$2'\)
```
# Running Yii project in Vagrant. (Simplified version)
- Do you want your colleague-developers to use identical environment like you have?
- Or do you want to easily setup whole server after you reinstalled your computer?
This is when Vagrant is helpful.
Vagrant creates a virtual machine with a web server (using almost any OS/PHP/SQL you specify), while the docroot is on your local disk so you can easily modify the PHP files in your IDE and in your OS. Vagrant works just like composer or NPM. It is a library of available OS images and you just pick one. Whole configuration is defined in one text-file, named Vagrantfile, and all you need is just a few commands to run it. And debugging is no problem.
List of all available OS images is here:
- https://app.vagrantup.com/boxes/search
Both Yii demo-applications already contain the Vagrantfile, but its setup is unclear to me - it is too PRO. So I want to publish my simplififed version which uses OS image named (scotch/box)[https://box.scotch.io]. (It has some advantages, the disadvantage is older PHP in the free version)
The Vagrantfile is stored in the root-folder of your demo-project. My Vagrantfile contains following commands.
```
Vagrant.configure("2") do |config|
config.vm.box = "scotch/box"
config.vm.network "private_network", ip: "11.22.33.44"
config.vm.hostname = "scotchbox"
config.vm.synced_folder ".", "/var/www/public", :mount_options => ["dmode=777", "fmode=777"]
config.vm.provision "shell", path: "./vagrant/vagrant.sh", privileged: false
end
# Virtual machine will be available on IP A.B.C.D (in our case 11.22.33.44, see above)
# Virtual can access your host machine on IP A.B.C.1 (this rule is given by Vagrant)
```
It requires file vagrant/vagrant.sh, because I wanted to enhance the server a bit. It contains following:
```
# Composer:
# (In case of composer errors, it can help to delete the vendor-folder and composer.lock file)
cd /var/www/public/
composer install
# You can automatically import your SQL (root/root, dbname scotchbox)
#mysql -u root -proot scotchbox < /var/www/public/vagrant/db.sql
# You can run migrations:
#php /var/www/public/protected/yiic.php migrate --interactive=0
# You can create folder and set 777 rights:
#mkdir /var/www/public/assets
#sudo chmod -R 777 /var/www/public/assets
# You can copy a file:
#cp /var/www/public/from.php /var/www/public/to.php
# Installing Xdebug:
sudo apt-get update
sudo apt-get install php-xdebug
# Configuring Xdebug in php.ini:
# If things do not work, disable your firewall and restart IDE. It might help.
echo "" | sudo tee -a /etc/php/7.0/apache2/php.ini
echo "[XDebug]" | sudo tee -a /etc/php/7.0/apache2/php.ini
echo "xdebug.remote_enable=1" | sudo tee -a /etc/php/7.0/apache2/php.ini
echo "xdebug.remote_port=9000" | sudo tee -a /etc/php/7.0/apache2/php.ini
echo "xdebug.remote_autostart=1" | sudo tee -a /etc/php/7.0/apache2/php.ini
echo "xxdebug.remote_log=/var/www/public/xdebug.log" | sudo tee -a /etc/php/7.0/apache2/php.ini
echo "xdebug.remote_connect_back=1" | sudo tee -a /etc/php/7.0/apache2/php.ini
echo "xdebug.idekey=netbeans-xdebug" | sudo tee -a /etc/php/7.0/apache2/php.ini
sudo service apache2 restart
```
... so create both files in your project ...
Before you use Vagrantfile, you must install Vagrant and VirtualBox (Sadly, these days VirtualBox does not work on the ARM-based Macs with the M1 chip):
- Install (Virtual Box)[https://www.virtualbox.org/wiki/Downloads], I recommend to install also the "Extension Pack", but is might be done automatically by "scotch/box".
- Install (Vagrant)[https://www.vagrantup.com/downloads] ... on Windows restart is needed :-(
- https://www.sitepoint.com/re-introducing-vagrant-right-way-start-php/
Now just open your command line, navigate to your project and you can start:
- "vagrant -v" should show you the version if things work.
- "vagrant init" creates a new project (You won't need it now)
- "vagrant up" creates the environment (this runs the Vagrantfile and creates/starts the virtual)
Once virtual is running, you can call also these:
- "vagrant ssh" opens Linux shell if the virtual machine is already running.
- "vagrant halt" stops the virtual
- "vagrant reload" restarts the virtual and does NOT run config.vm.provision
- "vagrant reload --provision" restarts the virtual and runs config.vm.provision
In the Linux shell you can call any command you want.
- To find what Linux version is installed: "cat /etc/os-release" or "lsb_release -a" or "hostnamectl"
- To get PHP version call: "php -version"
- If you are not allowed to run "mysql -v", you can run "mysql -u {username} -p" .. if you know the login
- Current IP: hostname -I
In "scotch/box" I do not use PhpMyAdmin , but (Adminer)[https://www.adminer.org/en/]. It is one simple PHP script and it will run without any installations.
Just copy the adminer.php script to your docroot and access it via browser. Use the same login as in configurafion of Yii. Server will be localhost.