Local development environments for PHP
4 June 2016
As a web developer you will most likely have to work on a temporary environment that you will setup for your project, so you will make sure everything is in place and that all pieces stick together before you deploy it for production. The environments I will discuss here are mostly focused on PHP, but some of them can be adapted to your needs.
Most of us have probably started by installing a local server on the main system, which is a good approach if you don't need a different version of PHP and you have no problem managing all your projects at the same place. Depending on your OS you might be using some of these: LAMP stack , XAMPP, MAMP
Those are the most common and there is nothing wrong using them as long as they fit to your needs. I used a software called EasyPHP on Windows XP and it does the same job as XAMPP. I moved on from those systems because it began to be difficult to maintain my active projects and also started to have different PHP versions and requirements across them. This can be really hard, after fixing a bug you can run into some deprecated functionality only because your local environment differs that much to the target environment. Also you risk to break environments of other projects on your machine.
You can avoid this by using a virtual machine, which will run completely isolated from your host OS. The idea is to setup the server as similar as possible to the server where your application is going to be deployed. By using VirtualBox you can go ahead using any Linux distribution or even Windows Server (some people use PHP on Windows Server, I know …). As long as you have a virtual machine which is preconfigured, you are ready to go and also extremely productive, but if you have to do it from scratch you will waste lots and lots of time, and you will constantly fall in the same pitfalls (setting mod_rewrite, enable vhosts, setup services correctly and so on). I made use of this development workflow quite a lot of time after I switched from Windows to Ubuntu in order to keep my system clean, and it worked as a charm. You may also go ahead and clone the virtual machine for new projects and update only the environment specific things. It comes with some massive costs in terms of space, as the virtual drives of the machine are increasing over time. I had a machine which almost took 100GB of space. This makes it extremely hard to share the environment with another team member working on the project, and he/she must also get used to what you have done on the machine.
About two years ago, I discovered Vagrant, but didn't cared that much about it, only after I used Laravel Homestead. Vagrant uses build scripts to create virtual machines, which makes it quite easy to replicate the system by running the build script. Homestead is build for Laravel, and it offers an easy way to get started developing with Laravel, without knowing much about the server side. By building the virtual machine (which requires VirtualBox) you get a fully functional Ubuntu machine with Nginx, PHP, MySQL and other Laravel dependencies. After using it for some time (it works great even for other PHP projects) I found tools to build Vagrant script for Puppet and for Ansible. You can add the build files into the repository, so every member of your team can use the same environment. You could as well use the Vagrant cloud to store preconfigured boxes and extend them with your project specific requirements. The only disadvantage you may have is the consumption of space (as it still requires VirtualBox) and the time it takes to rebuild your machine in case you need to destroy it. In conclusion, it is a great tool for developing Web Application.
Even if i used it for some time, I only considered recently to dive more into Docker. It looks like this has been the missing piece for optimal memory usage. You get a similar result as using Vagrant, but without having a virtual machine that reserves memory and CPU. It also opens new ways of building services by using components that work on their own. Instead of having a virtual machine running PHP and MySQL, you can have a Docker container running PHP (where your code lives) and another container where only your database runs. This opens new ways of developing web applications. It has the potential to make the life of developers much easier, and allowing them to focus on development.