Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.
Here on Wptuts+ we've covered a number of different workflows that people use when developing for WordPress. WAMP on Windows, MAMP on Mac, or XAMPP for either. Today we're going to explain some of the problems with these methods, and show you a new approach.
Most live websites run on a remote web server you have access to through a hosting plan. But you don't want to be developing on a remote machine. It's much easier, and safer, to develop on a local machine you have direct access to, and then deploy your code to the remote server.
Most remote web servers run an operating system that's specifically for servers. This might be a variant of linux, such as CentOS, it might be Microsoft Windows Server, or even Apple's OS X Server. They'll also be running web server software, such as Apache, Nginx, or IIS, and database server software such as MySQL, PostgreSQL, or Microsoft SQL. Then there's various other things they may or may not have available, such as PHP, Python or Ruby interpreters, and a plethora of other things.
The vast majority of these things aren't the kind of software you'll find on your average laptop or desktop computer. The aforementioned WAMP, MAMP, or XAMPP help to get the Apache, MySQL, and PHP required for WordPress development installed there, but then there's the various other bits and pieces not necessarily available, not to mention the operating system itself (running a server operating system isn't practical).
But ideally, you want to be developing in an environment that's as close to that of the web server you need to deploy on. It just makes sense. You know what is / isn't available, and you know everything should "just work".
So how do you get your development environment (local machine) the same as your production environment (remote machine)?
A Virtual Solution
Now you could always go out and buy another computer which you could plug into your network, install Linux, Apache, MySQL, and PHP on, and use that as your development environment. But then, that costs money, and what happens if you need to do some development from a different location? The plan has unravelled.
Fortunately, there are some geniuses out there who have created software that can emulate a computer. You can then install an operating system and software on this "virtual machine", and even connect it to your network.
So you can have a virtual machine that's running all the exact same software as the remote machine you'll eventually deploy to. While it's running it will use resources from your laptop or desktop machine, such as CPU power, RAM, and hard drive space, etc. But web sites typically use a lot less resources than modern laptops and desktops have available, so this is no problem.
Now what happens if you have multiple remote machines that you deploy to for various clients or projects, and they have different setups? Luckily, you can have essentially as many different virtual machines as you like! One for each project, if necessary.
Of course, having many virtual machines on your laptop or desktop is going to start to take its toll. You'll have to go through and install the required software on each one as you set it up, and they'll all take up hard drive space, so after a while you might find it gets a little unwieldy. You could always delete virtual machines once you're finished with a project, but then if the project picks back up, or the client comes back for more work, you have to go through the process of setting it all up again.
Perhaps there's a way to manage this better?
Enter Vagrant. Vagrant is some software that allows you to, in their words, "Create and configure lightweight, reproducible, and portable development environments."
Sounds good, right?
What Vagrant does is manage the virtual machines for you. It comes with support for VirtualBox built-in, and you can also add support for VMWare.
All you need to do is create a Vagrant configuration file in your project, and that will tell Vagrant what base virtual machine to use (what operating system, etc.), what software to install, and then Vagrant will create it for you! If you finish the project, you can then delete the machine, but if you need it back, Vagrant will re-create it for you with all the same software.
Let's Get WordPress Specific
So hopefully I've at least partially sold you on the idea of using a virtual machine for your development environment, and using Vagrant to help make that more streamlined. But there's not been any mention of WordPress development specifically yet…
Good point! But I'm going to save that for part two. We've covered the rationale behind "why", and next time I'll take you through actually getting all this setup, and I'm sure you'll be sold completely!