PHP for Beginners Part 1
CourseIntroduction to Vagrant
CourseWhy Vagrant?
FreeVirtualization allows multiple operating systems to simultaneously share processor resources in a safe and efficient manner. One tool we can use to create a virtual environment is Vagrant. By the end of this video, you will be able to describe the advantages, and disadvantages for creating a local development environment with Vagrant, and you should be able to answer the following questions:
- Do you want to proceed with Vagrant?
- Does it sound like it will solve your problems? [yes | no]
- Do you have the necessary hardware to proceed with the lessons? Or should you stick with WAMP/MAMP?
Note: Another common tool for creating development environments today is Docker, which is becoming very popular with Drupal (and other) developers. Take a look at our Drupal Development with Docker series to learn more.
Additional resources
Note: This video is outdated. The following command:
$ vagrant init precise32 http://files.vagrantup.com/precise32.box
...now has download problems if you try it. Try this instead:
$ vagrant init hashicorp/precise64
(The written instructions below contain this update.)
Before we jump into the automation of creating a new developer environment, we need to get the building blocks we'll be working with. You will need to download and install both Vagrant and VirtualBox.
Lesson Outcomes
By the end of this lesson, you will be able to install Vagrant and VirtualBox on a host platform of OSX, or Windows; and create and destroy a Vagrant instance.
Lesson Summary
- Install VirtualBox
- Install Vagrant
- Create a new directory for your configuration scripts. e.g. Websites, or Work-environments. This folder will not contain the VirtualBox binary files, just the Vagrant configuration scripts.
- From the command line, run the following commands:
$ vagrant init hashicorp/precise64
$ vagrant up
$ vagrant ssh
Note: this command will not work for Windows. You will need to use PuTTY to log into your machine. This is covered in the bonus lesson.
- To return to your host machine, run the command:
$ exit
- To destroy the VM and remove the binary disk image:
$ vagrant destroy
This command must be run from the directory which contains the Vagrantfile for the box you wish to destroy. It will not remove any of your configuration files. To recreate the machine, use the commands in step 4.
Troubleshooting and Gotchas
- The lessons were recorded using Vagrant 1.4.3, and VirtualBox 4.3.6.
- There are some gotchas to be aware of: Linux and Windows may need a reboot because of the kernel drivers being added.
- Sometimes upgrades don't work, and re-installing is your best bet. If you have automatic software updates turned on, and Vagrant stops working for you without an obvious reason, try removing everything and re-install using the instructions in this lesson. Removing Vagrant and VirtualBox are covered in one of the extra lessons.
Additional resources
VirtualBox Downloads
Vagrant Downloads
Note: another common tool for creating development environments today is Docker, which is becoming very popular with Drupal (and other) developers. Take a look at our Drupal Development with Docker series to learn more.
In this lesson you will learn how to install a Web server on your virtual machine. Ultimately we will use a provisioning script to automate this task, but this lesson shows you the virtual machine is just like any other server you might have worked with in the past. Any time you want to add new software, you can always come back to this lesson if you (for some reason) don't want to create a recipe and re-provision your machine. Hopefully, though, by the end of this series you'll see why provisioning is a more robust solution than installing software directly.
Lesson Outcomes
By the end of this lesson, you will be able to log into your virtual machine, and use the command line to install new software.
Lesson Summary
- Navigate to www.vagrantbox.es.
- Locate a base box you would like to use. Ensure you are matching the "provider" to what you have. e.g. VirtualBox for these lessons (not VMware).
- At the command lines, initialize your new server:
$ vagrant init <box name> <box URL>
- box name: ringtail64
- box URL (mind the line wrap): http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box
$ vagrant up
Ensure your server is up and running, and then log in with the command: vagrant ssh. This command must be issued from within the directory which contains the file Vagrantfile.
Install an AMP server using the directions for your base box. We'll use the same technique as is covered in the lesson Installing a Web Server on Ubuntu.
$ sudo apt-get install tasksel
$ sudo tasksel install lamp-server
This will install Apache, MySQL, and PHP.
To confirm the server is running, use the command: pstree
.
Troubleshooting and Gotchas
- OSX can use vagrant ssh; Windows will need to install PuTTY and use ssh vagrant. Using PuTTY is covered in the bonus lesson for this series.
- If you get the error
tasksel: aptitude failed (100)
, run the following commands:$ sudo apt-get update
$ sudo tasksel install lamp-server
(I got this error on a precise32 base box with a Windows 8 host running inside of Parallels on a Mac OSX machine.)
In order to "find" our virtual server in a Web browser, we need to make a map between the two host machine, and your guest server. To do this we will use a technique referred to as port forwarding. This allows any communication from the host machine (your Web browser) to be automatically redirected to the appropriate location on your guest machine (the Web server). Fortunately this is very simple to do. In this lesson we are going to map port 4567 traffic on the host machine, onto the standard Web port, 80, on the server. This means when you access Web traffic at http://localhost:4567, it will be automatically redirected to Apache on your server. It's almost like a sleight of hand magic trick where a rabbit appears in a hat, but way cooler.
Lesson Outcomes
By the end of this lesson you will be able to correctly configure your server's network settings so that Web pages can be viewed in a browser on your host machine.
Lesson Summary
- Edit your Vagrantfile (Vagrant configuration file to set the following properties:
- Configure port forwarding
config.vm.network "forwarded_port", guest: 80, host: 4567
- Reload the vagrant instance.
$ vagrant reload
- Log into the server, confirm Apache is running. (Hint: it's not running.)
$ curl 'http://localhost:80'
- Start Apache.
$ /etc/init.d/apache2 start
- In a Web browser, navigate to:
http://127.0.0.1:4567
(Hopefully) It works!
Gotchas
This can sometimes be the tricky bit, especially if you already have a lot of custom network configuration. If this lesson doesn't work for you, you will need to do an audit of your system to find any software which might be interfering with the connection. We're going to limit ourselves to one VM running at a time (all the same ports). This is mostly because of the hardware limitations I expect you'll have. If your machine is more powerful, great! But we're not covering it in this video series.
Typically when we work with a Web server, we edit our files locally, and then "push" them up to our Web server (perhaps using SCP, rsync, Git push, or maybe even FTP). The same will be true with our Vagrant server: you need a way to get files from your host machine, to the guest server. You could either use SCP, but we will enable file sharing to make life a lot easier. With file sharing enabled you will be able to drap-and-drop files into specific directories within your Vagrant instance using your local file manager (e.g. Finder or Windows Explorer). The files will be immediately shared, and recognized by your server, allowing you to continue working on the files with your favorite IDEs and code editing tools without the upload step. Technically, enabling file sharing isn't necessary, but I think you'll agree it is a lot more convenient than SCPing files to your guest server.
Lesson Outcomes
By the end of this lesson you will be able to share files between your host and guest machines without the use of SCP. After enabling file sharing, you should be able to copy a file into the shared folder in your Vagrantfile and have it automatically appear inside the Vagrant instance. e.g. you can load a static web page created on your local machine without having to use SCP.
Lesson Summary
As long as you're using basic file sharing (no NFS), it should work "out of the box" for Windows 8, and OSX.
- Configure synced folder in Vagrantfile.
- Locate and remove:
# config.vm.synced_folder "../data", "/vagrant_data"
- In its place, add:
config.vm.synced_folder "docroot", "/var/www"
- Create a new directory using the name identified in the file Vagrantfile.
$ mkdir docroot
- Reload the vagrant instance (and restart the server). The contents of
/var/www
will appear indocroot
.
$ vagrant reload
$ vagrant ssh
$ sudo /etc/init.d/apache start
$ exit
- Edit the Web page from the host machine.
- Refresh the browser to see the changed file.
Gotchas
This lesson was recorded using VirtualBox 4.3.6 and Vagrant 1.4.3. There is a known issue for file sharing with VirtualBox 4.3.10. If you get the following error message: "Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available." you have encountered this bug. Consider downgrading VirtualBox to 4.3.8, or follow the troubleshooting tips in the issue queue for your particular host machine.
If you are having problems with file permissions / ownership, try editing the permisisons from the host machine, not the guest machine. This is most noticeable when you are creating a new Drupal instance and there are folders / files created automatically by the system. Vagrant won't give a warning to explain why it won't change permissions, it simply doesn't show any changes after running chommands such as: chmod +w <directory_name>
.
If you are still having problems with silent file permissions, update your Vagrantfile as follows:
- Locate the setting for
config.vm.synced_folder
- Add a server-friendly owner, by adding the following to the end:
, :owner => "www-data"
- Add a server-friendly group, by adding the following to the end:
, :group => "www-data"
If you're STILL having problems, update your Vagrantfile to give more permissive defaults.
- Locate the setting for
config.vm.synced_folder
and add the following to the end:, :mount_options => ['dmode=775', 'fmode=664']
- The final line should be something like this (all one line):
config.vm.synced_folder "docroot", "/var/www", :owner => "www-data", :group => "www-data", :mount_options => ['dmode=775', 'fmode=664']
Configuring new servers by hand is tedious and prone to error. Instead of running all the installationa commands by hand to customize our Vagrant setup, we'll use a configuration management (CM) tool to automate this process for us. CM allows system administrators to establish rules which maintain the software installed on one or many computer systems. By automating the setup of a server, configuration management ensures there are no inconsistencies between machines. It also makes it faster for your to create a virtual environment without having to manually install a Web server, like we did in a previous lesson.
Lesson Outcomes
By the end of this lesson, you will be able to explain how configuration management relates to virtual machines, and why you would want to use a configuration management tool to provision new virtual machines.
Lesson Summary
- Vagrant is a wrapper which creates virtual development environments from base boxes. It acts as a wrapper around a provider, such as VirtualBox, and configuration management software, such as Chef.
- Configuration Management: Configuration management is a process which allows system administrators to establish and maintain consistency of a computer system throughout its life against a specified set of rules. Configuration management enables consistency across one, or many, machines.
- Popular open source configuration management tools supported by Vagrant include: Puppet, Chef, and Ansible. Chef tends to be popular among Drupal developers. Puppet tends to be popular among system administrators (who are not necessarily Web developers first-and-foremost).
- Provisioning is the process of creating, or altering, a system in order to prepare it for service.
- Vagrant allows you to use one or more configuration management tools from the same Vagrantfile to provision a machine; as well as use shell scripts.
Additional resources
This video covers some handy tips for reusing previously run commands and moving around through a line of text in the command line interface. These shortcuts make working on the command line much faster and nicer. Trying to remember all of these is not easy to do until you've used them for a while, so we also have a handy-dandy cheat sheet which you can find in the Downloads tab and refer to whenever you are in command line ninja mode. There are always new shortcuts you can learn. Another great shortcut !$.
. It's similar to !!
, but instead of repeating the last command, it repeats the last argument given. This sequence of commands shows how the shortcut could be used: mkdir sites/default/files
followed by chmod a+w !$
. That would be the same as typing out chmod a+w sites/default/files
Note: this video was originally released July 9, 2010 on Lullabot.com.
Note: In some places the command line prompt is cut-off. The YouTube version of this video doesn't have the cut-off problem. We are working on getting this fixed, but in the meantime, check out the YouTube version instead.
Command Line Basics 12: Handy Command Line Shortcuts (youtube.com)
In this tutorial we'll take a look at creating branches with the git branch
command, and tags with the git tag
command and explain the differences. Then we'll look at how and when they can be used and how to move changes from one branch to another using the git merge
and git rebase
commands and talk about the different ways in which those two commands effect the history of a project.
Acquia Learning Series
CourseSetting up Git
FreeThe video will walk you through getting your code setup with Acquia Cloud. This lesson covers:
- Adding your SSH public key to the Cloud dashboard.
- Performing a Git checkout with TortoiseGit to clone your repository.
- Adding new files to your repository.
- Pushing new (and changed) files to Acquia Cloud.
If you don't use Windows and TortoiseGit, or if you want a better overview of Git itself, we have an entire series on Git, which will show you how to install Git and use it from the command line, including doing a git clone command. Once you get things set up with Git, you can refer back to this video to see where you get the Acquia-specific Git information you need.
This is Step-1 of the Free Tier on-boarding process for Acquia Cloud.
After setting up Git, you may proceed in two directions: You can either Install Drupal Distro or if you already have an existing site, you can go ahead and import your site.
You can install one of several Drupal distributions from within the Cloud Network dashboard. Installing Drupal in this manner will remove a previously installed distribution. Assuming you want to create a new installation of Drupal, complete the following steps:
- On your Acquia Cloud dashboard, navigate to "Workflow".
- On the Dev server tab, mouse over the gear icon, and select "Install Drupal".
- From the list of supported distributions, select the one most appropriate for your needs. This tutorial uses Acquia Drupal 7.
- Enable the checkbox which reminds you your installation will overwrite everything in your Dev environment.
Once your site has been created, you may visit it by clicking on the link "visit" in the Dev environment tab. From here you will be able to complete the installation and log into your new site.
This is Step-2 of the Free Tier on-boarding process for Acquia Cloud.
If you already have a Drupal Web site, you can import it to Acquia Cloud using the following steps:
- Create a drush archive of your existing site, using the command: $ drush archive-dump
- Copy the archive to your local computer so that it can be uploaded via a Web interface.
- In the Acquia Cloud dashboard, navigate to the "Workflow" tab.
- Hover over the gear icon and click on "Import an existing site" from the list of options. A pop-up window will open.
- Click the link "Upload a Drush site archive".
- Click "Choose file" and select the archive you made previously from your local computer.
- Enable the checkbox to say you understand this will replace anything currently installed on your dev environment.
- Click "Import".
Note: This will only work for archives that are smaller than 500MB. If your site is larger than this, you will need to refer to Acquia Cloud's documentation.
This is Step-3 of the Free Tier on-boarding process for Acquia Cloud.
To be able to use Search and Insight, you will need to connect your site to the Acquia Network. The video will walk you through this process and help you get connected to the network easily.
- Install the Drupal Connector module if you did not use one of the Acquia distributions to install your site.
- From within your site, click on the link asking you to connect your site to the Acquia Network. It should appear as a nag screen on every page.
- Enter the email address and password you use to login to the Acquia Network.
- Click Next.
Your site should now be connected to the Acquia Network.
This is Step-4 of the Free Tier on-boarding process for Acquia Cloud.
This lesson describes how to drag-and-drop to deploy code and content using the Acquia Cloud UI.
- Navigate to the Acquia Cloud dashboard, and select the "Workflow" link.
- From the Dev column, click and drag the box labelled "Code", and place it onto the Stage column. A pop-up window will appear.
- Enter a commit message.
Repeat steps 2 and 3 for the "Files" and "DB" boxes in the Dev column.
You may drag-and-drop these boxes (Code, Files, DB) in either direction: from Dev to Stage; or from Stage to Dev. If you would like to deploy your code to a live server, you will need to upgrade from the Acquia Free account. You can do this by clicking the link "Upgrade now" from the "Prod" column.
This is Step-5 of the Free Tier on-boarding process for Acquia Cloud.
This overview will introduce you to Acquia's open cloud platform and this series of lessons that will show you how to best use it. If you do not already have a free Acquia Network account, you may obtain one from Acquia.
NOTE: Support of Dreditor has been nominal for a while. It's still the preferred tool for enhancing Drupal's issue queue, but keeping track of what the "official" version is can be tricky. For now, we recommend https://dreditor.github.io/. There's also been work happening to incorporate many of Dreditor's features right into Drupal.org itself. See https://www.drupal.org/project/drupalorg/issues/1673278
Dreditor is a great community tool that assists with things like patch reviews, and generally interacting with the Drupal.org issue queue. Dreditor is not a Drupal module, but is a plugin script you use in your browser. In this lesson, Joe walks through how to get Dreditor installed (on Chrome and Firefox), and then shows you how to use it to make your work in the issue queues more efficient.
Additional resources
In the previous lesson we discussed why you would want to use a configuration management tool. In this lesson we'll dive into how you automate the customization of new servers using the configuration management tool, Chef. Most of the work in assembling Chef recipes is already done for you. In this lesson you will be able to see how the pieces fit together; however, you will not be required to download each of the individual recipes to get everything working.
Lesson Outcomes
By the end of this lesson you will be able to describe how to add a Chef recipe to your setup, and re-provision Vagrant.
Lesson Summary
In the video, I've already setup Chef recipes, roles, and cookbooks for you. So instead of having to download all the pieces separately, you can just start with the code bundle for this lesson.
The outline provided below, however, includes each of the steps that I went through to prepare the configuration Chef recipes and roles that you're downloading.
Locating and Using Chef Cookbooks
- Locate Chef cookbooks for AMP stack, drush, and Drupal-specific PHP libraries you want pre-loaded.
- Place downloaded cookbooks into a sub-folder in your project directory. For example:
chef-recipes/cookbooks
. - Create a role file to include all of the recipes (from each of the cookbooks) you want to load. This file can be placed into its own roles sub-directory. For example:
chef-recipes/roles/lamp_stack.rb
. - Update the Vagrantfile to include:
- path to cookbooks folder
- path to roles folder
- role(s) to load for this server
- new values for any of the default settings for your recipes
- Provision the machine to enable / set-up / trigger the Chef configuration settings:
$ vagrant provision
The machine is now configured, but does not have Drupal installed. This is on purpose. You probably have a specific Drupal project you're working on.
Customizing Configuration Files to Use Chef Recipes
The role files contain a list of individual recipes, and/or roles that must be run to provision a specific type of server. Generally there are three or four parts to them.
- name
- description
- run list (recipes and roles to add)
- default settings for recipes (optional)
You should investigate the contents of the folder chef-recipes/roles
. There is a detailed README tailored to this learning series, as well as the two roles that were created for this lesson.
The Vagrantfile is well commented, this section includes a quick references of the settings that have been updated in the file.
config.vm.provision :chef_solo do |chef|
# default settings
chef.cookbooks_path = "path_to_cookbooks_folder"
chef.roles_path = "path_to_roles_folder"
# roles and recipes to load
# recipes are generally loaded from within roles
chef.add_role "name_of_role"
chef.add_recipe "name_of_recipe"
# overrides for settings in recipes
chef.json = {
"name_of_recipe" => {
"name_of_setting" => "new_value",
},
}
end
Gotchas
- If you are using these recipes for Drupal 8: the recipe provided for Drush won't work. You'll need to install Drush version 7 or greater (compatibility chart on the Drush project page).
- You will probably want to update Chef with the omnibus plugin for Vagrant if the cookbook you want to add requires a newer version of Chef. On the host machine, install the plugin:
$ vagrant plugin install vagrant-omnibus
. Note: Chef must be updated before it is "run". For this lesson, I opted to use older versions of the cookbooks. 20 hours of my life spent working on upgrades that didn't work. The later scripts provide more flexibility (e.g. install ANY version of Drush, not just 5.8), but they work well enough for our needs. - Apt will only run if the cache is older than a day. The first time the machine is provisioned, this might be a problem. You can force updating the cache with the following setting in Vagrantfile:
chef.json = { "apt" => {"compiletime" => true} }
- If you are running into problems with permissions, refer to the tips in Sharing Files Between Your Guest and Host Machine.
- Windows users will also need to download the following cookbooks, and add them to the directory you have configured for chef.cookbooks_path
- chef-handler
- chef-sugar
- iis
- windows
- xml
- yum
- yum-epel
In previous lessons you learned how to automate the setup of your Vagrant instance. We did not, however, automate the installation of Drupal. This is by design. It gives us the ability to quick create new virtual machines using standard pieces of a LAMP stack before finally installing the specific version of Drupal we want to use. In this lesson you will learn how to install Drupal from the command line using Drush. If you already know how to do this, you should review the Lesson Summary, but you are not required to watch the video as it will be entirely review for you.
Lesson Outcomes
By the end of this lesson you will be able to install Drupal via Drush from within your Vagrant instance.
Lesson Summary
The machine was configured in the previous lesson, but does not have Drupal installed. This is on purpose. You probably have a specific Drupal project you're working on. If you don't, you can use the following instructions to set up a generic instance of Drupal. Note: The Chef recipe we're using supports Drupal 6 and Drupal 7, but not Drupal 8. You'll need to update Drush first if you want to use these instructions to install Drupal 8.
$ vagrant ssh
$ cd /var/www/docroot
$ drush dl drupal
$ cd drupal-XXX
(where XXX is the version number for Drupal you've just downloaded)$ drush si standard --db-url=mysql://root:root@localhost/drupal7 --db-su=root --db-su-pw=root --site-name="Drupal on Vagrant"
(for the lessons, the MySQL database has the username root; and the password root, if you have deviated from these insecure passwords, you will need to update this line to get the site install to work).
Gotchas
This lesson assumes you want to install Drupal 7 using the Chef recipe which was given in the previous lesson. This lesson will not work if you are trying to install Drupal 8 as Drush 6 is not compatible with Drupal 8.