httpd虚拟主机配置的简单介绍

admin 171 2022-12-12

阿里云服务器优惠多,折扣错,惊喜多,请咨询:www.wqiis.com

本文目录一览:

编译安装httpd 2.4.4的服务器怎么设置虚拟主机

.

先打开Apache的配置文件httpd.conf,在这个文件,

找到这句:“#Include

etc/extra/httpd-vhosts.conf”

(根据安装路径不同,上面的路径会略有不同)

把前面的#曲调,即开启了使用虚拟主机的功能;

2.

在httpd-vhosts.conf中进行虚拟主机的配置操作;也可以在httpd.conf直接写入配置,但是

不建议这么做;

3.

打开httpd-vhosts.conf文件,找到NameVirutalHost

*:80,这句话的含义是制定这个主机的

IP地址和端口号;

4.

关键的virtualhost配置,在NameVirutalHost

*:80的下面;

注意事项:Apache在接受请求时,首先会默认第一个VirtualHost,然后再匹配其他虚拟主

机,如果没有匹配的,就是第一个VirtualHost起作用。故,在httpd.conf中,将

Dicrectory

/(这个是所有目录的默认配置)和Direcotry/opt/lampp/htdocs的权限,

都默认设置为deny

from

all。

5.

virtualhost配置实例:

VirtualHost*:80

ServerName

*

DocumentRoot

/opt/lampp/htdocs/guest

Directory

/opt/lampp/htdocs/guest

Order

deny,allow

Allow

from

all

/Direcotry

VirtualHost/

后面的,依次添加就可以

VirtualHost

*:80

ServerName

DocumentRoot

/opt/lampp/htdocs/myweb1

Directory

/opt/lampp/htdocs/myweb1

Order

deny,allow

Allow

from

all

/Direcotry

VirtualHost/

如何配置 Apache 的虚拟主机

1、基于ip地址的虚拟主机

复制代码代码如下:

Listen 80

VirtualHost 172.20.30.40

DocumentRoot /home/httpd/html1

ServerName

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

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

/VirtualHost

VirtualHost 172.20.30.40:8080

DocumentRoot /www/example1-8080

ServerName

/VirtualHost

VirtualHost 172.20.30.50:80

DocumentRoot /www/example2-80

ServerName

/VirtualHost

VirtualHost 172.20.30.50:8080

DocumentRoot /www/example2-8080

ServerName

/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

ServerAlias example1.com. *.example1.com

# Other directives here

/VirtualHost

VirtualHost *:80

DocumentRoot /www/example2

ServerName

# 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

H1hello~, 这是[对应的网站名,用以区别].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

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

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基于域名的虚拟主机配置完成。可以通过访问XAMPP欢迎页,通过和访问各自的主页。

#

# 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:;

# 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

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

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

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

# 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

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

http配置虚拟主机

1、设置虚拟主机通过/srv/default/www来为占位符站点提供内容。

创建目录 mkdir -p /srv/{default,allen}/www

2、使用文本编辑器创建index.html文化。/srv/defaul/www/www/index.文本内容 allen comming soon、在 /srv/allen/www/index/index 文件内容为allen。

3、重置上下文

restorecon -Rv /srv/

4、为 default:80 创建虚拟主机定义。并使用组合格式登录到logs/default-vhost.log

[root@solr1 conf.d]# cat default-vhost.conf

VirtualHost _default_:80

  DocumentRoot /srv/default/www

  CustomLog "logs/default-vhost.log" combined

/VirtualHost

#打开默认虚拟主机的内容目录

Directory /srv/default/www

  Require all granted #允许访问此目录,Require all Denied 将拒绝提供此目录的内容。

/Directory

5、为allen-vhost.conf配置内容。

[root@solr1 conf.d]# cat allen-vhost.conf

VirtualHost *:80

  ServerName allen.com

  ServerAlias allen

  DocumentRoot /srv/allen/www

  CustomLog "logs/allen.log" combined

/VirtualHost

Directory /srv/allen/www

  Require all granted

/Directory

6、配置完成并启动httpd服务

systemctl start httpd.service

7、访问显示default的返回结果。

访问显示allen的返回结果。

Apache服务器教程之配置一个虚拟主机的操作教程

方法/步骤

1、刚刚装好的Apache也可以来设置。首先找到Apache的配置文件,httpd.conf。在Apache安装目录下conf文件夹下。

2、记事本里面查找下图所示的文本。要找的就是#Include

conf/extra/httpd-vhosts.conf

这个是虚拟主机的一个功能,被注销了。我们要开启虚拟主机功能,所以把#给删除了。变为下图。

3、关闭httpd.conf文件,并保存。

4、在conf文件夹找到extra文件夹打开。打开httpd-vhosts.conf文件。这个就是专门来配置虚拟有关功能的一个配置文件吧。

5、在httpd-vhosts.conf的最后部分添加一段代码。代码如下图所示

6、红框框里的代码就是要添加的。注意大小写,要完全和红框里的内容一模一样,千万不能出错,只要是写错一个字母,Apache就错误!代码添加在文件的最后面!还有里面的参数要介绍一下,看下图

7、"d:/myweb2"表示,就是在D盘myweb2文件下就是Apache的主页了。输入localhost即寻找这里的页面。在d:/myweb2文件夹下,在创建一个new.html

8、这个红框里的则表示欢迎页面,主页面是什么的。如图的话,就是浏览器如果输入localhost,则先寻找D盘myweb2文件夹下的new.html,在是index.html,类推。寻找不到,就报错。

9、代码添加好,保存。这样配置基本就完成了,重启Apache。浏览器输入localhost

即可访问到D盘myweb2下的new.html文件。

10、在myweb2下在建一个new2.html。输入即可访问到了

上一篇:腾讯云图像识别(腾讯云图像识别原理)
下一篇:海外腾讯云(海外腾讯云服务器)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~