在Apache中配置基于主机名的虚拟主机
在同一台pc(ip:192.168.1.1)中配置三个虚拟主机:
www.1.com
www.2.com
www.3.com
访问不同的虚拟主机,可以显示不同的页面。
注意:以下的设置全部在Apache服务器上!!
1.
打开/etc/hosts,加入如下几句:
192.168.1.1 www.1.com
192.168.1.1 www.2.com
192.168.1.1 www.3.com
2.
为每个虚拟主机增加一个web目录:
比如我在/usr/local/apache2/htdocs/下新建了三个文件夹,分别对应三个虚拟主机:
文件夹1 --> www.1.com
文件夹2 --> www.2.com
文件夹3 --> www.3.com
然后在这三个目录中各自新建一个测试的index.php,只要不一样就行了:
1:
<html>
<body>
<?php echo "You are in 1"; ?>
</body>
</html>
2:
<html>
<body>
<?php echo "You are in 2"; ?>
</body>
</html>
3:
<html>
<body>
<?php echo "You are in 3"; ?>
</body>
</html>
3.
编辑apache的httpd.conf配置文件,我的是/usr/local/apache2/conf/httpd.conf,找到如下语句:
# Virtual hosts,把它下面的那一句前的#去掉(即取消注释)
然后打开它提示的XXX/extra/httpd-vhosts.conf配置文件,修改成如下所示:
#
# Virtual Hosts # # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd./docs/2.2/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any <VirtualHost> block. # <VirtualHost *:80> ServerName * DocumentRoot "/usr/local/apache2/htdocs" </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@www.1.com DocumentRoot "/usr/local/apache2/htdocs/1" ServerName www.1.com ErrorLog "logs/www.1.com-error_log" CustomLog "logs/www.1.com-access_log" common <Directory "/usr/local/apache2/htdocs/1"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:80>
ServerAdmin webmaster@www.2.com DocumentRoot "/usr/local/apache2/htdocs/2" ServerName www.2.com ErrorLog "logs/www.2.com-error_log" CustomLog "logs/www.2.com-access_log" common <Directory "/usr/local/apache2/htdocs/2"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@www.3.com DocumentRoot "/usr/local/apache2/htdocs/3" ServerName www.3.com ErrorLog "logs/www.3.com-error_log" CustomLog "logs/www.3.com-access_log" common <Directory "/usr/local/apache2/htdocs/3"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> 其实就是保留 NameVirtualHost *:80 这一句,然后把剩下的删除,再加上最后的4段,其中后三段是完全类似的,而第一段是必须要写的!
到这里,我们已经可以在服务器本地访问上述网站了:
在Apache服务器的浏览器中输入:www.1.com/index.php, www.2.com/index.php, www.3.com/index.php 可以依次访问上述的三个不同的网站。
下面来配置一下如何在局域网内访问:
有两种方法:
1.
如果局域网里已经有了DNS服务器了,那么只要打开本地的 /etc/resolv.conf 文件,然后加入那个DNS服务器的ip就可以了,比如DNS的ip是192.168.1.100,那么加入: nameserver 192.168.1.100(不过那个DNS里要有对应的记录,没有的话在那个DNS里添上就可以了)
2.
如果局域网里没有DNS服务器,那么我们也可以直接在本地的host里手动加入,就像我们之前在Apache服务器上做的:
打开本地的 /etc/hosts 文件,加入如下几句:
192.168.1.1 www.1.com
192.168.1.1 www.2.com
192.168.1.1 www.3.com
就可以了!在本地的浏览器中输入:www.1.com/index.php, www.2.com/index.php, www.3.com/index.php 同样可以依次访问在Apache上的上述的三个不同的网站。
完成!
|
|