WordPress Development Environments
Back in April, I had the pleasure of giving a talk about WordPress development environments at WordCamp San Diego (here are the slides from my talk). The thesis of my talk was that whether you are new to WordPress, or have been developing with WordPress for a long time, thinking about your environment and what roadblocks may exist in your workflow can help you speed up your development process and make deploying your code a whole lot easier.
In this article, we will examine what a development environment is, what tools exist for creating and configuring a development environment, and what tools exist to help you deploy your code.
Roadblocks
My early days developing with WordPress were clunky and awkward. I used MAMP, FTP, phpMyAdmin and other time-sucking tools, didn’t have a proper asset pipeline, and often cowboy coded directly on a staging or production server (yikes). I also encountered issues where old code I wrote wouldn’t run properly on my local environment, or code would work on my local environment, but not on the production server. This led to lots of headaches and lots of hours spent troubleshooting.
To remedy this, I had to take a step back and put time into redefining my development environment and workflow.
What is a development environment?
For all intents and purposes, let’s define a development environment as a collection of procedures and tools for writing, testing, debugging, and deploying code. With that as my definition, I decided to list out everything I needed to develop with WordPress.
- Linux
- Apache/Nginx
- MySQL/MariaDB
- PHP
- WordPress Core
- A solid text editor or IDE (PhpStorm/Sublime Text/Atom)
- A database client (Sequel Pro/Navicat)
- Version Control (Git/SVN)
- Task Runners (Gulp/Grunt)
- Dependency Managers (Composer/npm/Bower)
- Command-line tools (WP-CLI)
Along with these tools, I needed to ditch FTP (too slow and bad for version control), create a continuous integration workflow (more on that later), and have proper development, staging, and production environments to put an end to cowboy coding once and for all. Most importantly, I needed parity between all of these environments so that my code would be guaranteed to run anywhere every time. Take that hours of troubleshooting!
Considerations
After understanding the tools you need for a particular project, you can begin selecting an environment that works for you. When selecting a development environment there are a few key things to consider.
- Will you be working on a team? If so, what is your team using?
- What are you working on (theme, plugin, core)?
- Where and how will you deploy your code?
Keep in mind that everyone’s needs may differ and that you should do your best to find the right tools for the task at hand.
“If you do not know where you come from, then you don’t know where you are, and if you don’t know where you are, then you don’t know where you’re going. And if you don’t know where you’re going, you’re probably going wrong.”
- Terry Pratchett
There are a plethora of development environment tools out there, so in this article I will just be focusing on three of my favorite solutions for WordPress.
VVV
Varying Vagrant Vagrants (VVV) is an open source Vagrant configuration focused on WordPress development. It is the de facto standard for most WordPress developers, especially those contributing to core. Right out of the box you get a fully-functioning WordPress stack with tools like WP-CLI, PHPUnit, Node.js, Composer, and a lot more, and because it uses Vagrant, its configuration can travel to any server environment seamlessly and easily which makes deployment a breeze. If you are making a theme or plugin for the repository, or contributing to WordPress core, I would definitely check out VVV on GitHub.
Trellis
Trellis is a Vagrant configuration developed by the Roots team for use with Bedrock and Sage. If you aren’t familiar with the Roots team, they are heavily focused on modernizing WordPress development. Their Sage starter theme utilizes tools like Gulp for compiling assets and running tasks and Browsersync for auto refreshing and injecting styles which will help speed up your theme development workflow dramatically. Because Trellis is focused on modern WordPress development, it comes with tools like PHP 7 and MariaDB, utilizes FastCGI caching and Memcached, and has a focus on security by including tools like Fail2ban and ferm. It also uses Ansible for configuration management to help achieve server parity. Check out the Roots website to get started with Trellis.
Docker
While solutions that use Vagrant are fantastic options for spinning up development environments, they are driven by the use of virtual machines (VMs). VMs can be very CPU intensive and take up a lot of space on your local machine or server. In my experience, they can also take quite a long time to configure and deploy depending on the situation.
Docker is a container-based solution that let’s you run your application on any server environment where the Docker Engine is running. It does this by wrapping up your entire server configuration into an image and then running that image inside of a container. If you haven’t heard about Docker, you should head over to the Docker website to check it out as I believe it is the future of WordPress development and DevOps in general.
Docker has a steep learning curve, but once you understand how it works, it will become very fast and easy for you and your teammates to create, test, and deploy your code. Furthermore, that code will be guaranteed to work on any environment where the Docker Engine is running. Be sure to check out my article Docker + WordPress if you’re interested in learning more.
Continuous Integration
Once you have your development environment created, you will most likely want to implement some sort of continuous integration (CI) workflow. CI is a complex topic that deserves it’s own article, but in a nutshell it is the practice of pushing your code multiple times per day when features are ready as opposed to implementing some sort of weekly or monthly push schedule. Typically in a CI workflow, pushes to your Git repository trigger a build of your application on a build server that then gets tested and deployed automatically using scripts. There are some really great tools available for CI when it comes to WordPress. I’ve listed a few below.
Wercker has been my go-to as of late. They have a really great Docker-based solution that lets you configure multiple environments and trigger builds for each using environment variables. It is also very intuitive to set up as they use the concept of pipelines and workflows. Check the one of the wercker quickstart tutorials to get a taste of what it can do.
Going Further
When you start thinking about your WordPress builds more holistically, your workflow will improve and you are able to write more code that gets shipped faster with less headaches. Using tools like Vagrant and Docker will help you spin up multiple environments and makes working on teams and deploying your code a whole lot easier. Tools like Travis CI and wercker will help you establish a continuous integration workflow that will allow you to build, test, and deploy your code as soon as features are ready. All of these tools together will ultimately make you a better developer.
As always, I am here to help, and if you have a workflow or environment that you really love, feel free to share it!