Setting up your Local Development Environment ( A guide for Ubies ) Part 2

Earlier we went through all the steps necessary in order to download all the tools we would need to set up a virtual server and Drupal site in a pre-configured environment through vagrant. Now we are going to take a look at how we can customize that set up to exactly how we would like.

Configuring the virtual machine

During the setup, Drupal VM uses a make file to spin up a Drupal site onto the virtual server that is created. Make files are specially formatted text files that are parsed by Drush, think of them as a set of instructions that Drush will use to build a Drupal site configured in a way that you specify. By default, the config and make files included with Drupal VM are setup for a fresh install of a Drupal 8 site. We are going to change them for a Drupal 7 install and include a few contributed modules along with it just to help you get the hang of what a make file is.

Let's start with the config file, in a text editor of your choice go on and open up the example.config.yml file that is inside the Drupal VM folder. Let's break this down section by section.

Starting on line two, you should see a section titled Vagrant hostname and IP configuration. There is a line of text that reads vagrant_hostname: drupaltest.dev. Let's change the hostname to something more inline with what we are actually doing. I'm going to change it to vagrant_hostname: localsetuptest.dev. This will be the name of our machine that appears in VirtualBox and will help us identify the virtual machine, especially important if you have several virtual machines setup. The vagrant_ip can remain as is, I will be changing mine since I already have a vagrant install using that ip address.

You will need to add the ip address to your hosts file as well. You can do this manually by opening a terminal and use the following command.
sudo nano /private/etc/hosts

This will open the nano text editor inside your terminal where you can add the following line.
192.168.88.88 localsetuptest.dev
Use ctrl + x to exit, when asked to save say yes and hit enter then hit enter again to write the file.

Alternatively you can install a vagrant plugin to take care of this for you. From a terminal type in:
vagrant plugin install vagrant-hostupdater
This plugin also has the benefit of automatically removing the entries from the host file after you destroy a vagrant box.

Moving on, you will see the next section has to do with synced folders. The local path refers to the directory on your local machine where the drupal install will take place. I am going to remove the drupalvm from line 13 and replace it with localdevsetup.This directory will be created when we create the vagrant box and this is where the drupal files will be placed locally. This directory will also be shared with the virtual server with the path being indicated on line 14 destination: /var/www. This means any file that is created/placed in one location will also appear in the other. The rest of the settings in this section can all remain as they are now.

The next section dealing with memory and CPU usage has to do with how much of your system resources the virtual machine will use when it is running. These settings can remain how they are but you can adjust them to suit your own needs, just keep in mind the limitations of your own system.

Next we see the section that handles the make file. If you already had an existing drupal site installed locally on your machine and wanted to use that with the vagrant box, then you would change line 25 from true to false. We will leave it at true since we will indeed be installing a fresh drupal site using a makefile. The next line is the file path that the vagrant box will look for the make file during setup. We will also leave this as is.

Starting on line 28, this section handles the initial configuration of a drupal site if you are using a make file. If on line 25 from earlier, that was set to false then this section would be skipped over during the setup. We will need to make a few changes here as well. Since we are setting up a Drupal 7 site, we first want to change line 30 from drupal_major_version: 8 to drupal_major_version: 7. Let's also go on and change the drupal domain name on line 32 from drupal_domain: "drupaltest.dev" to drupal_domain: "localsetuptest.dev" to match up with our vagrant hostname. You can change the drupal site name on line 33 if you wish, but since this is just an example setup I will leave it as "Drupal" lets also go on an make changes to lines 38 and 39 from

drupal_mysql_user: drupal
drupal_mysql_password: drupal

to

drupal_mysql_user: root
drupal_mysql_password: root

We will leave the database name as drupal as well.

The next section automatically sets up your Drush aliases during the vagrant box install and we will leave it as it is.

We will need to add an Apache virtual host for our site in the next section. Let's edit line 49 to do just that, lets make the servername match up with drupal_domain name from line 32 and the document root to match up with the drupal_core_path from line 31.

The next section handles the database setup. lets edit it to match the initial drupal site install information from lines 38 through 40. We left the database name as "drupal" on line 40, so let's enter that information into line 56. We set our MySQL username and password as root on lines 38 and 39, so now let's enter that into the mysql_users section. On line 61 enter root for name and on line 63 enter root for password. On line 64, get rid of the double curly braces and white space surrounding drupal_mysql_databse.

Next are the extra utilities that come bundled with Drupal VM. you can comment out any utilities you don't want installed but we will leave it as is.

The last section we are concerned about starts on line 116 and deals with the enablement of Xdebug. by default Xdebug is turned off. You may or may not be currently using xdebug but we are going to go ahead and turn it on so that I can go over how to use it with phpstorm later on. Change both the "0" on line 117 and 118 to "1".

That's it for the config file. Save the file as config.yml

