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 , similar to IP-based configuration methods.
1. Apache -based domain Configuration virtual host:
Open the configuration file conf/extra/httpd-vhosts.confin the Apache installation directoryand 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 a port, the first virtual host specifies a domain name of "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 need to include the mapping ofthe domain name to the local IP address in the hosts file (thehosts 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: TheWeb Server uses a port by default , so You can not add ":" When you visit a site with a port. , but you must have the upper port number when you access other ports.
Configurations in the <VirtualHost></VirtualHost> section can be overridden outside the configuration.
Apache Virtual Host Configuration