这两天开始学习Linux环境下配置Apache服务器,结合现在行业实况,我特地注意学习了虚拟主机的配置方法,我买的书上讲的方法是在httpd.conf文件中添加VirtualHost来实现的,经过实践也成功了,但是这个方法需要在配置后重启httpd服务才能生效,下面我就介绍一种无需重启Apache即可更改域名、网站根目录的方法。

注:本方法来源于网上,本人还没有青紫实践。

第一步
打开apache的配置文件httpd.conf,去掉LoadModule rewrite_module modules/mod_rewrite.so前面的#号。 

第二步
在配置文件尾部加上 
RewriteEngine on 
RewriteMap lowercase int:tolower 
#定义映像文件 
RewriteMap vhost txt:/apache/vhost/vhost.map 
#处理变名 
RewriteCond % !^/icons/ 
RewriteCond % !^/cgi-bin/ 
RewriteCond $} ^(.+)$ 
这里做基于文件的重新映射 
RewriteCond $ ^(/.*)$ 
RewriteRule ^/(.*)$ %1/ 
RewriteCond % ^/cgi-bin/ 
RewriteCond $} ^(.+)$ 
RewriteCond $ ^(/.*)$ 
RewriteRule ^/(.*)$ %1/cgi-bin/ 

注意:vhost.map文件是域名和空间映像的文件,格式如下
www.abc.com  /vhostusr/www.abc.com 

(你不会连/vhostuser/www.abc.com目录也没建吧,会报错的哟) 

第三步
保存配置文件,重启apache,以后建立域名时,只要在vhost.map文件里添加相应记录,并建立相应文件夹就OK了~~~ 

其实这个可以用php脚本来完成的,很简单的~~呵呵~~~,就像现在的虚拟主机提供商一样,允许用户自行绑定域名。

注意啊:php_admin_value open_basedir “path",这个选项要配好啊~~新手如果不懂的,就不要动这个了

======================================================

贴一个简单的vhost.map文件的操作代码。。。随意写的,根据你自己的实际情况稍加修改就可以实现在线管理域名绑定了。

<html> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 

<meta http-equiv="Content-Style-Type" content="text/css"> 

<link rel="top" href="index.cgi" title="" /> 

<link rel="search" href="search.php" title="" /> 

<link rel="help" href="faq.php" title="" /> 

<link rel="author" href="memberlist.php" title="" /> 

 

<title>注册 - youname.sun126.com</title> 

 

<style type="text/css"> 

<!-- 

