Three configuration methods of Apache virtual host _linux

Source: Internet
Author: User
Tags mkdir

Use the virtual host must be commented out of the HTTPD host module, that is, modify the httd.conf main configuration file, find, the content of the comment off on it.

Apche has a total of three virtual host configurations, which are ip-based, port based, and domain based. In order to test later, you need to configure two IP addresses (my host's current 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 IP address, no longer repeat here, I will write a special article on the IP address.

First: ip-based

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

1. Create a virtual host configuration file

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

2. Edit the file and add the following

ServerName www.jb51.net
DocumentRoot "/www/jb51.net"

ServerName www.jbzj.com
DocumentRoot "/www/jbzj.com"

------Save exit, this is the simplest configuration
3. Create and Catalog files

#mkdir/www/{jb51.net,jbzj.com}

4. Create a home page file under the directory file for subsequent access tests

#vi/www/jbzj.com/index.html, add content

This is JB51 test

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

#vi/www/jbzj.com/index.html, add content

This is JBZJ test

5. Restart the HTTPD service (before restarting, it is best to check the syntax for problems 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 type of virtual host based on ports port

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

1. Modify the httpd main configuration file, vi/etc/httpd/conf/httpd.conf

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

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

ServerName www.jb51.net
DocumentRoot "/www/jb51.net"

ServerName www.jb51.org
DocumentRoot "/www/jb51.org"

3. Create the required root files

#mkdir/www/{jb51.net,jb51.org}

4. Create the default access page for each root file and create the process with the first step four, which is to replace the contents with what you can know. I use the jb51.net and jb51.org two notation respectively.

5. Check grammar

Httpd-t

6. Restart Service
7. Access Test

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 do not have to specify a port when you access a 80-port Web site.

A third domain based virtual host

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

Then add the following information below

ServerName hello.jb51.net
DocumentRoot "/hello/jb51.net"

ServerName hello.jb51.org
DocumentRoot "/hello/jb51.org"

2. Create the required file directory
mkdir/hello/{jb51.net,jb51.org}

3. Edit the homepage file below the root file directory to refer to the above method

4. Modify the Hosts file of the client host to ensure that we can correctly parse the two domain names we set.

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

Add the following two columns

10.10.50.100 hello.jb51.net
10.10.50.100 hello.jb51.org

5. Ping to ensure normal parsing

That is, ping hello.jb51.net and ping hello.jb51.org all appear to be pass, and the return value is 10.10.50.100.

6. Restart HTTPD Service

7. Browser Test

Enter Hello.jb51.net and hello.jb51.org in the browser and then see if the main page is our previous logo page.

Add:

1. The virtual host also supports the location redefinition of access logs and error logs, that is, you only need to add the log information entries under DocumentRoot, for example, I want to reposition Hello.jb51.org's access log and error log to/hello/jb51.org/log below, and need to add this line

Cutomlog "/hello.jb51.org/log" combined
ErrorLog "/hello/jb51.org/log"

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

2. The virtual host also supports return server status information

For example, I want to display the server, as long as the content of this information in the main configuration file in httpd to remove the comment, you will add allow from the back of your IP address, so that only allow you to access the view. How to view: You can view it by entering Hello.jb51.org/server-status in the browser. That is, if the directory is defined as the file path, then location is defined as the URL path, any path after adding Server-status can view the server information.

3. Virtual hosts also support the same access restrictions as real hosts

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.