让XAMPP / Apache在htdocs文件夹之外提供文件

330

是否可以配置 xampp 来提供位于 htdocs 目录之外的文件?

例如,假设我有一个文件位于以下位置:

C:\projects\transitCalculator\trunk\TransitCalculator.php

并且我的 xampp 文件通常是从以下位置提供服务的:

C:\xampp\htdocs\

(因为这是默认配置) 是否有一种方法可以使 Apache 认识并提供我的 TransitCalculator.php 文件,而不将其移动到 htdocs 下面?最好是让 Apache 提供/访问项目目录的全部内容,而不想将项目目录移动到 htdocs 下。

编辑:编辑问题标题以使 Q/A 更容易被搜索到。


1
标签应该足够可搜索,我认为。 - icedwater
18
安德鲁·科珀,我同意你的观点,但在Stack Exchange生态系统中,由于普遍存在的强硬不友好的水平,这并不令人意外。¯_(ツ)_/¯ - cmcculloh
也许有些偏题,但我想给个提示:有时候一个选项是将目录移动到你的“htdocs”文件夹中,例如如果你想在本地主机中打开Google Drive中的文件 ;) (http://stackoverflow.com/questions/41751330/install-google-drive-in-localhost-directory) - Gordova
在Ubuntu上,只需要一个符号链接即可。(https://brettclapper.wordpress.com/2012/07/06/how-to-create-a-symbolic-link-to-htdocs-in-xampp-on-ubuntu/) - M.C.
2
除了所有的答案,我想再补充一个。从 PHP 5.4.0 开始,你可以使用命令行进入项目根目录并执行 php -S <host>:<port> 命令。例如 php -S localhost:80 - radiantshaw
请考虑将您的主机文件更改为一个回环IP地址,例如:127.0.0.1 localhost,127.0.0.2 test.localhost,127.0.0.3 test2.localhost。 - Leandro Bardelli
6个回答

383

好的,根据pix0rSparksDave的回答,看起来有三种方法可以实现这个目标:


虚拟主机

  1. Open C:\xampp\apache\conf\extra\httpd-vhosts.conf.
  2. Un-comment ~line 19 (NameVirtualHost *:80).
  3. Add your virtual host (~line 36):

    <VirtualHost *:80>
        DocumentRoot C:\Projects\transitCalculator\trunk
        ServerName transitcalculator.localhost
        <Directory C:\Projects\transitCalculator\trunk>
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    
  4. Open your hosts file (C:\Windows\System32\drivers\etc\hosts).

  5. Add

    127.0.0.1 transitcalculator.localhost #transitCalculator
    

    to the end of the file (before the Spybot - Search & Destroy stuff if you have that installed).

  6. Save (You might have to save it to the desktop, change the permissions on the old hosts file (right click > properties), and copy the new one into the directory over the old one (or rename the old one) if you are using Vista and have trouble).
  7. Restart Apache.
现在,您可以通过浏览http://transitcalculator.localhost/来访问该目录。

创建别名

  1. Starting ~line 200 of your http.conf file, copy everything between <Directory "C:/xampp/htdocs"> and </Directory> (~line 232) and paste it immediately below with C:/xampp/htdocs replaced with your desired directory (in this case C:/Projects) to give your server the correct permissions for the new directory.

  2. Find the <IfModule alias_module></IfModule> section (~line 300) and add

    Alias /transitCalculator "C:/Projects/transitCalculator/trunk"
    

    (or whatever is relevant to your desires) below the Alias comment block, inside the module tags.


更改文档根目录

  1. 编辑C:\xampp\apache\conf\httpd.conf文件的第176行;将DocumentRoot "C:/xampp/htdocs"更改为#DocumentRoot "C:/Projects"(或您想要的任何内容)。

  2. 编辑第203行以匹配您的新位置(在此示例中为C:/Projects)。


注意:

  • 必须使用斜杠"/"而不是反斜杠"\ "。
  • 不要包含末尾的"/"。
  • 重新启动您的服务器

14
第三步是编辑 C:\Windows\System32\drivers\etc\hosts,最好的做法是先以管理员身份运行编辑器(如Notepad,Notepad ++或其他编辑器)。这样你就可以直接保存到C:\Windows\System32\drivers\etc\文件夹中。 - CallMeLaNN
3
如果您遇到403错误,请参考此链接 - Wojciech Owczarczyk
25
在Win7上使用Order allow,deny Allow from all会返回403错误,因为这些选项已经被弃用。应该使用Require all granted。参考链接:https://dev59.com/wGox5IYBdhLWcg3wl1P4 - hywak
4
FYI,按照我的经验,这一步似乎不是必需的: 取消注释第19行(NameVirtualHost *:80)。 - Silverback
4
使用“Order allow,deny Allow from all”对我无效,但是“Require all granted”有效。 - Danny Beckett
显示剩余7条评论

102
你可以通过编辑XAMPP\apache\conf\httpd.conf中的DocumentRoot设置来重新定位它。
目前应该是:

C:/xampp/htdocs

将其更改为:

C:/projects/transitCalculator/trunk


41
别忘了在要求DocumentRoot的两行中都进行编辑。如果你只改变了顶部的那一行,你将会得到读取访问错误等问题。 - Arcolye
2
这也适用于XAMP Mac吗? - angry kiwi
现在可以通过哪个URL访问主页? - user2726660

52
一个VirtualHost也可以实现这个功能,并且对你来说可能更好,因为你可以不需要子目录来托管多个项目。以下是实现方法:
httpd.conf文件(或相对于httpd.conf的extra\httpd-vhosts.conf文件。末尾的反斜杠“\”可能会导致它不能正常工作):
NameVirtualHost *:80
# ...
<VirtualHost *:80>  
    DocumentRoot C:\projects\transitCalculator\trunk\
    ServerName transitcalculator.localhost
    <Directory C:\projects\transitCalculator\trunk\>  
        Order allow,deny  
        Allow from all  
    </Directory>
</VirtualHost> 

HOSTS 文件(通常位于 c:\windows\system32\drivers\etc\hosts):

# localhost entries
127.0.0.1 localhost transitcalculator.localhost

现在重新启动XAMPP,您应该能够访问http://transitcalculator.localhost/,并且它将直接映射到该目录。如果您尝试复制一个生产环境并在域名根目录上开发站点,则这可能会很有帮助。例如,您可以使用绝对路径指向文件,并将其传送到服务器上。
<img src="/images/logo.png" alt="My Logo" />

相比使用别名或子目录来管理文件,你需要准确地追踪与当前文件相对位置的"images"目录。


我在我的电脑上有几个网络驱动器,它们被映射到驱动器字母(例如 X:\myfolder)。当我使用这种方法时,我会收到“访问被禁止”的警告。这是否意味着Apache用户没有读取该驱动器的权限? - harryg
1
在“目录”下,将“Order allow,deny”和“Allow from all”替换为“Require all granted”。请参见https://dev59.com/wGox5IYBdhLWcg3wl1P4。 - Michiel

17

您可以设置Apache从任何地方提供页面,而不受任何限制,但通常以更安全的形式分发。

编辑您的Apache文件(http.conf是较常见的名称)将允许您设置任何文件夹,以便它出现在您的Web根目录中。

编辑:

别名myapp c:\ myapp \

我编辑了我的答案,以包括在http.conf文件中创建别名的格式,这有点像Windows中的快捷方式或un * x下的符号链接,其中Apache“假装”一个文件夹位于Web根目录中。从长远来看,这可能对您更有用。


11
如果您想将网络驱动器用作XAMPP的文档根目录,您需要在httpd.conf中使用UNC路径。 XAMPP将不会识别您映射的网络驱动器。
例如,以下内容不起作用: DocumentRoot“X:/ webroot”
但是,以下内容将起作用: DocumentRoot“//192.168.10.100/webroot”(请注意正斜杠而不是反斜杠)

它与我的映射网络驱动器很好地配合工作。 - Sean Kendle

9

让Apache 2托管位于htdocs之外的网站的解决方案:

在httpd.conf中的"DocumentRoot"指令下面,您应该看到一个目录块。请将此目录块替换为:

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Allow from all
</Directory> 

请勿在生产环境中使用此配置

1
仅适用于支持符号链接的文件系统。没有解释为什么不应使用此选项。 - Danubian Sailor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接