Modifying php.ini setting with chef php cookbook

4.3k Views Asked by At

I have installed the PHP Cookbook from opscode and the chef-dotdeb cookbook found at chef-dotdeb so that I can run PHP 5.4 in the vagrant box.

I would like to modify some of the default php.ini settings.

According to the documentation for the chef php cookbook I can modify the settings using

node['php']['directives'] = {}

for example:

node['php']['directives'] = { :short_open_tag => 'Off' }

I have made the modification in the webserver.rb script I have created in my applications cookbook. When I provision or reload the vagrant box the php.ini settings remain unchanged.

Any ideas what is wrong?

The contents of the webserver.rb file are:

include_recipe "nginx"

include_recipe "php"

node.default["php"]["directives"] = { :short_open_tag => 'Off' }

Even when i remove the dotdeb cookbook so that the only php stuff comes from the official opscode php cookbook it still doesnt update any ini values.

ADDITIONAL INFO

I have looked at the code in the opscode php cookbook that actually injects the directives into it erb php.ini template: https://github.com/opscode-cookbooks/php/blob/master/templates/ubuntu/php.ini.erb

The code that injects the appends the directives to the end of the file is:

<% @directives.sort_by { |key, val| key }.each do |directive, value| -%>
<%= "#{directive}=\"#{value}\"" %>
<% end -%>

this is always empty {}

However.... if i modify it to...

<% node.default[:php][:directives].sort_by { |key, val| key }.each do |directive, value| -%>
<%= "#{directive}=\"#{value}\"" %>
<% end -%>

Then the directives ARE injected into the template. Im not a ruby expert. What is the fundamental difference between these 2 pieces of logic???

5

There are 5 best solutions below

2
On

Try using:

node.set['php']['directives'] = { :short_open_tag => 'Off' }

And if that doesn't work you can try to use the override option:

node.override['php']['directives'] = { :short_open_tag => 'Off' }

As of chef 11 you need to set the explicitly set the precedence level:

https://wiki.opscode.com/display/chef/Breaking+Changes+in+Chef+11

1
On

Might be a real long shot but I was trying to use this feature and was finding it doesn't work, then I actually found it's because I was looking at the apache2 php.ini when the cookbook by default only adds the setting to the cli php.ini file. This probably isn't an issue for Fedora/Redhat, but it is for Ubuntu because it separates the configs out under /etc/php5/ into different folders.

0
On

Got this this problem that my PHP directives where not applied on PHP running Ubuntu 16.04. My solution was to override PHP's conf_dir to make the recipe skip cli and update only the config used by Apache2.

"override_attributes": {   
      "php": {
            "conf_dir": "/etc/php/7.0/apache2"
0
On

I know it's quite old question, but it's might help others.

the @ symbol meant it's a variable. Template resources have variables property and you can add the directives attribute there.

More on the doc:

This attribute can also be used to reference a partial template file by using a Hash. For example:

template "/file/name.txt" do
  variables :partials => {
    "partial_name_1.txt.erb" => "message",
    "partial_name_2.txt.erb" => "message",
    "partial_name_3.txt.erb" => "message"
  },
end
where each of the partial template files can then be combined using normal Ruby template patterns within a template file, such as:

<% @partials.each do |partial, message| %>
  Here is <%= partial %>
<%= render partial, :variables => {:message => message} %>
<% end %>

https://docs.getchef.com/resource_template.html

If you see the ini recipe of cookbook php:

template "#{node['php']['conf_dir']}/php.ini" do
    source node['php']['ini']['template']
    cookbook node['php']['ini']['cookbook']
    unless platform?('windows')
        owner 'root'
        group 'root'
        mode '0644'
    end
    variables(:directives => node['php']['directives'])
end

https://github.com/opscode-cookbooks/php/blob/master/recipes/ini.rb

it's assigning the node['php']['directives'] to variables in template.

0
On

Had the same issue installing priestjims php with Apache. Installing Apache first and running apache2::mod_php5 does not mean you can leave out php::apache2 that was the mistake for me.

Solution is to include php::apache2 recipe and then it is writing it's php.ini to 'apache_conf_dir'. This way development ini settings like

default['php']['directives'] = {
     'display_errors' => 'On',
     'allow_url_fopen' => 'On',
     'allow_url_include' => 'On',
     'short_open_tag' => 'Off'
}

will be correctly applied to apache's php.ini.