Saturday, January 20, 2018

How to use Chef and create PHP environment automatically

At first, download and install Chef:
https://downloads.chef.io/chefdk

You can download Chef installer for Windows from the website:


After installing it, start Chef:

You will see the Powershell of Windows:


Run this command and hit y on the keyboard.
Set-ExecutionPolicy RemoteSigned
Close ChefDK and restart it.

Create a folder for Chef. For example, C:\Users\John\Documents\chef.


Go to the folder on Powershell.
cd C:\Users\John\Documents\chef

Install knife-zero on Powershell:
gem install knife-zero

And create a project of Chef:
chef generate repo .

Folders of Chef will be created like this:

Delete the example folders. And create "config.rb" which is used as a configuration file.
rm -d -force -r cookbooks/example
rm -d -force -r data_bags/example
rm -force -r environments/example.json
rm -force -r roles/example.json
touch config.rb

Add "berks-cookbooks" in ".gitignore". (Anywhere is OK except behind #.)

Add "cookbook_path ['berks-cookbooks']" in the folder "config.rb".


Create a project:
chef generate cookbook cookbooks/proj_lamp

Now 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.

Now, on Powershell, run these commands to create cookbooks:
cd C:\Users\John\Documents\chef
chef generate cookbook cookbooks/app_httpd
chef generate cookbook cookbooks/app_mysql_server
chef generate cookbook cookbooks/app_php71u
chef generate cookbook cookbooks/app_php71u_mysqlnd

Cookbook to Install httpd


Open the folder cookbooks/app_httpd and add cookbook 'yum-ius' in the file chef/cookbook/app_httpd/Berksfile like this:





Add depends 'yum-ius' in chef/cookbook/app_httpd/metadata.rb:




Add the following in chef/cookbook/app_httpd/recipes/default.rb
include_recipe 'yum-ius'

package 'httpd' do
  action   :install
end

bash 'link_html' do
  code <<-EOH
    sudo rm -rf /var/www/html
    sudo ln -fs /vagrant /var/www/html
    EOH
end

service 'httpd' do
    action [:enable, :start]
end




Cookbook to Install MySQL


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

package ['mariadb-server', 'mariadb-libs'] do
  action   :remove
end

bash 'wget_mysql' do
  code <<-EOH
    cd /vagrant
    wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
    EOH
end

bash 'rpm_mysql57' do
  code <<-EOH
    cd /vagrant
    sudo rpm -ivh mysql57-community-release-el7-11.noarch.rpm
    EOH
end

package 'mysql-server' do
  action   :install
end

service 'mysqld' do
    action [:enable, :start]
end

Cookbook to Install PHP

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

package ['php', 'mod_php', 'php-common', 'php-cli', 'php-intl', 'php-json', 'php-mbstring', 'php-xml'] do
  action   :remove
end

# for any framework
package 'php71u'
package 'php71u-cli'
package 'php71u-intl'
package 'php71u-json'
package 'php71u-mbstring'
package 'php71u-xml'

Add cookbook 'yum-ius' in chef\cookbooks\app_php71u_mysqlnd\Berksfile.
Add depends 'yum-ius' in chef\cookbooks\app_php71u_mysqlnd\metadata.rb.
Add ci-dessous in chef\cookbooks\app_php71u_mysqlnd\recipes\default.rb.
include_recipe 'yum-ius'

# libmysql is windows only
package ['php-mysql', 'php-mysqli', 'php-pdo', 'php-mysqlnd'] do
  action   :remove
end

# for any framework
package 'php71u-mysqlnd'
package 'php71u-pdo'


Update the project


Add the following in chef/cookbooks/proj_lamp/Berksfile
apps_path = 'C:/Users/John/Documents/chef/cookbooks/'
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_lamp/metadata.rb
depends 'app_httpd'
depends 'app_mysql_server'
depends 'app_php71u'
depends 'app_php71u_mysqlnd'

Add the following in chef/cookbooks/proj_lamp/recipes/default.rb
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

Run these commands and update the project:
berks update -b cookbooks/proj_lamp/Berksfile
berks vendor -b cookbooks/proj_lamp/Berksfile --delete

Finally, create PHP environment in a server "CentOsChef":
knife zero bootstrap CentOsChef -N chef_lamp -z -r 'recipe[proj_lamp]' --sudo
And the PHP environment will be created in your virtual server automatically.

If you want to run knife zero bootstrap.... again, make sure that there is no "chef_lamp.json" in the folders "chef\clients" and "chef\nodes". If there is "chef_lamp.json" in either of them, knife zero bootstrap.... doesn't work and just gets hung.