Three ways Apache configures a virtual host

Source: Internet
Author: User
Tags net domain fully qualified domain name

1 Apache virtual hosts are implemented in 3 ways.

    • IP-based virtual host
    • Port-based virtual host
    • Domain-based virtual host

2.1 Enabling Virtual Host preparation

2.1.1 Installation httpd

Yum Install httpd-y

2.1.2 Disabling the default host mode

[Email protected] httpd]# vim/etc/httpd/conf/"/var/www/html"

2.2 IP-based virtual host configuration

2.2.1 Adding multiple IPs to the host

[Email protected] conf.d]# IP addr Show dev eth0 #查看原有IP
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000
Link/ether 00:0c:29:77:77:7d BRD FF:FF:FF:FF:FF:FF
inet 192.168.137.200/24 BRD 192.168.137.255 Scope Global eth0
Inet6 FE80::20C:29FF:FE77:777D/64 Scope link
Valid_lft Forever Preferred_lft Forever
[Email protected] conf.d]# IP addr add 192.168.137.201/24 dev eth0 #添加一个IP
[[Email protected] conf.d]# IP addr Show dev eth0 #查看添加后的IP信息, at this time there are 2 IP addresses. 200,201
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000
Link/ether 00:0c:29:77:77:7d BRD FF:FF:FF:FF:FF:FF
inet 192.168.137.200/24 BRD 192.168.137.255 Scope Global eth0
inet 192.168.137.201/24 Scope Global secondary eth0
Inet6 FE80::20C:29FF:FE77:777D/64 Scope link
Valid_lft Forever Preferred_lft Forever

2.2.2 Adding a virtual host configuration file

[Email protected] conf.d]# cd/etc/httpd/conf.d/#进入配置目录[[ Email protected] conf.d]# vim virtualhost.conf #创建一个配置文件, edit content as follows [[email protected] conf.d]#Catvirtualhost.conf #查看并检查配置文件<virtualhost192.168.137.200: the>DocumentRoot"/var/www/test200"ServerName www.test200.com</virtualhost><virtualhost192.168.137.201: the>DocumentRoot"/var/www/test201"ServerName www.test201.com</VirtualHost>
[Email protected] conf.d]# cd/var/www            mkdir  test200 test201    #创建目录   echo test200 >>./test200/echo#创建IP为200的主页

2.2.3 Test

[Email protected] www]# service httpd restart
stopping httpd: [OK]
Starting httpd:httpd:Could not reliably determine the server ' s fully qualified domain name, using Localhost.localdomain For ServerName
[OK]
We use ELinks for testing, of course, the same as the browser test [[email protected] conf]# ELinks-source192.168.137.200Test200[[email protected] conf]# elinks-source192.168.137.201test201

2.3 Port-based virtual host configuration

2.3.1 Adding a listening port to the master profile

[Email protected] conf]# vim/etc/httpd/conf/8080

2.3.2 Adding a 8080 port virtual configuration

[Email protected] conf.d]#Catvirtualhost.conf<virtualhost192.168.137.200: the>DocumentRoot"/var/www/test200"ServerName www.test200.com</virtualhost><virtualhost192.168.137.201: the>DocumentRoot"/var/www/test201"ServerName www.test201.com</VirtualHost> #下面的内容是在上面的配置的基础上添加的.<virtualhost 192.168.137.201:8080> documentroot "/var/www/test201-8080" ServerName www.test201-8080.com& Lt;/virtualhost>
[Email protected] conf.d]# cd/var/www/           #切换目录 mkdir test201-8080        #创建目录  Echo " test201-8080 " >>./test201-8080/index.html       #创建主页

2.3.2 Test

[ [email protected] www]# service httpd restartstopping httpd: [OK]sta Rting httpd:httpd:Could not reliably determine the server's fully qualified domain name, using Localhost.localdomain for ServerName[OK][[email protected] conf]# elinks-source192.168.137.201: theTest201[[email protected] conf]# elinks-source192.168.137.201Test201[[email protected] conf]# elinks-source192.168.137.201:8080test201-8080

2.4 Domain-based virtual host configuration

2.4.1 Adding a domain name to the virtual host configuration

[ email protected] conf.d]# vim virtualhost.conf #编辑虚拟主机配置文件 [[email protected] conf.d]#Catvirtualhost.conf #内容如下, the red part is added on the basis above namevirtualhost 192.168.137.200:80 <virtualhost192.168.137.200: the>DocumentRoot"/var/www/test200"ServerName www.test200.com</VirtualHost><virtualhost 192.168.137.200:80> documentroot "/var/www/test200net" ServerName www.test200.net</ Virtualhost><virtualhost192.168.137.201: the>DocumentRoot"/var/www/test201"ServerName www.test201.com</virtualhost><virtualhost192.168.137.201:8080>DocumentRoot"/var/www/test2018080"ServerName www.test2018080.com</VirtualHost>
[[email protected] conf.d]#! serservice httpd restartstopping httpd:                                            [  OK  ]starting httpd:httpd:Could not reliably determine the server's fully qualified domain name, using Localhost.localdomain for ServerName                                                           [  OK  ]   
[Email protected] conf.d]# cd/var/www            mkdir  test200net          Echo " test200net " >>./test200net/index.html  #创建主页

2.4.2 Test

2.4.2.1 adding domain name resolution

Here we do not provide DNS to parse, simple to use the Hosts file area resolution on it.

[Email protected] www]# vim/etc/hosts      cat /etc/hosts127.0.  0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4::1          localhost localhost.localdomain localhost6 localhost6.localdomain6192.168. 137.200 www.test200.com 192.168. 137.200 www.test200.net

Then you can test it.

[[email protected] www]# Elinks-source http://www.test200.com       #测试. com domain  -source http: // www.test200.net       #测试. NET domain test200net

Three ways Apache configures a virtual host

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.