body { 

        background-color: #FFFFFF; 

        scrollbar-face-color: #DEE3E7; 

        scrollbar-highlight-color: #FFFFFF; 

        scrollbar-shadow-color: #DEE3E7; 

        scrollbar-3dlight-color: #D1D7DC; 

        scrollbar-arrow-color:  #; 

        scrollbar-track-color: #EFEFEF; 

        scrollbar-darkshadow-color: #98AAB1; 

font,th,td,p { font-family: 'song',Verdana } 

a:link,a:active,a:visited { color : #; } 

a:hover                { text-decoration: underline; color : #DD6900; } 

hr        { height: 0px; border: solid #D1D7DC 0px; border-top-width: 1px;} 

.bodyline        { background-color: #FFFFFF; border: 1px #98AAB1 solid; } 

.forumline        { background-color: #000000; border: 0px #9898ba solid; } 

td.row1        { background-color: #f0f3fa; } 

td.row2        { background-color: #fafbfc; } 

td.row3        { background-color: #D1D7DC; } 

td.rowpic { 

                background-color: #D1D9E2; 

                background-repeat: repeat-y; 

th        { 

        color: #333333; font-size: 12px; font-weight : bold; 

        background-color: #9898BA; height: 25px; 

 

td.cat,td.catHead,td.catSides,td.catLeft,td.catRight,td.catBottom { 

                        background-color:#d1d9e2; border: #9898ba; border-style: solid; height: 28px; 

 

td.cat,td.catHead,td.catBottom { 

        height: 29px; 

        border-width: 0px 0px 0px 0px; 

th.thHead,th.thSides,th.thTop,th.thLeft,th.thRight,th.thBottom,th.thCornerL,th.thCornerR { 

        font-weight: bold; border: #FFFFFF; border-style: solid; height: 28px; 

td.row3Right,td.spaceRow { 

        background-color: #D1D7DC; border: #9898ba; border-style: solid; 

 

th.thHead,td.catHead { font-size: 13px; border-width: 1px 1px 0px 1px; } 

th.thSides,td.catSides,td.spaceRow         { border-width: 0px 1px 0px 1px; } 

th.thRight,td.catRight,td.row3Right         { border-width: 0px 1px 0px 0px; } 

th.thLeft,td.catLeft          { border-width: 0px 0px 0px 1px; } 

th.thBottom,td.catBottom  { border-width: 0px 1px 1px 1px; } 

th.thTop         { border-width: 1px 0px 0px 0px; } 

th.thCornerL { border-width: 1px 0px 0px 1px; } 

th.thCornerR { border-width: 1px 1px 0px 0px; } 

.maintitle        { 

        font-weight: bold; font-size: 22px; font-family: "'song',Verdana",'song',Verdana; 

        text-decoration: none; line-height : 120%; color : #; 

.gen { font-size : 13px; } 

.genmed { font-size : 12px; } 

.gensmall { font-size : 12px; } 

.gen,.genmed,.gensmall { color : #; } 

a.gen,a.genmed,a.gensmall { color: #; text-decoration: none; } 

a.gen:hover,a.genmed:hover,a.gensmall:hover        { color: #DD6900; text-decoration: underline; } 

 

/* The register, login, search etc links at the top of the page */ 

.mainmenu                { font-size : 12px; color : # } 

a.mainmenu                { text-decoration: none; color : #;  } 

a.mainmenu:hover{ text-decoration: underline; color : #DD6900; } 

 

/* Forum category titles */ 

.cattitle                { font-weight: bold; font-size: 13px ; letter-spacing: 1px; color : #} 

a.cattitle                { text-decoration: none; color : #; } 

a.cattitle:hover{ text-decoration: underline; } 

 

/* Forum title: Text and link to the forums used in: index.cgi */ 

.forumlink                { font-weight: bold; font-size: 13px; color : #; } 

a.forumlink         { text-decoration: none; color : #; } 

a.forumlink:hover{ text-decoration: underline; color : #DD6900; } 

 

/* Used for the navigation text, (Page 1,2,3 etc) and the navigation bar when in a forum */ 

.nav                        { font-weight: bold; font-size: 12px; color : #;} 

a.nav                        { text-decoration: none; color : #; } 

a.nav:hover                { text-decoration: underline; } 

 

/* titles for the topics: could specify viewed link colour too */ 

.topictitle       {font-size: 12px; color : #; } 

h1,h2       { font-weight: bold; font-size: 12px; color : #; } 

a.topictitle:link   { text-decoration: none; color : #; } 

a.topictitle:visited { text-decoration: none; color : #333333; } 

a.topictitle:hover        { text-decoration: underline; color : #DD6900; } 

 

/* Name of poster in viewmsg.php and viewtopic.php and other places */ 

.name                        { font-size : 12px; color : #;} 

 

/* Location, number of posts, post date etc */ 

.postdetails                { font-size : 12px; color : #; } 

 

/* The content of the posts (body of text) */ 

.postbody { font-size : 13px; line-height: 18px} 

a.postlink:link        { text-decoration: none; color : # } 

a.postlink:visited { text-decoration: none; color : #333333; } 

a.postlink:hover { text-decoration: underline; color : #DD6900} 

 

/* Quote & Code blocks */ 

.code { 

        font-family: 'song',Verdana; font-size: 12px; color: #006600; 

        background-color: #FAFAFA; border: #D1D7DC; border-style: solid; 

        border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px 

 

.quote { 

        font-family: 'song',Verdana; font-size: 12px; color: #444444; line-height: 125%; 

        background-color: #FAFAFA; border: #D1D7DC; border-style: solid; 

        border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px 

 

/* Copyright and bottom info */ 

.copyright                { font-size: 12px; font-family: 'song',Verdana; color: #444444; letter-spacing: -1px;} 

a.copyright                { color: #444444; text-decoration: none;} 

a.copyright:hover { color: #; text-decoration: underline;} 

 

/* Form elements */ 

input,textarea, select { 

        color : #; 

        font: normal 12px 'song',Verdana; 

        border-color : #; 

 

/* The text input fields background colour */ 

input.post, textarea.post, select { 

        background-color : #FFFFFF; 

 

input { text-indent : 2px; } 

 

/* The buttons used for bbCode styling in message post */ 

input.button { 

        background-color : #EFEFEF; 

        color : #; 

        font-size: 12px; font-family: 'song',Verdana; 

 

/* The main submit button option */ 

input.mainoption { 

        background-color : #FAFAFA; 

        font-weight : bold; 

 

/* None-bold submit button */ 

input.liteoption { 

        background-color : #FAFAFA; 

        font-weight : normal; 

--> 

</style> 

</head> 

<body  bgcolor="#FFFFFF" text="#" link="#" vlink="#333333" /> 

<a name="top"></a> 

 

<!--栏目条--> 

<style type=text/css> 

A.title1:link 

A.title1:visited 

A.title1:active 

A.title1:hover 

A.title2:link 

A.title2:visited 

A.title2:active 

A.title2:hover 

</style> 

<?php 

if (empty($HTTP_POST_VARS[reg])) { 

 

    ?> 

<form action="<?=$PHP_SELF?>" enctype="multipart/form-data" method="post"> 

<table border="0" cellpadding="3" cellspacing="1" width="95%" align=center class="forumline"> 

        <tr> 

                <th class="thHead" colspan="2" height="25" valign="middle">&nbsp;<a href=http://www.sun126.com>精彩奇讯科技虚拟主机自助申请系统</a> 请填写注册信息</th> 

        </tr> 

        <tr> 

                <td class="row2" colspan="2"><span class="gensmall">请输入在本服务器新增的域名:</span></td> 

        </tr> 

        <tr> 

                <td class="row1" width="20%"><span class="gen">域名名称:&nbsp;&nbsp;&nbsp;http://</span></td> 

                <td class="row2"><input type="text" class="post" style="width:100px" name="domain" size="10" maxlength="25"><span class="gen"><font color=red>.sun126.com</font></span></td> 

        </tr> 

                <tr> 

                <td  class="row1" colspan="2" align="center" height="28"> 

<input type="hidden" name="dateformat" value="Y-m-d H:m" maxlength="14" class="post" /> 

                <input type="hidden" name="reg" value="1" /><input type="submit" name="submit" value="发送" class="mainoption" />&nbsp;&nbsp;<input type="reset" value="重设" name="reset" class="liteoption" /></td> 

        </tr> 

</table> 

</form> 

</center> 

</body> 

</html> 

<?php } 

?> 

<?php 

 

 

if ($HTTP_POST_VARS[reg]) { 

for($i=0;$i<9;$i++){ 

$char1=chr(rand(97,122)); 

$chara.=$char1; 

    $domain = $HTTP_POST_VARS[domain] . ".sun126.com   /" . $HTTP_POST_VARS[domain]; 

    // 检查并创建目录开始******************************* 

    if (is_dir('d:/vhostuser/user_'.$chara.'_'.$HTTP_POST_VARS[domain])) { 

        $domain = "http://" . $domain; 

        echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"95%\" align=center class=\"forumline\"><tr> 

                <td class=\"row2\" colspan=\"2\"><span class=\"gensmall\">此域名已经使用,请更换~~<a href=$PHP_SELF>返回</a></span></td> 

        </tr> 

        <tr> 

                <td class=\"row2\" colspan=\"2\"><span class=\"gensmall\">&nbsp;&nbsp;&nbsp;&nbsp;  <a href=$domain target=_blank><FONT COLOR=BLUE><u>点击查看已开空间</u></FONT></a></span></td> 

        </tr></table>"; 

        exit; 

    } else { 

        mkdir('d:/vhostuser/user_'.$chara.'_'.$HTTP_POST_VARS[domain]); 

    } 

 

    $HTTP_POST_VARS[reg] = "0"; 

    $domain = "http://" . $domain; 

    echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"95%\" align=center class=\"forumline\"><tr> 

                <td class=\"row2\" colspan=\"2\"><span class=\"gensmall\">空间开设成空!~~<a href=$PHP_SELF>返回</a></span><span class=\"gensmall\">&nbsp;&nbsp;  <a href=$domain target=_blank><FONT COLOR=BLUE>点击查看已开空间</FONT></a>&nbsp;&nbsp;<a href=http://www.sun126.com>< 联系我们 - 精彩奇讯科技></a> </span></td> 

        </tr></table>"; 

?>

--------------------------------------------------------------------------------

本人还是一名在校学生,喜欢Linux并希望以后能从事系统管理方面的工作,希望认识有同样爱好的朋友和以后的同行朋友,我的电子邮箱yukun7402@163.com,QQ740246333