Overview

Phalcon Box uses the default phalcon/xenial64 box from Vagrant Cloud for compatibility. If you choose to use a 64-bit ISO you may need to update your BIOS to enable virtualization with AMD-V, Intel VT-x or VIA VT.

The first time that you provision a new environment with vagrant up, the process will take a lot longer since the box (phalconphp/xenial64) will have to be downloaded to your local machine first. Any subsequent environment provisions will be much faster.

Requirements

  • Operating System: Windows, Linux, or macOS
  • Virtualbox >= 5.1 (if you want to build the VirtualBox box)
  • VMware Fusion (or Workstation - if you want to build the VMware box)
  • Vagrant >= 1.9.8

Packages Included

  • Ansible
  • Beanstalkd
  • Blackfire
  • Composer
  • Git
  • goreplace
  • Mailhog
  • Memcached
  • MongoDB
  • MySQL
  • Nginx
  • Ngrok
  • Node.js (with Yarn, Bower, Grunt, and Gulp)
  • PHIVE
  • PHP 7.1
  • PHPMD
  • PHP_CodeSniffer
  • Phalcon
  • Phing
  • PostgreSQL
  • Redis
  • Sqlite3
  • Ubuntu 16.04
  • Zephir

Installation

Installing the Vagrant Box

Before launching your Phalcon Box environment, you must install VirtualBox, or VMWare as well as Vagrant. All of these software packages provide easy-to-use visual installers for all popular operating systems.

Once VirtualBox/VMWare and Vagrant have been installed, you should add the phalconphp/xenial64 box to your Vagrant installation using the following command in your terminal. It will take a few minutes to download the box, depending on your Internet connection speed:

  1. vagrant box add phalconphp/xenial64

If this command fails, make sure your Vagrant installation is up to date.

To use the VMware provider, you will need to purchase both VMware Fusion / Workstation and the VMware Vagrant plug-in. Though it is not free, VMware can provide faster shared folder performance out of the box.

Installing the Phalcon Box

You can install Phalcon Box by simply cloning the repository. Consider cloning the repository into a workspace folder within your home directory, as the Phalcon Box box will serve as the host to all of your Phalcon projects:

  1. cd ~
  2. git clone https://github.com/phalcon/box.git workspace

The master branch will always contain the latest stable version of Phalcon Box. If you wish to check older versions or newer ones currently under development, please switch to the relevant branch/tag.

You can find the latest stable version on the Github Release Page:

  1. # Clone the desired release...
  2. git checkout v2.4.0

Once you have cloned the Phalcon Box repository, run the install command from the Phalcon Box root directory to create the settings.yml configuration file. The settings.yml file will be placed in the Phalcon Box directory:

  1. # macOS || Linux
  2. ./install
  1. rem Windows
  2. install.bat

Now you are ready to provision your Virtual Machine, run:

  1. vagrant up

Configuring

Setting your provider

The provider key in your settings.yml file indicates which Vagrant provider should be used: virtualbox, vmware_fusion or vmware_workstation. You may set this to the provider you prefer:

  1. provider: virtualbox

Memory and CPU

By default this setup uses 2GB RAM. You can change this in settings.yml and simply run vagrant reload:

  1. memory: 4096

You can also use more than one core if you like, simply change this line in the same file:

  1. cpus: 4

Shared folders

The folders property of the settings.yml file lists all of the folders you wish to share with your Phalcon Box environment. As files within these folders are changed, they will be kept in sync between your local machine and the Phalcon Box environment. You may configure as many shared folders as necessary:

  1. folders:
  2. - map: ~/workspace
  3. to: /home/vagrant/workspace

To enable NFS, just add a simple flag to your synced folder configuration:

  1. folders:
  2. - map: ~/workspace
  3. to: /home/vagrant/workspace
  4. type: "nfs"

You may also pass any options supported by Vagrant’s Synced Folders by listing them under the options key:

  1. folders:
  2. - map: ~/workspace
  3. to: /home/vagrant/workspace
  4. type: "nfs"
  5. options:
  6. rsync__args: ["--verbose", "--archive", "--delete", "-zz"]
  7. rsync__exclude: ["node_modules"]
