Migrating from WAMP to Vagrant
The article describes how WAMP and Vagrant work, the technological advantages and drawbacks of each, reasons why you may or may not want to migrate from one development environment to the other. The installation process and the problems encountered along the way are described.
When developing larger or smaller projects (websites, tools, systems) in PHP/MySQL, you may find yourself in a situation where the production environment differs from the development environment. PHP and MySQL are available on MS Windows, Mac and Linux, but such projects are most often hosted on Linux servers. Standard PHP functionality will be similar across all environments, but for more specific projects the production environment may offer specific services, for example Memcached, which apparently still has not been ported to Windows.
This article is intended for an audience already experienced with PHP who use Apache/PHP/MySQL for project development, as well as others interested in Vagrant.
Development environment: MS Windows (7, 8.1, 10).
Vagrant version: 1.81.
WAMP version: 2.5 (MySQL 5.6.17; PHP 5.5.12)
What is WAMP?
WAMP is a software bundle, an acronym for Windows/Apache/MySQL/PHP. Historically, PHP scripts in production environments are hosted on Linux, with Apache serving as the web server and MySQL storing the data. On Linux this software bundle would be called LAMP, on Mac - MAMP, and on Windows - WAMP.

What is Vagrant?
Vagrant is a helper tool that automates virtual machine setup and file synchronisation to the virtual machine. Vagrant itself is neither Linux nor virtualisation software. Therefore, an essential component of Vagrant is VirtualBox (or VMware).

From the illustrations it can be seen that two things remain unchanged: the PHP file on your computer, and the way the browser retrieves and displays the result. Since in the Vagrant case there is no longer a service on the computer to act as the web server, all requests are redirected to the virtual machine. Database connection requests are also redirected. By "redirect requests" we mean port forwarding. Respectively, Apache from host:80 to guest:80, MySQL from host:3306 to guest:3306.
The goal is to switch from WAMP to Vagrant while preserving the option to switch back (with all requests then going through WAMP).
1. Install VirtualBox.
Download here - [1] and install. This is the virtualisation program that will later allow creating virtual machines (computers).
2. Set up Vagrant.
Download here - [1]. In my case data is stored on the D: drive, including web projects: D:wampwww. During installation, specify that Vagrant should also install to D:HashiCorp.
Important!
The idea behind Vagrant: open the project folder and start Vagrant. For each (!) project a separate (!) virtual machine is created. Accordingly, anything you configure for one project will not be available to another. If you don't have many PHP projects, it may be worth running Vagrant in the main www folder.
In my case I use a single project called "tests".
3. Configure Vagrant.
Open the project folder and create the virtual machine via the command line (cmd). Vagrant provides pre-installed virtual machines [4], so nothing additional needs to be installed. Equally surprisingly, pre-configured Windows machines are also available (trial?). I will use Debian/Jessie64.
vagrant init debian/jessie64
A Vagrantfile is created in the project folder, which stores the project configuration. Specify which ports to forward and which folders to synchronise.
config.vm.network "forwarded_port", guest: 80, host: 80 config.vm.network "forwarded_port", guest: 3306, host: 3306
By default, Vagrant synchronises the project (.) folder to /vagrant. That is not suitable for us, so we disable it and specify synchronisation to /var/www/html/tests
config.vm.synced_folder ".", "/vagrant", disabled: true config.vm.synced_folder ".", "/var/www/html/tests", type: "rsync", owner: "www-data", group: "www-data", create: true
vagrant up
On first launch, Vagrant will download Debian/Jessie and configure the virtual machine, and...
"rsync" could not be found your PATH. (see image 1)
4. Install Cygwin.
Download here - [3] and install. Cygwin is a program that allows installing Linux programs which run on Windows. Required: rsync and ssh (see images 5 and 6). Check whether PATH was set up. Advanced system settings > Advanced > Environment Variables > Path should contain D:cygwinin (or C: drive if installed there; it doesn't matter where you install it).
vagrant reload
Rsyncing folder:
0 [main] rsync 4732 D:cygwin64in
sync.EXE: *** fatal error in forked process – fork: can't reserve memory for parent stack 0x600000 – 0x800000. (see image 2)
It should be said that the 64-bit Cygwin rsync command on 64-bit MS Windows 10 simply could not be made to work, even though it ran without command-line parameters. Delete the 64-bit version and install the 32-bit one.
vagrant reload
and... nothing. (see image 3)
There was an error when attempting to rsync a synced folder,
Please inspect the error message below for more info.
Important! This is a Vagrant bug. The fix was found by modifying one of Vagrant's files - d:HashiCorpVagrantembeddedgemsgemsvagrant-1.8.1pluginssynced_foldershelper.rb
Find:
rsh = [
"ssh -p #{ssh_info[:port]} " +
proxy_command +
"-o ControlMaster=auto " +
"-o ControlPath=#{controlpath} " +
"-o ControlPersist=10m " +
"-o StrictHostKeyChecking=no " +
"-o IdentitiesOnly=true " +
"-o UserKnownHostsFile=/dev/null",
ssh_info[:private_key_path].map { |p| "-i '#{p}'" },
].flatten.join(" ")
Change to:
rsh = [
"ssh -p #{ssh_info[:port]} " +
proxy_command +
"-o StrictHostKeyChecking=no " +
"-o IdentitiesOnly=true " +
"-o UserKnownHostsFile=/dev/null",
ssh_info[:private_key_path].map { |p| "-i '#{p}'" },
].flatten.join(" ")
vagrant reload
and voilà! (see image 4)
Except that the virtual Linux (Debian) machine has neither PHP, Apache, nor MySQL installed.
5. Install Apache/MySQL/PHP
You can enter the virtual machine via SSH. Since the Cygwin installation makes the ssh command available, we can use that.
ssh [email protected] -p 2222
password: vagrant.
apt-get install apache2 php5 php5-mysqlnd mysql-server mysql-client
Once the software packages are installed, MySQL server configuration adjustments remain.
5.1. To allow connections from addresses other than localhost:
sudo nano /etc/mysql/my.cnf
# bind-address = 127.0.0.1
Ctrl+X and Y
5.2. Also set the root user to allow connections from any IP address:
use mysql; update user set host='%' where user="root" and host="localhost"; flush privileges; exit; sudo service mysql restart
No additional configuration is required for Apache and PHP.
Entering http://localhost/tests in the browser will display the "tests" project files.
Closing Notes
Vagrant does not watch for changes made to project files. Synchronisation occurs on startup or when running the vagrant rsync command.
Important! vagrant rsync-auto is a command that will continuously (!) check for changes in project files and synchronise them.
Conclusions:
Essentially Vagrant does about the same as setting up a virtual or real Linux server and copying files from the local computer to the server. Vagrant, as promised, is merely a helper tool that simplifies these actions.
While the fact that in the virtual Linux computer you can use all tools and services available in the Linux environment - and moreover develop new ones - it should not be forgotten that the production environment, i.e. where the project will reside once developed, may differ from your test environment. Before using any additional tools, it is worth checking whether they will also be available in the production environment.
Sources:
[1] https://www.virtualbox.org/
[2] https://www.vagrantup.com/downloads.html
[3] https://www.cygwin.com/
[4] https://atlas.hashicorp.com/boxes/search
comments