有没有办法检查 Apache 配置文件中是否存在目录?
有没有办法根据目录是否存在在 Apache 中包含配置设置?基本上,我有一个便携式硬盘,可以在工作和家庭之间携带,其中有一些我正在开发的东西。我只希望 Apache 配置加载特定的虚拟主机(如果该文件夹存在)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 Apache 2.4.34 开始,您现在可以使用
...
来检查文件是否存在。有关
< 的更多详细信息/a> 页面。Since Apache 2.4.34 you can now use
<IfFile>...</IfFile>
which will check to see if a file exists. There's more details on the<IfFile>
page.不,似乎没有直接的方法可以做到这一点。
唯一可能的解决方案是
IfDefine
指令。您可以使用-d
参数来定义服务器启动的时间。您也许可以检查批处理或 bash 文件中是否存在目录,并相应地设置
-d
参数。这是否是一个选项,将取决于您的服务器如何从便携式硬盘启动。
No, there seems to be no direct way to do this.
The only thing that might be a solution is the
IfDefine
directive. You can define defines using the-d
parameter to when the server is started.You might be able to check for the existence of a directory in a batch or bash file, and set the
-d
parameter accordingly.Whether that is an option, will depend on how your server is started from the portable hard drive.
我提出了一个似乎适用于 Linux 和 OS X 的解决方案,它取决于“挂载点”。也可以在 Windows 中模拟它,但您可能必须使用 FUSE 和/或 Cygwin 发挥创意。
如果您在主目录中创建一个空文件夹,例如“/Users/username/ExtraVhosts”,则可以将 apache 指令添加到“Include /Users/username/ExtraVhosts/*”。
然后,当您插入拇指驱动器时,您可以安装在某个位置,然后使用安装点“绑定”将 ExtraVhosts 文件夹交叉链接到移动设备上的文件夹。
一个 OS X 示例:
此时,只需在 /etc/hosts 中添加条目即可获取服务器别名。
Linux 等效项将使用 mount 命令的“--bind”参数。
需要注意的是:这使得快速卸载 USB 驱动器变得困难,因为它总是被 apache 标记为“正在使用”。以下是删除过程:
然后您可以以图形方式或手动卸载它(umount /Volumes/Cherrybomb)。
如果您的工作计算机和家用计算机将驱动器安装到不同的位置,您可能有多个虚拟主机文件夹 - home_vhost、work_vhost 等 - 并在绑定步骤中使用它们。
我希望这可以帮助别人:)
I've come up with a solution that seems to work for Linux and OS X, and it hinges on "mountpoints". It might be possible to emulate it within Windows, as well, but you would probably have to get creative with FUSE and/or Cygwin.
If you create an empty folder in your home directory, such as "/Users/username/ExtraVhosts", you can add an apache directive to "Include /Users/username/ExtraVhosts/*".
Then, when you insert your thumb drive, you can mount somewhere and then use mountpoint "binding" to cross-link the ExtraVhosts folder to a folder on the mobile device.
An OS X example:
At that point, it's just a matter of adding entries in /etc/hosts for server aliases to get picked up.
The linux equivalent would be using the "--bind" parameter of the mount command.
One caveat: This makes it difficult to quickly unmount the USB drive, since it is always marked as "in use" by apache. Here's a removal procedure:
Then you can either unmount it graphically or manually (umount /Volumes/Cherrybomb).
If your work and home machines mount the drive to different locations, you could have multiple vhosts folders - home_vhost, work_vhost, etc - and use that in the binding step.
I hope this helps someone out :)
如果您仅将 apache 指向安装点,那么就不会有问题。只是不要将目录指令指向驱动器内的目录。
例如,如果您挂载 /dev/somedisk /mnt/somevhost,则
无论您是否安装了驱动器,/mnt/somevhost 目录都会存在,并且 apache 都会启动。 Apache 不关心目录是否为空,因此如果未安装驱动器,
不会导致服务器无法启动。与 UNIX 一起工作,而不是反对它:-p 这个解决方案对于开发来说应该足够了。
If you point apache to the mountpoint only there shouldn't be an issue. Just don't point Directory directives to directories within the drive.
eg, if you mount /dev/somedisk /mnt/somevhost, the
/mnt/somevhost directory will be there whether or not you have the drive mounted and apache will start. Apache doesn't care if the directory is empty so a
<Directory "/mnt/somevhost"/>
won't cause server to not start if the drive isn't mounted.Work with UNIX not against it :-p This solution should be sufficient for development.