Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant and symfony2

I'm having kind of a bizzare issue related to installing Symfony2 from within a vagrant environment. The environment is set up correctly and is running a web server that is serving files from a folder that is shared with the vagrant environment that is located in the base directory of vagrant.

Basically, vagrant is initiated in directory foo and then within foo, there is a directory called webroot. Vagrant automagically shares the foo directory. An apache server is set up to run so that webroot is the base http directory. This all works fine and I am able to serve basic HTML, PHP and the MySQL connection is tested to be fine.

I used composer to install vagrant the recommended way, into directory inside /webroot/ called Symfony. All of the files now exist within the correct directory. The configuration is correct and there are no items that Symfony claims need to be changed in /config.php.

The issue comes when I attempt to load /app_dev.php. It throws an exception claiming that it cannot create a file named cache in the /app directory.

As chmod +a is not supported within the vagrant box I am using, I elected to set permissions by uncommenting umask(0000) in app_dev. Assuming it was a permission problem, I tried using chmod to adjust the permissions both within the vagrant environment and within osx to 777 for everything.

What's strange is that when I chmod a file or directory inside the vagrant environment, it claims to set 777 correctly but then when I ls -l, the permissions have not changed. However, when I chmod a file or directory from OUTSIDE The vagrant environment within the webroot folder, the permissions persist. As symfony does not have r/w permissions within the environment, it cannot create the necessary cache and log files. When i run symfony from the command from osx, everything works fine.

Does anyone have any insight as to how to change the permissions for the /webroot directory so things within the vagrant environment can actually read and write to it as chmod doesn't appear to work?

like image 398
Isaac Diamond Avatar asked Aug 03 '13 06:08

Isaac Diamond


4 Answers

Update as of 15th Jan 2016. Instructions for Vagrant 1.7.4+ and Symfony 3. This works.

On a fresh Ubuntu 14.04 install, ACL was installed but I couldn't use +a or setfacl to fix the permissions issues, and of course, as soon as you change any permissions in terminal in vagrant, they're reset to vagrant:vagrant again.

I added the following to my vagrant file:

# Symfony needs to be able to write to it's cache, logs and sessions directory in var/
config.vm.synced_folder "./var", "/vagrant/var",
 :owner => 'vagrant',
 :group => 'www-data',
 :mount_options => ["dmode=775","fmode=666"]

This tells Vagrant to sync var/logs and var/cache (not to be confused with /var/, these are in the root Symfony directory) and have them owned by vagrant:www-data. This is the same as doing a sudo chown vagrant:www-data var/, except Vagrant now does it for you and enforces that instead of enforcing vagrant:vagrant.

Note there are no 777 'hacks' here.

As soon as I added that, I didn't get any more permissions errors in the apache log and I got a nice Symfony welcome screen. I hope that helps someone!

like image 126
Jimbo Avatar answered Nov 04 '22 07:11

Jimbo


I think it's a question of user rights. Your apache + php is probably launched by root. You have to set rights with root.

Two possibilities :

sudo su
chmod -R 777 app/cache

or

sudo chown -v app/cache
sudo chmod -R 777 app/cache

You will probably have to do the same thing with the log file.

My vagrant file if you need it :

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "precise64"  #Box Name
  config.vm.box_url = "http://files.vagrantup.com/precise64.box" #Box Location
  config.vm.provider :virtualbox do |virtualbox|
      virtualbox.customize ["modifyvm", :id, "--memory", "2048"]
  end
  config.vm.synced_folder ".", "/home/vagrant/synced/", :nfs => true
  #config.vm.network :forwarded_port, guest: 80, host: 8080 # Forward 8080 rquest to vagrant 80 port
  config.vm.network :private_network, ip: "1.2.3.4"
  config.vm.network :public_network
  config.vm.provision :shell, :path => "vagrant.sh"
end

vagrant.sh

#!/usr/bin/env bash

#VM Global Config
apt-get update

#Linux requirement
apt-get install -y vim git

#Apache Install
apt-get install -y apache2

#Apache Configuration
rm -rf /var/www
ln -fs /home/vagrant/synced/web /var/www
chmod -R 755 /home/vagrant/synced

#Php Install
apt-get install -y python-software-properties
add-apt-repository -y ppa:ondrej/php5
apt-get update

apt-get install -y php5 libapache2-mod-php5

#Php Divers
apt-get install -y php5-intl php-apc php5-gd php5-curl

#PhpUnit
apt-get install -y phpunit
pear upgrade pear
pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear channel-discover pear.symfony.com
pear install --alldeps phpunit/PHPUnit


#Php Configuration
sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 10M/" /etc/php5/apache2/php.ini
sed -i "s/short_open_tag = On/short_open_tag = Off/" /etc/php5/apache2/php.ini
sed -i "s/;date.timezone =/date.timezone = Europe\/London/" /etc/php5/apache2/php.ini
sed -i "s/memory_limit = 128M/memory_limit = 1024M/" /etc/php5/apache2/php.ini
sed -i "s/_errors = Off/_errors = On/" /etc/php5/apache2/php.ini

#Reload apache configuration
/etc/init.d/apache2 reload

#Composer
php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
mv -f composer.phar /usr/local/bin/composer.phar
alias composer='/usr/local/bin/composer.phar'

#Postgres
apt-get install -y postgresql postgresql-client postgresql-client php5-pgsql
su - postgres -c "psql -U postgres -d postgres -c \"alter user postgres with password 'vagrant';\""
like image 30
edlingeer Avatar answered Nov 04 '22 07:11

edlingeer


An updated answer for nfs:

config.vm.synced_folder "www", "/var/www", type:nfs, :nfs => { :mount_options => ["dmode=777","fmode=777"] }
like image 3
Dr.Knowitall Avatar answered Nov 04 '22 06:11

Dr.Knowitall


Nothing worked for me other than changing location of cache and logs folder to /tmp

AppKernel.php

public function getCacheDir()
{
    if (in_array($this->getEnvironment(), ['test','dev'])) {
        return '/tmp/sfcache/'.$this->getEnvironment();
    }
    return parent::getCacheDir();
}

public function getLogDir()
{
    if (in_array($this->getEnvironment(), ['test','dev'])) {
        return '/tmp/sflogs/'.$this->getEnvironment();
    }
    return parent::getLogDir();
}
like image 1
Muzafar Ali Avatar answered Nov 04 '22 05:11

Muzafar Ali