Virtual Host)It is a technology for building multiple website services with different domain names or based on different IP addresses on the same machine. you can specify different IP addresses and ports for each website running on the same physical machine, or assign different domain names to multiple websites.
Apache is the most widely used web server in the world. It has supported virtual hosts since version 1.1. this article will explain how to use Apache to build virtual hosts on different servers (Redhat Enterprise Linux, Ubuntu Linux, and Windows) to build multiple websites.
Redhat Enterprise Linux
Redhat Enterprise Linux (including centos Linux) is the most widely used Linux server. A large number of website applications are deployed on it.
1. open the file/etc/httpd/CONF/httpd. conf, search for virtualhost example, and find the Code as follows:
## VirtualHost example:# Almost any Apache directive may go into a VirtualHost container.# The first VirtualHost section is used for requests without a known# server name.##<VirtualHost *:80># ServerAdmin [email protected]# DocumentRoot /www/docs/dummy-host.example.com# ServerName dummy-host.example.com# ErrorLog logs/dummy-host.example.com-error_log# CustomLog logs/dummy-host.example.com-access_log common#</VirtualHost>
2. Add a piece of code to specify the website of a domain name.
# DocumentRoot is the root directory for storing website files # servername is the website domain name, which must be consistent with the domain name pointed to by DNS # <virtualhost *: 80> serveradmin [email protected] t.example.com DocumentRoot/var/www/httpdocs/demo_neoease_com servername demo.neoease.com errorlog logs/demo.neoease.com-error. log customlog logs/demo.neoease.com-access. log common </virtualhost> 3. restart the HTTPd service and execute the following statement. service httpd restart
Ubuntu Linux Ubuntu has the largest number of individual users in various Linux releases. many users use it in their local machines and virtual machines. however, the virtualhost setting methods for Ubuntu and RedHat are different. 1. open the/etc/apache2/sites-available/directory and find the default and default-SSL files. default is the configuration file of the http vm service, default-SSL is used to configure the HTTPS service. you can copy a default file. and modify the configuration file name. The file name must be consistent with the domain name (for example, demo.neoease.com) 2. open the new configuration file and modify the DocumentRoot, servername, and corresponding configuration directory. example: # DocumentRoot is the root directory for storing website files # servername is the website domain name and must be consistent with the domain name pointed to by DNS # <virtualhost *: 80> serveradmin [email protected] DocumentRoot/var/www/httpdocs/demo_neoease_com servername demo.neoease.com errorlog $ {apache_log_dir}/demo.neoease.com-error. log customlog $ {apache_log_dir}/demo.neoease.com-access. log combined </virtualhost> 3. use a2ensite to activate the VM and configure sudo a2ensite demo.neoease.com 4. open the/etc/apache2/sites-enabled/directory, and you will find that all activated virtual hosts can log out of sudo a2dissite demo.neoease.com 5 through a2dissite. restart the apache service and activate the VM sudo/etc/init. d/apache2 restart Windows is the PC operating system with the highest market share and is also a development environment for many people. its virtualhost configuration method is somewhat different from that on Linux. The following methods are suitable for native Apache, XAMPP, and Wamp suites. 1. open the directory {apache2 installation directory} \ conf \ extra \ and find the httpd-vhosts.conf file. 2. add a piece of code to specify the website of a domain name. # DocumentRoot is the root directory for storing website files # servername is the website domain name, which must be consistent with the domain name directed to by DNS # <virtualhost *: 80> serveradmin [email protected] DocumentRoot "D: /workspace/PHP/demo_neoease_com "servername demo.neoease.com errorlog" logs/demo.neoease.com-error. log "customlog" logs/demo.neoease.com-access. log "common </virtualhost> 3. open httpd. CONF file, add the following statement. # virtual hostsinclude CONF/extra/httpd-vhosts.conf 4. restart the apache service. in recent years, Apple's Mac OS has become a popular choice for many developers. the configuration method is similar to that of Linux. 1. open the file/private/etc/apache2/extra/httpd-vhosts.conf. 2. add a piece of code to specify the website of a domain name. # DocumentRoot is the root directory for storing website files # servername is the website domain name, which must be consistent with the domain name pointed to by DNS # <virtualhost *: 80> serveradmin [email protected] DocumentRoot "/usr/docs/httpdocs/logs" servername demo.neoease.com errorlog "/private/var/log/apache2/demo.neoease.com-error_log" customlog "/private/ var/log/apache2/demo.neoease.com-access_log "common </virtualhost> 3. open the file/private/etc/apache2/httpd. conf, search for virtual hosts, find the code as follows: # virtual hosts # include/private/etc/apache2/extra/httpd-vhosts.conf remove the comment symbol #, save the file. 4. restart the apache service and execute the following statements. sudo apachectl restart
The virtual directory is configured for the virtual host configuration under wamp5 today. The following is my configuration method. 1 edit httpd. conf, find include CONF/extra/httpd-vhosts.conf, and delete the comment symbol. 2 edit httpd-vhosts.conf, I installed wampserver on D:/Wamp, so here I am path is D: \ Wamp \ apache2 \ conf \ extra. Clear or comment the content and replace it with the following content: namevirtualhost *: 80, which is the new working directory <virtualhost *: 80> servername www. CMS. comserveralias www. CMS. comdocumentroot "F: // workplace" </virtualhost> original default directory <virtualhost *: 80> servername localhost serveralias localhost DocumentRoot "D: /Wamp/www "</virtualhost> 3 edit httpd. conf, find the DocumentRoot "D:/Wamp/www/" option. This is the default root directory path, but it is not the default root directory path. Keep searching and find <directory "D: /Wamp/www "> and add the following content after </directoory>: <dire Ctory "F: // workplace"> options indexes followsymlinks AllowOverride all order allow, deny allow from all </directory> <directory "D: /Wamp/www "> options indexes followsymlinks AllowOverride all order allow, deny allow from all </directory> ------------------------ example ----------------------- <virtualhost *: 80> documentd: /DS/WWW servername www.ds.com <directory "D:/DS/www"> options indexes follo Wsymlinks AllowOverride none order allow, deny allow from all </directory> </virtualhost> 4. Modify the C:/Windows/system32/Drivers/etc/host file and open it in notepad, add the following content: 127.0.0.1 www.cms.com, restart Apache, enter www.cms.com in the browser, and check whether the accessed content is in the workplace directory.
Virtual Host)