MWD03 – Provisioning a local development stack

In the previous post, we set up the Mac workstation and got it ready for modern web development.
In this chapter, we’ll discuss the next key step in setting ourselves up the right way to develop a web application, and this is about creating and provisioning a development environment.
Using Linux is not a crime!
VMs are fantastic
If you are planning to create your app using PHP, Java, Python and or Ruby, then there are 90% chances you will do that on a Unix/Linux powered stack. Otherwise, you would go for Windows, and anyway things would not be very different.
Before we throw money through the window renting a server on the cloud, let’s be practical and consider the most obvious option, which is to leverage your own local workstation to setup a virtual environment. Note again that I advise against using platform ports of xAMP (Apache-MySQL-PHP) and there are a few good reasons for that, along the lines of consistency:
  • Operating system discrepancies (starting with file systems)
  • Software versions
  • Files and folder permissions
  • Stability
This said, the best thing to do is to provision a virtual machine which replicates as closely as possible the target production environment. For this end, we use a virtualisation platform like Virtual Box, as proposed in the previous article, and can install with it any preferred OS stack. Let’s assume a CentOS 6.5 64bits for the example, but it could be anything else, including a custom and home brewed VM.
Fortunately for us, instead of downloading the ISO at cents.org, and going through the full install process, ready made boxes are available on the web, and I can mention the following repositories:
My Vagrant is rich
My Vagrant is rich!
Vagrant is an amazing, accessible and free utility, and I hardly see how the modern web developer could ignore it. It allows them to create and configure lightweight, reproducible, and portable development and staging environments, with the exact combination of services and utilities I need for my project. And obviously I will consistently use and refer to Vagrant hereafter, as I am now using it both in my hobbyist and professional lives.
The basics for Vagrant are very well explained on the official site, here: http://docs.vagrantup.com/v2/getting-started/index.html
To install Vagrant, just visit the page and download the right package for your OS: http://www.vagrantup.com/downloads.html
Done, we are ready to provision our Linux stack for development (and possibly staging) purposes:
As I am a RedHat/Fedora/CentOS enthusiast, I go for a CentoS 5.5 64 bits stack, which I pick from the shelves of Vagrant Cloud (but could have been anywhere else): https://vagrantcloud.com/mixpix3ls/centos65_64
This one has been setup with the Virtual Box Guest additions (an I have a specific short article to help you out with upgrading your VB guest additions in case you update Virtual Box).
Let’s first create a working folder:
     $ mkdir ~/sandbox/my_project
     $ cd ~/sandbox/my_project
Now I initialise my local Linux stack:
     $ vagrant init mixpix3ls/centos65_64
=> This immediately create a local .Vagrantfile in your project folder, which you can freely edit and tweak to suit you need, as we will see later.
One thing you might like to immediately do though is to organise proper web port forwarding by inserting the following line in this .Vagrantfile:
 
config.vm.network “forwarded_port”, guest: 80, host: 8080
As you understand, this .Vagrantfile should later on be part of your GIT repository, as a way to share with your fellow developers what sort of server environment your app is supposed to run on.
For now, let’s just switch the Linux machine ON:
     $ vagrant up
 
It will take some time to download the gig of data corresponding to the image, just look at progress in your terminal window. But eventually your VM is up and running, and you can seamlessly SSH into it using the simple command:
     $ vagrant ssh
 
3 commands in total: Isn’t that amazingly straightforward?
In case you wonder where all this magic happens, beyond the 5kb .Vagrantfile stored in the project folder:
 
