To use name-based virtual hosting, you must designate the IP address (and possibly port) on the server that will be accepting requests for the hosts. This is configured using the
NameVirtualHostdirective. In the normal case where any and all IP addresses on the server should be used, you can use
* as the argument to
NameVirtualHost. If you’re planning to use multiple ports (e.g. running SSL) you should add a Port to the argument, such as
*:80. Note that mentioning an IP address in a
NameVirtualHost directive does not automatically make the server listen to that IP address. SeeSetting which addresses and ports Apache uses for more details. In addition, any IP address specified here must be associated with a network interface on the server.
The next step is to create a
block for each different host that you would like to serve. The argument to the
directive must match a defined
NameVirtualHost directive. (In this usual case, this will be “*:80”). Inside each
block, you will need at minimum a
ServerName directive to designate which host is served and a
DocumentRoot directive to show where in the filesystem the content for that host lives.
Main host goes away
If you are adding virtual hosts to an existing web server, you must also create a
block for the existing host. The
DocumentRoot included in this virtual host should be the same as the global
DocumentRoot. List this virtual host first in the configuration file so that it will act as the default host.
For example, suppose that you are serving the domain
www.domain.tld and you wish to add the virtual host
www.otherdomain.tld, which points at the same IP address. Then you simply add the following to
ServerAlias domain.tld *.domain.tld
然后在本机的/etc/resolv.conf文件下把nameserver指定成本机就可以了，最后分别重启下DNS和HTTPD服务就行了，这里要注意的是如果selinux是开启的，记得把它关了，或者执行下chcon -u system_u -t httpd_sys_content_t -R /www这条命令，不然的话，访问会提示：