Your server has only one IP address, and many domain names (cnames) in DNS are mapped to this machine. You want to runwww.example.com
Andwww.example.org
Two sites.
Note:
Creating a virtual host in your Apache server configuration does not automatically update the host name in your DNS. YouRequiredAdd your own domain name in DNS to point to your IP address. Otherwise, you cannot view your website. You canhosts
Add this entry to the file for testing, but this method only applieshosts
File machines.
Server Configuration
# Ensure Apache listens to port 80
Listen 80
# Listen on all IP addresses for a VM
Namevirtualhost *: 80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com
# You can add other commands here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example.org
# You can add other commands here
</VirtualHost>
Because the asterisk matches all IP addresses, the master server does not receive any requests. Becausewww.example.com
It appears in the configuration file first, so it has the highest priority, which can be consideredDefaultOrMasterServer. This means that if a request cannot be associated withServerName
The command matches<VirtualHost>
Segment servo.
Note:
If you want to, you can replace it with a fixed IP address"*
". In this case,VirtualHost
ParametersRequiredAndNameVirtualHost
Parameters are consistent:
NameVirtualHost 172.20.30.40
<Virtualhost 172.20.30.40>
# Others...
However, when your IP address cannot be determined, use"*
"It is very convenient. For example, when your ISP configures Dynamic IP addresses for you and you use a dynamic domain name resolution system. Because"*
"Matches any IP address. In this case, no additional configuration is required regardless of the IP address.
The above configuration will be used when you use a domain name-based virtual host in most cases. In fact, this configuration will not satisfy you only in one case: You want to provide different content for different IP addresses or ports.