Virtual Box And Vagrant

You need to install Virtual box first
* there are multiple ways to install virtual box

Problems with FTP

If you are using FTP you will need to install lftp on your ubunto server box

$ sudo apt-get install lftp

Virtual Box

Install

But an easier way is to install homebrew.

Install with brew

We just need to add the cask tap and then install brew cask.

$ brew install caskroom/cask/brew-cask

source of this info

$ brew cask install virtualbox

Now install Vagrant either from the website or use homebrew for installing it.

$ brew cask install vagrant

Vagrant-Manager helps you manage all your virtual machines in one place directly from the menubar.

$ brew cask install vagrant-manager

Add the Vagrant box you want to use.

$ vagrant box add precise64 http://files.vagrantup.com/precise64.box
$ vagrant init precise64

Now lets start the machine using the following command.

$ vagrant up

You can ssh into the machine now.

$ vagrant ssh

go into your folder that has Vagrant file

$ vagrant up

Go to this URL http://vvv.dev and you will see the dashboard

The Backstory

On a Mac the tradition has been to use MAMP. It is free to get PHP, MySQL, phpMyAdmin and Apache up and running quickly. This is great for WordPress. Millions of people currently use this process.

There is a problem though and that is that is what if you are working on a team and member of your team is using different machines and different setups of MAMP which could have different versions of PHP, MySQL and Apache.

The solution is to have your own box to test in and that is where Vitural Box comes to the rescue. It doesn’t matter if you have a PC or a Mac because you can create a virtual Linux box on either system. Oh, and it’s free.

But then we have another problem and assuming we are not all sysops people (Systems Operations) and have a thorough knowledge about servers and how to configure them that is where Vagrant comes into play. Vagrant will make creating our server box easy and dare I say fun.

Vagrant

Useful commands

  • any time you make a change to the configuration you can do the following to stop your vagrant instance.
$ vagrant halt

Then you want to run the following to take into consideration all the changes to the configurations.

$ vagrant up --provision

SSH into your box

You’ll be using this one a heck of a lot

$ vagrant ssh

Update your vagrant box

$ vagrant box update

“Create and configure lightweight, reproducible and portable development environments.”
* Download Vagrant
* Video How to Install and Use Vagrant

  • Where do you want to put your box on your machine?
~/Documents/dev/
  • Now let’s add a virtual box to our machine
$ vagrant box add hashicorp/precise32

Box Added!

  • Now we need to create a Vagrantfile
$ vagrant init hashicorp/precise32

Now get the box running

$ vagrant up

You now can easily SSH into the box

$ vagrant ssh

You are now logged in via SSH to your virtual box!

Installing Stuff On Your New Box

Install git on the box

$ sudo apt-get install git

Bam! after a couple of minutes you now have git install on your virtual box.

Exit out of SSH

$ exit

Open the Vagrantfile in ST3 (using your alias)

$ sop Vagrantfile
  • After file opens change syntax (bottom right of ST3) to Ruby to make it pretty

