Vagrant folder permissions using nginx

2019-06-28 05:30发布

问题:

I am new to vagrant and having issues getting working correctly. It is running fine with port forwarding and I can access it. However I am having trouble getting bower and gulp to work correctly.

The issue seems to stem from the /var/www directory being owned by www-data/www-data. The vagrant user doesn't have write permissions to any of the directories even after adding vagrant to the www-data group. I am no even able to use sudo chmod to add the write permission to any file.

I get no access permission denied errors any time I try to run bower, gulp or even git.

Any help would be greatly appreciated.

Vagrant file:

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

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "hashicorp/precise32"

  config.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true

  config.ssh.private_key_path = ['~/.vagrant.d/insecure_private_key', '~/.ssh/id_rsa.pub']
  config.ssh.forward_agent = true

  config.vm.synced_folder "/home/develop/b3c-dev", "/var/www", create: true, group: "vagrant", owner: "www-data"
  config.vm.synced_folder "/home/vagrant/b3c_ee/provision", "/var/provision", create: true, group: "root", owner: "root"

  config.vm.provider "virtualbox" do |v|
    v.name = "B3C Expression Engine Dev Vagrant"
    v.customize ["modifyvm", :id, "--memory", "1024"]
  end

  config.vm.provision "shell", path: "provision/setup.sh"
end

Nginx config:

server {
    listen 80;
    server_name test.dev www.test.dev;

    root /var/www/public/;
    index index.php index.html;

    access_log /var/log/nginx/b3c-dev-access.log;
    error_log  /var/log/nginx/b3c-dev-error.log info;
    # Important for VirtualBox
    # sendfile off;

    location / {
        index index.php;
        try_files $uri $uri/ @ee;
      }

      location @ee {
        rewrite ^(.*) /index.php?$1 last;
      }

    location ~* \.php {
        include fastcgi_params;

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_cache off;
        fastcgi_index index.php;
    }
}

回答1:

I would load the shared folder with vagrant as the owner and the group and then change the user and the group to vagrant in /etc/php5/fpm/pool.d/www.conf.

To change the user and group in the php-fpm's config, just add these to lines to the end of provision/setup.sh:

sed -i 's/user = www-data/user = vagrant/g' /etc/php5/fpm/pool.d/www.conf
sed -i 's/group = www-data/group = vagrant/g' /etc/php5/fpm/pool.d/www.conf

If this does not help, try to increase the permission of /var/www recursively.