macOS users probably will need to install vagrant-bindfs plugin to fix shared folder (NFS) permission issue:
  1. vagrant plugin install vagrant-bindfs

Nginx sites

The sites property allows you to easily map a “domain” to a folder on your Phalcon Box environment. A sample site configuration is included in the settings.yml file. You may add as many sites to your Phalcon Box environment as necessary. Phalcon Box can serve as a convenient, virtualized environment for every Phalcon project you are working on:

  1. sites:
  2. - map: phalcon.local
  3. to: /home/vagrant/workspace/phalcon/public

You can use the type parameter to specify the type of Nginx configuration for the site. For example:

  1. sites:
  2. - map: landing.local
  3. to: /home/vagrant/workspace/landing/public
  4. type: spa

The default type is phalcon. If the desired type is not allowed or not available phalcon will be used as fallback.

Available types:

If you change the sites property after provisioning the Phalcon Box, you must re-run vagrant reload —provision to update the Nginx configuration on the virtual machine.

Custom Nginx configuration

You can also create your own type. To do this take any template from the provisioning/templates/nginx folder as a basis and make the necessary changes. You need to place this file into the same folder. After that, you will be able to use your own custom type:

  1. sites:
  2. - map: my-site.local
  3. to: /home/vagrant/workspace/my-site/public
  4. # provisioning/templates/nginx/phalcon-advanced.conf.j2
  5. type: phalcon-advanced

Do you need a custom global Nginx configuration? Yes, this is possible. Fox example, let’s create the autoindex configuration.

File /home/user/nginx.d/00-autoindex.conf:

  1. # Processes requests ending with the slash character (‘/’) and produces a directory listing
  2. autoindex on;

Add the desired settings to your file and then add it to the copy section:

  1. copy:
  2. - from: /home/user/nginx.d/00-autoindex.conf
  3. to: /etc/nginx/conf.d/

Configuring the hosts file

You must add the “domains” for your Nginx sites to the hosts file on your machine. The hosts file will redirect requests for your Phalcon sites into your Phalcon Box machine. On Mac and Linux, this file is located at /etc/hosts. On Windows, it is located at C:\Windows\System32\drivers\etc\hosts. The lines you add to this file will look like the following:

  1. 192.168.50.4 phalcon.local

Make sure the IP address listed is the one set in your settings.yml file. Once you have added the domain to your hosts file and launched the Vagrant box you will be able to access the site via your web browser:

  1. http://phalcon.local
To enable adding new sites to the hosts file automatically use vagrant-hostsupdater plugin:
  1. vagrant plugin install vagrant-hostsupdater

Install additional packages

We did our best to provide Phalcon Box with all necessary programs and libraries. However, it should be understood that the typical user does not need all possible packages which can be installed. Phalcon Box must be of reasonable size so that it could be used by even those people who are experiencing difficulties with bandwidth of the Internet channel.

Because of these considerations, we allow users to specify which custom packages they need by every provision. To install the necessary packages add their names in the apt section:

  1. # Provisioning features
  2. provision:
  3. # do full system update for each full provisoning
  4. update: true
  5. # Install wkhtmltopdf and libffi-dev packages
  6. apt:
  7. - wkhtmltopdf
  8. - libffi-dev

Launching the Phalcon Box

Once you have edited the settings.yml to your liking, run the vagrant up command from your Phalcon Box directory (for example $HOME/workspace). Vagrant will boot the virtual machine and automatically configure your shared folders and Nginx sites.

To destroy the machine, you may use the vagrant destroy —force command.

Daily usage

Accessing Phalcon Box globally

Sometimes you may want to vagrant up your Phalcon Box machine from anywhere on your filesystem. You can do this on Mac or Linux systems by adding a Bash function to your Bash profile. On Windows, you may accomplish this by adding a “batch” file to your PATH. These scripts will allow you to run any Vagrant command from anywhere on your system and will automatically point that command to your Phalcon Box installation:

Mac || Linux

  1. function box()
  2. {
  3. ( cd $HOME/workspace && vagrant $* )
  4. }
Make sure to tweak the $HOME/workspace path in the function to the location of your actual Phalcon Box installation. Once the function is installed, you may run commands like box up or box ssh from anywhere on your system.

Windows

