Creating a workspace for a PHP developer is a long and painstaking job. As a rule, these are hundreds of “hackneyed” actions related to setting up a web server, databases, PHP and its components, caching, mail servers … – the list can be considered endless and two or three applications are definitely not enough. With a few additional tools, you can quickly and easily manage this workflow by assembling a local server (environment) for development and testing in a couple of minutes. It will be useful for all those who come across WordPress at any level of difficulty.
Why do you need a local server?
Imagine that you have to create or test a ready-made WordPress theme. It would be great to run a local site, wouldn’t it? But making a workstation with your own hands in a couple of minutes is impossible – the prospect of installing a huge heap of applications and their settings will stop any desire to do this.
In addition to just a lot of work, you also need extensive knowledge of how to do it all right.
Mini Server
In this article, we consider applications (sites) that need complex solutions – PHP, MySQL or Redis. But in some cases, a Swiss knife is not required at all.
Grunt and Gulp. Static files
For example, front-end developers usually need a web server serving only static content (HTML, js, and CSS). Grunt Connect and Gulp Webserver allow you to start such a server in 1 command from the terminal without having to manually download, unpack or edit configuration files.
Server inside PHP
PHP also has a web server inside. In addition to static files, it is expected to be able to work with PHP files. To do this, just go to the root folder of the project and run the PHP command. And you can run Symfony – not that hard for the most complex framework.
Old tools. Denwer, Open Server, MAMP Pro
Many of us are probably familiar with such applications as Denver, Open Server, MAMP Pro – to be honest, I myself once tried to use them. With varying degrees of success, all these programs try to launch the very environment required for WordPress or other CMS to work. But they all have a number of shortcomings with which sooner or later no one wants to put up.
1. It is impossible to easily and quickly transfer the configuration of the environment from computer to computer. Imagine that you have changed the work computer or your colleague has connected to the development, who also needs a server. It will take a week to transfer all the settings and start something.
2. All applications run in a Windows or macOS environment – systems that are not particularly suitable for running server applications.
3. The limitation in applications and their versions forces us to use what is. It is unlikely that you will be able to change the PHP version or add an additional application that you need. Some applications are not available at all on Windows and macOS (for example, server utilities for image compression).
4. Lack of site configuration. As in the case with the program settings, the sites themselves and their content must also be transferred manually. For example, such a familiar and beloved export-import of a database via phpMyAdmin may sooner or later disappoint.
Modern tools. Introducing VVV
The problems we talked about above have already been resolved in Varying Vagrant Vagrants or VVV for short – that’s good news!
Without getting into technical details from the outset, VVV can be described as a set of instructions (configurations) to automatically create a server, which is available for free on Github.
VVV is designed to meet the needs of WordPress developers as much as possible. The server created with VVV is much more than just a regular HTTP and MySQL server. In addition to the most basic applications like Nginx and MySQL, inside the server there are already several pre-deployed sites with different versions of WordPress, as well as many amazing tools, for example, WP CLI, Composer, XDebug , completely ready for workdays without any configuration (a complete list can see the project Readme ).
VVV. What is and how it works
VVV is really just a set of instructions (as mentioned above) and is not a standalone program in itself, unlike Denwer or Open Server. If you try to find an analogy, then VVV-configuration is like an MP3 file for a music player. And to start the server, you will need a few more popular applications that you may have heard of before – these are VirtualBox and Vagrant.
VirtualBox. Virtual machines
VirtualBox is a program for running virtual machines based on your computer. This virtual machine from VirtualBox will be our server with many installed programs and tools.
The new virtual machine in VirtualBox is a computer that uses the resources of your physical computer, which initially has nothing, not even an operating system. Such a virtual computer itself has a number of important features.
First, everything you run inside it is isolated from your physical computer. This is good for security and simply because you don’t have to worry about the state of the server. If something goes wrong, you can simply delete the server as a regular file and recreate it. In short, a great opportunity for experimentation.
Secondly, working on macOS or Windows, we get the opportunity to work with real Linux without any restrictions.
Thirdly, the virtual machine is in no way tied to the system from which it is launched and can be used on macOS, Linux or Windows – on any platform for which VirtualBox is written.
Vagrant. Virtual machine manager
In order to build, configure and run a virtual machine, we need someone to do all of this.
Vagrant was conceived as a program that automatically manages virtual machines and their contents. And for Vagrant, VVV instructions are exactly what you need. That is, returning to analogies, Vagrant is our music player.
Using instructions from the VVV repository, Vagrant automatically downloads an image of a pre-built, empty virtual machine with Ubuntu inside, and then makes a number of other preparations to get our ideal system for a WordPress developer.
Some technical details
If you want to understand in more detail what Vagrant is, then there are several important points to note.
Firstly, Vagrant does not install operating systems but takes pre-created images of virtual machines with operating systems. Images can be found on the official website or you can create your own.
Secondly, since Vagrant is only a manager, it doesn’t care which virtualization system to work with. Therefore, analogs of VirtualBox and even Amazon Web Services can be used. In any case, there are already enough ready-made images for all platforms.
The VVV documentation states that it is intended for use with VirtualBox, but there are also some references to Parallels inside the files, so you should assume that everything will work on this system too.
Thirdly, Vagrant is written in Ruby and complete with fairly detailed documentation makes it not the most difficult and confusing to learn. And some familiarity with Ruby may come in handy in the future, for example, for Chef.
Starting The Server
We got acquainted with all three tools (VVV, VirtualBox, Vagrant) for creating a server – now let’s try to use them. Detailed instructions in English are available in README.md on Github. Below I will tell you all the same:
1. Download VirtualBox from the official site and install. During installation, you need to make sure that the “Install Command Line Tools” checkbox is checked, otherwise Vagrant will not be able to manage virtual machines.
2. Download and install Vagrant – a common and familiar installation. Vagrant, like VirtualBox, is available for many platforms.
3. Install some plugins for Vagrant. To do this, you need to run a couple of commands and wait – Vagrant will do the rest on its own.
vagrant plugin install vagrant-hostsupdater
The Hosts Updater plugin manages the domains on your local machine. So that sites inside the server open with beautiful domain names like local.wordpress.dev. Technically speaking, this plugin automatically updates the contents of the host’s file on your main computer.
vagrant plugin install vagrant-triggers
The Triggers plugin is triggered when the server is turned on and off. Vagrant will automatically export databases to .sql files (you can add your own actions in the future).
4. Download the VVV repository to your computer. If you don’t know what Git is, you can simply download the ZIP archive from Github.
For those familiar with Git, I recommend using git clone. A little later, we will look at how by adding an additional remote server with its own branch, it will be easy and simple to update your VVV when updates are released in the main repository.
5. We have completely prepared the computer to run the server and Vagrant is ready to go. To start all this, you need to go to the folder with the downloaded VVV and execute vagrant up.
cd path / to / vvv / vagrant up
After that, Vagrant will download the image of the finished virtual machine and configure it. At this time, information about the current actions will be displayed on the screen. The whole process will take a decent amount of time and will require a constant internet connection.
Note that the constant internet connection during the initial installation is one of the major drawbacks. If access to Github is lost during server preparation, most likely, some of the server functionality will not work. Restarting vagrant up or vagrant provision will most likely not help.
The fact is that in many places the server preparation algorithm only looks at the presence of a folder on the disk and, if there is one, considers that everything has been installed successfully. If you encounter such a problem, then the quickest and easiest solution would be to delete the entire VVV folder and start over by downloading the repository (step 4). If you are familiar with Bash, you can manually try to fix the situation by studying provision/provision.sh.
At the very end, if everything went well, you will see that the machine is started and ready to go. You can try opening one of the sites right now. For example, in order to open PhpMyAdmin, just click on its link on the start page.
How do I start XDebug?
Usually, XDebug is not used because it is difficult to install and configure. But in VVV it is enough to connect to the server (see above) and execute the command xdebug_on.
# connect to the server vagrant ssh # enable XDebug xdebug_on # To turn off the command xdebug_off
The Code Debugging reference section is collected in more detail about the capabilities of code debugging and profiling.
By the way, using XDebug is not necessary at all. It is quite normal that someone might like to use the usual var_export()and var_dump()instead of languidly waiting for the launch of many favorite IDE written in Java.
Also see: Sail: new CLI utility for managing WordPress applications
How do I add new sites?
Usually adding new sites to the server is not the most interesting process – download and unpack WordPress, create a new user and database, configure a web server, go through several stages of installing WordPress.
Inside VVV, the site can be described as configuration files and saved. For quick and automatic adding of sites, a special manager called VV was made. With its help, it takes a few minutes to install a hundred or two different versions of WordPress.
To create a site, you just need to start vv create and answer a few questions about how to name the site or whether you need to enable the mode WP_DEBUG.
Mounting folders. For plugin and theme developers
When participating in the development of plugins or themes, the product repository is usually stored somewhere separately on the computer. But also this folder and its contents must be present on the server inside wp-content. It is not a good idea to constantly copy the contents of a folder by hand. It is not always appropriate to use links (shortcuts) for a folder (only if your web server is running directly on your operating system).
In my experience, in the case of “symlinks” (links, shortcuts) PHP incorrectly “calculates” the paths of files and folders if you use something like __FILE__(tested on PHP built into macOS). As a result of this, at least, the plug-in activation-removal functions stop working.
Mounting folders solves this problem. Moreover, you can mount one folder in a dozen paths on the server – this is convenient if you need to check the plugin’s compatibility with multiple WordPress versions.
To mount at the root of the folder with VVV, you need to create a file Custom file(without extension), where you specify the folders and paths for mounting them. Thus, the contents of directories from your main computer will appear inside the virtual machine.
# Customfile content config.vm.synced_folder "~ / Documents / Repositories / setka-editor / plugin /", "/ srv / www / wordpress-default / htdocs / wp-content / plugins / setka-editor", : owner => "www-data",: mount_options => ["dmode = 775", "fmode = 774"]
It should be noted that keeping plugin files inside the plugin folder, as I showed at WordCamp Moscow 2016, is not a good solution if you are thinking about the possibility of installing your plugin via Composer and Git.
Support for such a plugin installation system also requires storing the “compiled” CSS and JS files directly in the repository, because Composer does not build the project. So, store the main (bootstrap) plugin file right in the root of the repository. And the ideal file structure looks something like this.
Saving server configuration
Once you’ve cloned the VVV repository, you’ll want to make the changes and save them. To do this, you can add a separate remote Git server to the repository. In a separate branch on this server, you can add commits with changes.
VVV update
You can also use the repository for convenient VVV updates. If you save changes in the repository on a separate branch, then to update it is enough to “merge” master from origin into your own branch, resolve conflicts, if any, and then launch vagrant provision.
How do I connect to the server?
To connect, you can use any SFTP client or a regular console. To connect to the virtual machine from the console, just type vagrant ssh from the VVV folder and Vagrant will automatically establish a connection using the SSH key (it is located in the hidden .vagrant folder).
And for connections via SFTP clients, the following details should be used:
Address (host): vvv User: vagrant Password:vagrant
This data, like many others, for example, for connecting to a MySQL server, can be found in a file README.mdat the root of the repository.
Conclusion
The resulting server is ideal for developing and testing products of any complexity. This environment can be used not only for WordPress development – the server is universal.
Finally, it should be noted that VVV is not the only “build” for Vagrant – you can make your own “fork” or look for other ready-made solutions. For example, you can find popular Vagrant configurations for frameworks like Symfony and Laravel.