Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set up virtual host by apache

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article to share with you is about apache how to set up a virtual host, Xiaobian feel quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.

Apache virtual hosting can be implemented in three ways: IP-based virtual hosting, port-based virtual hosting, and domain-based virtual hosting.

Based on IP

1. Assuming that the server has an IP address of 192.168.1.10, use ifconfig to bind three IPs on the same network interface eth0:

[root@localhost root]# ifconfig eth0:1 192.168.1.11[root@localhost root]# ifconfig eth0:2 192.168.1.12[root@localhost root]# ifconfig eth0:3 192.168.1.13

2. Modify the hosts file and add three domain names corresponding to one of them:

192.168.1.11 www.test1.com192.168.1.12 www.test2.com192.168.1.13 www.test3.com

3. Create the root directory of the virtual host to store the web page, for example, create the test1, test2, test3 folders under the/www directory, in which 1.html, 2.html, 3.html are stored respectively.

/www/test1/1.html/www/test2/2.html/www/test3/3.html

4. Include the additional configuration file httpd-vhosts.conf in httpd.conf, and then write the following configuration in httpd-vhosts.conf:

ServerName www.test1.comDocumentRoot /www/test1/Options Indexes FollowSymLinksAllowOverride NoneOrder allow,denyAllow From AllServerName www.test1.comDocumentRoot /www/test2/Options Indexes FollowSymLinksAllowOverride NoneOrder allow,denyAllow From AllServerName www.test1.comDocumentRoot /www/test3/Options Indexes FollowSymLinksAllowOverride NoneOrder allow,denyAllow From All

5. After completion, test each virtual host, visit www.test1.com, www.test2.com, www.test3.com respectively

2. Based on host name

1. Set domain name mapping to the same IP, modify hosts:

192.168.1.10 www.test1.com192.168.1.10 www.test2.com192.168.1.10 www.test3.com

2. As above, create a root directory for hosting web pages

/www/test1/1.html/www/test2/2.html/www/test3/3.html

3. Include the additional configuration file httpd-vhosts.conf in httpd.conf, and then write the following configuration in httpd-vhosts.conf:

In order to use domain-based virtual hosting, you must specify the server IP address (and possibly port) for the host to accept requests. This can be configured with the NameVirtualHost directive. If all IP addresses on the server are used, you can use * as the NameVirtualHost parameter. Indicating an IP address in the NameVirtualHost directive does not cause the server to automatically listen on that IP address. The IP address must correspond to a network interface on the server.

The next step is to configure the configuration block for each virtual host you create with the same parameters as the NameVirtualHost command. Each definition block contains at least one ServerName directive specifying which host to serve and a DocumentRoot directive indicating where the contents of that host reside on the file system.

If you add virtual hosts to an existing web server, you must also build a definition block for the existing hosts. ServerName and DocumentRoot should be consistent with the global content, and should be placed at the top of the configuration file, playing the role of default host.

NameVirtualHost *:80ServerName *DocumentRoot /www/ServerName www.test1.comDocumentRoot /www/test1/Options Indexes FollowSymLinksAllowOverride NoneOrder allow,denyAllow from allServerName www.test2.comDocumentRoot /www/test2/Options Indexes FollowSymLinksAllowOverride NoneOrder allow,denyAllow from allServerName www.test3.comDocumentRoot /www/test3/Options Indexes FollowSymLinksAllowOverride NoneOrder allow,denyAllow from all

4. After completion, test each virtual host, visit www.test1.com, www.test2.com, www.test3.com respectively

Third, based on the port

1. modify the configuration file

the original

Listen 80

changed to

Listen 80Listen 8080

2. Change virtual host settings:

DocumentRoot /var/www/test1/ServerName www.test1.comDocumentRoot /var/www/test2ServerName www.test2.com The above is how apache sets up virtual hosts. Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report