有没有办法检查 Apache 配置文件中是否存在目录?

发布于 2024-09-19 18:29:46 字数 112 浏览 3 评论 0 原文

有没有办法根据目录是否存在在 Apache 中包含配置设置?基本上,我有一个便携式硬盘,可以在工作和家庭之间携带,其中有一些我正在开发的东西。我只希望 Apache 配置加载特定的虚拟主机(如果该文件夹存在)。

Is there a way to include configuration settings in Apache based on if a directory exists? Basically I have a portable hard drive that I transport between work and home that has some stuff I'm developing on it. I only want the Apache config to load a particular virtual host if the folder exists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌梦深 2024-09-26 18:29:47

从 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.

还不是爱你 2024-09-26 18:29:47

不,似乎没有直接的方法可以做到这一点。

唯一可能的解决方案是 IfDefine 指令。您可以使用 -d 参数来定义服务器启动的时间。

参数名称参数是在服务器启动时通过 -Dparameter- 在 httpd 命令行上给出的定义。

您也许可以检查批处理或 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.

The parameter-name argument is a define as given on the httpd command line via -Dparameter-, at the time the server was 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.

凡尘雨 2024-09-26 18:29:47

我提出了一个似乎适用于 Linux 和 OS X 的解决方案,它取决于“挂载点”。也可以在 Windows 中模拟它,但您可能必须使用 FUSE 和/或 Cygwin 发挥创意。

如果您在主目录中创建一个空文件夹,例如“/Users/username/ExtraVhosts”,则可以将 apache 指令添加到“Include /Users/username/ExtraVhosts/*”。

然后,当您插入拇指驱动器时,您可以安装在某个位置,然后使用安装点“绑定”将 ExtraVhosts 文件夹交叉链接到移动设备上的文件夹。

一个 OS X 示例:

  • 我有一个名为“Cherrybomb”的拇指驱动器
  • ,当我插入它时,它总是安装到 /Volumes/Cherrybomb,
  • 然后我可以使用 bindfs (sudo port install bindfs) 来安装它的子文件夹,如下所示:
    • sudo bindfs /Volumes/Cherrybomb/Projects/vhosts /Users/用户名/ExtraVhosts
  • 然后我可以重新启动 apache 以读取更新的配置:
    • sudo /opt/local/apache2/bin/apachectl restart

此时,只需在 /etc/hosts 中添加条目即可获取服务器别名。

Linux 等效项将使用 mount 命令的“--bind”参数。

需要注意的是:这使得快速卸载 USB 驱动器变得困难,因为它总是被 apache 标记为“正在使用”。以下是删除过程:

  • 关闭所有打开的文件和使用该驱动器的终端会话(终端中的当前工作目录可能会导致卸载问题)
  • 停止 apache: sudo /opt/local/apache2/bin/apachectl stop
  • umount /Users/ username/ExtraVhosts

然后您可以以图形方式或手动卸载它(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:

  • I have a thumb drive called 'Cherrybomb'
  • When I insert it, it always gets mounted to /Volumes/Cherrybomb
  • I can then use bindfs (sudo port install bindfs) to mount a subfolder of it, like so:
    • sudo bindfs /Volumes/Cherrybomb/Projects/vhosts /Users/username/ExtraVhosts
  • Then I can restart apache to read in the updated configuration:
    • sudo /opt/local/apache2/bin/apachectl restart

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:

  • Close all open files and terminal sessions that are using the drive (the present-working-directory in terminal can cause unmount issues)
  • Stop apache: sudo /opt/local/apache2/bin/apachectl stop
  • umount /Users/username/ExtraVhosts

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 :)

执手闯天涯 2024-09-26 18:29:47

如果您仅将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文