Setup myapp role and myapp deploy role: create 2 file myapp.json and myapp_deploy.json in roles folder
myapp.json
123456789101112131415161718192021222324
{"name":"myapp","description":"The role for app servers","json_class":"Chef::Role","default_attributes":{},"override_attributes":{},"chef_type":"role","run_list":["recipe[apt]","recipe[build-essential]","recipe[composer]","recipe[myapp]","recipe[myapp::deploy]"],"env_run_lists":{"dev":["recipe[apt]","recipe[build-essential]","recipe[composer]","recipe[myapp]","recipe[myapp::deploy]"]}}
myapp_deploy.json
12345678910111213141516
{"name":"myapp_deploy","description":"The role for app servers","json_class":"Chef::Role","default_attributes":{},"override_attributes":{},"chef_type":"role","run_list":["recipe[myapp::deploy]"],"env_run_lists":{"dev":["recipe[myapp::deploy]"]}}
Create a owner cookbook called myapp, this should be store in site-cookbooks folder
knife cookbook create myapp
Notes:
1) 2 recipes, one for myapp default and other for myapp deploy
# grant permission for webrootdirectorynode["myapp"]["root_path"]doowner"root"group"root"mode"0755"action:createrecursivetrueend# reading the data bag#secrets = Chef::EncryptedDataBagItem.load("secrets", "myapp")ifnode.chef_environment=="dev"# enable kizang-api site.web_app'myapp'dotemplate'site.conf.erb'docrootnode['myapp']['root_path']server_namenode['myapp']['server_name']endelseend
# -*- mode: ruby -*-# vi: set ft=ruby :# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!VAGRANTFILE_API_VERSION="2"CHEF_PATH="."SYNC_PATH="./www"Vagrant.configure(VAGRANTFILE_API_VERSION)do|config|config.vm.box="ubuntu14.04"config.vm.box_url="https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-14.04-amd64-vbox.box"config.vm.network"private_network",ip:"192.168.34.100"config.vm.hostname="devphp.congdang.com"config.ssh.forward_agent=trueconfig.ssh.forward_x11=trueconfig.vm.provider"virtualbox"do|vb|vb.customize(["modifyvm",:id,"--natdnshostresolver1","off"])vb.customize(["modifyvm",:id,"--natdnsproxy1","off"])vb.customize(["modifyvm",:id,"--memory","1024"])endconfig.omnibus.chef_version='11.16.0'config.vm.provision:chef_solodo|chef|chef.cookbooks_path="#{CHEF_PATH}/cookbooks","#{CHEF_PATH}/site-cookbooks"chef.environments_path="#{CHEF_PATH}/environments"chef.environment="dev"chef.roles_path="#{CHEF_PATH}/roles"chef.add_role('myapp')endconfig.vm.synced_folder("#{SYNC_PATH}","/vagrant",:owner=>"vagrant",:group=>"vagrant",:mount_options=>['dmode=777','fmode=777'])end
Notice that, I used www vagrant’s sync folder. That means you should store your web application in www folder. In this article I just created a simple php file in that folder
index.php
123
<?phpecho"Hello PHP world!!!!";?>
Finally you need to run vagrant and check the site on the browser
vagrant up
Once vagrant upping is done, you can check your application on browser
http://192.168.34.100
Conclusion
You can pull all code from my github
repository on gihub
1
https://github.com/code-for-food/php-cooking.git # fork from github
In this Chef, I’ve setup mysql as well but I just only used php and apache. You can add more code in defaul.rb recipe for using mysql for your project. If you have any question, you can drop an email to me codeforfoods.