简体   繁体   English

Apache2 - 基于子目录设置文档根目录

[英]Apache2 - Set Document Root based on subdirectory

I have a default Virtual Host.我有一个默认的虚拟主机。 It is configured to watch Document Root inside /var/www/html and works great.它被配置为在/var/www/html观看 Document Root 并且效果很好。 But I have a problem.但我有一个问题。 I have multiple sites inside subdirectories and I don't know how to set Document Root for each of them.我在子目录中有多个站点,我不知道如何为每个站点设置文档根目录。 For example, /var/www/html/test , /var/www/html/test2 , etc. When I include file from /var/www/html/test , it searches inside var/www/html .例如, /var/www/html/test/var/www/html/test2等。当我包含来自/var/www/html/test ,它会在var/www/html搜索。 For example, I include file like include_once '/core.php' , but I have an error Failed opening required '/var/www/html/core.php' , because there is no current file here.例如,我包含了像include_once '/core.php'这样的文件,但是我有一个错误Failed opening required '/var/www/html/core.php' ,因为这里没有当前文件。 Is it possible to set Document Root for each of directories inside main Virtual Host?是否可以为主虚拟主机内的每个目录设置文档根目录? Thank you!谢谢!

My main Virtual Host configuration:我的主要虚拟主机配置:

<VirtualHost *:80>
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    AccessFileName .htaccess

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Also, you can say, that I need to use relative paths, but it isn't possible in some ways, because, sometimes, I work with ready sites, created by another people.此外,您可以说,我需要使用相对路径,但在某些方面是不可能的,因为有时我会使用由其他人创建的现成网站。

Is it possible to set Document Root for each of directories inside main Virtual Host?是否可以为主虚拟主机内的每个目录设置文档根目录?

Short answer is no, you cannot.简短的回答是否定的,你不能。 1 Document Root per Virtual Host.每个虚拟主机 1 个文档根。 In order to understand how the things are working you need to understand what is the directives context .为了了解事情是如何工作的,您需要了解什么是指令上下文 Lets take as an example VirtualHost directive .让我们以VirtualHost 指令为例。 As we can see from the docs it has only 1 Context which is Server Config.正如我们从文档中看到的那样,它只有 1 个上下文,即服务器配置。 And server config context tells next:然后服务器配置上下文告诉接下来:

directive may be used in the server configuration files (eg, httpd.conf), but not within any VirtualHost or Directory containers.指令可以在服务器配置文件(例如,httpd.conf)中使用,但不能在任何 VirtualHost 或 Directory 容器中使用。 It is not allowed in .htaccess files at all. .htaccess 文件中根本不允许使用它。

But DoocumentRoot has two contexts: server config and virtual host.但是DoocumentRoot有两个上下文:服务器配置和虚拟主机。 Rest you should figure out for yourself ;)休息你应该自己弄清楚;)

There are plenty ways how you can achieve desired result有很多方法可以达到预期的效果

  1. Running several name-based web sites on a single IP address 在单个 IP 地址上运行多个基于名称的网站
  2. Running different sites on different ports 在不同的端口上运行不同的站点
  3. Mixed port-based and ip-based virtual hosts 基于端口和基于 ip 的混合虚拟主机

Hope that this answer will help you!希望这个回答能帮到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM