Logo
  • PHP
    • HipHop / HHVM
    • Modern PHP
    • PHPStorm
    • LAMP
    • Laravel
    • Composer
    • PDO
  • JavaScript
    • node.js
    • AngularJS
  • CSS
    • SASS
    • “CSS4” (CSS level 4)
  • HTML
  • Git
  • LAMP
  • Vagrant
  • UI / UX
  • Architecture of …
  • Off-Topic
With ♥ from Berlin
August 31, 2013
Chris
LAMP, Linux, Local Development, PHP, Vagrant, Virtual Machine / VM
Comments Off on How to setup a (or multiple) server/LAMP-stack inside a virtual machine for local development with Vagrant and PuPHPet

How to setup a (or multiple) server/LAMP-stack inside a virtual machine for local development with Vagrant and PuPHPet

PreviousNext
vagrant

You know this: You need a new server, something to develop on, something to test on, something to put your project. That’s probably the most common thing every developer faces. Usually you would

A.) setup a development environment locally, which means you install PHP, MySQL and all the stuff you need on your local machine, by hand or in pre-configured bundles (like Ampps [Win, Mac], EasyPHP [Win], WampServer [Win], SecureWAMP [Win] or even Xampp [Win, Mac, Linux]).

B.) rent a real server somewhere, for development usually something smaller, preconfigured or naked.

C.) create a virtual machine on your local machine.

 

THE PROBLEM WITH LOCAL DEVELOPMENT ENVIRONMENT:

As you are working on your very personal machine, you’ll have to develop all your projects, all your clients’s sites, all your tests etc. in the same environment. Change one config value and it’s activated for all your projects. Beside that, you are mixing up private projects, client’s (sensitive?) data and maybe critical tests, that may block/freeze/kill your system. Especially in linux, a wrong bash command command can kill your working system (which is your server at the same time). For Win/Mac users it’s also diffcult as the most command line options and linux tools are not directly available there.

 

THE PROBLEM WITH RENTED DEVELOPMENT SERVERS:

An established workflow, but this has also some big disadvantages, especially when you work for several clients: Do you want to keep a development machine for all the clients you have (and ever had) running 24/7 – and pay for the server ? Plus, handling problems, eventual attacks and pay for overtraffic ? Pro-argument: The server is usually reachable for your client, making it easy to give live-access.

 

THE PROBLEM WITH LOCAL VIRTUAL MACHINES:

In theory an excellent solution, as you can run a perfectly fitting server on your local machine, you have full root access, you can create masses of VMs and handle them like real server, you can also zip them away an archive them (useful for non-active projects or older clients). You can also route the IPs and ports to make them public accessable! But, it’s a shitload of work to get this to work.

 

VAGRANT IS THE SOLUTION !

Vagrant & PuPHPet solve exactly that problem:

Vagrant is an installable tool for Windows, Mac OS X and Linux that creates ready-to-go virtual machines, out of a simple config file. You say which linux distribution you need, what version of PHP, MySQL, Apache, modules, extensions etc, you can even define the to-be-installed packages, set passwords and create database users. All you need is a config file. You install these machines with one line on the command line (works perfectly on Windows too), start them with one command, stop them with one command, delete them with one command. You can access the machines via SSH like every other server. You can zip these “boxes” if you need space, and you can give the config file to somebody else, who will be able to create EXACTLY the same box. Perfect for team development. Killer feature: The /var/www folder (or else) can be live-synced with a local folder on your host system, so you don’t even need to login and transfer files.
PuPHPet is, as the name implies, related to Puppet (which creates and handles servers), basically it’s “a web application that allows you to easily and quickly generate custom Vagrant and Puppet controlled virtual machines”. In other words: puphpet.com is an extremely easy-to-use website that will create the Vagrant config files for you: You simply select distro, version of PHP, etc. on the site and let generate a downloadable config file, which will then install a virtual machine on your local computer. Boom!

 