This is great but still a little bit bare bones for a web server, and we now have to continue provisioning the image at least with a web server and a database server.
Do you manual?
Do you manual?
The old dog and control freak in me can’t help, at least once, to do it the hard manual way, and that’s what it looks like:
$ sudo yum update # Careful because a Kernel update may ruin the Virtualbox Guest Tools
$ sudo yum install ntp
$ sudo service ntpd start
$ sudo rm /etc/localtime
$ sudo ln -s /usr/share/zoneinfo/Australia/Sydney /etc/localtime #properly set server time to this part of the world we love
$ sudo yum install nano # I need my fancy text editor
$ sudo yum install httpd # This is Apache, and you could choose Nginx or Tomcat
$ sudo service httpd start
$ sudo chkconfig httpd on
$ sudo nano /etc/httpd/conf/httpd.conf # Change Admin email
$ cd /etc/httpd/conf.d
$ sudo mkdir vhosts
$ sudo yum install php php-mysql
$ sudo yum install php-* # all extensions, because I can’t exactly tell which ones I need for now
$ sudo nano /etc/php.ini # Change Memory parameters
$ cd /var/www/html
$ sudo nano /var/www/html/phpinfo.php
$ sudo tar -jxf phpMy*
$ sudo rm phpMyAdmin-4.1.13-all-languages.tar.bz2
$ sudo mv phpMyAdmin-4.1.13-all-languages/ phpMyAdmin
$ cd phpMyAdmin
$ sudo find . -type f -exec chmod 644 {} \;
$ sudo find . -type d -exec chmod 755 {} \;
$ sudo mv config.sample.inc.php config.inc.php
$ sudo nano config.inc.php # Change blowfish secret
$ sudo rm -R setup
$ yum install php-mcrypt # these 2 lines are necessary to install MCrypt, and be able to deliver some reasonably serious work around cryptography
$ sudo yum install mod_ssl
$ sudo service httpd restart
$ sudo yum install mysql mysql-server
$ sudo service mysqld start
$ sudo mysqladmin -u root password ******
$ sudo chkconfig mysqld on
$ sudo yum install git
$ sudo nano /etc/conf/con.d/my_website.conf
$ sudo service httpd reload
Now just hit
=> http://127.0.0.1:8080/phpMyAdmin/ to access MySQL via PHPMyAdmin
=> http://127.0.0.1:8080 in your local browser, and you should see the magic happening, with the default Apache page.
It does not seem much, yet you might quickly spend 30min+ going through the above, if everything goes right and you do not inadvertently commit any typo.
Plus what you’ve just done, your fellow developers in the team would have to do it as well, as they VagrantUp their own local development environment: This is of course not acceptable.
One obvious and immediate countermeasure is to share the custom BOX we’ve just created with our peers via network or cloud storage. This is well covered by Vagrant here: http://docs.vagrantup.com/v2/boxes.html . Your peers would simply have to use the box add command:
$ vagrant box add my-box /path/to/the/new.box
$ vagrant init my-box
$ vagrant up
However, this is still a bit brute force and not fully flexible and future proof: What if I realise I need to add a specific service or configuration? I would have to update my box and to copy it again over the network for my peer developers, and moving around 1Gb of data is not the most elegant thing to do, is it?
Therefore we are looking for a more scripted and flexible way to provision our Linux stack on the fly.
In my next article, I will discuss a couple of simple enough yet professional solutions to provision your development environment in a robust and agile manner using either Chef or Puppet.
Next to MWD04 – Provisioning the stack with Chef

MWD02 – Get the MAC workstation ready

Continuing this series about Modern Web Development!
As a non Microsoft Developer working on a Mac, you need to get yourself a few productivity tools which unfortunately are not all available out of the box on your Macbook or iMac.
STEP 1 – Starting with a few Mac OS X enhancements
Total Finder Features
The Finder: Even with the latest OS X 9 Mavericks update, the Finder still has some shortcomings, and in particular still lacks the ability to quickly switch display of hidden system files. My recommended solution for that is to switch to TotalFinder  that is US$15 well invested for your productivity. And it’s more lightweight and stable than the very ambitious Pathfinder (US$39)
Total Terminal
The Terminal: The native Mac terminal is OK, but not always easy to summon in context. To improve usability in that space, I have taken 2 actions: First, I have downloaded the excellent and free TotalTerminal utility from the same BinaryAge company publishing Total Finder. With its Visor feature, you can always summon the Terminal in context using CTRL+~, very handy. And secondly, I am leveraging the new service available in Mavericks which allows to open a “new Terminal tab at Folder”, as per instructions found here. Right-click any folder, select Services>Open Terminal, and here you go. A true game changer.
Textmate 2
Textmate 2A decent Text Editor: Textedit is definitely outdated, and I wonder when will Apple decide to give it a bit of attention span! In the meantime, the modern developer will invest in a Sublime Text or Textmate 2 license. My preference goes to the latter which has a much more comprehensive syntax library.
Other CLI developer tools:
Mac OS X native tools: First we need Mac native tools, which are not installed by default but usually come with Xcode. If you are not planning to develop Mac or iOS apps and do not need Xcode just yet, these tools can simply be installed by using the following command in Terminal: $ xcode-select —install or even simply $ gcc
This will trigger a dependency pop-up indicating that Xcode requires the CLI developer tools and offering to install them: Just click install and wait for 30min.
CLI Developers Tools
Java: Java is an important language, and unfortunately it is not available natively anymore in the latest releases of Mac OS X.
The current Java version recommended by Apple is 1.6 and it can be downloaded here.
If for some reason you need to work on Java 1.7, visit the official Java website, at your own risk:
Ruby and Python: Ruby and Python, two immensely popular object-oriented scripting languages, have been installed as part of OS X for many years now. But their relevance to software development, and especially application development, has assumed an even greater importance since OS X v10.5. And as a modern developer, in particular in the PHP space, you are likely to use Ruby scripts for automation and deploy on, with Composer, Capistrano and Chef for instance.
Ruby with HomebrewHomebrew  s a package manager for Mac and it helps installing additional services and utilities, that Apple didn’t, in an organised manner, managing symlinks into the /usr/local folder. Just like CHEF consumes recipes written in Ruby forserver provisioning, Homebrew consumes Ruby-written formulas to install software packages on your Mac.
To install Homebrew (Note a dependency on XQuartz)
$ brew doctor
To install Ruby with Homebrew:
$ brew install ruby
To manage the different versions of Ruby you may have on your system, use RVM:
$ \curl -sSL https://get.rvm.io | bash -s — –autolibs=read-fail
PHP Storm
STEP 2: Get a good IDE
PHP StormThere are several options on Mac and I can mention here the well know Eclipse (All languages), Coda 2(PHP), Netbeans (Java, PHP, C++) or Aptana Studio (PHP, …)
But my preference now clearly goes for the great and consistent suite of IDEs offered by JetBrains for the different languages:
  • IntelliJ for Java
  • PHPStorm for PHP => That’s the one I am mostly using (US$99 for a personal license)
  • Web Storm for general purpose HTML/CSS development
  • PyCharm for Python
  • Rubymine for Ruby
  • Appcode for iOS development
