Skip to content

Sun VirtualBox as a virtual development environment for PHP

Dear Reader,

The problem

For the past year I’ve been looking at the way I develop web based projects and how I can make it easier, better, faster, more stable, but most of all, portable. Like many PHP developers out there, I work on multiple projects at the same time. It’s not unusual for me to have three projects under development at any given time with an additional five projects that I’ve abandoned that I’m not yet willing to delete. I’ve developed a way of working with them that works for me. I’ve even written a Zend Framework based cli tool that sets up my work environment the way I like it.

All of that is wonderful if I’m sitting here in my lair connected to my local network. However, if I’m moving around, I have two choices, setup something like Zend Core or XAMPP on my laptop and duplicate my development server. This is the model I’ve used for the past few years but what I end up with is projects on my development server that I wanted on my laptop and visa-versa. The more I thought about it though, the more I realized there had to be a better way. My current setup doesn’t allow me to do things like test PHP 5.3 without setting up a whole new server just for that purpose, or maintain an environment like my production server – it has it’s own little quirks – so that I can work on legacy code.

Here is a short list of my criteria:

  • Must work on my laptop.
  • Must allow me to easily test different environments
  • Must let me work on multiple projects

The solution that doesn’t work

Yesterday I was browsing phpdeveloper.org like all good PHP developers should when I ran across a link to Kevin van Zonneveld’s blog post, How virtualization will improve your code. Reading it over, I begin to think he had hit upon the solution. He was using Sun’s new VirtulBox 2.0 to virtualize the development server. The only problem with Kevin’s solution is that he is running Linux as his primary OS (Host) and runs Linux as the virtualized (Guest) OS. Actually, it’s not a problem for him but it is for me since I use Windows as my desktop environment. I did feel though that he was moving in the right direction.

Installing VirtualBox is very easy and grabbing a copy of Ubuntu Desktop (Don’t use Server, it won’t work According to this thread The problem was a setting in VirtualBox and not Ubuntu itself. It may be safe to use Server if you like.) was actually the longest part of the process. Once downloaded, setting up the virtual server was actually anti-climatic. You just let the wizard get you going and then watch as Ubuntu installs.

One of the great things about VirtualBox is that it has a “shared folders” option. I simply define a directory on my Windows OS (Host), c:\web, as a shared folder and then mount that share inside the Linux OS. Once you you get it working the way you want, you jsut add the mount to your virtual server’s /etc/fstab and never think of it again.

The end result is I can still use Windows/Komodo as my development environment. My virtual server has Apache, PHP and MySQL installed so it can serve up the pages from the same directory. So far, all was going well. Here is where Kevin and I parted company though. Kevin was content to browse the work area using the browsers installed on the virtualized server. This is the easy way to do it because the browser and Apache are on the same virtual machine. I however, wanted to be able to browse the server from my Windows environment so I can test with IE, FF, Safari, etc.

Following the instructions in the Users Guide I setup a “Host Interface”, bridged it to my wi-fi card and setup my virtual server to use the new interface. (It should be noted that I know enough networking to be dangerous so the sentence above actually covered about 3 hours of running down blind alleys) Each time I tried a different combination, my new virtual server fired up just fine but failed to connect to the network.

After a lot of failed tries and a lot of googeling, I found the answer. (Which was, of course, in the manual to begin with) Most wi-fi cards are not capable of promiscuous mode. For bridging to work, Windows has to put the card in promiscuous mode.

Mine card, an Intel® PRO/Wireless 3945ABG, is capable of it but you have to have an admin tool to do it and while I can find the manual for the admin tool, I can’t find anyplace to download the actual tool itself.

Just to confirm that this was actually the problem, I snaked a cable into my lair from my server closet and bridged the host interface and my wired interface. Voila! it worked exactly how I wanted it to. But alas, since the solution is not portable, it won’t fit all my criteria.

Conclusion

Even though VirtualBox won’t meet my need as a development server, it’s still a cool toy to play with. It scratches my Linux itch while letting me keep my Windows desktop. Running it in seamless mode is a little more processor intensive but very cool. It puts the Linux toolbar at the top of the screen and Linux apps open as regular Windows windows.

I’m still in search of the perfect development environment. If I find it, I’ll let you know.

Until next time,
(l)(k)(bunny)
=C=

