Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the "works on my machine" excuse a relic of the past.
Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.
A Virtualbox virtual machine with Ubuntu Trusty64 provisioned with this:
- Common Role:
- git
- curl
- wget
- ntp
- vim
- htop
- sendmail
- lynx
- dot files: ~/.bash_aliases, ~/.bash_colors, ~/.bashrc
- A copy of your local .gitconfig, .gitignore and .ssh folder
- Webtier Role:
- apache2 with modules: actions, alias, fastcgi, rewrite, ssl, wsgi
- php with packages: php-dev, php-pear, php-xdebug, php-curl, php-mcrypt, php-imagick, php-intl, php-gd, phpunit, php-mongodb, php-mysql, php-mbstring, php-zip, php-bcmath
- python with packages: python-setuptools, python-mysqldb, python-pip, python-virtualenv
- npm with components: bower, gulp-cli, grunt-cli, live-server
- ruby with packages: ruby-bundler
- composer
- nodejs
- Oracle Java
- DBtier Role:
- MySQL
- MongoDB
- ElasticSearch
- Webproject Role:
- Clone repositories
- SSL apache config
- Apache virtual host config
- Shell script
You must have installed the following tools in order to work:
sudo apt-get install software-properties-common
sudo apt-get update
sudo apt-get install git vagrant ansible virtualbox virtualbox-guest-additions-iso
- https://www.virtualbox.org/wiki/Downloads
- https://www.vagrantup.com/downloads.html
- https://git-scm.com/download/mac
- http://docs.ansible.com/ansible/intro_installation.html#latest-releases-on-mac-osx
b. HomeBrew way http://brew.sh/
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install caskroom/cask/brew-cask
brew cask install vagrant
brew cask install virtualbox
brew install ansible
- https://www.virtualbox.org/wiki/Downloads
- https://www.vagrantup.com/downloads.html
- https://git-scm.com/download/win
- Ansible is not compatible with Windows, so do not install it. Don't worry the virtual machine will be provisioned with an ansible installed locally in the virtual machine.
I recommend this directory structure:
Workspace/
├── hittten/
| ├── dev-server/
| ├── .
| ├── .
| └── another-web-project/
├── .
├── .
└── another-vendor/
└── another-web-project/
- Open a terminal (if you are using windows open git-bash as an Administrator. Why?)
- Go to your workspace directory
mkdir hittten
cd hittten
git clone git@github.com:hittten/dev-web-server.git
- Setup your projects config file: Create projects.yml in the config directory (../dev-web-server/config/projects.yml) and build your projects:
Parameter | Type | Required | Default | Comments |
---|---|---|---|---|
domain | string | Yes | None | Apache ServerName. See ServerName Directiva and VirtualHost Examples. |
ssl | string | Yes | None | Only options: 'false' or 'true' . Note: If there are no single quotes, it will not work. |
workspace | string | Yes | None | Path of the project code. |
host | string | No | '*:80' or '*:443' depends of ssl parameter. |
This parameter will be in <VirtualHost host >.See VirtualHost Directiva and VirtualHost Examples. |
public_html | string | No | same workspace parameter | Path to the project folder that is public from the browser. See Directory Directiva |
repository | string | No | None | git, SSH, or HTTP(S) protocol address of the git repository. |
version | string | when repository is defined | None | What version of the repository to check out. This can be the the literal string HEAD , a branch name, a tag name. |
config/projects.yml example:
---
projects:
- {
domain: 'domain.com.dev',
ssl: 'false',
repository: 'git@github.com:vendor/repository.git',
version: 'dev',
workspace: '/home/ubuntu/Workspace/vendor/domain.com.dev',
public_html: '/home/ubuntu/Workspace/vendor/domain.com.dev/web',
script: 'cd /home/ubuntu/Workspace/vendor/domain.com.dev && yes | composer install --no-interaction'
}
- {
domain: 'other.domain.com.dev',
ssl: 'true',
workspace: '/home/ubuntu/Workspace/vendor/other.domain.com.dev',
}
- If you build a project with ssl, you must put the crt file and key file in (../dev-web-server/config/ssl/) path with the same name of the
domain
parameter:- dev-web-server/config/ssl/other.domain.com.dev.crt
- dev-web-server/config/ssl/other.domain.com.dev.key
- Edit your local hosts file: Windows: C:\Windows\System32\drivers\etc\hosts, Linux: /etc/hosts or OSX: /etc/hosts:
- Add new line with your domains projects
192.168.2.100 domain.com.dev
- Add new line with your domains projects
cd dev-web-server # Note: you always have to be in this directory to run these commands
#To manage the virtual machine
vagrant up #To start the virtual machine.
vagrant ssh #To connect to the virtual machine.
vagrant reload #To restart the virtual machine.
vagrant status #To see the status of virtual machine.
vagrant halt #To turn off the virtual machine (always turn off your virtual machine before restart or shutdown your host computer).
vagrant destroy #To delete the virtual machine (you can use this if you have any problem with your machine and start over with a "vagrant up").
#To manage provision
vagrant up --provision #When you do vagrant up in a first time the ansible will provisioned the machine automatically, if you need reprovision you can use this command with the machine off
vagrant provision #When you do vagrant up in a first time the ansible will provisioned the machine automatically, if you need reprovision you can use this command with the machine on
- When the provision finish you can open http//domain.com.dev and see your web project
- For more commands see: https://www.vagrantup.com/docs/cli/
Then in your host machine you can use any IDE software to develop in your Workspace directory and you will see all these projects in your virtual machine (~/Workspace)
To enable */app_dev.php in your project, remember to add your project ip in web/app_dev.php
if (isset($_SERVER['HTTP_CLIENT_IP'])
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|| !(in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '192.168.2.1', '::1'], true) || PHP_SAPI === 'cli-server')
) {
header('HTTP/1.0 403 Forbidden');
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
Create config.rb in the config directory (../dev-web-server/config/config.rb), copy and paste this:
# Set the ip machine in order to work with your router gateway.
# You can set almost any IP depend of your router configuration.
# Example: router gateway: 192.168.1.1 => 192.168.2.xxx (the 2 can be any number except 1)
# Example: router gateway: 10.1.1.1 => 10.1.2.xxx (the 2 can be any number except 1)
# Note: if you don't have any special config in your router, you can leave the default value.
VIRTUAL_MACHINE_IP = '192.168.2.100'
# The memory RAM of your virtual machine
VIRTUAL_MACHINE_MEMORY = '2048'
GIT_CONFIG_FILE = '~/.gitconfig'
GIT_IGNORE_FILE = '~/.gitignore'
SSH_PATH = '~/.ssh'
Edit whatever you want and save.