Uncomment (remove #) on this line and change the IP to whatever you want

  # config.vm.network "private_network", ip: "192.168.33.10"

Changed to:

config.vm.network "private_network", ip: "1.2.3.4"
  1. Save
  2. Reload Vagrant with:
  • The following command will shut your box down and reboot it with new configuration setting (new IP address)
$ vagrant reload

If you open your browser and try to go to http://1.2.3.4 you will get page not found error. It tries to load it but stops right away. The reason is there is no web server running (like Apache).

Before we go any further it is a good idea to update your box. Always a good idea when you spin up a box the FIRST THING YOU SHOULD ALWAYS DO IS RUN THIS COMMAND.

NGINX Server

$ sudo apt-get update

Install nginx

$ sudo apt-get install nginx

Once installed we need to start the server.

$ sudo service nginx start

Now if you browse to the IP you set http://1.2.3.4 the page runs!

But if my domain I am working with is http://ilovela.com how can I get this domain to show my test site?

Go to the domain now and see that it does not work.

Now we need to map this IP to a domain name.

$ exit

Now open the hosts file on your Mac inside ST3

$ sop /etc/hosts

Or if you don’t have ST3 and Terminal connected using an alias use this instead:

$ sudo open /etc/hosts -a "Sublime Text"
  • In this file DO NOT CHANGE ANYTHING ALEADY THERE
  • Just go to last line and add your IP and to the right of the IP, your domain name.
  • Just like this(the last line is the one I added):
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost
1.2.3.4         ilovela.com

Save and visit ilovela.com and you will see your same site from before.

But an even better way is prefix the domain you are working on with dev, so our site will now be known as dev.ilovela.com

Make the change in your host, save and browse to that new dev subdomain.

Why Vagrant instead of MAMP or XAMP or WAMP?

  • You can play around with linux machines
  • You can destroy them when you are finished
  • Running a Vagrant file in the repository for their project. You will be able to see a box that can be exactly like your production box.
  • Your code is not tied to one machine. Other devs on your team could be using an older version of MAMP or XAMP or a different PHP version or a different Apache version or a different MySQL or phpMyAdmin. If all of your team is using Vagrant you are all testing on the exact same instance of your production server. So the question that comes up with developers all the time is Well that doesn’t happen on my machine, why is it happening on your machine? is solved.

How to create a PHP Environment on Vagrant?

puphpet.com(https://puphpet.com/)
* Sets up:
– php
– mysql
– phpmyadmin
– ip and other useful names

When you use puPHPet to get your vagrant config file generated here are some important points to remember. The site has changed a lot in the last 3 years so as of 1/18/2016 this is the way it works:

  • Under Deploy Target choose local
    • Set your IP address to what you want (example: 10.20.30.40)
    • Set your host name to what you want (wordpresstest.dev)
    • Increase your memory from 512 to 1024 (1Gig)
  • Under Web Servers choose Apache
    • It is what most hosts use when working with WordPress and most people know more how to configure it than nginx.
  • For language choose php and right now the most popular stable version is 5.6
  • For Databases choose MySQL (although they recommend MariaDB)
    • Install Adminer (similar to phpMyAdmin (although they recommend Sequel Pro or Workbench))
    • by default the root user pwd is 123
  • Create Archive
    • Save to your Downloads folder
    • Extract and rename to your project title
    • Move to your dev sandbox
    • Once there, you can use the vagrant up to spin up your server instance
      • and vagrant SSH to gain access to that box via SSH

You will need to remember your IP and hostname to update your hosts file.

One very cool thing is that when you SSH into the server

$ vagrant ssh
$ cd /var/www
$ touch somefile.md

So you are in the www folder and you create a file. If you have your project folder on your machine open my location for that is Documents/wordpresstest you will see the file you created on our linux box has also been created on our machine folder. This is possible because the two folders are sym linked. This enables us to use all our cool dev tools we like on our Mac machine and see the files run on linux. Very Cool!

Start and Stop the Apache server

$ sudo service apache2 stop
$ sudo service apache2 start

To turn your virtual machine off

$ exit

Logs you out of the server.

$ vagrant halt

Turns your box off

How to install PHP on a virtual machine using a Mac

vagrant destroy This permanently removes the virtual environment from your machine.

vagrant reload and vagrant reload --provision If your environment suddenly stops working, it’s useful to reboot by running this command. If you add the —provision flag, it will reprovision the box as well. This is useful with removing or adding things to the server via Cookbooks or Puppet.

vagrant ssh Anything you edit on your computer in the public folder is automatically shared to the virtual environment without having to SSH in. However, if for whatever reason you need to connect to the virtual server, just run this command.

vagrant ssh-config This lets you see the detailed login credentials of how you could connect to the virtual environment.

Tip – Sharing folders with local VM
* Change from ./ to ./workspace/

When you get the config file from puPHPet, save the zip file to this path:

Documents/dev/puPHPet/wp-project-name-here
* extract the zip
* the random named file is the name of your server so you just need to make sure if you are running multiple server instances that the server folder name is not duplicated, the IP is not duplicated and the domain name is not duplicated.
* delete the zip file (keep your stuff clean!)
* create a workspace folder

Download WordPress

Download link to Downloads
* Extract WordPress
* Delete Zip file
* Move files inside wordpress folder into your workspace folder for your project.

Visit your domain or IP address for your virtual server.
Install WordPress as you normally would.

  • Using LastPass with Vagrant has an added benefit – instead of saving a billion username and passwords for all the localhost project sites, lastpass can work with your unique dev domain names. Way easier to find because there is only one.
  • Choose a hard password because you most likely will forget when you push it live and then you’ll have a weak password in production which is a huge security risk.
  • Never make your username admin. It is the default and hackers will first plug that in when you they are using a brute force attack against your site and just by using any other name besides admin your site is at least 35% more safe from the prying hands of dubious hackers.
  • Make sure you check the Discourage search engines from indexing this site when you are developing but when you do finally deploy your site into production, it is equally essential you UNCHECK this box so you are NOT INVISIBLE TO SEARCH ENGINES which would be really bad for your business.

Speed Up WordPress install with WP CLI!

How do I access Adminer (similar to phpMyAdmin)

ip-address/adminer
example: 10.20.30.40/adminer
Create your database: (I named mine wordpresstest)
default user: root
default pwd: 123

VVV
* first install Virtual Box
* second install Vagrant 1.7.x

$ vagrant plugin install vagrant-hostsupdater

then install triggers

$ vagrant plugin install vagrant-triggers

Variable VVV
VVV Dashboard

Clone the VVV project into a local directory
* Varying-Vagrant-Vagrants / VVV github

with this command

$ git clone git://github.com/Varying-Vagrant-Vagrants/VVV.git vagrant-local

cd into vagrant-local

then

$ vagrant up

Go get coffee. Could take 30 minutes.

WordPress Stable

LOCAL PATH: vagrant-local/www/wordpress-default
VM PATH: /srv/www/wordpress-default
URL: http://local.wordpress.dev
DB Name: wordpress_default
WordPress Trunk

LOCAL PATH: vagrant-local/www/wordpress-trunk
VM PATH: /srv/www/wordpress-trunk
URL: http://local.wordpress-trunk.dev
DB Name: wordpress_trunk
WordPress Develop

LOCAL PATH: vagrant-local/www/wordpress-develop
VM PATH: /srv/www/wordpress-develop
/src URL: http://src.wordpress-develop.dev
/build URL: http://build.wordpress-develop.dev
DB Name: wordpress_develop
DB Name: wordpress_unit_tests
MySQL Root

User: root
Pass: root
See: Connecting to MySQL from your local machine

Solutions to common problems

vagrant ssh isn’t working

And you get an error similar to this:

vagrant ssh error

Just make sure to to run vagrant up before you run vagrant ssh

update your vagrant box

It’s a best practice
update vagrant box message

If you see something like above

Just run this:

$ vagrant box update

Caution on vagrant box update

When you run in and then try to vagrant ssh your box sometimes forgets about your web site directories. What I do to fix this is open virtual box and manual shut down the vagrant box. I then run vagrant up and then when I use vagrant ssh, it knows about my web site directories.
This has happened a few times to me when I updated Vagrant box so just be aware of it if it happens to you.
(It’s pretty scary when you feel all your sites are gone.. the config should be on another computer too… just to be safe)

Leave a Reply

Your email address will not be published. Required fields are marked *