/magento2-vagrant-for-developers

Vagrant Box for Magento 2 Developers

Primary LanguageShellMIT LicenseMIT

Vagrant project for Magento 2 developers (optimized for Mac, Windows and *nix hosts)

MIT License Semver Latest GitHub release

What You get

It is expected that Magento 2 project source code will be located and managed on the host. This is necessary to allow quick indexing of project files by IDE. All other infrastructure is deployed on the guest machine.

Current Vagrant configuration aims to solve performance issues of Magento installed on Virtual Box for development. Custom solution is implemented for Windows hosts. See explanation of the proposed solution.

Environment for Magento EE development is configured as well.

It is easy to install multiple Magento instances based on different codebases simultaneously.

Project initialization script configures complete development environment:

  1. Adds some missing software on the host
  2. Configures all software necessary for Magento 2 using custom Ubuntu vagrant box (Apache 2.4, PHP 7.0 (or 5.5.9), MySQL 5.6, Git, Composer, XDebug, Rabbit MQ)
  3. Installs Magento 2
  4. Configures PHP Storm project (partially at the moment)

How to install

If you never used Vagrant before, read Vagrant Docs

Requirements

Software listed below should be available in PATH (except for PHP Storm).

  • Vagrant 1.8+

  • VirtualBox

  • Git. Make sure you have SSH keys generated and associated with your github account, see how to check and how to configure if not configured.
    ℹ️ It is possible to use another way of getting codebase instead of cloning, it does not matter for successful installation. Just put Magento 2 codebase inside of vagrant-magento/magento2ce.
    ℹ️ On Windows hosts Git must be v2.7+, also make sure to set the following options to avoid issues with incorrect line separators:

    git config --global core.autocrlf false
    git config --global core.eol LF
    git config --global diff.renamelimit 5000
    
  • PHP (any version) to allow Magento dependency management with Composer

  • PHP Storm is optional but recommended.

  • NFS server must be installed and running on *nix and OSX hosts. Is usually available, so just try to follow installation steps first.

Installation steps

ℹ️ In case of any issues during installation, please read troubleshooting section

  1. Open terminal and change directory to the one which you want to contain Magento project. On Windows use Git Bash, which is available after Git installation

  2. Download project with Vagrant configuration:

git clone git@github.com:paliarush/magento2-vagrant-for-developers.git vagrant-magento
  1. Optionally, if you use private repositories on GitHub or download packages from Magento Marketplace
  • copy etc/composer/auth.json.dist to etc/composer/auth.json
  • specify your GitHub token by adding "github.com": "your-github-token" to github-oauth section for GitHub authorization
  • add Magento Marketplace keys for Marketplace authorization
  1. Optionally, copy etc/config.yaml.dist as etc/config.yaml and make necessary customizations

  2. Initialize project (this will configure environment, install Magento, configure PHPStorm project):

cd vagrant-magento
bash init_project.sh
  1. Use vagrant-magento directory as project root in PHP Storm (not vagrant-magento/magento2ce). This is important, because in this case PHP Storm will be configured automatically by init_project.sh. If NFS files sync is disabled in config and on Windows hosts verify deployment configuration in PHP Storm

  2. Configure remote PHP interpreter in PHP Storm. Go to Settings => Languages & Frameworks => PHP, add new remote interpreter and select "Deployment configuration" as a source for connection details.

Default credentials and settings

Some of default settings are available for override. These settings can be found in the file etc/config.yaml.dist. To override settings just create a copy of the file under the name 'config.yaml' and put there your custom settings. When using init_project.sh, if not specified manually, random IP address is generated and is used as suffix for host name to prevent collisions, in case when 2 or more instances are running at the same time. Upon a successful installation, you'll see the location and URL of the newly-installed Magento 2 application in console.

Web access:

  • Access storefront at http://magento2.vagrant<random_suffix>
  • Access admin panel at http://magento2.vagrant<random_suffix>/admin/
  • Magento admin user/password: admin/123123q
  • Rabbit MQ control panel: http://magento2.vagrant<random_suffix>:15672, credentials guest/guest

Codebase and DB access:

  • Path to your Magento installation on the VM:
    • Can be retrieved from environment variable: echo ${MAGENTO_ROOT}
    • On Windows hosts: /var/www/magento2ce
    • On Mac and *nix hosts: the same as on host
  • MySQL DB host: localhost (not accessible remotely)
  • MySQL DB name: magento, magento_integration_tests
  • MySQL DB user/password: just use mysql with no user and password (root/password will be used by default)

Codebase on host

  • CE codebase: vagrant_project_root/magento2ce
  • EE codebase will be available if path to EE repository is specified in etc/config.yaml: vagrant_project_root/magento2ce/magento2ee

Getting updates and fixes

