祖国繁荣富强 發表於 2023-6-3 00:00:00

如何配置apache虚拟主机的实例小结

<p>
        1、基于ip地址的虚拟主机</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code27686">
        <br>
        Listen 80<br>
        &lt;VirtualHost 172.20.30.40&gt;<br>
            DocumentRoot /home/httpd/html1<br>
            ServerName www.ok1.com<br>
            ErrorLog /usr/local/apache/logs/error1_log<br>
            CustomLog /usr/local/apache/logs/access1_log combined<br>
        &lt;/VirtualHost&gt;<br>
        &lt;VirtualHost 172.20.30.50&gt;<br>
            DocumentRoot /home/httpd/html2<br>
            ServerName www.ok2.com<br>
            ErrorLog /usr/local/apache/logs/error2_log<br>
            CustomLog /usr/local/apache/logs/access2_log combined<br>
        &lt;/VirtualHost&gt;</div>
<p>
         </p>
<p>
         2、基于IP 和多端口的虚拟主机配置</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code19400">
        <br>
        Listen 172.20.30.40:80<br>
        Listen 172.20.30.40:8080<br>
        Listen 172.20.30.50:80<br>
        Listen 172.20.30.50:8080<br><br>
        &lt;VirtualHost 172.20.30.40:80&gt;<br>
         DocumentRoot /www/example1-80<br>
         ServerName www.example1.com<br>
        &lt;/VirtualHost&gt;<br>
        &lt;VirtualHost 172.20.30.40:8080&gt;<br><br>
         DocumentRoot /www/example1-8080<br>
         ServerName www.example1.com<br>
        &lt;/VirtualHost&gt;<br>
        &lt;VirtualHost 172.20.30.50:80&gt;<br>
         DocumentRoot /www/example2-80<br>
         ServerName www.example1.org<br>
        &lt;/VirtualHost&gt;<br>
        &lt;VirtualHost 172.20.30.50:8080&gt;<br>
         DocumentRoot /www/example2-8080<br>
         ServerName www.example2.org<br>
        &lt;/VirtualHost&gt;</div>
<p>
         </p>
<p>
        3、单个IP 地址的服务器上基于域名的虚拟主机配置<br>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code97374">
        <br>
        # Ensure that Apache listens on port 80<br>
        Listen 80<br>
        # Listen for virtual host requests on all IP addresses<br>
        NameVirtualHost *:80<br>
        &lt;VirtualHost *:80&gt;<br>
        DocumentRoot /www/example1<br>
        ServerName www.example1.com<br>
        ServerAlias example1.com. *.example1.com<br>
        # Other directives here<br>
        &lt;/VirtualHost&gt;<br>
        &lt;VirtualHost *:80&gt;<br>
        DocumentRoot /www/example2<br>
        ServerName www.example2.org<br>
        # Other directives here<br>
        &lt;/VirtualHost&gt;</div>
<p>
        <br><br>
        如果您感觉上面的文章还不够详细可以看下下面的文章:<br><br>
        实验目标:在apache实现基于域名的虚拟主机<br>
        实验用的XAMPP版本为1.7.7,内含apache版本为2.2.21<br><br>
        实验前准备:</p>
<p>
         </p>
<p>
        1. 为了测试不同的域名,在Windows/System32/drivers/etc/下觅得hosts文件,在其中添加实验用的域名若干,如 -</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code36895">
        <br>
        127.0.0.1   test1.net<br>
        127.0.0.1   test2.net</div>
<p>
         </p>
<p>
        如此,则在浏览器中输入该俩域名时,Windows将其解析为127.0.0.1本地地址。即,在浏览器中访问localhost, test1.net, test2.net均可访问XAMPP的欢迎页。</p>
<p>
        2. 在apache目录下建立目录,以放置您不同的网站。为保护XAMPP原有的htdocs中的欢迎页内容,实验另外建立了与htdocs平级的htdocs1目录,在其下建立了test1.net, test2.net两个子目录用以放置实验用的网站。如下 -<br>
        apache/htdocs1/test1.net - 放置test1.net网站内容<br>
        apache/htdocs1/test2.net - 放置test2.net网站内容</p>
