Question

I am trying to develop a CakePHP application, and I am using Vagrant to run a testing environment. However, I was getting this error in the browser

Warning (2):
  session_start() [http://php.net/function.session-start]:
    open(/var/lib/php/session/sess_speva7ghaftl8n98r9id5a7434, O_RDWR) failed:
      Permission denied (13) [CORE/Cake/Model/Datasource/CakeSession.php, line 614]

I can get rid of the error by SSHing to the vm and doing

[vagrant@myserver ~]$ sudo su -
[root@myserver ~]# chown -R vagrant. /var/lib/php/session/

I don't want to have to do this every time I restart the vm, so I tried adding this to myserver.pp

exec { 'chown':
  command => 'chown -R vagrant. /var/lib/php/session/',
  path => '/bin',
  user => 'root'
}

but it gets an error while starting up the vm...

err:
/Stage[main]/Myserver/Exec[chown]/returns: change from notrun to 0 failed:
  chown -R vagrant. /var/lib/php/session/
  returned 1 instead of one of [0] at /tmp/vagrant-puppet/manifests/myserver.pp:35

I was unable to find any useful examples of how to use exec on the internet, and I have never used Vagrant or Puppet before, so the above code is just the best guess I could come up with, and I apologize if it is a simple fix to get this working.

I have verified using which chown within the vm that the path is /bin, and the command is exactly the same as when I run it in the vm myself. I'm thinking it is the user that is causing problem. Do I have that line right? Is it even possible to exec commands as root from a .pp file?

Was it helpful?

Solution

When using exec, you normally have to enter the full path to the command you execute. So if you change your command into

exec { 'chown':
  command => '/bin/chown -R vagrant:vagrant /var/lib/php/session/',
  path => '/bin',
  user => 'root'
}

it should work imo.

However, it depends a lot how you install your application. If the setup/start of the application is also managed with Puppet, you can also manage the directory you're interested in with Puppet, like this

file { "/var/lib/php/session" : 
  ensure => directory,
  group => "vagrant",
  owner => "vagrant",
  recurse => true,
}

before you start your app. This would be much more the Puppet way, as you manage a reource then instead of executing commands. However, normally /var/lib/... should not be owned by someone other than root.

So you should maybe look into how your app is started and make it start with another user or as root. If it is started with an exec, you can add an additional property

user => root

to it and that should also do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top