Current vagrant project follows semantic versioning so feel free to pull the latest features and fixes, they will not break your project. For example your current branch is 2.0, then it will be safe to pull any changes from origin/2.0. However branch 3.0 will contain changes backward incompatible with 2.0. Note, that semantic versioning is only used for x.0 branches (not for develop).

Troubleshooting

  1. On OSX and *nix hosts NFS will be used by default to sync your project files with guest. On some hosts Vagrant cannot configure NFS properly, in this case it is possible to deploy project without NFS by setting use_nfs option in config.yaml to 0
  2. On Windows hosts you might face Composer Install Error: ZipArchive::extractTo(): Full extraction path exceed MAXPATHLEN (260) exception during composer install. This can be fixed in 2 ways: decrease path length to the project directory or set composer_prefer_source option in config.yaml to 1
  3. Make sure that you used vagrant-magento directory as project root in PHP Storm (not vagrant-magento/magento2ce)
  4. If code is not synchronized properly on Windows hosts (or when NFS mode is disabled in config.yaml explicitly), make sure that PhpStorm is running before making any changes in the code. This is important because otherwise PhpStorm will not be able to detect changes and upload them to the guest machine
  5. Please make sure that currently installed software, specified in requirements section, meets minimum version requirement

Day-to-day development scenarios

Reinstall Magento

To save some time and get clear Magento installation, you can skip installation of software like web server or php. The following command will clear Magento DB, Magento caches and reinstall Magento instance.

Go to 'vagrant-magento' created earlier and run in command line:

bash m-reinstall

Clear Magento cache

Go to 'vagrant-magento' created earlier and run in command line:

bash m-clear-cache

Switch between CE and EE

Assume, that EE codebase is available in vagrant_project_root/magento2ce/magento2ee. The following commands will link/unlink EE codebase, clear cache, update composer dependencies and reinstall Magento. Go to 'vagrant-magento' created earlier and run in command line:

bash m-switch-to-ce
OR
bash m-switch-to-ee

ℹ️ On Windows hosts (or when NFS mode is disabled in config.yaml explicitly) you will be asked to wait until code is uploaded to guest machine by PhpStorm (PhpStorm must be lunched). To continue the process press any key.

Update Composer dependencies

Go to 'vagrant-magento' created earlier and run in command line:

bash m-composer install
OR
bash m-composer update

Use Magento CLI (bin/magento)

Go to 'vagrant-magento' created earlier and run in command line:

bash m-bin-magento <command_name>
e.g.
bash m-bin-magento list

Debugging with XDebug

XDebug is already configured to connect to the host machine automatically. So just:

  1. Set XDEBUG_SESSION=1 cookie (e.g. using 'easy Xdebug' extension for Firefox). See XDebug documentation for more details
  2. Start listening for PHP Debug connections in PhpStorm on default 9000 port. See how to integrate XDebug with PhpStorm
  3. Set beakpoint or set option in PhpStorm menu 'Run -> Break at first line in PHP scripts'

To debug a CLI script:

  1. Create remote debug configuration in PhpStorm, use PHPSTORM as IDE key
  2. Run created remote debug configuration
  3. Run CLI command on the guest as follows (xdebug.remote_host value might be different for you):
php -d xdebug.remote_host=192.168.10.1 -d xdebug.idekey=PHPSTORM -d xdebug.remote_connect_back=0 -d xdebug.remote_autostart=1 <path_to_cli_script>

To debug Magento Setup script, go to Magento installation script and find php ${install_cmd}. Follow steps above for any CLI script

Connecting to MySQL DB

Answer can be found here

Accessing PHP and other config files

It is possible to view/modify majority of guest machine config files directly from IDE on the host. They will be accessible in etc/guest directory only when guest machine is running. The list of accessible configs includes: PHP, Apache, Mysql, Varnish, RabbitMQ. Do not edit any symlinks using PhpStorm because it may break your installation.

After editing configs in IDE it is still required to restart related services manually.

Multiple Magento instances

To install several Magento instances based on different code bases, just follow Installation steps to initialize project in another directory on the host. Unique IP address, SSH port and domain name will be generated for each new instance if not specified manually in etc/config.yaml

Reset environment

It is possible to reset project environment to default state, which you usually get just after project initialization. The following command will delete vagrant box, vagrant project settings and PhpStorm project settings. After that it will initialize project from scratch. Magento 2 code base (magento2ce directory) and etc/config.yaml will stay untouched, but guest config files (located in etc/guest) will be removed.

Go to 'vagrant-magento' created earlier and run in command line:

bash init_project.sh -f

You can reset project settings and Magento 2 code base at the same time. Magento 2 code base will be deleted and then cloned from the repositories specified in etc/config.yaml

Go to 'vagrant-magento' created earlier and run in command line:

bash init_project.sh -fc

To reset PhpStorm project configuration, in addition to -f specify -p option:

bash init_project.sh -fp
OR
bash init_project.sh -fcp