Skip to content

Instantly share code, notes, and snippets.

@loren138
Forked from ryanzhou/pf.md
Last active August 29, 2015 14:07
Show Gist options
  • Save loren138/b0d5c2f7f62c747f71cb to your computer and use it in GitHub Desktop.
Save loren138/b0d5c2f7f62c747f71cb to your computer and use it in GitHub Desktop.

Getting Apache Port Forwarding to work in OS X Yosemite

Former code: http://echo.co/blog/os-x-109-local-development-environment-apache-php-and-mysql-homebrew

Some parts taken from: https://gist.github.com/kujohn/7209628

ipfw is officially deprecated and removed in OS X Yosemite. Pow requires another program pf to handle the port forwarding.

Note: While this does forward port 80, I haven't figured out how to get apache to see the incoming domain to make things like project.dev work correctly. If anyone gets that to work, please let me know how. (So visiting http://localhost/ or http://localhost/~username/ works as expected. Visiting http://project.dev does not.) I ended up starting apache as root and running it as _www for now.

1. Anchor file

Create file /etc/pf.anchors/apache

rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port = 80 -> 127.0.0.1 port 8080

Note: Trailing line break is important.

2. Reference anchor in pf.conf

Insert rdr-anchor "apache" and load anchor "apache" from "/etc/pf.anchors/apache" at correct places in /etc/pf.conf, so that it looks like this:

scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
rdr-anchor "apache"  # Apache port forwarding
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"
load anchor "apache" from "/etc/pf.anchors/apache"  # Apache port forwarding

3. Enabling pf

You can enable pf manually via sudo pfctl -ef /etc/pf.conf

However, to enable pf automatically after every boot, save the following as /System/Library/LaunchDaemons/com.apple.pfctl.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<!--<key>Disabled</key>
	<false/>-->
	<key>Label</key>
	<string>com.apple.pfctl</string>
	<key>WorkingDirectory</key>
	<string>/var/run</string>
	<key>Program</key>
	<string>/sbin/pfctl</string>
	<key>ProgramArguments</key>
	<array>
		<string>pfctl</string>
		<string>-e</string>
		<string>-f</string>
		<string>/etc/pf.conf</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment