You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS下Vagrant Linux环境mkdir创建文件夹权限只读问题求助

Fixing Vagrant + macOS: Dynamic Folder Permissions for Web Uploads

Hey Darren, I’ve dealt with this exact headache before when building web apps with Vagrant on macOS—nothing kills momentum like uploads failing because dynamically created folders are read-only! Let’s walk through the most reliable fixes:

1. Tweak Vagrant Shared Folder Permissions Directly

The root issue often lies in how Vagrant syncs folders between your macOS host and Linux guest. By default, shared folders might inherit restrictive permissions or misalign with your web server’s user/group. Update your Vagrantfile to explicitly set permissions:

config.vm.synced_folder "./your-host-project-folder", "/vagrant",
  owner: "www-data",       # Match your web server user (Apache/Nginx use this by default)
  group: "www-data",
  mount_options: ["dmode=775", "fmode=664"]  # dmode = folder permissions, fmode = file permissions

After updating, run vagrant reload to apply the changes. This ensures any new folders created by your web server (via mkdir()) inherit the correct write permissions.

2. Adjust PHP’s Umask Setting

PHP’s default umask can override the permissions you specify in mkdir(). For example, if your umask is 022, calling mkdir($path, 0777) will actually create a folder with 0755 permissions—too restrictive for uploads.

  • Temporary fix in code: Set the umask right before creating the folder:
    umask(002); // Resets umask to allow group write access
    mkdir($uploadDir, 0775, true); // 0775 lets owner/group read/write, others read
    umask(022); // Reset back to default if needed
    
  • Permanent fix in php.ini: Edit your PHP config file (usually /etc/php/8.x/apache2/php.ini or similar) and set:
    umask = 002
    
    Don’t forget to restart your web server afterward (sudo systemctl restart apache2 or sudo systemctl restart nginx).

3. Update VirtualBox Guest Additions

Outdated Guest Additions can cause wonky permission sync between macOS and the Linux guest. Install the vagrant-vbguest plugin to keep them automatically updated:

vagrant plugin install vagrant-vbguest
vagrant reload

This fixes a lot of hidden filesystem sync bugs that lead to unexpected read-only issues.

4. Verify Parent Directory Permissions

Make sure the parent folder where you’re creating subfolders has write access for the web server user. Run this on your Linux guest:

sudo chown -R www-data:www-data /path/to/parent-upload-folder

You can even automate this in your Vagrantfile with a provision script so it runs every time you boot the VM:

config.vm.provision "shell", inline: <<-SHELL
  sudo chown -R www-data:www-data /path/to/parent-upload-folder
SHELL

Start with the Vagrantfile shared folder config—it’s the most common fix for this exact scenario. If that doesn’t do it, adjusting the umask should seal the deal.

内容的提问来源于stack exchange,提问作者Darren Cooper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:04:26