Create a box.bat batch file anywhere on your machine with the following contents:

  1. @echo off
  2. set cwd=%cd%
  3. set box=C:\workspace
  4. cd /d %box% && vagrant %*
  5. cd /d %cwd%
  6. set cwd=
  7. set box=
Make sure to tweak the example C:\workspace path in the script to the actual location of your Phalcon Box installation. After creating the file, add the file location to your PATH. You may then run commands like box up or box ssh from anywhere on your system.

Connecting via SSH

You can SSH into your virtual machine by issuing the vagrant ssh terminal command from your Phalcon Box directory.

But, since you will probably need to SSH into your Phalcon Box machine frequently, consider adding the “function” described above to your host machine to quickly SSH into the Phalcon Box.

Connecting to databases

To connect to your MySQL, Postgres or MongoDB database from your host machine’s database client, you should connect to 127.0.0.1 and port 33060 (MySQL), 54320 (Postgres) or 27017 (MongoDB). The username and password for databases is phalcon / secret.

You should only use these non-standard ports when connecting to the databases from your host machine. You will use the default 3306 and 5432 ports in your Phalcon database configuration file since Phalcon is running within the Virtual Machine.

To access to the interactive db console from Phalcon Box type:

  • Postgres: psql -U phalcon -h localhost (password secret)
  • MySQL: mysql (password not needed for CLI tool)
  • MongoDB: mongo (password not needed for CLI tool)

Adding additional sites

Once your Phalcon Box environment is provisioned and running, you may want to add additional Nginx sites for your applications. You can run as many Phalcon projects as you wish on a single Phalcon Box environment. To add an additional site, simply add the site to your settings.yml file:

  1. sites:
  2. - map: phalcon.local
  3. to: /home/vagrant/workspace/phalcon/public
  4. - map: pdffiller.local
  5. to: /home/vagrant/workspace/pdffiller/public
  6. - map: blog.local
  7. to: /home/vagrant/workspace/blog/public

If Vagrant is not managing your “hosts” file automatically, you may need to add the new site to that file as well:

  1. 192.168.50.4 phalcon.local
  2. 192.168.50.4 pdffiller.local
  3. 192.168.50.4 blog.local
To enable adding new sites to the hosts file automatically use vagrant-hostsupdater plugin:
  1. vagrant plugin install vagrant-hostsupdater

Once the site has been added, run the vagrant reload —provision command from your Phalcon Box directory.

Environment variables

Global variables

You can easily register global environment variables. Just add variable and its value to the variables section:

  1. variables:
  2. - key: TEST_DB_MYSQL_USER
  3. value: phalcon
  4. - key: TEST_DB_MYSQL_PASSWD
  5. value: secret
  6. - key: TEST_DB_MYSQL_DSN
  7. value: "mysql:host=127.0.0.1;dbname=phalcon_test"

This way you will be able to use these variables in your applications or scripts. For example when configuring Codeception in such way:

  1. # File codeception.yml
  2. params:
  3. # Get params from environment
  4. - env

you will able to configure Unit suite as follows:

  1. # File tests/unit.suite.yml
  2. class_name: UnitTester
  3. modules:
  4. enabled:
  5. - Db
  6. config
  7. Db:
  8. dsn: %TEST_DB_MYSQL_DSN%
  9. user: %TEST_DB_MYSQL_USER%
  10. password: %TEST_DB_MYSQL_PASSWD%
  11. populate: true
  12. cleanup: false
  13. dump: tests/_data/schemas/mysql/mysql.dump.sql

Site variables

Site variables are how you can easily add fastcgi_param values to your site host configuration within Phalcon Box. For example, we may add a APP_ENV variable with the value development:

  1. sites:
  2. - map: phalconbox.local
  3. to: /var/www/phalconbox/public
  4. variables:
  5. - key: APP_ENV
  6. value: development
  7. # Yet another example
  8. - key: AMQP_DEBUG
  9. value: true

Ports

By default, the following ports are forwarded to your Phalcon Box environment:

Forwarded portPhalcon BoxHost system
SSH222222
HTTP808000
HTTPS44344300
MySQL330633060
Postgres543254320
MailHog80258025

Forwarding additional ports

