如何配置apache虚拟主机的实例小结
发布时间:2023-07-23 18:40:54
1、基于ip地址的虚拟主机
Listen 80
<VirtualHost 172.20.30.40>
DocumentRoot /home/httpd/html1
ServerName www.ok1.com
ErrorLog /usr/local/apache/logs/error1_log
CustomLog /usr/local/apache/logs/access1_log combined
</VirtualHost>
<VirtualHost 172.20.30.50>
DocumentRoot /home/httpd/html2
ServerName www.ok2.com
ErrorLog /usr/local/apache/logs/error2_log
CustomLog /usr/local/apache/logs/access2_log combined
</VirtualHost>
2、基于IP 和多端口的虚拟主机配置
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
<VirtualHost 172.20.30.40:80>
DocumentRoot /www/example1-80
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example1.org
</VirtualHost>
<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example2.org
</VirtualHost>
3、单个IP 地址的服务器上基于域名的虚拟主机配置
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
</VirtualHost>
如果您感觉上面的文章还不够详细可以看下下面的文章:
实验目标:在apache实现基于域名的虚拟主机
实验用的XAMPP版本为1.7.7,内含apache版本为2.2.21
实验前准备:
1. 为了测试不同的域名,在Windows/System32/drivers/etc/下觅得hosts文件,在其中添加实验用的域名若干,如 -
127.0.0.1 test1.net
127.0.0.1 test2.net
如此,则在浏览器中输入该俩域名时,Windows将其解析为127.0.0.1本地地址。即,在浏览器中访问localhost, test1.net, test2.net均可访问XAMPP的欢迎页。
2. 在apache目录下建立目录,以放置您不同的网站。为保护XAMPP原有的htdocs中的欢迎页内容,实验另外建立了与htdocs平级的htdocs1目录,在其下建立了test1.net, test2.net两个子目录用以放置实验用的网站。如下 -
apache/htdocs1/test1.net - 放置test1.net网站内容
apache/htdocs1/test2.net - 放置test2.net网站内容
在这两个目录中各新建hello world一网页 index.html,内容 -
<HTML>
<HEAD></HEAD>
<BODY>
<H1>hello~, 这是[对应的网站名,用以区别].net</H1></BODY>
</HTML>
实验步骤:
1. 找到apache/conf/httpd.conf, 将其中的
ServerAdmin
ServerName
DocumentRoot
注释掉。
2. 在httpd.conf中,找到行
Include "conf/extra/httpd-vhosts.conf"
如被注释则解注。该文件记载了虚拟主机的参数。[以前虚拟主机参数是直接填写在httpd.conf中的,为了更好地组织文件,将其分离出去,类似于某些编程语言一样。因此httpd.conf中include它,即相当于把它的内容填在了httpd.conf中。]
3. 这个httpd-vhosts.conf文件格式基本如下 -
#blah-blah
NameVirtualHost *:80
#blah-blah
#blah-blah
<VirtualHost *:80>
ServerAdmin XXXXXXXX
DocumentRoot "XXXXXXXX"
ServerName XXXXXXX
ServerAlias XXXXXX
ErrorLog "logs/XXXXXX-error.log"
CustomLog "logs/XXXXXXX-error.log" combined
</VirtualHost>
需要修改的,就是<VirtualHost>中的参数了。这个可以参见apache官方文档。根据实验域名,可以增加两个<VirtualHost>:
<VirtualHost *:80>
ServerAdmin adm@test1.net
DocumentRoot "C:/xampp/htdocs1/test1.net"
ServerName test1.net
ServerAlias www.test1.net
ErrorLog "logs/test1-error.log"
CustomLog "logs/test1-access.log" combined
<Directory "C:/xampp/htdocs1/test1.net">
order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin adm@test2.net
DocumentRoot "C:/xampp/htdocs1/test2.net"
ServerName test2.net
ServerAlias www.test2.net
ErrorLog "logs/test1-error.log"
CustomLog "logs/test1-access.log" combined
<Directory "C:/xampp/htdocs1/test2.net">
order allow,deny
allow from all
</Directory>
</VirtualHost>
注意,如果不在各VirtualHost中定义Directory的可访问性,你将遇到的是Access Forbidden!就连原来的localhost也是。
4. 由于之前注释掉了httpd.conf中的ServerName, DocumentRoot等,为了仍然能以localhost访问原XAMPP欢迎页,就在增加一个VirtualHost,如下 -
<VirtualHost *:80>
ServerAdmin adm@localhost
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" combined
<Directory "C:/xampp/htdocs">
order allow,deny
allow from all
</Directory>
</VirtualHost>
为了避免出错,把它放置在第一个Virtualhost位置。
至此,apache基于域名的虚拟主机配置完成。可以通过http://localhost访问XAMPP欢迎页,通过http://test1.net和http://test2.net访问各自的主页。
#
# 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.apache.org/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>
ServerAdmin kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs"
ServerName localhost
ServerAlias www.skydao.com
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" combined
<Directory "E:/skydao/apache2/htdocs">
order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs/project1"
ServerName project1.com
ServerAlias www.project1.com
ErrorLog "logs/project1-error.log"
CustomLog "logs/project1-access.log" combined
<Directory "E:/skydao/apache2/htdocs/project1">
order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs/zendTest/public"
ServerName zendTest.com
ServerAlias www.zendTest.com
DirectoryIndex index.php
<Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs/testRewrite"
ServerName testRewrite.com
ServerAlias www.testRewrite.com
# DirectoryIndex index.php
<Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs/test"
ServerName test.com
ServerAlias www.test.com
ErrorLog "logs/zendTest-error.log"
CustomLog "logs/zendTest-access.log" combined
<Directory "E:/skydao/apache2/htdocs/test">
order allow,deny
allow from all
</Directory>
</VirtualHost>


