Building a LAMP Environment with Vagrant + Chef
Purpose
Use Vagrant and Chef to build a LAMP environment in a local virtual machine.
Background
When you’re working on a shared server, you might think
“I’d love to upgrade MySQL and run some performance tests”,
but doing so would inconvenience everyone else, and you can’t just get a new server bought for you.
In situations like that,
you can try things out in a local environment.
That’s why I introduced this setup.
Environment
- MacOSX Mavericks
- Vagrant
- VirtualBox
Prerequisites
The following must be installed:
- Vagrant
- VirtualBox
- Chef
- knife-solo
↑ For the installation steps, I referred to the dotinstall lessons listed in the “Afterword” at the very bottom.
1. Initialize knife-solo
1 | $ knife configure |
You’ll be asked a bunch of questions, but basically you can just hit [Enter] repeatedly without any problems.
2. Add a BOX
A BOX is the base image file used when launching a virtual machine.
Add a box named centos64.
1 | $ vagrant box add centos64 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-i386-v20131103.box |
- If you want to match the server environment you’re using, it’s a good idea to log in to your server and run the following to check its environment information.
1 | $ uname -a |
You can choose a box URL from the following site.
http://www.vagrantbox.es/
3. Initialize the Virtual Environment
1 | $ mkdir [vagrant用ディレクトリ] |
On success, you’ll see that a Vagrantfile has been created.
1 | $ ls |
4. Edit the Vagrantfile
Configure the private network so that you can access it from your local environment.
This is access from MacOS → VirtualBox.
Just uncomment the line as shown below!
1 | #config.vm.network :private_network, ip: "192.168.33.10" |
5. Start the Virtual Environment
Run the following in the directory that contains the Vagrantfile.
1 | $ vagrant up |
Running the following shows “running(virtualbox)”, which lets you confirm that it’s up and running.
1 | $ vagrant status |
1 | Current machine states: |
Log in with the following.
1 | $ vagrant ssh |
After logging in, run the following when you want to log out.
1 | $ exit |
6. Create an ssh Alias
1 | $ vagrant ssh-config --host [sshエイリアス] >> ~/.ssh/config |
You can then access it with the following.
1 | $ ssh [sshエイリアス] |
You can also confirm that it has been written to the ssh config by running the following.
1 | $ cat ~/.ssh/config |
7. Create a Chef Repository
I think the following structure is easy to manage, so let’s create it like this.
1 | | |
Move one level up from the [vagrant directory] and run the following.
1 | $ knife solo init [Chefのリポジトリ名] |
8. Make the Virtual Machine Chef-ready
1 | $ cd [Chefのリポジトリ名] |
9. Create a cookbook
1 | $ knife cookbook create [cookbook名] -o site_cookbooks/ |
10. Describe the Environment Settings to Build in the cookbook
1 | $ vim [Chefのリポジトリ名]/[cookbook名]/recipe/default.rb |
11. Specify the recipe to Run
1 | $ vim [Chefのリポジトリ名]/nodes/[sshエイリアス名].json |
1 | { |
12. Create a Template
1 | $ vim [Chefリポジトリ名]\[cookbookの名前]\template\default\index.html.erb |
Feel free to edit the contents of index.html.erb however you like.
As an example:
1 | <h1>Hello, World</h1> |
13. Apply the cookbook to the Vagrant Virtual Environment
1 | $ knife solo cook [sshエイリアス名] |
13. Access the Web Server of the Virtual Environment You Configured
Access http://192.168.33.10 in your browser.
Confirm that the following is displayed in the browser!
1 | Hello, World |
Also, when you access the virtual environment, you can confirm that the following file has been created, so give it a try.
1 | $ vagrant ssh |
Afterword
Going through the following dotinstall lessons first makes this easier to get into.
Building a LAMP Environment with Vagrant + Chef