Requirements

  • VirtualBox (free for Win, Mac, Linux)! Vagrant will also run with other virtualization tools, but VirtualBox is the default use case, so let’s focus on that.
  • To connect to the server you’ll need a SSH tool, sure ;)
  • Windows users will need PuttyGen (to convert the ssh key) and a real ssh command line tool like Putty would also be useful. For visible FTP-like access to the server I recommend WinSCP.

 

Setting up the first machine

So, what will happen, basically ? We will create a config file (which is indeed a zipped folder, but more on that later) on https://puphpet.com by selecting what we want to have on our virtual machine, download that file, unzip it, open the terminal/bash/command line tool, navigate to that unzipped folder and trigger the creation of the virtual machine by just typing in one command (while being in that folder). Vagrant will create a virtual machine from the information inside that folder (some config parts will be saved by Vagrant in that folder, the virtual machine itself will be saved into VirtualBox’s virtual machine’s folder, more on that deeper in the tutorial). 3-5 minutes later you’ll have a fully installed server that is accessable via SSH and has its own IP. When you open that IP with a browser you’ll already see the well-known “It works!” page from Apache, and all the software is installed exactly as it should be.

 

Download and install Vagrant

Get Vagrant on http://www.vagrantup.com/ and install it on your machine.

 

Create a config file on Puphpet.com

puphpet.com is an extremely useful web interface for creating Vagrant config files. In this tutorial we will create a config file for LOCAL usage. But as the sites states, you could also get an account on DigitalOcean.com or Rackspace and use their servers.

Choose the OS, in this tutorial we will use Ubuntu Precise 12.04:

vagrant-puphpet-select-operating-system-ubuntu precise 12.04 ubuntu lucid 10.04 debian squeeze 6.0 debian wheezy 7.0 7.1

In the “Set Up VM Locally” box, DON’T CHANGE the Vagrant Box Name ! This name shows vagrant which operating system to download, so don’t change it manually !!! The possibility to change this form field is obviously an error in puphpet.com.

Change “Name Your Box” to something remarkable, in this tutorial we name it “box1” (to recognize the box better when using multiple VMs). The IP should stay like it is. Please note: the given standard IP ends with .101, which is perfect for our first box. When we set up more boxes, we will give them IPs like .102, .103 etc.!

Keep the rest like it is.

vagrant-puphpet-box-name-ip-adress-memory-host-port-box-port

In the “Sharing Your Folders” box, change the “Box Sync Folder Source” from

./

to

./www

which will sync the contents of the folder “www” in your local vagrant box folder with  the /var/www folder inside your virtual machine. We will create that folder later. It should look like this now:

vagrant-puphet-shared-folder-var-www

Leave the sections “Server Basics” and “HTTP Server” as they are – unless you know what you are doing.

In the “PHP” section, set your PHP version. Please note: PHP 5.3 is not available for Debian 7 and PHP 5.5 is not available for Debian 6. Selecting PHP 5.5 seems a good idea, and having composer on the machine is also a good idea.

vagrant-puphpet-choose-php-version-composer

Skip the rest of the PHP section (but have a look on it, these things might be interesting for you) and go to “Setup XHProf”. For this tutorial we uncheck the tick box, simply because we don’t need XHProf (and to be honest, I’m not really into the tool).

Select your timezone (might be important for database stuff).

vagrant-puphpet-timezone-xhprof

In the “Database” section, set a MySQL root password and UNCHECK the phpmyadmin installation. Currently, in August 2013, phpmyadmin doesn’t work when installed with a puphpet-generated Vagrant config file. We will install phpmyadmin later.

 

vagrant-puphpet-mysql-postgres-root-password-and-install-phpmyadmin

Finally, let’s finish this and generate the Vagrant config file with the big button the page’s bottom.

 

Create a local virtual machine from Vagrant config file