Atlassian Sourcetree
STEP 3: Source control with GIT
GIT is essential and mandatory, and what you need is the holy trinity: The command line on your workstation, a repository storage service, and a GUI client.
Mac OS X 10.9 comes with GIT 1.8.5 bundled, which is not the most recent version.
To upgrade GIT on Mac, the official GIT site proposes an installer.
Another option is to use Homebrew, with:
$ brew update
$ brew install git
This will get you the latest GIT, version 1.9.2 as I write these lines.
.
Source TreeIn terms of GUI, I enthusiastically recommend Atlassian Sourcetree, which by the way will come with its own version of GIT bundled in, for self-consistency purposes. (GIT 1.8.4 for Sourcetree 1.8.1). The desktop client for Mac proposed by Github is also a decent one.
.
Lastly, make sure you have an account on Github, offering public repositories only for the free account. For this reason, I urge everyone to consider Atlassian Bitbucket, which offers free accounts with illimited private repositories for teams of up to 5 members: It can’t get any better!
.
TransmitSTEP 4: Other essential network utilities
.
For FTP/SFTP and S3 access, I have found nothing better than Transmit 4 from Panic Software (US$34).
If you are after a completely free software though, you’d would fall back to Cyberduck 4. And finally, Filezilla has now enjoyed a decent port for Mac, but unfortunately lacks S3 support.
.
NavicatFor database connectivity, the “ned plus ultra” solution is Navicat  which offers connectivity to MySQL, PostgreSQL, Oracle, MS SQL Server, SQL Lite, MariaDB. Choose the Essential edition for US$16 only, or go pro with Navicat Premium ($239).
If you use exclusively MySQL, then Sequel Pro is the perfect and free solution. Likewise for PostgreSQL, simply use the Postgres.App.
.
.
Charles ProxyAlso, you need a web debugging proxy and the obvious choice here is Charles 3.9 (US$50).
Interesting to note though that Telerik is currently porting the remarkable Fiddler to Linux and Mac (Apha release).
.
STEP 5: Virtualisation software
First and foremost, I would strongly advise against setting up localk xAMP suites, such as MAMP and XAMP for Mac. It may look handy at first glance to run your Apache and MySQL servers locally, but it quickly becomes a pain in the back with compatibility issues, when you start using PHP and Apache extensions. Very messy and inaccurate.
.
Virtual BoxAccording to the idea that there is “nothing like the real stuff”, I urge any serious developer to work with locally virtualised environments replicating LIVE destination environments, whatever the operating system.
The best choice is to use Oracle Virtual Box, free and well supported.
Now you can manually install any of your favourite OS, and set your code folders as shared mount points.
.
VagrantBut the latest delightful trick to do so is to use Vagrant to flick up an environment in 1 command line and no time:
Download Vagrant for your system, and then in the terminal simply VargrantUP! You can select an image on Vagrant Cloud: https://vagrantcloud.com/discover/featured
$ cd /~/my_dev_folder // Putting yourself in the folder you intend to store your web app
$ vagrant init hashicorp/precise64  // Installing a standard Ubuntu 12.04 LTS 64bits box
$ vagrant up
$ vagrant ssh // You are logged in!
In just a few minutes, you are logged into your VM and can start setting up the services you need fort your app, starting with Apache, MySQL, PHP and more.