<p>
         在这两个目录中各新建hello world一网页 index.html,内容 -</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code18332">
        <br>
        &lt;HTML&gt;<br>
        &lt;HEAD&gt;&lt;/HEAD&gt;<br>
        &lt;BODY&gt;<br>
        &lt;H1&gt;hello~, 这是[对应的网站名,用以区别].net&lt;/H1&gt;&lt;/BODY&gt;<br>
        &lt;/HTML&gt; </div>
<p>
         </p>
<p>
        实验步骤:</p>
<p>
        1. 找到apache/conf/httpd.conf, 将其中的</p>
<p>
        ServerAdmin<br>
        ServerName<br>
        DocumentRoot<br>
        注释掉。<br><br>
        2. 在httpd.conf中,找到行</p>
<p>
         Include "conf/extra/httpd-vhosts.conf"<br>
        如被注释则解注。该文件记载了虚拟主机的参数。[以前虚拟主机参数是直接填写在httpd.conf中的,为了更好地组织文件,将其分离出去,类似于某些编程语言一样。因此httpd.conf中include它,即相当于把它的内容填在了httpd.conf中。]<br><br>
        3. 这个httpd-vhosts.conf文件格式基本如下 -</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code578">
        <br>
        #blah-blah<br>
        NameVirtualHost *:80<br>
        #blah-blah<br>
        #blah-blah<br>
        &lt;VirtualHost *:80&gt;<br>
            ServerAdmin XXXXXXXX<br>
            DocumentRoot "XXXXXXXX"<br>
            ServerName XXXXXXX<br>
            ServerAlias XXXXXX<br>
            ErrorLog "logs/XXXXXX-error.log"<br>
            CustomLog "logs/XXXXXXX-error.log" combined  <br>
        &lt;/VirtualHost&gt;</div>
<p>
         </p>
<p>
        需要修改的,就是&lt;VirtualHost&gt;中的参数了。这个可以参见apache官方文档。根据实验域名,可以增加两个&lt;VirtualHost&gt;:</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code27282">
        <br>
        &lt;VirtualHost *:80&gt;<br>
            ServerAdmin adm@test1.net<br>
            DocumentRoot "C:/xampp/htdocs1/test1.net"<br>
            ServerName test1.net<br>
            ServerAlias www.test1.net<br>
            ErrorLog "logs/test1-error.log"<br>
            CustomLog "logs/test1-access.log" combined<br><br>
            &lt;Directory "C:/xampp/htdocs1/test1.net"&gt;<br>
            order allow,deny<br>
            allow from all<br>
            &lt;/Directory&gt;   <br>
        &lt;/VirtualHost&gt;<br>
        &lt;VirtualHost *:80&gt;<br>
            ServerAdmin adm@test2.net<br>
            DocumentRoot "C:/xampp/htdocs1/test2.net"<br>
            ServerName test2.net<br>
            ServerAlias www.test2.net<br>
            ErrorLog "logs/test1-error.log"<br>
            CustomLog "logs/test1-access.log" combined<br><br>
            &lt;Directory "C:/xampp/htdocs1/test2.net"&gt;<br>
            order allow,deny<br>
            allow from all<br>
            &lt;/Directory&gt;   <br>
        &lt;/VirtualHost&gt;</div>
<p>
         </p>
<p>
        注意,如果不在各VirtualHost中定义Directory的可访问性,你将遇到的是Access Forbidden!就连原来的localhost也是。<br><br>
        4. 由于之前注释掉了httpd.conf中的ServerName, DocumentRoot等,为了仍然能以localhost访问原XAMPP欢迎页,就在增加一个VirtualHost,如下 -</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code47172">
        <br>
        &lt;VirtualHost *:80&gt;<br>
            ServerAdmin adm@localhost<br>
            DocumentRoot "C:/xampp/htdocs"<br>
            ServerName localhost<br><br>
            ErrorLog "logs/localhost-error.log"<br>
            CustomLog "logs/localhost-access.log" combined<br><br>
            &lt;Directory "C:/xampp/htdocs"&gt;<br>
            order allow,deny<br>
            allow from all<br>
            &lt;/Directory&gt;   <br>
        &lt;/VirtualHost&gt;</div>
<p>
         </p>
<p>
        为了避免出错,把它放置在第一个Virtualhost位置。<br><br>
        至此,apache基于域名的虚拟主机配置完成。可以通过http://localhost访问XAMPP欢迎页,通过http://test1.net和http://test2.net访问各自的主页。<br><br>
        #<br>
        # Virtual Hosts<br>
        #</p>