Configuring the Drupal site install

Let's tackle configuring the drupal.make.yml file now. Open a terminal window and change directory back into the original directory where Drupal VM was installed. Perform a ls and you should see a file that is called example.drupal.make.yml. Open this file with a text editor of your choice. You should see a file that looks like what you see in the picture below. We won't be making too many changes to this file as we did with the last one. The first thing we want to take note of is line 8 that says core: "8.x". Recall that in the previous config file we indicated that we would be installing Drupal 7 so we need to make that adjustment here. Simply change the eight to a seven. While we are at it, look down at line 14 and you should see version: "8.0.x". You can probably guess what we will be doing here.....replace the 8.0.x with simply 7.x. At this point we are basically done with the make file if we want to be. Looking at the make file you will see that it says to install the latest version of Drupal core along with the devel module. Let's add a few more modules just so we get a better idea of how the make.yml file should look. You can find a full example of one here. When using make files to install modules you can specify specific versions, patches, or simply use a ~ to install the latest version. We will just stick with the ~ for now but as you progress further along in your Drupal path you may wish to start being more specific with your modules for various reasons. Lets go on and add a few commonly used modules: views, ctools, admin menu, and feeds. It's fairly simple to make additions to a make file, basically just follow the format of the example that was linked earlier. When you are done your code should look something like this:

---
api: 2

# Basic Drush Make file for Drupal. Be sure to update the drupal_major_version
# variable inside config.yml if you change the major version in this file.

# Drupal core (major version, e.g. 6.x, 7.x, 8.x).
core: "7.x"

projects:

  drupal:
    type: "core"
    version: "7.x"
    download:
      working-copy: true

  devel: 
    version: "1.x-dev"
  ctools:
    version: ~
  views:
    version: ~
  admin_menu:
    version: ~
  feeds: 
    version: ~

Pretty simple right, now all you need to do is save the file as drupal.make.yml

At this point all we have to do is type in a few simple commands and our virtual server will be set up and ready to go with a Drupal 7 site along with it. Go ahead and open a terminal window again. Make sure you are inside the drupal-vm directory that was cloned from earlier. If you aren't for some reason simply cd back into it: cd ~/Sites/vagrant_example/drupal-vm
Now all you need to do is type the command vagrant up and let Drupal VM do its thing. It should take roughly ten minutes or so to complete the installation. Once it is finished you should be able to access the new drupal site from a browsewr by navigating to localsetuptest.dev or if you chose to use a different domain in the config file then navigate to that instead. Once there you can login to the site by using admin as the username and admin as the password or if you changed these in the config file then use the values you entered for drupal_account_name and drupal_account_password. Once logged in you can navigate to the modules section and you should see that the modules we specified in the make file are there and ready to be enabled. There you go, a Drupal site ready to go.

Now we can navigate back to the command line, you should still be in the same drupal-vm directory. Type in vagrant ssh> and just like that you should now be logged into your virtual server.
Look back in the config file, on line 14 you should see the destination path that should've been created during the vagrant box install process, let's try to cd into that directory now. Once there use the ls command to list the contents of the directory. You should see the drupal core folder sitting there now, go on and cd into it as well, perform another ls and there you should see all of your Drupal site files.

cd /var/www
ls
cd drupal
ls

Open another terminal window, according to the config file on line 13, we set the path on our local machine of the drupal install to be ~/Sites/localdevsetup. Change to that directory now, cd ~/Sites/localdevsetup. List the files for that directory and you should see the drupal directory there as well, go on and cd into that directory. You should see all the Drupal install files here as well, this directory on your local machine is synced with the directory on your virtual server. Files you put in here will show up on your virtual server and vice versa.

So now we have a fully installed Drupal site and a fully configurable server that it is running on. This may have seemed like a lot of steps to go though, but think about it, once you have a make file setup to your liking there are really only a few lines you have to change in the config file then run a simple vagrant up command and you can install servers and sites as much as you want quickly and efficiently. You could create pre-packaged environments and distribute them to a team you are working on and now everyone has the same working environment which is great for keeping consistency. When you have completed whatever development you were doing simply run vagrant destroy from the same directory you installed it from and it is gone off your system.

Hopefully you found this post useful. If you followed along with the steps, try destroying your vagrant install and going through the process again, this time use your own configurations and see how it affects the setup. Remember that this is just one of many ways to setup your local environment, it's a method that I have come to prefer over others but it's not a bad idea to familiarize yourself with other setups as well to find what works best for you.

In part three we will look out how we can utilize PhpStorm to streamline our dev environment even more.

About the Author

John Cunningham

I joined the U.S. Marine Corp at the age of 17. I served for five years part of which fell during Operation Enduring Freedom. Following the Marines, I attended the university of Central Florida and received my Bachelors of Science in Biology.

User login