ubuntu apache 配置虚拟主机

来源:互联网 发布:iphone office办公软件 编辑:程序博客网 时间:2024/04/26 07:44

比如有两个域名www.linuxidc 和www.linuxidc.net两个域名要做解析

  在/etc/apache2/sites-available 目录下建立a和b两个文件,这里只是为了使用的方便,你仔细观察可以看出在sites-enabled文件下也会有相关的文件连接(只是一个软连接)到sites-available 文件下相应的文件上

  为了方便管理,这里做的都使用一个名称也容易记忆。

  编辑相应文件内容如下

NameVirtualHost www.linuxidc 
<VirtualHost www.linuxidc> 
ServerName www.linuxidc 
ServerAlias linuxidc *.linuxidc 
ServerAdmin webmaster@linuxidc 

DocumentRoot /var/www/a/ 
<Directory /> 
Options FollowSymLinks 
AllowOverride None 
</Directory> 
<Directory /var/www/a/> 
Options Indexes FollowSymLinks MultiViews 
AllowOverride None 
Order allow,deny 
allow from all 
# This directive allows us to have apache2's default start page 
# in /apache2-default/, but still have / go to the right place 
# Commented out for Ubuntu 
#RedirectMatch ^/$ /apache2-default/ 
</Directory> 

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 
<Directory "/usr/lib/cgi-bin"> 
AllowOverride None 
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch 
Order allow,deny 
Allow from all 
</Directory> 

ErrorLog /var/log/apache2/a.log 

# Possible values include: debug, info, notice, warn, error, crit, 
# alert, emerg. 
LogLevel warn 

CustomLog /var/log/apache2/access.log combined 
ServerSignature On 

Alias /doc/ "/usr/share/doc/" 
<Directory "/usr/share/doc/"> 
Options Indexes MultiViews FollowSymLinks 
AllowOverride None 
Order deny,allow 
Deny from all 
Allow from 127.0.0.0/255.0.0.0 ::1/128 
</Directory> 
</VirtualHost>   

  

  文件b里的内容基本和a是一样的,只需要所少数几处改一下

  执行a2ensite a a2ensite b 命令使虚拟主机生效

  重启apache

  Java代码

  /etc/init.d/apache2 restart

  或者重新加载apache2

  Java代码

  /etc/init.d/apache2 reload

  修改hosts文件

  Java代码

  vi /etc/hosts

  插入下面两行

  127.0.0.1 www.linuxidc

  127.0.0.1 www.linuxidc.net

  现在访问两个域名,会发现解析到了不同的目录

  如果哪个虚拟主机不想使用只需要执行命令

  Java代码

  a2dissite b

  这样再访问www.linuxidc.net就不能正确解析

原创粉丝点击