<p>
        # If you want to maintain multiple domains/hostnames on your<br>
        # machine you can setup VirtualHost containers for them. Most configurations<br>
        # use only name-based virtual hosts so the server doesn't need to worry about<br>
        # IP addresses. This is indicated by the asterisks in the directives below.<br>
        #<br>
        # Please see the documentation at<br>
        # &lt;URL:http://httpd.apache.org/docs/2.2/vhosts/&gt;<br>
        # for further details before you try to setup virtual hosts.<br>
        #<br>
        # You may use the command line option '-S' to verify your virtual host<br>
        # configuration.</p>
<p>
        #<br>
        # Use name-based virtual hosting.<br>
        #<br>
        NameVirtualHost *:80</p>
<p>
        #<br>
        # VirtualHost example:<br>
        # Almost any Apache directive may go into a VirtualHost container.<br>
        # The first VirtualHost section is used for all requests that do not<br>
        # match a ServerName or ServerAlias in any &lt;VirtualHost&gt; block.<br>
        #</p>
<p>
        &lt;VirtualHost *:80&gt;<br>
            ServerAdmin kongdaoxian@gmail.com<br>
            DocumentRoot "E:/skydao/apache2/htdocs"<br>
            ServerName localhost<br>
            ServerAlias www.skydao.com<br>
            ErrorLog "logs/localhost-error.log"<br>
            CustomLog "logs/localhost-access.log" combined<br><br>
            &lt;Directory "E:/skydao/apache2/htdocs"&gt;<br>
            order allow,deny<br>
            allow from all<br>
            &lt;/Directory&gt;   <br>
        &lt;/VirtualHost&gt;</p>
<p>
        &lt;VirtualHost *:80&gt;<br>
            ServerAdmin kongdaoxian@gmail.com<br>
            DocumentRoot "E:/skydao/apache2/htdocs/project1"<br>
            ServerName project1.com<br>
            ServerAlias www.project1.com<br>
            ErrorLog "logs/project1-error.log"<br>
            CustomLog "logs/project1-access.log" combined<br><br>
            &lt;Directory "E:/skydao/apache2/htdocs/project1"&gt;<br>
            order allow,deny<br>
            allow from all<br>
            &lt;/Directory&gt;   <br>
        &lt;/VirtualHost&gt;</p>
<p>
        &lt;VirtualHost *:80&gt;<br>
            ServerAdmin kongdaoxian@gmail.com<br>
            DocumentRoot "E:/skydao/apache2/htdocs/zendTest/public"<br>
            ServerName zendTest.com<br>
            ServerAlias www.zendTest.com<br>
            DirectoryIndex index.php<br>
            &lt;Directory /&gt;<br>
        Options FollowSymLinks<br>
        AllowOverride None<br>
        Order allow,deny<br>
        Allow from all<br>
            &lt;/Directory&gt;   <br>
        &lt;/VirtualHost&gt;</p>
<p>
        &lt;VirtualHost *:80&gt;<br>
            ServerAdmin kongdaoxian@gmail.com<br>
            DocumentRoot "E:/skydao/apache2/htdocs/testRewrite"<br>
            ServerName testRewrite.com<br>
            ServerAlias www.testRewrite.com<br>
            # DirectoryIndex index.php<br>
            &lt;Directory /&gt;<br>
        Options FollowSymLinks<br>
        AllowOverride None<br>
        Order allow,deny<br>
        Allow from all<br>
            &lt;/Directory&gt;   <br>
        &lt;/VirtualHost&gt;</p>
<p>
        &lt;VirtualHost *:80&gt;<br>
            ServerAdmin kongdaoxian@gmail.com<br>
            DocumentRoot "E:/skydao/apache2/htdocs/test"<br>
            ServerName test.com<br>
            ServerAlias www.test.com<br>
            ErrorLog "logs/zendTest-error.log"<br>
            CustomLog "logs/zendTest-access.log" combined<br><br>
            &lt;Directory "E:/skydao/apache2/htdocs/test"&gt;<br>
            order allow,deny<br>
            allow from all<br>
            &lt;/Directory&gt;   <br>
        &lt;/VirtualHost&gt;</p>
頁: [1]
查看完整版本: 如何配置apache虚拟主机的实例小结