Apache Virtual host three ways to configure

Source: Internet
Author: User

Straight to the theme!!!

The use of virtual host must be commented out httpd of the host module, that is, modify the httd.conf master configuration file, find <directoryroot "XXXXXX", and this section of the content can be commented out.

There are three virtual host configurations in Apche, which are IP-based, Port-based, and domain-based. For the next test, you need to configure two IP addresses (my host is now the IP address is 10.10.50.100), the command is as follows:

#ip addr Add 10.10.50.101/16 dev eth0

#ip addr Add 10.10.50.102/16 dev eth0

about how to configure the IP address, no longer repeat here, I will write a special article about IP address.

The first type: IP-based

Apache silently installed, the main configuration file is located under/etc/httpd/conf/httpd.conf, can be at the bottom of the file to add virtual host, but I am accustomed to re-establish a virtual host configuration file, so convenient and future management operations.

1. Create a virtual host configuration file

# mkdir/etc/httpd/conf.d/virtual.conf

2. Edit the file and add the following:

<virtualhost 10.10.50.100:80>

ServerName www.longfei.com

DocumentRoot "/www/longfei.com"

</Virtualhost>

<virtualhost 10.10.50.101:80>

ServerName www.longfei2.com

DocumentRoot "/www/longfei2.com"

</Virtualhost>

------Save exit, this is the simplest configuration

3. Create and Catalog files

#mkdir/www/{longfei.com,longfei2.com}

4. Create a home page file with the directory file for subsequent access testing

#vi/www/longfei2.com/index.html, add a little bit of content

<title>Longfei</title>

Save exit. Then edit the home page file under another directory

#vi/www/longfei2.com/index.html, add a little bit of content

<title>Longfei2</title>

5. Restart the httpd service (it's a good idea to check the syntax before restarting HTTPD-T)

Service httpd Restart

6. Client Access Testing

Enter in the browser separately:

10.10.50.100 and

10.10.50.101

will see the home page defined between us.

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

The second port-based virtual host

Also use the vitual.conf created above as the configuration file, assuming we use the 80 and 8080 ports at this time, the IP address is 10.10.50.102

1. Modify the httpd Master profile, vi/etc/httpd/conf/httpd.conf

First, listen 80 before the comment is removed, and then add a row listen 8080, save exit.

2. Modify the virtual host configuration file and add the following:

<virtualhost 10.10.50.102:80>

ServerName www.a80.com

DocumentRoot "/www/a80.com"

</VirtualHost>

<virtualhost 10.10.50.102:8080>

ServerName www.a80.org

DocumentRoot "/www/a80.org"

</VirtualHost>

3. Create the required root file

#mkdir/www/{a80.com,a80.org}

4. Create the default access page for each root file, create the process with the first step four, this is to change the contents of the inside you can know something else. I used the a80.com and a80.org two notation respectively.

5. Check grammar

Httpd-t

6. Restart the service

7. Access Testing

Enter 10.10.50.102 and 10.10.50.102:8080 on the browser, respectively, because the browser uses port 80 by default, so you can not specify a port when accessing a 80-port Web site.


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

The third domain-based virtual host

1. Domain-based virtual host , the first to specify Ip:port in the virtual configuration file, that is, modify the virtual configuration file, the first line to add Namevirtualhost 10.10.50.100:80

Then add the following information below

<virtualhost 10.10.50.100:80>

ServerName hello.longfei.com

DocumentRoot "/hello/longfei.com"

</VirtualHost>

<virtualhost 10.10.50.100:80>

ServerName hello.longfei.org

DocumentRoot "/hello/longfei.org"

</VirtualHost>

2. Create the required files directory

mkdir/hello/{longfei.com,longfei.org}

3. Edit the home page file under the root file directory, referring to the method above

4. Modify the Hosts file of the client host to ensure that the two domain names we set are resolved properly

Hosts file is located in/c/windows/drivers/etc/hosts (as if yes, remember not very clear, children's shoes can Baidu a bit)

Add the following two columns

10.10.50.100 hello.longfei.com

10.10.50.100 hello.longfei.org

5. Ping to ensure normal parsing

That is, the ping hello.longfei.com and ping hello.longfei.org are both displayed and the return value is 10.10.50.100.

6. Restart the HTTPD service

7. Browser Testing

Enter hello.longfei.com and hello.longfei.org in the browser respectively and then check the main page is not our previous logo page.

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

Add:

1. The virtual host also supports the location redefinition of the access log and the error log, that is, only need to add the log information entry below the DocumentRoot, such as I want to let hello.longfei.org's access log and error log are relocated to/hello/ Longfei.org/log below, you need to add this line

Cutomlog "/hello.longfei.org/log" combined

Errorlog "/hello/longfei.org/log"

After you save the exit, create the log directory below longfei.org.

2. The virtual host also supports returning the server's status information

For example, I want to display the server, as long as in the main configuration file of the httpd <Location/server-status> this piece of information to remove the comment, will allow to add your IP address, so that only allows you to access the view. View the method: in the browser input hello.longfei.org/server-status can be viewed. That is, if directory is defined as a file path, then location defines the URL path, and any one of the paths that you add Server-status can view the server information.

3. Virtual host also supports the same access restrictions as real hosts, please refer to http://blog.csdn.net/brad_chen/article/details/45749123 for details.

Apache Virtual host three ways to configure

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.