Apache default hosting page root directory, is under/var/www/html/, that is, by default, a server, can only run a Web site. But a good performance of the server, if only run a website, is not a bit wasteful? In fact, Apache is to support the virtual host function, to the virtual host mode can run N sites.
The way Apache turns on the virtual host feature is also simple, taking CentOS 6.3 and Apache 2.2 as an example, creating a vhost.conf configuration file in the/etc/httpd/conf.d/directory with the following content format:
Namevirtualhost *:80
<virtualhost *:80>
ServerName
aaa.com serveralias www.aaa.com documentroot/srv/www/aaa.com/public_html/
</VirtualHost>
<virtualhost *:80>
ServerName bbb.com
serveralias www.bbb.com
documentroot/srv/www/bbb.com/public_html/
</VirtualHost>
Then create the appropriate directories for the two sites:
$ mkdir-p/srv/www/aaa.com/public_html
$ mkdir-p/srv/www/bbb.com/public_html
In the above two domain names, add a record point to the IP address of the server, and then restart the Apache server:
If you need to log access logs and error logs, as well as implement URL rewriting functions, you will also need to make the following modifications:
<virtualhost *:80>
ServerAdmin webmaster@aaa.com
ServerName
aaa.com serveralias www.aaa.com documentroot/srv/www/aaa.com/public_html/
errorlog/srv/www/aaa.com/logs/error.log
customlog/srv/www/ Aaa.com/logs/access.log combined
<Directory/srv/www/aaa.com/public_html/>
Options followsymlinks
allowoverride
All Order Allow,deny
allow from all
</Directory>
</VirtualHost>
At this point, the virtual host is created to complete, with different domain name access, the server will return different content.
Additionally, if you use an IP address to access the server at this point, the returned content will no longer be the content of the/var/www/html/, but the contents of the first virtual host in the vhost.conf configuration file.