Carried on from CHEF install WordPress on AWS EC2 Ubuntu

WordPress keeps its configuration in a file called wp-config.php. We need to create that file and put database names and user access details inside it. Chef provides a resource called template that can do just that.

cd cookbooks/phpapp

vi recipes/default.rb # add the lines

wp_secrets = Chef::Config[:file_cache_path] + ‘/wp-secrets.php’

if File.exist?(wp_secrets)

salt_data = File.read(wp_secrets)

else

require ‘open-uri’

salt_data = open(‘https://api.wordpress.org/secret-key/1.1/salt/’).read

open(wp_secrets, ‘wb’) do |file|

file << salt_data

end

end

template node[‘phpapp’][‘path’] + ‘/wp-config.php’ do

source ‘wp-config.php.erb’

mode 0755

owner ‘root’

group ‘root’

variables(

:database        => node[‘phpapp’][‘database’],

:user            => node[‘phpapp’][‘db_username’],

:password        => node[‘phpapp’][‘db_password’],

:wp_secrets      => salt_data)

end

WordPress comes with an example configuration file which we’d usually alter to create our template but for brevity we’ll just specify a cut down version below.

Create templates/default/wp-config.php.erb

vi templates/default/wp-config.php.erb

 

Variables that should be inserted into the template are done so with . You can also include attributes inside templates using although that can prevent your template from being easily reused elsewhere so is not considered best practice.

cd ../..

chef-solo -c solo.rb -j web.json

Next

CHEF create an Apache Virtualhost on AWS EC2 Ubuntu