Sunday, January 21, 2018

How to use Chef and create Laravel environment automatically

Follow steps written here and create basic cookbooks for PHP environment.

After that, you should have cookbooks like this:


Create Virtual Box for chef


We will create a virtual server. Create a folder for the virtual server and Vagrant and run these commands on Command Prompt. (Not Powershell now)


(If you don't know how to use Vagrant and Virtual box)

cd C:\Users\John\Documents\vagrant\CentOsChef
vagrant init bento/centos-7.3



Change the file "Vagrantfile" and use the private network.

And start the virtual server.
vagrant up

Please make sure there is ".ssh" folder in C:\Users\John. If not, create the folder by running this command:
cd C:\Users\John
mkdir .ssh





After that, run these commands:
cd C:\Users\John\Documents\vagrant\CentOsChef
vagrant ssh-config --host CentOsChef >> %USERPROFILE%/.ssh/config
"CentOsChef" is a name of your virtual machine. "ssh-config" is a command to create a file and to make connection to the virtual machine easier. If there is old ssh-config information using a same alias name, delete the old one and leave only the new one.

Create a cookbook for laravel


Open Powershell (ChefDK) and go to the folder of Chef on Powershell:
cd C:\Users\John\Documents\chef

Create a cookbook for laravel environment.
chef generate cookbook cookbooks/proj_laravel


Update the virtual machine so that the shared folder works


And create a new cookbook for updating vagrant:
chef generate cookbook cookbooks/vagrant_update

Add depends 'yum-ius' in chef\cookbooks\vagrant_update\metadata.rb.
Add cookbook 'yum-ius' in chef\cookbooks\vagrant_update\Berksfile.
Add the following in chef\cookbooks\vagrant_update\recipes\default.rb:
include_recipe 'yum-ius'

package ['kernel-devel', 'kernel-headers', 'dkms', 'gcc', 'gcc-c++'] do
  action   :install
end

package 'kernel' do
  action   :upgrade
end


Update the cookbook of laravel



Add the following in chef\cookbooks\proj_laravel\Berksfile
apps_path = 'C:/Users/Shu/Documents/chef/cookbooks/'
cookbook 'vagrant_update',     path: apps_path + 'vagrant_update'
cookbook 'app_httpd',           path: apps_path + 'app_httpd'
cookbook 'app_mysql_server', path: apps_path + 'app_mysql_server'
cookbook 'app_php71u',             path: apps_path + 'app_php71u'
cookbook 'app_php71u_mysqlnd',     path: apps_path + 'app_php71u_mysqlnd'

Add the following in chef\cookbooks\proj_laravel\metadata.rb
depends 'vagrant_update'
depends 'app_httpd'
depends 'app_mysql_server'
depends 'app_php71u'
depends 'app_php71u_mysqlnd'

Add the following in chef\cookbooks\proj_laravel\recipes\default.rb
include_recipe 'vagrant_update'
include_recipe 'app_httpd'
include_recipe 'app_mysql_server'
include_recipe 'app_php71u'
include_recipe 'app_php71u_mysqlnd'

service 'httpd' do
    action :restart
end

service 'mysqld' do
    action :restart
end

bash 'install_composer' do
  code <<-EOH
    curl -sS https://getcomposer.org/installer | php
    mv composer.phar /usr/local/bin/composer
    EOH
end

bash 'laravel_install' do
  code <<-EOH
    cd /vagrant
    curl -sS https://getcomposer.org/installer | php
    mv composer.phar /usr/local/bin/composer
    composer create-project --prefer-dist laravel/laravel laravel
  EOH
end

bash 'set_permission' do
  code <<-EOH
    chmod -R 777 /vagrant/laravel/storage
  EOH
end

ruby_block "httpd conf replace to make allowOverride all" do
  block do
    fe = Chef::Util::FileEdit.new("/etc/httpd/conf/httpd.conf")
    fe.search_file_replace(/\<Directory \"\/var\/www\/html\"\>[\s\S\n]*?\<\/Directory\>/,
                               "<Directory \"/var/www/html\" />Options FollowSymLinks\nAllowOverride All\nRequire all granted\n</Directory>")
    fe.write_file
  end
end

ruby_block "httpd conf replace to change document root" do
  block do
    fe = Chef::Util::FileEdit.new("/etc/httpd/conf/httpd.conf")
    fe.search_file_replace(/DocumentRoot \"\/var\/www\/html\"/,
                               "DocumentRoot \"/var/www/html/laravel/public\"")
    fe.write_file
  end
end

service 'httpd' do
    action :restart
end

service 'mysqld' do
    action :restart
end

Then update the Chef files on Powershell (ChefDK).
berks vendor -b cookbooks/proj_cake3/Berksfile --delete

Start creating the Laravel environment.
knife zero bootstrap CentOsChef -N chef_laravel -z -r 'recipe[proj_laravel]' --sudo

Wait for a while until the result appears on Powershell (ChefDK)....
If all of them worked successfully, you can access http://192.168.33.10/ and see the laravel's top page.








But maybe you need to manually do:
composer install
php artisan key:generate