Apache搭建多个站点方法详解

五月 10, 2019 | views
Comments 0

Apache的虚拟主机是一种允许在同一台机器上配置多个不同站点的web服务器环境的,就是iis一样可以创建多站点了,但是apache需要在编辑状态操作,不能像windows iis直接点击几下就好了,以下是介绍配置的方法。

最平常的大概有3种方法。

第一种:单IP不同端口

第二种:多IP同端口(独立IP的虚拟空间)

第三种:域名绑定根目录的方式(共享IP的虚拟空间)

Apache的核心配置文件名是”httpd.conf”,其所存放的路径在Apache目录下的conf文件夹下。修改它只需要使用记事本(建议使用其他编辑器,带行数的那种,方便修改),生效的话只需要保存httpd.conf,重启apache即可。

下面多站点支持的话,修改httpd.conf的第187~264行(不同的httpd.conf可能有差异),也就是在ServerAdmin和ServerName那里,大部分是注释。下面是主要修改的地方。

注意:如果是服务器请备份httpd.conf后再修改文件。


  1. # 'Main' server configuration 
  2. # 
  3. # The directives in this section set up the values used by the 'main' 
  4. # server, which responds to any requests that aren't handled by a 
  5. # <VirtualHost> definition.  These values also provide defaults for 
  6. # any <VirtualHost> containers you may define later in the file. 
  7. # 
  8. # All of these directives may appear inside <VirtualHost> containers, 
  9. # in which case these default settings will be overridden for the 
  10. # virtual host being defined. 
  11. # 
  12. # 
  13. # ServerAdmin: Your address, where problems with the server should be 
  14. # e-mailed.  This address appears on some server-generated pages, such 
  15. # as error documents.  e.g. admin@your-domain.com 
  16. # 
  17. ServerAdmin admin@example.com 
  18. # 
  19. # ServerName gives the name and port that the server uses to identify itself. 
  20. # This can often be determined automatically, but we recommend you specify 
  21. # it explicitly to prevent problems during startup. 
  22. # 
  23. # If your host doesn't have a registered DNS name, enter its IP address here. 
  24. # 
  25. ServerName www.example.com:80 
  26. # 
  27. # Deny access to the entirety of your server's filesystem. You must 
  28. # explicitly permit access to web content directories in other  
  29. # <Directory> blocks below. 
  30. # 
  31. <Directory /> 
  32.     AllowOverride All 
  33.     Require all denied 
  34. </Directory> 
  35. # 
  36. # Note that from this point forward you must specifically allow 
  37. # particular features to be enabled - so if something's not working as 
  38. # you might expect, make sure that you have specifically enabled it 
  39. # below. 
  40. # 
  41. # 
  42. # DocumentRoot: The directory out of which you will serve your 
  43. # documents. By default, all requests are taken from this directory, but 
  44. # symbolic links and aliases may be used to point to other locations. 
  45. # 
  46. DocumentRoot "g:/www" 
  47. <Directory "g:/www"
  48.     # 
  49.     # Possible values for the Options directive are "None", "All", 
  50.     # or any combination of: 
  51.     #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews 
  52.     # 
  53.     # Note that "MultiViews" must be named *explicitly* --- "Options All" 
  54.     # doesn't give it to you. 
  55.     # 
  56.     # The Options directive is both complicated and important.  Please see 
  57.     # http://httpd.apache.org/docs/2.4/mod/core.html#options 
  58.     # for more information. 
  59.     # 
  60.     Options Indexes FollowSymLinks 
  61.     # 
  62.     # AllowOverride controls what directives may be placed in .htaccess files. 
  63.     # It can be "All", "None", or any combination of the keywords: 
  64.     #   Options FileInfo AuthConfig Limit 
  65.     # 
  66.     AllowOverride All 
  67.     # 
  68.     # Controls who can get stuff from this server. 
  69.     # 
  70.     Require all granted 
  71. </Directory> 

第一种一般是测试环境,毕竟加了端口,如何绑定域名,访问的时候域名后面也需加端口。

例子分别通过80和8080访问不同的根目录,大概在50几行有个Listen 80,在下面添加8080端口。



  1. Listen 80 
  2. Listen 8080<VirtualHost *:80>  
  3.     ServerAdmin admin@myxzy.com  
  4.     ServerName localhost:80  
  5.     DocumentRoot "g:/www1"  
  6.      <Directory "g:/www1">  
  7.      Options  Indexes FollowSymLinks  
  8.      AllowOverride All  
  9.      Require all granted  
  10.    </Directory>    
  11. </VirtualHost>  
  12. <VirtualHost *:8080>  
  13.     ServerAdmin admin@myxzy.com 
  14.     ServerName localhost:8080   
  15.     DocumentRoot "g:/www2"  
  16.    <Directory "g:/www2">  
  17.      Options Indexes FollowSymLinks  
  18.      AllowOverride All  
  19.      Require all granted  
  20.    </Directory>        
  21. </VirtualHost> 

第二种多IP同端口。

IP地址1:192.168.2.2  IP地址2:192.168.1.68  端口同是80端口。



  1. <VirtualHost 192.168.1.68:80>  
  2.     ServerAdmin admin@myxzy.com  
  3.     ServerName localhost:80  
  4.     DocumentRoot "g:/www1"  
  5.      <Directory "g:/www1">  
  6.      Options FollowSymLinks  
  7.      AllowOverride All  
  8.      Require all granted  
  9.    </Directory>    
  10. </VirtualHost>  
  11. <VirtualHost 192.168.2.2:80>  
  12.     ServerAdmin admin@myxzy.com 
  13.     ServerName localhost:80  
  14.     DocumentRoot "g:/www2"  
  15.    <Directory "g:/www2">  
  16.      Options FollowSymLinks  
  17.      AllowOverride All  
  18.      Require all granted  
  19.    </Directory>        
  20. </VirtualHost> 

第三种同IP不同域名和根目录(域名的话修改本地host演示)。



  1. <VirtualHost 192.168.2.2:80>  
  2.     ServerAdmin admin@myxzy.com  
  3.     ServerName www.hzhuti.com  
  4.     DocumentRoot "g:/www1"  
  5.      <Directory "g:/www1">  
  6.      Options FollowSymLinks  
  7.      AllowOverride All  
  8.      Require all granted  
  9.    </Directory>    
  10. </VirtualHost>  
  11. <VirtualHost 192.168.2.2:80>  
  12.     ServerAdmin admin@myxzy.com 
  13.     ServerName www.111cn.net 
  14.     DocumentRoot "g:/www2"  
  15.    <Directory "g:/www2">  
  16.      Options FollowSymLinks  
  17.      AllowOverride All  
  18.      Require all granted  
  19.    </Directory>        
  20. </VirtualHost> 




zend