Create a folder that will hold your Vagrant boxes, i named my “vm” on my D:/ drive (the development drive). Unzip your just downloaded config file here. You’ll have a “precise64” folder inside “vm” now. Rename the “precise64” folder to “box1“. I don’t know why puphpet generates folders with the distribution name, not the box name, but we don’t care yet. Honey badger.

Create a folder “www” inside the “box1” folder. The contents of “www” will be synced with /var/www of the box in realtime.

Open the command line tool / terminal (Windows users: search for cmd, in case you never worked with the command line), go into the “box1” folder and type

vagrant up

That’s it. After a short time you’ll see something like this:

vagrant-up-on-the-windows-command-line-cmd

Two minutes for setting up a full stack Ubuntu server. Well done, Vagrant!

 

Logging into the VM

Linux/Mac users can now ssh directly into the VM via [Dear readers, please verify this!]

vagrant ssh

Windows users need to open PuttyGen, then “File” -> “Load private Key” and select the “insecure_private_key” file from C:\Users\XXXXX\.vagrant.d\

Note that you need to select “All files” in the file opening screen as the “insecure_private_key” has no file ending.

Click “save private key” and save the file in the same folder, name it “my_private_vagrant_key.ppk” or something like this.

After the key is created, log into the server with WinSCP (or via Putty, if you prefer that and know how to do it): Select protocol (I use SCP), enter the IP of your VM (192.168.56.101 or .102 etc.), port stays 22, user name is “vagrant”, password keeps blank, the private key file is the file you just created. Save that stuff (for further use) and log in:

You’ll see something like this:

logged-into-vagrant-vm

 

BOOOM! You’ve set up a real server in a virtual machine on your local pc. To prove that everything runs fine, check the installed version of PHP on the command line (Windows users: this time in putty, not the windows command line):

php -v

You should see something like “PHP 5.5.3 …“. By the way, as I’m writing this tutorial, PHP 5.5.3 has been released just a few days ago – and puphpet has provided us the very latest version of PHP already! This is really awesome, as getting up-to-date PHP versions is usually a big pain in the ass on linux. To check the Apache, simply type the VM’s IP into your browser. You’ll see the very well known Apache index page:

vagrant-puphpet-successfully-installed-checking-apache

We are done! If you need phpmyadmin, install it with:

sudo apt-get install phpmyadmin

 

Stopping and deleting a VM

To stop the VM, type

vagrant halt

on the windows command line (or your host’s command line) while being into the “box1” folder. To delete the VM (this means Vagrant will really delete the full server, but leaving the config files available) type

vagrant destroy

while being in the “box1” folder. Don’t simply delete the box’s folder without destroying the box first, or you will leave dead virtual machines on your system. In case you run into problems, delete the box folder, and the according virtual machine file in VirtualBox.

The “vagrant xxx” commands only do things to the boxes in which folder you currently are.

 

Where does Vagrant save the VMs ?

Vagrant keeps some configs in the box’s folder (“box1”), and saves the VMs into VirtualBox’s Virtual Machines folder. Keep an eye on that, as a fresh precise64 is more than 2 GBs!

 

How to setup another virtual machine

Do exactly the same like before, but this time name your box “box2” on puphpet.com, give it another IP (if your first box was 192.168.56.101, then this box will be 192.168.56.102), and move the extracted files into a folder named “box2”. Open the command line, go into the folder, type

vagrant up

and your machine is running. To connect, use the IP (… .102), vagrant as username and the already created ssh key file.

 

Creating multiple server within ONE virtual machine

Vagrant offers the possibility to create multiple servers from one virtual machine, at least if I understand them right. This is done with “forks” of the initial snapshots of the virtual machine. However, I think to set this up for WEEKS, but totally failed. I also have never seen people working like this, so setting up a real VM for every servers seems legit.

 

Troubleshooting

1. Don’t change the Set Up VM Locally field manually.

2. Every box (on your machine) needs a unique “Box Name” !

3. Every box (on your machine) needs a unique IP! Setting the IPs to .101, .102 etc. seems totally valid.

 

