Monday, February 24, 2014

How to setup an apache web server with PHP using vagrant and chef-solo

Note to self.
I will change this post in the future to cover the entire stack installation. but for now lets get this apache server running. and also in the next version i will write about how to do this much easily using librarian.

Steps followed:

1. create the project folder
2. create the Vagrantfile inside it using the command 'vagrant init'.
3. update your Vagrantfile to use the correct base box, share the correct folders and configure the port mapping
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu_precise64"
  config.vm.provider :virtualbox do |vb|
    vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
    vb.customize ["modifyvm", :id, "--memory", "2048"]
  end
  config.vm.network :forwarded_port, guest: 80, host: 6001
  config.vm.synced_folder "prototypev2/", "/var/www/prototypev2", :extra => "dmode=777,fmode=777"
  
  config.vm.synced_folder "chefrepo/", "/var/chefrepo", :extra => "dmode=777,fmode=777"

  config.vm.provision :shell, :inline => <<-eot font="">
    apt-get install -y build-essential
    gem install chef --version 11.4.0 --no-rdoc --no-ri --conservative
  EOT
end
4. inside the chefrepo folder make sure you have the solo.rb file, config.json file and a folder for cookbooks
5. solo.rb should read like:
file_cache_path "/var/chefrepo"
cookbook_path "/var/chefrepo/cookbooks"
6. execute 'vagrant up' from host machine and login to the box using 'vagrant ssh''
7. cd /var/chefrepo/cookbooks
8. download php and apache cookbooks using knife utility:
knife cookbook site download php
tar -xvf php-1.3.14.tar.gz
knife cookbook site download apache2
tar -xvf apache2-1.9.0.tar.gz
rm *.gz
8.1 download apt cookbook, this is needed as the first thing on the runlist so it will always execute a 'apt-get update' before the installation process begins
knife cookbook site download apt
tar -xvf apt-2.3.8.tar.gz
9. create a new cookbook for installing and configuring web server
sudo knife cookbook create appserver -o /var/chefrepo/cookbooks
10. update metadata.rb inside appserver folder, don't forget to include the dependencies
depends "apache2"
depends "php"
11. download all dependencies mentioned on php and apache cookbooks (you can get them by looking at metadata.rb of each cookbook)
build-essential
xml
mysql
yum-epel
windows
iis
iptables
logrotate
pacman
chef_handler
yum
openssl

12. create a template for apache site config in appserver/templates/default/ folder named apache_site_config.erb
        ServerAdmin ops@example.com
        DocumentRoot /var/www/prototypev2/
       
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
       
        ErrorLog /var/log/apache2/error.log
        LogLevel warn
        CustomLog /var/log/apache2/access.log combined
        ServerSignature On

13 update cookbooks/appserver/recipes/default.rb as below:
include_recipe "apache2"
include_recipe "php"
include_recipe "apache2::mod_php5"

apache_site "default" do
  enable false
end

execute "change permission web" do
  command "chmod -R 0777 /var/www/prototypev2 "
  action :run
end

web_app 'prototypev2' do
  template 'apache_site_conf.erb'
end

14. update the runlist variable of config.json which lets chef know the recipes it need to run
{
  "run_list": ["recipe[apt]","recipe[appserver]"]
}

15. run chef solo
cd /var/chefrepo
sudo chef-solo -c solo.rb -j config.json