Apache 本地配置以正确解析文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2603932/
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
Apache local configuration to resolve files correctly
提问by Alex E.
I have just configured Apache and PHP to work on my local Mac OS X computer. Now PHP works fine, except when I try to load the files for my live sites. The live sites have separate directories and are sorted by client name etc.
我刚刚将 Apache 和 PHP 配置为在我的本地 Mac OS X 计算机上工作。现在 PHP 工作正常,除非我尝试为我的实时站点加载文件。实时站点具有单独的目录,并按客户名称等排序。
I've created symlinks in the default root for the local web server documents. My issue is that Apache doesn't seem to want to load any of the relative paths that are found in the HTML pages. For example, I have src="/css/main.css" but Apache doesn't load the file, similarly for images, it just resolves as a file not found 404 error. I then thought it might be the symlinks so I copied the full directory into the Apache document root, and still had the same result.
我已经在本地 Web 服务器文档的默认根目录中创建了符号链接。我的问题是 Apache 似乎不想加载在 HTML 页面中找到的任何相对路径。例如,我有 src="/css/main.css" 但 Apache 不加载文件,与图像类似,它只是解析为文件未找到 404 错误。然后我认为这可能是符号链接,所以我将完整目录复制到 Apache 文档根目录中,但结果仍然相同。
I would really love to setup my local development environment to run Apache, PHP, MySQL to develop locally then publish when ready. I also tried the MAMP installation, and had the same issues.
我真的很想设置我的本地开发环境来运行 Apache、PHP、MySQL,以便在本地开发,然后在准备好后发布。我也尝试过 MAMP 安装,也遇到了同样的问题。
回答by Robert McIntyre
First you might want to try using src="./css/main.css".
首先,您可能想尝试使用 src="./css/main.css"。
When dealing with multiple live sites I like to setup a single configuration file for each site with apache and then load them all together in the httpd.conf file.
在处理多个实时站点时,我喜欢使用 apache 为每个站点设置一个配置文件,然后将它们一起加载到 httpd.conf 文件中。
for my setup it looks like this:
对于我的设置,它看起来像这样:
in /etc/apache2/httpd.conf
在 /etc/apache2/httpd.conf
I have:
我有:
# Begin virtual host directives.
Include conf/bortreb.conf
Include conf/rlmcintyre.conf
Include conf/laserkard.conf
Include conf/judyates.conf
and then in /etc/apache2/conf/judyates.conf
然后在 /etc/apache2/conf/judyates.conf
I have:
我有:
<VirtualHost *:80>
#localhost site
ServerAdmin [email protected]
DocumentRoot "/home/r/Desktop/web/judyates"
ServerName localhost
ServerAlias judyates.localhost
ErrorLog "/home/r/Desktop/web/judyates/log/error_log.log"
ScriptAlias /cgi-bin/ "/home/r/Desktop/web/judyates/cgi-bin/"
<Directory "/home/r/Desktop/web/judyates">
Options Indexes FollowSymLinks
Options +ExecCGI
AddHandler cgi-script cgi pl py
AllowOverride Options
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
#live site
ServerAdmin [email protected]
DocumentRoot "/home/r/Desktop/web/judyates"
ServerName judyates.com
ServerAlias *.judyates.com
ErrorLog "/home/r/Desktop/web/judyates/log/error_log.log"
ScriptAlias /cgi-bin/ "/home/r/Desktop/web/judyates/cgi-bin/"
<Directory "/home/r/Desktop/web/judyates">
Options Indexes FollowSymLinks
Options +ExecCGI
AddHandler cgi-script cgi pl py
AllowOverride Options
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This way works really well, because you can set the subdomain yoursite.localhost to loop back to your home ip address.
这种方式非常有效,因为您可以将子域 yoursite.localhost 设置为循环回您的家庭 IP 地址。
With this setup, when I work on judyates.com on my computer and want to test anythig, I just go to judyates.localhost in my web browser.
通过此设置,当我在计算机上使用 judyates.com 并想要测试任何内容时,我只需在 Web 浏览器中访问 judyates.localhost。
I have about 5 other sites all set up this way in their own *.conf file, so they can each live in their own directories on my computer that exactly match the directories they'll be in on the server.
我有大约 5 个其他站点都在它们自己的 *.conf 文件中以这种方式设置,因此它们每个都可以位于我计算机上的自己的目录中,这些目录与它们将在服务器上的目录完全匹配。
The key is to use virtual hosts to go to different sites based on the subdomain.
关键是使用虚拟主机根据子域去不同的站点。
You can learn how to configure subdomains that point to yourself here: http://digitalpbk.blogspot.com/2007/01/making-subdomains-on-localhost.html
您可以在此处了解如何配置指向您自己的子域:http: //digitalpbk.blogspot.com/2007/01/making-subdomains-on-localhost.html
My setup goes even one step further because I setup the server too. Whenever I want to update I load both the webfiles AND the apache config files, and that way the server exactly mirrors my local setup. The only difference is that the real judyates.com points to the server and not my home computer, so when people try to visit the site they get everything from the server.
我的设置更进一步,因为我也设置了服务器。每当我想更新时,我都会加载 web 文件和 apache 配置文件,这样服务器就会准确地反映我的本地设置。唯一的区别是真正的 judyates.com 指向服务器而不是我的家用计算机,因此当人们尝试访问该站点时,他们会从服务器获取所有内容。
回答by Tagore Smith
Have you tried src="css/main.css"? That is, without the leading slash? If you have a leading slash there your files would have to be in a directory named css that was in the root directory of the webserver, and if I understand you correctly that's not the case.
你试过 src="css/main.css" 吗?也就是说,没有前导斜线?如果您在那里有一个前导斜杠,则您的文件必须位于网络服务器根目录中名为 css 的目录中,如果我理解正确,情况并非如此。
EDIT: OK, from reading your comments it seems like you are not quite clear on how relative urls work. "/css/main.css" is not relative to the page's location in the directory tree. It means a file named "main.css" in a directory named "css" in the root directory of the webserver. When you put your files on the deployment server your css directory isat the webserver's root directory. But it sounds like you are currently putting the css directory in a subdirectory named for the client... so your css file is now living at "/clientname/css/main.css".
编辑:好的,从阅读您的评论来看,您似乎不太清楚相对网址的工作原理。“/css/main.css”与页面在目录树中的位置无关。它的意思是在web服务器根目录下一个名为“css”的目录中的一个名为“main.css”的文件。当你把部署服务器上的文件你的CSS目录是在Web服务器的根目录。但听起来您目前正在将 css 目录放在为客户端命名的子目录中...所以您的 css 文件现在位于“/clientname/css/main.css”。
If I understand you correctly, you can do what you want by using relative urls. If your html file is in the same directory as the css directory you would need "css/main.css". If it's in a subdirectory of the directory that contains the css directory you would need "../css/main.css"- the ".." means the parent directory of the current directory. If you use relative urls they will continue to work as long as the relationship between the files doesn't change.
如果我理解正确,您可以使用相对网址来做您想做的事。如果您的 html 文件与 css 目录在同一目录中,您将需要“css/main.css”。如果它位于包含 css 目录的目录的子目录中,您将需要“../css/main.css”——“..”表示当前目录的父目录。如果您使用相对 url,只要文件之间的关系没有改变,它们就会继续工作。
Here's a page on the subject that explains it adequately, I think: http://www.webreference.com/html/tutorial2/3.html. Was pretty much the first thing I found in Google though, so there are likely better explanations out there.
这是一个关于该主题的页面,我认为它充分解释了它:http: //www.webreference.com/html/tutorial2/3.html。不过,这几乎是我在 Google 中发现的第一件事,因此可能有更好的解释。
There are a number of Apache directives that you could use to do this, but if using relative urls would work for you (and if I understand you correctly it would) then that's likely to be a lot simpler and less likely to cause you further trouble.
您可以使用许多 Apache 指令来执行此操作,但是如果使用相对 url 对您有用(如果我理解正确的话),那么这可能会简单得多,而且不太可能给您带来更多麻烦.

