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 =


require ‘open-uri’

salt_data = open(‘’).read

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

file << salt_data



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

source ‘wp-config.php.erb’

mode 0755

owner ‘root’

group ‘root’


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

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

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

:wp_secrets      => salt_data)


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


CHEF create an Apache Virtualhost on AWS EC2 Ubuntu