I’ve always been somewhat disappointed with the available options for running WordPress locally. There are pros and cons to each method (MAMP, WAMP, XAMPP, native, self-compiled, Fink, Homebrew, etc) … but they’ve taken too long to setup, don’t allow for enough configuration options (or in most cases allow me to run multiple WordPress websites at the same time) and leave unused config files and data deep inside my computer when I decide to uninstall. Vagrant is a new way of approaching this setup and seems to solve all of the issues I’ve had in the past when trying to get a local environment up for WordPress.
Vagrant makes it easy to run a full virtual linux (or Unix) server on your local machine … kind of like you’d run windows on a mac using VMWare (only with much less RAM & CPU being drained on your local machine).
Vagrant isn’t always used to run WordPress locally (obviously, it has many…many other uses) but I’ve found that one of the main ways I use Vagrant now is for running a LAMP (Linux, Apache, MySQL & PHP) server locally … which I then run WordPress on.
There are several reasons why I think Vagrant is a great solution for running WordPress locally. Here are a few:
- Its relatively easy to get working
- You can create a LAMP server that will run the same whether its on Mac or Windows
- If you work in a team then you can make changes to your LAMP server specific to your team and each member can have identical local environments
- You’ll be running your site with Linux locally (most webhosts run on Linux) … so your local environment will be as close as to your production environment as possible
- Your website files will be located locally (for development / design purposes) and can be edited with your favorite editing software (TextMate, Notepad++, Sublime, Photoshop, Espresso, vi, emacs, etc) … when you’re done editing, and things look good locally you can ftp the files up to your web host.
- You can run several (possibly conflicting) environments side by side in different vagrant boxes on your local computer. I have a lamp box and a ruby box now … But I could configure boxes to run Java, ffmpeg or any other number of different environments that would be difficult to setup together in a clean way natively.
- Advanced users can ssh into these virtual machines, add new virtual hosts, install new software (via apt-get, yum, etc), run nginx instead of apache or pretty much anything else you want to do … you can even create your own network of vagrant servers if you want
- Vagrant boxes are easy to remove from your computer without leaving unused files cluttering your system
Setting Up Vagrant
Here’s how you can set up Vagrant on Mac (the Windows setup should be almost identical) for running WordPress locally. The box that Vagrant maintains (http://files.vagrantup.com/lucid32.box) is a basic Ubuntu Linux virtual machine … so, I took that box, added the LAMP stack on top of it and have made it available for you to easily get this working on your computer:
- Download & Install Oracle’s VirtualBox application from VirtualBox.org
- Download & Install Vagrant from VagrantUp.com
- Download WordPress from WordPress.org
- Unzip wordpress anywhere on your local machine (double click the latest.zip file you just downloaded)
- Open Terminal (its in Applications -> Utilities)
- Drag the wordpress folder you just unzipped from Finder into the Terminal icon in your dock (or just “cd” into your this directory if you’re comfortable with the command line)
- Now issue these three commands (don’t be alarmed if the first command takes a while — Vagrant is downloading a full virtual machine) …
- Go to http://localhost:8080 in your browser, you’ll magically see the setup for wordpress (if all went well). When setting up you can use the following database credentials:Database Name: vagrant
Database Host: localhost
Database User: root
I’ve also installed PhpMyAdmin with this box so if you need database access you can just go to http://localhost:8080/phpmyadmin and use the database username / password from above.
Getting to Know Vagrant
Here are some other useful commands that you can run in the terminal to interact with vagrant now that its installed:
To power up your vagrant box:
To access your box via ssh:
To suspend your vagrant box:
To shut down your vagrant box:
To destroy your vagrant box (this will remove it from your wordpress directory):
To remove the vagrant box from vagrant itself:
vagrant box remove caseproof-lamp
You may have noticed that when you ran
vagrant init ... that it created a file in your wordpress folder named ‘Vagrantfile’ … this is the vagrant config file for your current vm. It has all kinds of useful options … but perhaps the most useful one is for forwarding ports which you can use to change the port your local website will be available on (so instead of http://localhost:8080 you could access it on http://localhost:4567) which is going to be necessary if you use this same box for multiple different websites on your local machine.
Now, of course, this vagrant box is a full LAMP server so now if you wanted to run Drupal, Joomla, or CakePHP you could easily download into another directory and since you don’t need to add the caseproof-lamp box twice — just issue
vagrant init caseproof-lamp, edit your Vagrantfile to forward to a different port (remove the # before the forward port line and replace 8080 with whatever port you want), issue
vagrant up and you’re done.
One thing to note is that if you need to do some more advanced things with your Apache or PHP configuration … you’ll have to ssh into your box and configure it just like you would a Linux server. So, if you have this requirement and you’re uncomfortable with editing files on your vagrant box then it may be best to go with MAMP PRO or something that will provide a GUI for this. However, if all you want is to get a standard WordPress site up locally then this box should work well for you.
I’ve also got a ruby box that is configured with Ruby, Rubygems, Apache, Passenger, Git & MySQL (yeah, you can install Postgresql or Mongodb if you need to). I find that with ruby I still like to ssh into vagrant (mainly because of all the commands you have to run within the ruby environment) … the shared folder with your vm is located in /vagrant/ once you’ve ssh’d in. I’ve also set this box to run on port 3000 rather than 8080.
If you want to do some more advanced stuff or need more detail than I’ve offered here, there’s quite a bit of documentation over on the Vagrant Website.
I’m still playing around with Vagrant though. If you’re doing something cool with Vagrant I’d love to hear about it here.