Domains with virtual host with Apache on Mac
I am currently running multiple domains for local development
http://wordpress.dev
http://phpmyadmin.dev
http://projectx.dev
http://projecty.dev
...
Most of these projects are located in the users' Sites directory, but some of them are located elsewhere:
/Users/[username]/Sites/wordpress
/Users/[username]/Sites/phpmyadmin
/Users/[username]/Sites/projectx
/Users/[username]/OtherDirectory/projecty
I am currently setting everything up by adding highlighted entries to /etc/hosts
and/etc/apache2/extra/httpd-vhosts.conf
/ etc / hosts:
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>
...
Is there a way to use some kind of lookup setting to map a domain to a specific directory like
http://foo.dev => /Users/[username]/Sites/foo
http://bar.dev => /Users/[username]/Sites/bar
...
but keep working projecty
(with some additional tweaks of course) so I need to create a folder in Sites
which is instantly accessible viahttp://[foldername].dev
[username]
can be hardcoded
source to share
Use mod_vhost_alias
andVirtualDocumentRoot
Until you catch VirtualHost foo.dev
as ServerName
or ServerAlias
, you should do the following:
<VirtualHost *:80>
ServerName default.dev
VirtualDocumentRoot /Users/youruser/%-2
...
...
</VirtualHost>
You will need a username for hardcoding, although you say this is not a problem.
%-2
in VirtualDocumentRoot
represents the penultimate part divided by dots foo.com
, that is foo
. Then you can have directories of your choice that are displayed on sites:
http://foo.dev => /Users/youruser/Sites/foo
http://bar.dev => /Users/youruser/Sites/bar
http://subdom.baz.dev => /Users/youruser/Sites/baz
You need to make sure that any additional domains you want to map this way have a matching entry in your hosts file or DNS if you are using that.
source to share
To accomplish what you need, you can set up a dynamically configured bulk virtual host.
To have this, you must add httpd.conf
something like the following to your file :
# 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
This will do the following:
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>
For more details on this, here
source to share