Multiple virtual hosts can be configured on an Apache server, enabling a single server to provide multi-site services, in fact, to access different directories on the same server.
A server host can run multiple Web sites, each Web site is a virtual host;
Any domain name resolves to this machine, can access the virtual host is the default virtual host;
When DNS is not yet in effect, configure the domain name by modifying the Hosts file to set the mapping resolution for IP and domain names.
First, edit the Hosts file in Windows
Hosts location "C:\Windows\System32\drivers\etc\hosts" in Windows system
Description: This allows you to customize the local IP and domain names to add temporary access addresses (used when DNS is not in effect).
hosts
add a line in:
192.168.204.128 www.abc.com
Ping on Windows to see if you can access the defined IP:
In the browser:
Second, set httpd default virtual host in Linux
The domain name accessed by the physical machine is www.abc.com
not defined in the virtual machine Apache configuration file, only a domain name is defined in the virtual machine, and ServerName www.example.com:80
the domain name is the default host for Apache, at which point a domain name that is bound to the virtual machine's IP is accessed to jump to that host.
Because a server can run multiple domain names, in order to facilitate management, the virtual host needs to be configured:
1. Edit the httpd configuration file
[[email protected] ~]# vim /usr/local/apache2/conf/httpd.conf //搜索httpd-vhost,去掉#
2. Edit the virtual host configuration file
What to modify:
<virtualhost *:80>//Each VirtualHost represents a host, a host represents a Sites ServerAdmin [email protected]//Define the administrator's mailbox DocumentRoot "/data/webroot/abctest"//means Fixed root directory ServerName zlinux.com//define site name Serveralias zlinuxbak.com Set site alias errorlog "Logs/dummy-host.example.com-error_log"//error log C Ustomlog "Logs/dummy-host.example.com-access_log" Common</virtualhost><virtualhost *:80> ServerAdmin [ Email protected] DocumentRoot "/data/webroot/123test" ServerName 123test.com serveralias www.example.com Errorlog "Logs/dummy-host2.example.com-error_log" Customlog "Logs/dummy-host2.example.com-access_log" common</ Virtualhost>
Description: A website is defined at this time, the core parameters of the definition site are: DocumentRoot (website root directory), ServerName (domain name).
Note: After the virtual host is in effect, the default host (www.example.com 192.168.204.128) in the original Apache configuration file becomes invalid.
3. Add the corresponding directory of the virtual host
[[email protected] ~]# mkdir /data/wwwroot/[[email protected] ~]# mkdir /data/wwwroot/abctest[[email protected] ~]# mkdir /data/wwwroot/123test
To create a PHP test file in the directory:
[[email protected] ~]# vim /data/wwwroot/abctest/index.php<?phpecho "this is a testfile!";?>
[[email protected] ~]# vim /data/wwwroot/123test/index.php<?phpecho "这是一个123test文件!";?>
4. Test the virtual host
[[email protected] ~]# curl -x 192.168.204.128:80 zlinux.comThis is a testfile![[email protected] ~]# [[email protected] ~]# curl -x 192.168.204.128:80 123test.com这是一个123test文件![[email protected] ~]# [[email protected] ~]# curl -x 192.168.204.128:80 000test.com
HTTPD configuration-Default virtual host