在Mac上使用带有Apache的虚拟主机的通配符域

时间:2014-10-26 16:35:21

标签: apache localhost virtualhost

我目前正在为本地开发运行多个域

http://wordpress.dev
http://phpmyadmin.dev
http://projectx.dev
http://projecty.dev
...

这些项目大多位于用户"网站"目录,但有些位于其他地方:

/Users/[username]/Sites/wordpress
/Users/[username]/Sites/phpmyadmin
/Users/[username]/Sites/projectx
/Users/[username]/OtherDirectory/projecty

我目前通过向/etc/hosts/etc/apache2/extra/httpd-vhosts.conf添加专用条目来设置所有内容

的/ etc /主机:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0     localhost
# Virtuelle Hosts
127.0.0.1       wordpress.dev
127.0.0.1       phpmyadmin.dev
127.0.0.1       projectx.dev
....

/etc/apache2/extra/httpd-vhosts.conf:

<VirtualHost *:80>
    ServerName [PROJECT].dev
    ServerAlias [PROJECT].dev
    DocumentRoot /Users/[username]/Sites/[PROJECT]/
    <Directory /Users/[username]/Sites/[PROJECT]/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
<VirtualHost *:80>
 ...

有没有办法使用某种通配符设置将域映射到某个目录,如

http://foo.dev =>  /Users/[username]/Sites/foo
http://bar.dev =>  /Users/[username]/Sites/bar
...

同时保持projecty正常工作(当然还有一些额外的设置)所以我只需要在Sites创建一个文件夹,可以通过http://[foldername].dev立即访问

[username]可以硬编码

2 个答案:

答案 0 :(得分:2)

使用mod_vhost_aliasVirtualDocumentRoot

只要您没有将foo.devServerName作为ServerAlias,您应该能够执行以下操作:

<VirtualHost *:80>
  ServerName default.dev

  VirtualDocumentRoot /Users/youruser/%-2
  ...
  ...
</VirtualHost>

你需要硬编码用户名,尽管你说这不是问题。

%-2中的

VirtualDocumentRoot代表foo.com的倒数第二个点分隔部分,即foo。然后,您可以根据需要将目录映射到站点:

http://foo.dev =>  /Users/youruser/Sites/foo
http://bar.dev =>  /Users/youruser/Sites/bar
http://subdom.baz.dev =>  /Users/youruser/Sites/baz

您需要确保以这种方式映射的任何其他域在您的hosts文件中具有适当的条目,或者如果您正在使用它,则需要DNS。

答案 1 :(得分:0)

为了满足您的需求,您可以配置动态配置的群发虚拟主机。

要拥有它,您应该在httpd.conf文件中添加以下内容:

# get the server name from the Host: header
UseCanonicalName Off

# this log format can be split per-virtual-host based on the first field
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon

# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /www/hosts/%0/docs
VirtualScriptAlias /www/hosts/%0/cgi-bin 

这将与以下内容相同:

NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
    ServerName www.customer-1.com
    DocumentRoot /www/hosts/www.customer-1.com/docs
    ScriptAlias /cgi-bin/ /www/hosts/www.customer-1.com/cgi-bin
</VirtualHost>
<VirtualHost 111.22.33.44>
    ServerName www.customer-2.com
    DocumentRoot /www/hosts/www.customer-2.com/docs
    ScriptAlias /cgi-bin/ /www/hosts/www.customer-2.com/cgi-bin
</VirtualHost>
# blah blah blah
<VirtualHost 111.22.33.44>
    ServerName www.customer-N.com
    DocumentRoot /www/hosts/www.customer-N.com/docs
    ScriptAlias /cgi-bin/ /www/hosts/www.customer-N.com/cgi-bin
</VirtualHost> 

有关详细信息,请查看here