The year 2014 is almost over, but looking around my developer colleagues, I see that there is still a lot of old knowledge and outdated techniques for using your Mac for (web-) development purposes. To counter this lack of information I will outline the steps I took to lift my two primary OS X systems into the new age. Gone are the days where we need to have a webserver, database system and a plethora of different packages installed on our main operating system and deal with breaking changes whenever a new OS update (Hello Yosemite!) comes along.
This post talks about setting up your own private local development server with all the needed software to start developing applications with PHP and MySQL without having to install any server software on your main operating system. The end product will also give you the means to quickly set up an identical virtual machine on any number of additional computers you might want to use for your development.
Disclaimer: As I am a heavy user of Laravel these days, this set-up will mainly center around web development and PHP.
If you think this is entirely to much to read, here are the distilled down steps to get you running in minutes:
- Install VirtualBox, Vagrant and Ansible (pip install ansible)
- Download my pre-configured package: devbox.zip
- Or use Phansible to configure and download your desired virtual machine set-up
- Extract the downloaded package, open the target directory in Terminal and type vagrant up
The detailed explanation of every step
As stated above, my main goal is to have a self contained environment that is as independent as possible from my main OS and the machine I am currently using and duplicates my primary production set-ups as closely as possible. To achieve this goal all webserver and database related stuff will run in a virtual machine and only the most needed packages will be installed on the host system. So let’s dive right in!
Follow these steps in chronological order:
- Download and install VirtualBox – Just click on the Link saying x86/amd next to VirtualBox 4.3.18 for OS X hosts. After installing the downloaded package this gives you an environment to run your virtual machines in and serves as the underbelly of the whole system. But don’t worry about the rather bloated UI and complicated settings, we will never have to actually open the VirtualBox application.
- Download and install Vagrant – This program will give you a convenient way to have pre-configured virtual machines and control them via a few simple terminal commands. Vagrant also takes care of the otherwise often confusing SSH key configuration and port forwarding you’d have to deal with if you had manually set up your virtual machine.
- Download and install Ansible – The easiest way to do this is to type pip install ansible in your Terminal, but if you want to download a tarball you can do so by clicking on the previous link. Ansible is an astonishing way to provision systems and manage their configuration. If you have heard about Puppet or Chef, this is similar but with the great difference that Ansible doesn’t need any agent programs on the clients nor any complicated configuration language. Everything just runs from simple YAML files via common SSH connections. I manage all of my hosting and application servers via Ansible and it couldn’t be easier.
- Configure and start your development machine – This step would incorporate writing a Vagrantfile, outlining the exact configuration for your machine and which packages (webserver, database, PHP packages etc.) to install on creation. But we don’t want to concern ourselves with such tasks and instead use @erikaheidi‘s wonderful webpage Phansible that allows us to download a pre-configured Vagrantfile with just a few clicks.
Configuring your Vagrant virtual machine with Phansible
Open the Phansible webpage and take a look at it. I realise that most of those settings rely on personal preferences, but I will list the steps I took which are sensible defaults from my point of view.
- Base Box – I will always choose the latest Ubuntu version here to be able to take advantage of the latest software packages. (Ubuntu Trusty Tahr (14.04) [LTS] 64 at the time of this writing)
- Name – Choose whatever you like. Mine is called “devbox”.
- IP Adress – Now this really depends on your specific DHCP setup but I always go with the IP 10.20.30.40, which turned out to not clash with the majority of default router settings in most places. Of course you can also just use the default of 192.168.33.99 which should be save as well, but is by far not as funny and easy to remember as mine.
- Memory – The default of 512MB should be fine, unless you plan to use resource intensive things like JAVA in the future.
- Shared Folder – All my source code lives in ~/Sites in the Homefolder for my OS X User, so I set this option to that path. This allows me to work on my code in my usual editor or IDE on my Mac and also have the devbox VM access this as its webserver document root.
- Advanced Options
- I tend to de-select the windows.sh script option since I don’t need it.
- Using Vagrant Cloud let’s you share your virtual machine to the internet, this can come in handy if you’d like to show off some of your work to colleagues.
- Shared folder type: From my experience the option that gives you the least grief concerning directory and file permissions is “VBox default”.
- Webserver – Here I stick to the default of NGINX + PHP5-FPM because that’s what I also like to use on my production systems. Document Root can be left at the default as well, only your Server name should be set to your desired one. For me it is “devbox.app”. We will use it later on the host system, so remember this.
- Version 5.6 is the most sensible setting. After all we are already only using the latest version everywhere, right
- Composer should be enabled. PHP development simply doesn’t make any sense without it anymore. If you don’t know what this is, you can think about it like package management for PHP. Like APT for Debian based Linux systems, Bower for frontend packages or npm for node.js. You can look up packages on Packagist and easily import them into your application. No need to re-invent the wheel every time.
- I don’t use Xdebug since I feel it’s a bit slow and when developing new applications your clear code structure and testing framework saves you from having to hunt down bugs.
- For PHP modules I chose the following: php5-cli, php5-curl, php5-imagick, php5-common, php5-fpm, php5-gd, php5-geoip, php5-mcrypt, php5-memcached, php5-mysql
- Database – If you fill this in, a default database and user will be provided for you. My defaults are the following:
- Database: db
- User: root
- Password: root
- System Packages – I only need vim and git here. Most of the work will be done on the OS X host system anyway. Usually you only need to concern yourself with the commandline on the virtual machine when making quick changes to the webserver for a new project.
- Timezone – I leave this at “UTC” since that’s what all production servers are running on as well. It is the universal standard after all.
Well this was quite a long run down on all of Phansible’s options. But this was really the hard part (and after all you can just download my configuration from the TL;DR section above), all that is left to do now is to download your package from Phansible and extract it in a directory of your choosing. On my machine all my Vagrant files live in their own subfolders in ~/Vagrant.
The final step is to open Terminal and navigate to the directory that hosts your Vagrantfile. Once there, type the following to boot and initialize your new development virtual machine.
After a short while the system will be up and running and ready to serve as your local development server.
This was originally planned to be part 1 of a series. Sadly part 2 never emerged and since then so much has changed – as it always does – that I don’t really feel writing a continuation of this post makes sense at the time.