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. Apache Virtual Host Configuration has 3 methods: Based on IP configuration, domain-based configuration and port-based configuration, described here is based on the domain name configuration and port-based configuration, based on the IP configuration method is similar.
1. Apache-based domain configuration virtual host:
Open the configuration file conf/extra/httpd-vhosts.conf in the Apache installation directory and add the following configuration information:
<virtualhost _default_:80>
DocumentRoot "D:/wamp/www"
ServerName www.jtw.com
</VirtualHost>
<virtualhost *:80>
ServerAdmin [email protected]
DocumentRoot "D:/WAMP/WWW2"
ServerName www.wujuntian.com
Errorlog "Logs/wujuntian.log"
Customlog "Logs/wujuntian.log" common
<directory "D:/WAMP/WWW2" >
Options Indexes followsymlinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
There are two virtual hosts configured, the first of which is the default virtual host, and the access to the virtual host's domain name is also provided by this virtual host. Both hosts use 80 ports, the first virtual host specifies the domain name "www.jtw.com", the server directory is "D:/wamp/www", the second virtual your host specifies the domain name "www.wujuntian.com", and the server directory is "d:/wamp/ WWW2 ".
This configuration file is then included in the Apache master configuration file conf/httpd.conf:
Include conf/extra/httpd-vhosts.conf
Finally restarting the Apache server, you can access two different domains to access two directories on the same server.
Note: If you are only configuring tests locally, you will need to add the domain name to the local IP address mapping in the Hosts file (Hosts file location: C:\Windows\System32\drivers\etc):
127.0.0.1 www.jtw.com
127.0.0.1 www.wujuntian.com
2. Apache-based port configuration virtual host:
First modify the configuration in Apache config file conf/httpd.conf to have the Apache server listen on multiple ports:
Listen 8080
Listen 80
Here, listen for two ports and configure two virtual hosts.
Then, in the configuration file conf/extra/httpd-vhosts.conf, add the following configuration information:
<virtualhost _default_:80>
DocumentRoot "D:/wamp/www"
</VirtualHost>
<virtualhost *:8080>
ServerAdmin [email protected]
DocumentRoot "D:/WAMP/WWW2"
<directory "D:/WAMP/WWW2" >
Options Indexes followsymlinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
This configuration file is included in the Apache master configuration file conf/httpd.conf:
Include conf/extra/httpd-vhosts.conf
By restarting the Apache server, you can access two different directories on the same server through different ports on the same IP address.
Note: The Web server uses 80 ports by default, so you can access a 80 port site without adding ": 80", but you must have the upper port number when you access the other ports.
Configurations in the <VirtualHost></VirtualHost> section can be overridden outside the configuration.
Apache Virtual Host Configuration