If you wish, you may forward additional ports to the Phalcon Box, as well as specify their protocol:

  1. ports:
  2. - send: 63790
  3. to: 6379
  4. - send: 50000
  5. to: 5000
  6. - send: 7777
  7. to: 777
  8. protocol: udp

Sharing your environment

Sometimes you may wish to share what you’re currently working on with coworkers or a client. Vagrant has a built-in way to support this via vagrant share; however, this will not work if you have multiple sites configured in your settings.yml file.

To solve this problem, Phalcon Box includes its own share command. To get started, SSH into your Phalcon Box machine via vagrant ssh and run share <your-site-here>, for example: share blog.local. This will share your site from your settings.yml configuration file. Of course, you may substitute any of your other configured sites for blog.local:

  1. share blog.local

After running the command, you will see an Ngrok screen appear which contains the activity log and the publicly accessible URLs for the shared site. If you would like to specify a custom region, subdomain, or other Ngrok runtime option, you may add them to your share command:

  1. share blog.local -region=eu -subdomain=phalcongelist
Vagrant is inherently insecure and you are exposing your virtual machine to the Internet when running the share command.

Network interfaces

The networks property of the settings.yml configures network interfaces for your Phalcon Box environment. You may configure as many interfaces as necessary:

  1. networks:
  2. - type: "private_network"
  3. ip: "192.168.50.99"

To enable a bridged interface, configure a bridge setting and change the network type to public_network:

  1. networks:
  2. - type: "private_network"
  3. ip: "192.168.50.99"
  4. bridge: "en1: Wi-Fi (AirPort)"

To enable DHCP, just remove the ip option from this configuration:

  1. networks:
  2. - type: "private_network"
  3. bridge: "en1: Wi-Fi (AirPort)"

Updating Phalcon Box

You can update Phalcon Box in two simple steps.

  • First, you will need to update the Vagrant box using the vagrant box update command:
  1. vagrant box update
  • Next, you need to update the Phalcon Box source code. If you cloned the repository you can simply
  1. git pull origin master

at the location you originally cloned the repository.

The new version of Phalcon Box will contain updated or amended configuration files:

  • settings.yml
  • .bash_aliases
  • after_provision.sh
    When you run the command ./install (or install.bat) the Phalcon Box creates these files in the root directory. However, if the files already exist, they will not be overwritten.

We recommend that you always take backups of those files, and remove them from the project so that the new updated ones can be copied over. You can then compare your own files with the phalcon box ones to apply your personalized changes and take advantage of the new features offered by the update.

Provider specific settings

VirtualBox

By default, Phalcon Box configures the natdnshostresolver setting to on. This allows Phalcon Box to use your host operating system’s DNS settings. If you would like to override this behavior, add the following lines to your settings.yml file:

  1. natdnshostresolver: off

Mail catcher

By default, Phalcon Box redirects all PHP emails to MailHog (instead of sending them to the outside world). You can access the MailHog UI at http://localhost:8025/ (or whatever domain you have configured in settings.yml).

Troubleshooting

Problem:

An error occurred in the underlying SSH library that Vagrant uses. The error message is shown below. In many cases, errors from this library are caused by ssh-agent issues. Try disabling your SSH agent or removing some keys and try again. If the problem persists, please report a bug to the net-ssh project. timeout during server version negotiating

Solution:

  1. vagrant plugin install vagrant-vbguest

Problem:

Vagrant was unable to mount VirtualBox shared folders. This is usually because the filesystem “vboxsf” is not available. This filesystem is made available via the VirtualBox Guest Additions and kernel module. Please verify that these guest additions are properly installed in the guest. This is not a bug in Vagrant and is usually caused by a faulty Vagrant box. For context, the command attempted was: mount -t vboxsf -o uid=900,gid=900 vagrant /vagrant

Solution:

  1. vagrant plugin install vagrant-vbguest

Problem:

There was an error while executing VBoxManage, a CLI used by Vagrant for controlling VirtualBox. The command and stderr is shown below. Command: ["startvm", "9d2b95e1-0fdd-40f4-ad65-4b56eb4315f8", "—type", "headless"] Stderr: VBoxManage.exe: error: VT-x is not available (VERR_VMX_NO_VMX) VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component ConsoleWrap, interface IConsole

Solution:

You need to update your BIOS to enable virtualization with Intel VT-x.