Vagrant synced folder link to Apache /var/www/ in Ubuntu VM - windows

I'm not sure how to phrase the title; hopefully, it made sense. I am learning about Vagrant and just found out about synced folders. I am doing web development and want to edit/create my files and folders on my local machine instead of Ubuntu 14.04 (on Vagrant).
I store my files in my /vagrant/ folder, which end up appearing on both machines. This is perfect. I want these folders/files to be automatically moved to /var/www/ as soon as a file or folder is changed in the synced folder.
For example:
Make folder on Windows 10 called test inside the synced folder. Put index.html inside test.
The folder and file are uploaded to the Ubuntu machine.
Inside of /var/www/ in the Ubuntu machine, there should be a copy of the test folder along with all the files inside of it.
The test folder inside /var/www/ updates if any changes are made in the synced folder /vagrant/.
I was looking into the ln command but I keep getting an error saying "hard links not allowed". I was trying like this on Ubuntu:
cd /vagrant
ln test /var/www/
Should I approach this differently? How can I accomplish what I want to do? Thanks in advance!

You can do sharing folder using vagrant, it is called synced folder in Vagrant terminology
If you want to sync the guest /var/www you can add the following in your Vagrantfile
config.vm.synced_folder "www/", "/var/www"
so if you create the www/ folder within your local/host project folder, any files that you will save under this www folder will automatically be synchronized with the /var/www folder of your ubuntu machine
vagrant/virtualbox do not really limit you in the number of sync folder so you should adopt that rather than using one and work with symlink.

Another way of syncing your folder is to create a soft link in linux. Just type this into your command line:
rm -rf /var/www
ln -fs /vagrant /var/www
This will remove the /var/www folder and create a soft link so that whoever tries to go to /var/www will be automatically be redirected to your synced folder which in this case is /vagrant but you can change that to something different.
you can also create a shell script for this so that every time you create a virtual instance, it can automatically do this for you instead of you having to manually do it every time.
Here is a link to how to make the vagrant file run a shell script when you vagrant up:
https://www.vagrantup.com/docs/getting-started/provisioning.html

Related

Vagrant synced folders disappearing

I'm using vagrant and have hashicorp/precise32 box. My projects in /products/ folder on this box. I want to reach this folder from my host computer. So added this configuration to my Vagrantfile
config.vm.synced_folder "/products","/products"
Also created /products folder on host. But after reload when I connect to box over SSH, /products folder's all contents are disappearing. When I comment out the config.vm.synced_folder line and reload, contents appering again.
Can you please tell me what's I missing?
Thank you.
Vagrant sync folder works the other way around
Synced folders enable Vagrant to sync a folder on the host machine to
the guest machine, allowing you to continue working on your project's
files on your host machine, but use the resources in the guest machine
to compile or run your project.
so if you have an existing folder on your guest machine and you ask vagrant to create a sync folder with one of your folder from the host, it will do but replace the content from the content of your host.
There is an old question with some good reference to understand it further

Vagrant won't sync its parent folder on host

I am running Vagrant on Windows 8.1 using Puphpet. Using following sync settings:
source: ../
target: /var/www/myserver
sync_type: default
As far as I know ../ should sync the parent folder of the vagrant, but its syncing the same folder its in.
EDIT: Let's say I have folder structure like /folder1/folder2 and here I have vagrantfile folder1/folder2/Vagrantfile now I wanted to sync the folder1. It doesn't matter if I have ./ or ../ folder2 will be synced. I want to sync the parent folder (folder1).
How can I sync the parent folder of the vagrant file?
All directories use the location of the Vagrantfile as the base:
https://github.com/puphpet/puphpet/blob/master/src/Puphpet/Extension/VagrantfileLocalBundle/Resources/views/Vagrantfile.rb.twig#L3
dir = File.dirname(File.expand_path(__FILE__))
To share the actual directory containing your Vagrantfile (and everything within), use ./.

Cannot access Vagrant shared folders

I just trying to add Vagrant to my workflow and I have following probably noob problem. I pull standard hashicorp standard 14.04 image, vagrant up it, SSH to it install my python requirements on it and then then try to execute build commands against code in Vagrant shared folder and run in to problems. Basic errors I get say those locations don't exist or cannot be found.
First action I, go ls /vagrant and can see my shared folders. I cannot cd to them from Vagrant machine I have tried to halt the machine.
Vagrantfile shared folder code.
{"virtualbox":{"/vagrant":{"guestpath":"/vagrant","hostpath":"/Users/Kimmo/Documents/Mist.io","disabled":false}}}
I am using Virtualbox as provider newest version.
My dev machine is OSX 10.9.5
There are not access limitations on the folder itself.
Thanks for you help in advance :)
Does /vagrant exists inside the VM just after you start it ?
If no you can add this parameter : "create":true. According to the doc, for the create: true paramater : If true, the host path will be created if it does not exist. Defaults to false.
If the folder /vagrant exists but you can cd or ls it, you can add parameters wich will define the right/owner of this folder:
owner: (string) the user who should be the owner of this synced folder. By default this will be the SSH user. Some synced folder types don't support modifying the owner.
group: (string) the group that will own the synced folder. By default this will be the SSH user. Some synced folder types don't support modifying the group.
Also for the hostpath parameter you have to give him a folder path, not a file path. In your conf I can see: "hostpath":"/Users/Kimmo/Documents/Mist.io". If Mist.io is a file and you want to access this file inside your VM, just give the path to the folder containing this file, /Users/Kimmo/Documents in your case.

Understanding Vagrant Synced folder setup with the default & html folder

I'm working on getting a Vagrant setup running for the first time on a Windows 8.1 machine. I've been pouring over all the documentation to understand how it all works. I used puphpet to help me configure a Ubuntu 14 LAMP development box to use.
My confusion is around how the synced folders are setup. It seems that the synced folders on install, always create a new 'default' and 'html' folder in whatever local directory I specify in my puphpet setup. I then have to put any code files I want to run in the default folder.
For example: host folder: ./www ends up having /www/default and www//html in it after Vagrant up. Why is this occurring? I left the remote folder to the default /var/www.
My ideal setup was to have a Vagrant box folder setup on my machine, and then have my code project reside in the same directory as the Vagrant box files. However, it seems that I can only put the code project in the 'default' directory that Vagrant creates.
I can't find any mention of this in the documentation. Is this normal behaviour for this to be happening? If not, how can I get around this so that I can keep the code in whatever directory that I want?
Here's the relevant code from the puphpet config file:
synced_folder:
Rh7fCuil7xE4:
source: ./www
target: /var/www
sync_type: default
rsync:
args: '-z'
exclude: .vagrant/
auto: 'false'
Author of puphpet here.
Nginx creates a folder for its default vhost (same with Apache). That's what those two folders are. It also appears there's a small bug in puphpet that will create both folders, even though one is the right one. I'll see about fixing it, but it shouldn't affect your VM in the slightest.
/var/www is what you've defined as your target, so anything that appears in your master's ./www will also appear in your VM's /var/www and vice-versa.

How can I set permissions for Magento using a Vagrant shared folder?

I am trying to install a Magento instance using vagrant. I used the MageVagrant repository found here, then modified it so that the default installtion directory on the vagrant machine points to a folder in the /vagrant folder, which is the shared folder (I did this so that I could edit the Magento files on my local machine and store them in version control), so that on my vagrant machine I have a symlink that looks like the following:
/srv/www/magento.localhost.com -> /vagrant/magento.localhost.com
When I try to run the Magento installer, it is fine until I get to the Configuration step of installation, and then I get the following error message:
Path "/srv/www/magento.localhost.com/public_html/app/etc" must be writable.
Path "/srv/www/magento.localhost.com/public_html/var" must be writable.
Path "/srv/www/magento.localhost.com/public_html/media" must be writable.
even though I have run chmod 777 -R on the directories both on my local machine and on the vagrant box. However, it still thinks it is not writable. How can I make these directories writable for the vagrant server?
By default it makes the mounted folder owned by the vagrant user and group, so you could make the web server you are using have permissions for that, or the way I do it is to add another shared folder and explicitly set the permissions on that share to how I want.
Line in my VagrantFile:
config.vm.synced_folder "../project_folder", "/var/www/", owner: "apache", group: "apache"
this will mount the folder "project_folder" which is one level above my vagrant directory to the location "/var/www" inside the virtual machine with the ownership of apache:apache.
This should work.
If you are hosting multiple sites inside the vm I suggest setting this shared folder as the parent of all the sites.

Resources