Autohosts for Symfony projects on Mac
We all know, that web development is not as easy as it sounds and that it involves lot of different and sometimes really complex tools to just run or test stuff. We also know, that bootstrap (setup) of the project could take more time than you want it to. It’s not a big deal if you’re working on single project full time, but at KnpLabs, i’m bootstraping new sf2 project every 2 weeks. So this manual crafting of virtual host, edition of /etc/hosts and maintaining of those temp hosts everywhere made me really sick. So, at some point i’ve started to look for an elegant solution. Here is it.
Apache2 supports autohosts out of the box - you just need to configure them. On Mac it’s really easy, just put:
NameVirtualHost *:80
UseCanonicalName Off
<Directory "/Users/YOUR_USER/Sites/*/web">
AllowOverride All
Allow from All
Options +FollowSymLinks
</Directory>
<Directory "/Users/YOUR_USER/Sites/*/web/cgi-bin">
Allow from All
Options +ExecCGI
</Directory>
VirtualDocumentRoot "/Users/YOUR_USER/Sites/%0/web"
into vim /etc/apache2/users/autohosts.conf. After that restart Apache (sudo apachectl restart) and forget this action (apache restarting) forever. From this point, every subfolder with web subdirectory inside your ~/Sites folder will be autoloaded as proper vhost. No need to restart Apache, just put your shiny downloaded symfony distribution into ~/Sites/symfony.dev and you’ll automagically will have virtual host for it.
Awesome, right? Well… not yet. You still need to add 127.0.0.1 symfony.dev to your /etc/host. But no worries, i have solution for this problem too - we’ll use local DNS forwarder for that. It’s easier than it sounds, actually:
First, install dnsmasq. I use homebrew for that:
$> brew install dnsmasq
Next, copy default dnsmasq configuration:
$> cp /usr/local/Cellar/dnsmasq/2.63/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf
And add dev domain resolving rule to it:
$> print "address=/dev/127.0.0.1" >> /usr/local/etc/dnsmasq.conf
Copy launch list to autostart dnsmasq on boot:
$> sudo cp /usr/local/Cellar/dnsmasq/2.63/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
And run it:
$> sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
Next step is to add rule for dev domain to resolver configuration:
$> sudo print "nameserver 127.0.0.1\ndomain dev" > /etc/resolver/dev
And general rule, which will be used when you’re offline (without internet connection) as there’s bug in Mac OS resolver service:
$> sudo print "nameserver 127.0.0.1\ndomain ." > /etc/resolver/offline
And that’s it. You’re ready to go. Download latest symfony version from symfony.com, put it into ~/Sites, add .dev suffix and you have working website with virtual host on your local machine.
P.S.: You might need restart your WiFi in order to update resolver config.
1 Notes/ Hide
-
shashikant86 reblogged this from everzet
-
everzet posted this