LAMPlinuxPHPpuphpetpuppetUbuntuvagrantvirtual machinevm
Share this

Microsoft enters post-password era with Hello (promo video)

Just a short promotion clip, but definitly interesting: Microsoft announces Hello, the authentication system inside Windows 10, using fingerprints, iris

php

A super-simple introduction into PHP namespaces (7min video)

Once again, Jesse of JREAM totally nails it right on the head. A super-simple introduction into PHP namespaces. If you

A quick history of Comic Sans, the most wrongly used font ever

Excellent 10min presentation every frontend developer should see. Yes, even if you never touched Photoshop and never chose a font,

Migrating Wikipedia to HHVM (@Scale Conference 2014)

Awesome topic, superfresh, directly from Scale Conference 2014. The title says everthing. Full quote from video’s description: As a top

Rare Steve Jobs AND Bill Gates video interview from 2007’s D5 conference (90min)

This is something great! Two of the most important people in the last 20 years’s mainstream consumer IT history in

O’Reilly sells EVERY ebook for -50% right now!

A good chance to grab some IT books: O’Reilly sells every single book in their portfolio with a -50% discount

Symfony devs: Creator of Symfony framework is hiring (Cologne, Germany)!

Probably one of the most interesting companies for a PHP developer is currently hiring Juniors and Seniors in Cologne, Germany.

hack-php

Facebook releases HipHop (HHVM) 3.0, adds mysqli and support for Hack language

Lots of movement in the PHP world these days! After releasing Hack (a dramatically improved fork of the entire PHP

composer

How to install Composer on Windows 7 / 8 or Ubuntu

Installation on Windows 7 / 8 Installing Composer on Windows 7 or 8 is extremely easy: Get the latest version

harper reed about big data

Harper Reed – The magic and mystery of Big Data (30min video from Webstock’15)

Harper Reed is speaking, so nothing can go wrong. :) The former CTO of Threadless.com and Obama for America definitly

1/4

Categories

Search

atomic-design
An introduction into Atomic Design, a super-clean way to style web applications
phpstorm 7.0 php
How to debug code on a remote server (or in vagrant box) with PHPStorm
[Link] Improving Smashing Magazine’s Performance: A Case Study
Frontend Ops Conf 2014 – Paul Irish: Delivering The Goods In Under 1000ms (40min video)
phpstorm 7.0 php
How to install GitHub’s, NetBeans’s and Sublime2’s syntax highlighting code colours theme in PHPStorm 6/7
Microsoft enters post-password era with Hello (promo video)
O’Reilly sells EVERY ebook for -50% right now!
the-php-login-project
How to install php-login-minimal on Ubuntu 12.04 LTS
mod-rewrite-ubuntu-14-04-lts
EOL lists of Ubuntu, Debian and CentOS for your server plannings
frontend-workflow
[german] Modernes Frontend-Development mit Bower, Grunt, Yeoman (45min Video, Thorsten Rinne auf der IPC2013)
php
PHP 5.6.0 RC1 is available
css3-chrome-font
Google rolls out Chrome 37, finally fixes horrible font-rendering
hiphop php
PHP’s HipHop outperforms PHP 5.5 with Zend OPCache and Nginx by 15-20 times
Support FLARUM, the future of PHP forum scripts (with some dollars on Kickstarter)
sass laravel
SASSmeister is a real-time JSfiddle for SASS / CSS. Awesome!

Tags

apache bash centos composer conference coupon CSS debian fonts framework git GitHub hack HHVM HipHop HTML HTML5 IDE JavaScript JS LAMP laravel linux mod_rewrite MVC MySQL Nginx optimization PHP PHP 5.5 PHP 5.6 phpmyadmin PHPStorm security server SSD Ubuntu UI UX vagrant video virtual machine voucher VPS wordpress
Side-Project: Wordle-Solver:
www.wordle-helper.info

Pages

  • Privacy Policy