php 在 htdocs 之外制作 XAMPP/Apache 服务文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make XAMPP/Apache serve file outside of htdocs
提问by cmcculloh
Is it possible to configure xamppto serve up a file outside of the htdocsdirectory?
是否可以配置xampp以提供htdocs目录外的文件?
For instance, say I have a file located as follows:
例如,假设我有一个文件如下:
C:\projects\transitCalculator\trunk\TransitCalculator.php
C:\projects\transitCalculator\trunk\TransitCalculator.php
and my xamppfiles are normally served out from:
我的xampp文件通常来自:
C:\xampp\htdocs\
C:\xampp\htdocs\
(because that's the default configuration) Is there some way to make Apache recognize and serve up my TransitCalculator.phpfile without moving it under htdocs? Preferably I'd like Apache to serve up/have access to the entire contents of the projects directory, and I don't want to move the projects directory under htdocs.
(因为这是默认配置)有什么方法可以让 Apache 识别并提供我的TransitCalculator.php文件而不将它移到 下htdocs?最好我希望 Apache 提供/访问项目目录的全部内容,并且我不想将项目目录移动到htdocs.
edit: edited to add Apache to the question title to make Q/A more "searchable"
编辑:编辑以将 Apache 添加到问题标题中,使 Q/A 更“可搜索”
回答by cmcculloh
Ok, per pix0r's, Sparks' and Dave's answers it looks like there are three ways to do this:
好的,根据pix0r、Sparks和Dave的回答,似乎有三种方法可以做到这一点:
Virtual Hosts
虚拟主机
- Open C:\xampp\apache\conf\extra\httpd-vhosts.conf.
- Un-comment ~line 19 (
NameVirtualHost *:80). 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>Open your hosts file (C:\Windows\System32\drivers\etc\hosts).
Add
127.0.0.1 transitcalculator.localhost #transitCalculatorto the end of the file (before the Spybot - Search & Destroy stuff if you have that installed).
- 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).
- Restart Apache.
- 打开 C:\xampp\apache\conf\extra\httpd-vhosts.conf。
- 取消注释~第 19 行 (
NameVirtualHost *:80)。 添加您的虚拟主机(~第 36 行):
<VirtualHost *:80> DocumentRoot C:\Projects\transitCalculator\trunk ServerName transitcalculator.localhost <Directory C:\Projects\transitCalculator\trunk> Order allow,deny Allow from all </Directory> </VirtualHost>打开您的主机文件 (C:\Windows\System32\drivers\etc\hosts)。
添加
127.0.0.1 transitcalculator.localhost #transitCalculator到文件末尾(在 Spybot 之前 - 搜索和销毁东西,如果你安装了它)。
- 保存(您可能必须将其保存到桌面,更改旧主机文件的权限(右键单击 > 属性),然后将新文件复制到旧文件的目录中(或重命名旧文件)(如果您正在使用) Vista和有麻烦)。
- 重新启动阿帕奇。
Now you can access that directory by browsing to http://transitcalculator.localhost/.
现在您可以通过浏览到http://transitcalculator.localhost/来访问该目录。
Make an Alias
创建别名
Starting ~line 200 of your
http.conffile, copy everything between<Directory "C:/xampp/htdocs">and</Directory>(~line 232) and paste it immediately below withC:/xampp/htdocsreplaced with your desired directory (in this caseC:/Projects) to give your server the correct permissions for the new directory.Find the
<IfModule alias_module></IfModule>section (~line 300) and addAlias /transitCalculator "C:/Projects/transitCalculator/trunk"(or whatever is relevant to your desires) below the
Aliascomment block, inside the module tags.
从
http.conf文件的~第 200 行开始,复制<Directory "C:/xampp/htdocs">和</Directory>(~第 232 行)之间的所有内容并将其粘贴到下面,并C:/xampp/htdocs替换为所需的目录(在本例中为C:/Projects),以便为服务器提供新目录的正确权限。找到
<IfModule alias_module></IfModule>部分(~第 300 行)并添加Alias /transitCalculator "C:/Projects/transitCalculator/trunk"(或任何与您的愿望相关的东西)在
Alias评论块下方,在模块标签内。
Change your document root
更改您的文档根目录
Edit ~line 176 in C:\xampp\apache\conf\httpd.conf; change
DocumentRoot "C:/xampp/htdocs"to#DocumentRoot "C:/Projects"(or whatever you want).Edit ~line 203 to match your new location (in this case
C:/Projects).
在 C:\xampp\apache\conf\httpd.conf 中编辑 ~line 176;更改
DocumentRoot "C:/xampp/htdocs"为#DocumentRoot "C:/Projects"(或任何您想要的)。编辑 ~line 203 以匹配您的新位置(在本例中
C:/Projects)。
Notes:
笔记:
- You have to use forward slashes "/" instead of back slashes "\".
- Don't include the trailing "/" at the end.
- restart your server.
- 您必须使用正斜杠“/”而不是反斜杠“\”。
- 不要在末尾包含尾随的“/”。
- 重新启动您的服务器。
回答by Dave Ward
You can relocate it by editing the DocumentRootsetting in XAMPP\apache\conf\httpd.conf.
您可以通过编辑XAMPP\apache\conf\httpd.conf 中的DocumentRoot设置来重新定位它。
It should currently be:
目前应该是:
C:/xampp/htdocs
C:/xampp/htdocs
Change it to:
将其更改为:
C:/projects/transitCalculator/trunk
C:/projects/transitCalculator/trunk
回答by pix0r
A VirtualHost would also work for this and may work better for you as you can host several projects without the need for subdirectories. Here's how you do it:
VirtualHost 也适用于此,并且可能更适合您,因为您可以托管多个项目而无需子目录。这是你如何做到的:
httpd.conf (or extra\httpd-vhosts.conf relative to httpd.conf. Trailing slashes "\" might cause it not to work):
httpd.conf(或相对于 httpd.conf 的额外\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 file (c:\windows\system32\drivers\etc\hosts usually):
HOSTS 文件(通常为 c:\windows\system32\drivers\etc\hosts):
# localhost entries
127.0.0.1 localhost transitcalculator.localhost
Now restart XAMPP and you should be able to access http://transitcalculator.localhost/and it will map straight to that directory.
现在重新启动 XAMPP,您应该能够访问http://transitcalculator.localhost/,它将直接映射到该目录。
This can be helpful if you're trying to replicate a production environment where you're developing a site that will sit on the root of a domain name. You can, for example, point to files with absolute paths that will carry over to the server:
如果您尝试复制一个生产环境,在该环境中您正在开发一个位于域名根目录上的站点,这会很有帮助。例如,您可以指向具有将转移到服务器的绝对路径的文件:
<img src="/images/logo.png" alt="My Logo" />
whereas in an environment using aliases or subdirectories, you'd need keep track of exactly where the "images" directory was relative to the current file.
而在使用别名或子目录的环境中,您需要准确跟踪“图像”目录相对于当前文件的位置。
回答by sparkes
You can set Apache to serve pages from anywhere with any restrictions but it's normally distributed in a more secure form.
您可以将 Apache 设置为从任何地方提供任何限制的页面,但它通常以更安全的形式分发。
Editing your apache files (http.conf is one of the more common names) will allow you to set any folder so it appears in your webroot.
编辑您的 apache 文件(http.conf 是更常见的名称之一)将允许您设置任何文件夹,使其出现在您的 webroot 中。
EDIT:
编辑:
alias myapp c:\myapp\
别名 myapp c:\myapp\
I've edited my answer to include the format for creating an alias in the http.conf file which is sort of like a shortcut in windows or a symlink under un*x where Apache 'pretends' a folder is in the webroot. This is probably going to be more useful to you in the long term.
我已经编辑了我的答案,以包含在 http.conf 文件中创建别名的格式,这有点像 Windows 中的快捷方式或 un*x 下的符号链接,其中 Apache“假装”一个文件夹位于 webroot 中。从长远来看,这可能对您更有用。
回答by Jason
If you're trying to get XAMPP to use a network drive as your document root you have to use UNC paths in httpd.conf. XAMPP will not recognize your mapped network drives.
如果您试图让 XAMPP 使用网络驱动器作为您的文档根目录,您必须在 httpd.conf 中使用 UNC 路径。XAMPP 将无法识别您映射的网络驱动器。
For example the following won't work, DocumentRoot "X:/webroot"
例如以下将不起作用,DocumentRoot "X:/webroot"
But this will, DocumentRoot "//192.168.10.100/webroot" (note the forward slashes, not back slashes)
但这会, DocumentRoot "//192.168.10.100/webroot" (注意正斜杠,而不是反斜杠)
回答by Jason
Solution to allow Apache 2 to host websites outside of htdocs:
允许 Apache 2 在 htdocs 之外托管网站的解决方案:
Underneath the "DocumentRoot" directive in httpd.conf, you should see a directory block. Replace this directory block with:
在 httpd.conf 中的“DocumentRoot”指令下面,您应该看到一个目录块。将此目录块替换为:
<Directory />
Options FollowSymLinks
AllowOverride All
Allow from all
</Directory>
REMEMBER NOT TO USE THIS CONFIGURATION IN A REAL ENVIRONMENT
切记不要在真实环境中使用此配置