猜你喜欢
- 这几天很多朋友的站都不同程度的快照回档,有的甚至直接被k的一页都不剩了,包括我自己的小站114美女也不能幸免,于是咨询了很多业内知名人士,总
- 问题现象:通过vmware8的完全克隆功能快速创建一台版本为CentOS 6.4的linux虚拟机。创建后症状:启动之后使用ifconfig
- 自从我们发布了服务器端广告设置这个功能以后,大家修改代码就更加方便了。Google AdSense代码投放的最好办法是直接通过帐户获取代码并
- 一抬头,又是窗外朦朦,不知多少人家,此时梦中。多少草根站长,此时挑灯夜战。回首间,已埋在Admin5近两年。虽然钱赚的不多,但是阅历增加无数
- 本文主要给大家介绍了关于linux尝试登录失败后锁定用户账户的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍吧。pam_tall
- 整合搜索,或者叫通用搜索,universal search,是这几个月对SEO行业有重要影响的另一个搜索引擎发展趋势。我个人感觉整合搜索比个
- 前段时间暴风影音的DNS攻击事件导致大量用户无法上网,闹得沸沸扬扬,至今网上仍然有人讨论。DNS到底是什么呢?为什么DNS挂了就不能上网?有
- IIS的相关设置: 删除默认建立的站点的虚拟目录,停止默认web站点,删除对应的文件目录c:inetpub,配置所有站点的公共设置,设置好相
- Linux中多线程详解及简单实例1.概念 进程:运行中的程序。 线程:一个程序中的多个执行路径。更准确的定义是:线程是一个进程内部的一个控制
- 安装Server CoreServer Core的安装本身很简单,你只要插入光盘,点击"Setup",跟随屏幕向导就能完
- WordPress是世界上使用最为广泛PHP博客程序,他拥有近乎无尽的主题和插件,简单易用。他既可以通过各种各样的插件他自己武装成一个CMS
- 1. Packetdrill 编译与安装源码链接 https://github.com/google/packetdrill.git源码编译
- 虚拟机正在使用或无法连接到报类似如下错误时解决方案首先进入该页面获得虚拟机所在位置删除.ick后缀的文件,该文件应该是保存了虚拟机上次运行时
- Windows7(vista)系统自带的iis7.0(7.5),有很多新的功能让人耳目一新,在这里让我们看看IIS7是如何继续支持ASP的.
- 还是做企业站的时候遇到的问题,因为做产品栏目的时候,产品有很多的参数需要显示,比如产品的大小、重量、单价等,于是我想到的方法就是使用自定义栏
- 以下是提高IIS 5.0网站服务器的执行效率的八种方法:1. 启用HTTP的持续作用可以改善15~20%的执行效率。2. 不启用记录可以改善
- 三月一次的PR地震最近震完了,虽然我并不在意PR,但做前端的,多少也得了解这东东。我首页的PR从三个月前的没有跳到了5,首先感谢所有把PR分
- 7月28日消息,有网游爆料称QQ最新版 2010SP1(版本号1760)有巨大漏洞,发送代码之后,打开聊天记录就可以执行。TechWeb从
- 全球最大的域名交易平台Sedo,今天透露chinese.com的成交价为110万美元。位居该平台2007年最贵的几个域名之一。chinese
- 每年,我都会密切关注推出的新设计网站SEM最佳案例。在过去的四年或是五年时间里,我的大部分建议都是相同的。今天,我们讨论一些可以改变的技巧。