venerdì 7 novembre 2014

Vagrant on port 80 (Mac OS X Yosemite, Mavericks and older)

On Mac OS X, you know, it's not possible to start Vagrant on 80 port.

In this article I will explain how to forward 80 port connections to 8080 port and configure a startup script to start this rule together with the Vagrant virtual machine.

Yosemite (10.10) and earlier

First, open your terminal and create a new packet filter anchor file:

~$ sudo nano /etc/pf.anchors/com.vagrant

Insert your password, if requested, then paste this line:
--
rdr pass on lo0 inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080
--

Save with CTRL+O, Enter, CTRL+X.

Now edit the packet filter configuration:

~$ sudo nano /etc/pf-vagrant.conf

Insert your password, if requested,  then paste these 3 lines:
--
rdr-anchor "com.vagrant"
load anchor "com.vagrant" from "/etc/pf.anchors/com.vagrant"

--
Note: put empty newline in the bottom of the file, or it won't work

Save with CTRL+O, Enter, CTRL+X.

To reload your rules, launch this command:

~$ sudo pfctl -ef /etc/pf-vagrant.conf

To merge port forwarding and vagrant start, we can create a simple script.

~$ sudo nano /usr/local/bin/vgup.sh

Insert your password, if requested,  then paste these 3 lines:
--
#!/bin/sh
sudo pfctl -ef /etc/pf-vagrant.conf
vagrant up
--

Save with CTRL+O, Enter, CTRL+X.

Add executing permission to the script:

~$ sudo chmod +x /usr/local/bin/vgup.sh

Now you can start your Vagrant machine with this command:

~$ vgup.sh

Mavericks (10.9) and older

On Mac OS X Mavericks and older versions activating a port forwarding is really simple!

~$ sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to any 80 in

As with Yosemite case, to merge port forwarding and vagrant start, we can create a simple script.

~$ sudo nano /usr/local/bin/vgup.sh

Insert your password, if requested,  then paste these 3 lines:
--
#!/bin/sh
sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to any 80 in
vagrant up
--

Save with CTRL+O, Enter, CTRL+X.

Add executing permission to the script:

~$ sudo chmod +x /usr/local/bin/vgup.sh

Now you can start your Vagrant machine with this command:

~$ vgup.sh

Sources:
http://www.abetobing.com/blog/port-forwarding-mac-osx-mavericks-port-80-8080-and-443-8443-79.html
http://www.abetobing.com/node/81