25 thoughts on “Sun VirtualBox as a virtual development environment for PHP

  1. I already tried to use VirtualBox to keep a good environment, but I got another problem…

    The filesystem seems to be very slow ! When I build a symfony project, each pages are loading for about 1 minute, because of the – bad – virtual filesystem.

    Did you get this issue ?

  2. “Yesterday I was browsing phpdeveloper.org like all good PHP developers should” that or one of the dozens other resources referencing the same articles and more. :)

    Anyway I agree with Martin about this, however as you pointed out you’re on Windows so maybe you thought having Linux somewhere where you can test your code because the development target is also Linux would be better?

    If you just want to make your code portable (both the PHP code and the browser interface) then virtual machines help because you can run your test suite on any OS and fix the few problems or launch browser XYZ and test the usability of your application. But there’s no need to have your development server (apache+php) directly on a virtual machine, just run the tests once in a while.

    A last point where virtual machines help is when you need to support extensions or tools that do not run on the OS you develop with. Example: MSSQL database when your dev OS is Linux.

  3. Whats wrong with having a XAMPP installation on your local machine. You’re able to set it up with support for multiple PHP version. You have to install your PHP stack anyway so it doesn’t matter where. Create a script to deploy a new project and create vhost setting. Virtual machines are IMO only the ‘cool’ factor and won’t help you in the long term (in this case). Oh and running a XAMPP install locally is way faster then having 3 virtual machines running. Eventually it will costs you less time then all this tampering with virtual machines.

  4. BTW: Please fix the comment form :) I had to enter my comment twice because of an error in the form. Doesn’t keep the values unfortunately.

  5. i’ve tried with virtualization (using parallels desktop on osx running ubuntu desktop inside) a year ago. i had big problems compiling php with all my required extensions (not in the standard distribution) on osx at that time, so i had no other choice. but i felt never very happy with it cause of various reasons, including speed. luckily i was able to solve my compilation issues and am back on osx with my development environment.

  6. I’ve solved the same problem with a internal network between my host and guest. And for ubuntu to access the internet – another NAT interface. Worked everything very well.

  7. @Adrien
    Honestly, I didn’t get that far in my testing. Thanks for the tip.

    @Martin
    There is nothing wrong with XAMPP, I’ve used it since it’s early days and love it. However, right now I need to be able to easily swap between PHP 5.2.6 and PHP 5.3. Also, I have legacy systems that use MySQL 4.0 and new systems using 5.0 I’d love to be able to have environments for all of them and just fire up the server I want. That’s not easily done with XAMPP.

    @Loïc Hoguin
    I actually have 2 Linux development machines and a production server. However, it’s not feasible to keep rebuilding my test machine every time I want to try out a new version of a piece of the LAMP stack. I could virtualize my Linux serve but that still doesn’t solve the issue of portability. Finally, I could just reformat my laptop and go Ubuntu all the way, but then I lose access to a lot of software that I really like.

    And yes, there were other sites linking to that article but my buddy Chris Cornutt’s phpdeveloper.org is where *I* turn to for news. :)

    @Tomas
    Do you have details or instructions on how you solved it?

    Thanks EVERYONE for your comments! :)

    =C=

  8. Personally I love the idea of having the virtual machines running. For the issue of portability of development environments I offer some ideas:

    Subversion. Obviously I need not lecture you all in the importance of Subversion, but you should remember there are Post-Commit hooks available that would allow you to push new versions of projects onto your development servers after every commit.

    Coupling subversion hooks with (public accessible/VPN’d) development servers would solve the problem of being able to develop from your laptop. All you would need to do is checkout your code and every time you committed a change your server would update, and on next sync your desktop would have the same code your laptop does, and voila you can now test and maintain consistent code between your development locations.

    Now up to this point you can work on your laptop, you can setup multiple projects easily with multiple SVN repos, and you can have different projects go to different environments. The tricky part comes in if you want to have one project being posted to multiple environments. For example testing the ZF in PHP 5.2.5 and 5.3. I am sure you can have subversion update multiple environments from one hook, but the question comes to play on how you setup DNS and routing to those environments. Do you setup “dev1.projectname.local” and a “dev2.projectname.local” domains or do you switch your local HOSTS file to access different projects each time? That is more a matter of personal preference.

    As for the ease of creating multiple environments I cannot speak to Virtual Box but I can say its literally a copy and paste when using VMware.

    Now the overall thing you have to think about is whether or not this whole system is worth it. You are very borderline to replicating continuous integration systems out there already.

    So take what you can out of this but remember don’t re-invent the wheel. Unless you can make it better, faster, or with better profit margins.

  9. @Brad Gushurst

    Yes, I left out source code control and unit testing on purpose, just to simplify things. Those would both be added in once I got everything else working. Honestly I see svn still running on my development server, not the VM server.

    I still think it’s worth it. I’ve not explored CI setups but I can’t see CI giving me different environments to work in without multiple copies of Apache, PHP and MySQL installed. That’s just a pain. a Virtual LAMP server makes all that pain go away.

    @Matt Farley
    Yes but I’d really like Host OS===Windows XP. That’s the killer. And I did more research today and my WiFi card is capable of promiscuous mode but the Windows drivers don’t allow you to turn it on. The Linux ones of course, do. (Windows developers, please stop treating users like children! Let *me* decide if I want to use the full power of my tools, quit making the decision for me.)

    Anyhow, at this point, my choices are:

    * Switch to Linux (defeats the purpose)
    * Buy a PCCard wifi card that will do promiscuous mode (Don’t like carrying extra gear)
    * Continue doing things the way I’m doing them now. (Most likely course of action)

    Thanks guys for the comments!
    =C=

  10. I’ve been using VirtualBox for the same purpose and love it:

    Host OS: Ubuntu
    VM1: Dev Environment — Eclipse/SVN/CodeSniffer/Pixy/PHP/MySQL/Apache
    VM2: Build Server — phpUnderControl/CruiseControl/PHPUnit/Selenium/etc

    Production = Dreamhost

    The great thing with having my dev environment as a vm is that when people want to join the project they can just take a copy of the vm, boot it up, and they instantly have Eclipse/MySQL/Apache all configured for our project.

    Also if I’m going on a trip, I can copy the vm from my desktop to my laptop (which is Windows) and boot it up and work on.

  11. @Pugi

    Actually, I stand corrected. I did hit an error with Server, the one described here. However, I did not see this post until just now so I will correct the article to reflect that. This for pushing me on it. :)

    Thanks for the comment!
    =C=

  12. Why do you say that ubuntu server doesn’t work ? Is it because of your windows environment ? Have you tried ? A week ago I installed ubuntuserver on my macbook using virtualbox. Works fine. I have http, ftp and ssh access from my macbook to the virtual ubuntuserver. For me one of the main reasons for running a virtual machine on my macbook or linux/windows desktop is to have a as close to production/development environment close at hand. So I install a server, less ram, cpu and space than the real thing, but OS and software (incl. versions) identical to the production/development environment. You can save yourself some trouble this way.

  13. Having both PHP5.2.x and PHP5.3 is a very good point, I might go with that solution too. I only had Win and Mac VMs until now, guess I’ll also have Linux soon. Thanks for clarifying your post.

    Btw: you could switch to Linux AND keep your apps thanks to Wine. It works well enough for me. Maybe it’d work for you.

  14. I do something very simular under WinXP, using VMware.

    I got a free 30 day trial of VM Workstation where I created a Linux and WinXP virtual machines. I set them both to use Bridged Ethernet which means they share their own private network between them.

    Then I replaced VM Workstation with VM Player which is free to use for ever.

    Job done.

  15. Cal,

    you might want to have a look at KVM, which is a free virtualization solution that comes with Ubuntu. I am in the process of switching from Windows to Linux, running virtual Windows machines for that stuff I currently can’t get to work on Linux (like online banking).

    The KVM management tools still have their quirks, but I had no problems setting up Ubuntu and Windows virtual machines on an Ubuntu server. Recently, RedHat announced that they’ll also switch from XEN to KVM, so I definitely recommend having a look at it.

  16. Hi Cal,

    Reading your blog post I was reminded to the challenges I had to overcome a couple years ago, where I had to set up several development environments that were matching the staging environments as closely as possible. At that point in time, VMWare was the best solution out there.

    Over the years I became a big fan of the VMware solutions, because now it was easy to build an environment and distribute it to fellow developers on an USB stick or over the network.

    Even when giving training courses, it was easy to provide the “students” a small image they could run with a free available player.

    Over the years other virtualization solutions came into play, but I stayed with VMWare because all my images were prepped for VMWare.

    The most important thing about virtualization solutions is how flexible you can implement it’s usage, and in my opinion VMWare has already proven this for a long time.

    Cheers,

    Michelangelo

  17. Hi Cal,

    I was attempting exactly the same setup as you and saw the same issues, after a bit of a trawl around the web though I ran into a VirtualBox command line interface called VBoxManage which is provided as part of VirtualBox.

    The following article provides a quick guide to how you can setup access to your guest Linux OS by redirecting web traffic directed at a certain port – works beautifully.
    Take a look at http://allisterx.blogspot.com/2008/05/web-server-on-virtualbox-gues-os.html

    Cheers,

    Alan.

  18. Thought I’d drop a quick note. I’ve been using VMWare Player for this all along. I did try Virtualbox but it isn’t yet as stable as VMWare is – at least the last time I looked and on a Ubuntu 7.10.
    You can actually make VMWare images with VMWare *Player*, you just need the good VMX file to start with. These are plain text files you can edit.
    Here’s a site that creates them for you: http://www.easyvmx.com/
    And for an step-by-step article of how to do it all you can go there: http://blogbuildingu.com/articles/using-vmware-player-to-create-images-for-new-virtual-machines (forget about the Empty VMWare Images & QEMU stuff and get what you need from EasyVMX).
    2 sites I use for downloading slim distribs:
    http://www.pouf.org (in French)
    http://http://www.thoughtpolice.co.uk/vmware/

    Hope it’s useful for some,
    Steven

  19. Pingback: To take away » Blog Archive » Un environnement de développement PHP portable?
  20. Hello,

    I did read a comment, that recommend to use subversion.
    The process described is develop, commit and then (code is auto exported to the dev server) test !

    You should, (have to), test BEFORE, commiting your code ! Never commit before test.

    The virtualisation solution have a big advantage : in big companies, developpers don’t manage production servers and don’t manage versions.
    With virtualisation give responsabilities to the servers managers to provide, the environnement as a virtual image.

    One more big advantage, the deployment of a new workspace for a new developper, is FAST, and under control !

  21. Another option:

    1) Use Vista (despite all the Mac commercials I use and it is reliable, stable, and better than XP) I have both 64-bit and 32-bit systems. 64-bit is better for me. Install as much RAM as you can afford.

    2) Install Vistual PC from Microsoft – It’s free

    3) Create as many virtual machines as you want. VPC supports shared folders also.

  22. @Cal, I didnt’ read any of the comments, just the post.

    I wanted to tell you since you’re host machine is windows, did you consider using Virtual PC? I just used it for the very first time last night to setup on my new Vista laptop and I was actually impressed with it. I use Virtual Server 2005 on my development machine for my VM’s and was looking to setup something similar on my laptop. I started with Virtualbox, but realized that you have to do so many hacks to get a vhd working on it or converted over to it. Than I gave Virtual PC a try, in 5 minutes it was installed and I fired up a vhd from my USB drive. Another thing that I want to recommend, and again, i didn’t read the comments. But I wanted to tell you that you should consider setting up an SVN repository on one of your machines that’s accessible through the web so that you will always have the same code from environment to environment. This will help you detach yourself from any 1 particular machine since the environments are the same and all you have to do is update the version you’re working on the machine you happen to be in front of. Sorry, one more recommendation. I would also create skeleton VM’s with just the operating system only. If you need a new machine, you simply copy the file and place it wherever you have your VM’s running from and just install PHP 5.3 or whatever new version you want. This way you always have a good starting point. These are things that I picked up on over the last couple of years messing around with virtualization. I think it’s the best thing ever and I can’t imagine working without it.

  23. @Hatem

    No, I’ve not tried Virtual PC. As with most MS products, it seems to think that everyoine lives in a MS world. Other virtualization products explicitly support OS’ like Linux, BSD, Solaris, etc. Virtual PC either doesn’t or MS doesn’t feel that the support is important enough to mention.

    Thanks for the comment,
    =C=

  24. Pingback: Daily Digest for 2009-01-22 | Pedro Trindade
  25. Whats wrong with having a XAMPP installation on your local machine. You’re able to set it up with support for multiple PHP version. You have to install your PHP stack anyway so it doesn’t matter where. Create a script to deploy a new project and create vhost setting.

Comments are closed.