Apache2 是否支持子域的虚拟托管? [英] Does Apache2 support virtual hosting of subdomains?

查看:21
本文介绍了Apache2 是否支持子域的虚拟托管?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的 Apache 服务器是这样设置的

服务器名称 www.example.comServerAlias example.com文档根目录/var/www</虚拟主机>

问题是 /var/www 下的所有内容都可以从其他任何地方访问.如果我有一个网页 /var/www/john/bio.html,那么该网页可以从 var/www/jane/

我想像这样设置我的 Apache 服务器

服务器名称 www.example.comServerAlias example.com文档根目录/var/www</虚拟主机><虚拟主机 *:80>服务器名称 www.john.example.com服务器别名 john.example.comDocumentRoot/var/www/john</虚拟主机><虚拟主机 *:80>服务器名称 www.jane.example.comServerAlias jane.example.comDocumentRoot/var/www/jane</虚拟主机>

所以用户 john 的所有文件都放在 /var/www/john/ 文件夹中,用户 jane 也是如此.然后,关闭符号链接(默认情况下),并且仅从 /var/www/user/ 向下提供访问权限(再次默认情况下),我不必担心 john 的 网页,包括来自 jane's 网页的脚本/图像.

解决方案

仅使用本地措施(/etc/hosts 而不是 DNS)我发现这确实可行.

首先,更改您的 /etc/hosts 文件以具有所需网站名称的映射(www.example.com) 和目标 IP 地址 (192.168.1.1).我使用了我的本地 IP 地址.

 IPAddress 主机名别名----------- -------------------------- ------------------192.168.1.1 www.example.com example.com192.168.1.1 www.john.example.com john.example.com192.168.1.1 www.jane.example.com jane.example.com

您的网络浏览器将在查看万维网之前检查您的/etc/hosts 文件.

接下来检查所有的 Apache 配置文件(httpd.confapache2.confports.confconf.d/*) 并确保在恰好一个文件中发出命令NameVirtualHost *:80(它不必是端口:80 但如果多次发布,您将遇到这个问题).我的是在 /etc/apache2/ports.conf 中发布的,所以如果你需要,把你的放在那里.最后,像这样更新您的 Apache 配置文件(我的位于 /etc/apache2/sites-available/default).

服务器名称 www.example.comServerAlias example.com文档根目录/var/www</虚拟主机><虚拟主机 *:80>服务器名称 www.john.example.com服务器别名 john.example.comDocumentRoot/var/www/john</虚拟主机><虚拟主机 *:80>服务器名称 www.jane.example.comServerAlias jane.example.comDocumentRoot/var/www/jane</虚拟主机>

作为最后一步,您可能需要通过发出以下命令将网站添加到 Apache(如果您将所有网站都设置为 sites-available/default 而不是各个网站的单独文件).

# a2ensite www.example.com# a2ensite www.john.example.com# a2ensite www.jane.example.com

执行此操作后,john.example.com 将转到 /var/www/john.该目录将作为根目录,并且 john 将无法再访问 www,因此也无法访问 /var/www/简.

同样,执行此操作后,jane.example.com 将转到 /var/www/jane.该目录将作为根目录,并且 jane 将无法再访问 www,因此也无法访问 /var/www/约翰.

符号链接关闭--​​默认情况下--两个目录都不能相互访问

Currently my Apache server is set up like so

<VirtualHost *:80>
 ServerName www.example.com
 ServerAlias example.com
 DocumentRoot /var/www
</VirtualHost>

The problem is that everything below /var/www is accessible from everywhere else. If I have a web page /var/www/john/bio.html, then that web page could borrow scripts/pictures from var/www/jane/

I want to set up my Apache server like so

<VirtualHost *:80>
 ServerName www.example.com
 ServerAlias example.com
 DocumentRoot /var/www
</VirtualHost>

<VirtualHost *:80>
 ServerName www.john.example.com
 ServerAlias john.example.com
 DocumentRoot /var/www/john
</VirtualHost>

<VirtualHost *:80>
 ServerName www.jane.example.com
 ServerAlias jane.example.com
 DocumentRoot /var/www/jane
</VirtualHost>

So all the files for user john go in the /var/www/john/ folder, and likewise for user jane. Then, with symbolic links turned off (by default), and access only provided from /var/www/user/ downwards (again by default), I don't have to worry about john's web page including scripts/images from jane's web page.

解决方案

Using local measures only (/etc/hosts instead of a DNS) I found that this can indeed work.

First, change your /etc/hosts file to have a mapping of your desired website name(s) (www.example.com), and target IP address (192.168.1.1). I used my local IP address.

 IPAddress               Hostname                   Alias
-----------     --------------------------    ------------------
192.168.1.1     www.example.com               example.com 
192.168.1.1     www.john.example.com          john.example.com
192.168.1.1     www.jane.example.com          jane.example.com

Your web browser will check your /etc/hosts file before looking at the world wide web.

Next go through all your Apache config files (httpd.conf, apache2.conf, ports.conf, conf.d/*) and make sure in exactly one file the command NameVirtualHost *:80 is issued (it doesn't have to be port :80 but if it is issued more than once, you will get this problem). Mine was issued in /etc/apache2/ports.conf, so put yours there if you have to. Finally, update your Apache configuration file (mine was at /etc/apache2/sites-available/default) like so.

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www
</VirtualHost>

<VirtualHost *:80>
    ServerName www.john.example.com
    ServerAlias john.example.com
    DocumentRoot /var/www/john
</VirtualHost>

<VirtualHost *:80>
    ServerName www.jane.example.com
    ServerAlias jane.example.com
    DocumentRoot /var/www/jane
</VirtualHost>

As a final step, you may need to add the websites to Apache by issuing the below commands (this step is not necessary, if you give all websites into sites-available/default and not into separate files for individual websites).

# a2ensite www.example.com
# a2ensite www.john.example.com
# a2ensite www.jane.example.com

After doing this, john.example.com will go to /var/www/john. That directory will then act as the root directory, and john will no longer have access to www, and, therefore, have no access to /var/www/jane.

Likewise, after doing this, jane.example.com will go to /var/www/jane. That directory will then act as the root directory, and jane will no longer have access to www, and, therefore, have no access to /var/www/john.

With symbolic links turned off --by default-- neither directories will be able to access each other

这篇关于Apache2 是否支持子域的虚拟托管?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