5 Easy Ways to Get Started with PHP on Vagrant

Share this article

Vagrant is a tool for creating and managing virtual environments that help many developers not have to care about the “works on my machine…” problem. Vagrant creates reusable development systems that can be used again and again, helping you keep your system clean of too many installations.

When you are focused on development and lack the skills of a sys admin, the best thing to do (if you are going to use Vagrant during development) is to try another way of setting up your Vagrant configuration.

When it comes to PHP development, there are some online services that simplify the setup and configuration of these Vagrant virtual machines. Here is a list of some of these services.

1. PuPHPet

PuPHPet is probably the most “famous” of these services, and probably the most “specialized” one of them. It gives you the ability to host the VM on your local machine, on Digital Ocean, Rackspace or Amazon Web Services. You get to add forwarded ports with just a click of a button, and sharing source folders is as easy as that too. You can customize the firewall by setting rules and removing them as needed. As for web servers, you can choose between Apache and Nginx, choosing aliases and ports where to access the application.

When it comes to programming languages, PuPHPet is not all about PHP. Besides PHP, you can choose to install Python, Ruby, NodeJS and HHVM. You can’t install both PHP and HHVM, since the service doesn’t allow this – use Homestead Improved for running both at the same time. When installing HHVM, you can also choose to install the nightly release, which gives you the possibility of testing Facebook’s Hack language.

As for databases, PuPHPet has the biggest repository of those, with 6 different databases available. You can choose between MySQL/MariaDB (can’t have both of them), PostgreSQL, Redis, SQLite and MongoDB. You can chose to install Adminer for DB management, but don’t install it onto the production server.

Before finishing, PuPHPet allows you to install tools like RabitMQ, Elasticsearch, etc.

All that PuPHPet wants from you is that you use a Vagrant version not lower than 1.6. Also, you need VirtualBox in order to use the configurations on your local machine. You can find the source code of PuPHPet on this GitHub repo and a more in-depth guide here.

2. Protobox

Protobox is another online Vagrant service. There are many reasons to use Protobox. It actually started as a fork of PuPHPet, but turned out to be a complete other project, due to some issues related to Puppet (not PuPHPet). Protobox has grown into a great service that helps create Vagrant configurations for web application development.

When using Protobox, you can choose from a list of application types:

You can add more than one of these applications to the configuration. As for the web servers, both Apache and Nginx are available. You can choose only three languages: PHP, HipHop (with HHVM) and Ruby.

At the time of writing this, Protobox supports only MySQL and MariaDB, but other databases, such as PostgreSQL, MongoDB, Redis and Riak are coming soon. Tools such as Ngrok and NewRelic tools (PHP Agent and Node Agent) can be installed by ticking a checkbox.

You can find the source code for Protobox in their GitHub repo.

3. Phansible

Phansible is another tool for creating Vagrant configurations based on Ansible, also inspired from PuPHPet.

Phansible is somewhat simpler than the above services. It allows both Apache and Nginx, with HHVM only when you choose Nginx. You can’t choose any other language than PHP, but you can chose between versions of PHP. Also, you can’t choose any other database other than MySQL.

When you’re predetermined to use only Apache/Nginx, PHP and MySQL, Phansible doens’t differ that much from the other services above. Otherwise, you would have to install other tools manually.

4. Rove

Rove is probably the simplest of them all. You can choose any of the typical configurations, Rails and LAMP, or you can create your configuration manually.

Rove supports CouchDB, MongoDB, MySQL, PostgreSQL, Redis and SQLite as databases. You can select as many as you want of them. It also supports the same languages as PuPHPet, all except HHVM.

5. Customized configurations

There are also some other Vagrant configurations that are case-specific, i.e. for a single PHP framework.

I would mention here Laravel’s Homestead, which makes Laravel development a piece of cake, both front and back end. It comes with PHP5.6, Nginx as the web server, both MySQL and PostgreSQL; and NodeJS, with Bower, Grunt and Gulp for the front-end development. There’s an improved version of Homestead here.

Phalcon also has a Vagrant configuration, which saves you the time of installing it and configuring it in an existent server, although you can follow this quick tip to install Phalcon on a Homestead Improved instance.

WordPress has its own Vagrant configuration, VVV – Varying Vagrant Vagrants. There is a full tutorial on SitePoint which shows you how to use VVV.

Final words

Vagrant may be difficult to use sometimes, but is worth pursuing as it can be exceptionally useful. If you are tired of configuring your machine for every project you have, then Vagrant is the right tool for you. If you face problems while configuring your machine, then using a prepackaged vagrant box is the best answer to your problems. Any alternatives to the services above we’ve missed? Let us know!

Frequently Asked Questions (FAQs) about Getting Started with PHP and Vagrant

What is Vagrant and why is it important for PHP development?

Vagrant is a tool for building and managing virtual machine environments in a single workflow. It provides a simple and easy to use command-line client for managing these environments, and an interpreter for the text-based definitions of what each environment looks like, called Vagrantfiles. Vagrant is important for PHP development because it allows developers to create a reproducible environment which is easy to set up and is the same on every machine. This eliminates the “works on my machine” problem and makes collaboration easier.

How do I install Vagrant for PHP development?

To install Vagrant, you first need to install VirtualBox, which is a free and open-source virtualization software. After installing VirtualBox, you can download and install Vagrant from the official website. Once Vagrant is installed, you can create a new Vagrantfile in your project directory with the command ‘vagrant init’. This will create a new Vagrantfile with a basic configuration.

How do I configure Vagrant for PHP development?

Configuring Vagrant for PHP development involves creating and modifying a Vagrantfile. This file is used to specify the configuration of your virtual machine. You can specify the operating system, the amount of memory, the number of CPUs, and the software that should be installed. For PHP development, you might want to install a LAMP (Linux, Apache, MySQL, PHP) stack.

How do I use Vagrant to manage my PHP development environment?

Once you have created and configured your Vagrantfile, you can use the ‘vagrant up’ command to start your virtual machine. You can then use the ‘vagrant ssh’ command to log into your virtual machine and start developing. When you are done, you can use the ‘vagrant halt’ command to stop your virtual machine.

What are some common issues with Vagrant and how can I troubleshoot them?

Some common issues with Vagrant include problems with networking, synchronization, and provisioning. If you are having trouble with networking, you can try changing the network settings in your Vagrantfile. If you are having trouble with synchronization, you can try using a different synced folder type. If you are having trouble with provisioning, you can try provisioning your virtual machine manually.

How can I use Vagrant with other tools for PHP development?

Vagrant can be used with a variety of other tools for PHP development. For example, you can use Vagrant with Puppet or Chef to automate the provisioning of your virtual machine. You can also use Vagrant with Docker to manage your application’s containers.

How can I optimize my Vagrant environment for PHP development?

There are several ways to optimize your Vagrant environment for PHP development. For example, you can use the NFS synced folder type for faster file synchronization. You can also use a smaller base box to reduce the amount of disk space used by your virtual machine.

How can I share my Vagrant environment with others?

Vagrant makes it easy to share your development environment with others. You can use the ‘vagrant package’ command to package your virtual machine into a box file, which can then be distributed to others. You can also use Vagrant Cloud to share your boxes with others.

How can I use Vagrant to test my PHP applications?

Vagrant is a great tool for testing PHP applications. You can use it to create a separate virtual machine for each of your test environments, ensuring that your tests are run in a clean and controlled environment.

How can I keep my Vagrant environment up to date?

Keeping your Vagrant environment up to date is important for security and performance. You can use the ‘vagrant box update’ command to update your base box, and the ‘vagrant provision’ command to update your software. You should also regularly check for updates to Vagrant itself.

Aldo ZiflajAldo Ziflaj
View Author

Aldo is a Code-Lover and a student of Computer Engineering from Albania. His short-term goal is that of becoming a full-stack developer, focusing on Android, Ruby technologies and DevOps techniques.

BrunoSOOPHPphansiblePHPprotoboxpuphpetrovevagrantvirtual machineVM
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week