LoginSignup
14
13

More than 5 years have passed since last update.

HomebrewでインストールしたunboundをLaunchctlで動かす

Last updated at Posted at 2012-04-16

開発中のWebアプリがいくつもあって、いちいち/etc/hostsに書くのめんどくさいし、DNSサーバとかたてるのもめんどくさいとか思っていたけど、Unboundなら簡単に開発用のDNSサーバをたてられましたっていうお話。

手順は、Unboundを入れてiPhoneやiPadでローカルで開発しているWebアプリとかにアクセスできるようにするの通りなんですが、コマンドうつの忘れそうだし、常駐させようと考えlaunchctlで動かすことにしました。

前提

  • MacOSXです。
  • Homebrewを使います。
  • 開発機はlocalhost(127.0.0.1)
  • 開発用のdomain(適当なヤツ)にアクセスしてきたら、Unboundで127.0.0.1を返すように設定する
  • iPhoneやAndroidで開発機に接続したいときは、開発機に割り当てられているアドレスに変更する(127.0.0.1を192.xxxみたいなアドレスに変更)
  • 利用ケースとしては、*.dev.local.とかのサブドメインで何個もVirtualHostを切っているときに使います。

Unboundのインストール

$ brew install unbound

Unboundの設定

ポイントは、local-zonelocal-dataの設定です。
local-zoneに、ドメイン名とredirectを設定し、local-dataにドメインのAレコードを設定すれば、下記の設定だと、admin.myapp.dev.local.rh.jp.とかtest.myapp.dev.local.rh.jp.とかアクセスした場合は、127.0.0.1を返すので、複数のWebアプリをlocalhostで集約することができます。

ちなみに、unbound-controlコマンドを使う場合には、設定ファイルに、remote-control: control-enable: yesと設定するのと、unbound-control-setupコマンドを起動前に実行して証明書を作るのをわすれないようにしてください。コマンド使わない場合は、remote-controlを削除します。

# /usr/local/etc/unbound/unbound.conf
server:
    statistics-interval: 3600
    extended-statistics: yes
    interface: "0.0.0.0"
    access-control: 192.168.100.0/24 allow
    username: "roothybrid7"
    local-zone: "myapp.dev.local.rh7.jp." redirect
    local-data: "myapp.dev.local.rh7.jp. A 127.0.0.1"
remote-control:
    control-enable: yes

launchd.plistの設定

launchd.plistの設定はあまりよくわかっていないので、mongodbの設定をパクりました。ただ、unboundの起動は、一般ユーザではできないので、plistの置き場所を~/Library/LaunchAgentsではなく、/Library/LaunchDaemons/に配置します。
Lable名とunbound起動するコマンドとコマンド引数を記述すればよいです。
OS起動時に自動起動するには、OnDemandfalseにします。

ファイルパス: /Library/LaunchDaemons/homebrew.mxcl.unbound.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>Label</key>
  <string>homebrew.mxcl.unbound</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/sbin/unbound</string>
    <string>-c</string>
    <string>/usr/local/etc/unbound/unbound.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>OnDemand</key>
  <false/>
</dict>
</plist>

Launchctlでunboundの起動

$ sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.unbound.plist

これで

参考ページにあるように、DNSの設定を書き換えれば、開発用のドメインはすべてlocalhostにアクセスがいくようになります。

Webアプリが増殖できるので、開発が楽になりますね。

Apacheのサンプル設定

NameVirtualHost *:80

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

Alias /resources /Users/roothybrid7/Web/resources
<Directory "/Users/roothybrid7/Web/resources">
    Order Allow,Deny
    Allow from all
</Directory>
ErrorDocument 401 /resources/401.jpg
ErrorDocument 403 /resources/401.jpg
ErrorDocument 404 /resources/401.jpg

<VirtualHost *:80>
    ServerAlias *.myapp.dev.local.rh7.jp
    VirtualDocumentRoot "/Users/roothybrid7/Web/dev/sub/%1/public"

    Header add DeviceDebug "true"

    ErrorLog "/private/var/log/apache2/devsub-error_log"
    CustomLog "/private/var/log/apache2/devsub-access_log" combined
    Options FollowSymLinks

    Alias /shared /Users/roothybrid7/Web/dev/public/shared
    <Directory "/Users/roothybrid7/Web/dev/public/shared">
        Order Allow,Deny
        Allow from all
    </Directory>

    RewriteEngine On
    RewriteLogLevel 3
    RewriteLog "/private/var/log/apache2/devsub.rewrite.log"

    RewriteCond %{REQUEST_URI} !^/shared/(resource|test)/
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.myapp\.dev\.local\.rh7\.jp
    RewriteRule /shared(.*) http://myapp.dev.local.rh7.jp/shared$1 [P,L,QSA]

    ProxyPassReverse /shared http://myapp.dev.local.rh7.jp/shared
    ProxyPassReverseCookieDomain myapp.dev.local.rh7.jp %1.1+.myapp.dev.local.rh7.jp

    <Location />
        Options Indexes FollowSymLinks MultiViews
        Order Allow,Deny
        Allow from all
    </Location>
</VirtualHost>
<VirtualHost *:80>
    ServerName myapp.dev.local.rh7.jp
    DocumentRoot "/Users/roothybrid7/Web/dev/public"

    Header add DeviceDebug "true"
    Header set Access-Control-Allow-Origin "*"

    ErrorLog "/private/var/log/apache2/dev-error_log"
    CustomLog "/private/var/log/apache2/dev-access_log" combined
    Options FollowSymLinks

    RewriteEngine On
    RewriteLogLevel 3
    RewriteLog "/private/var/log/apache2/dev.rewrite.log"

    RewriteCond %{REQUEST_URI} !^/shared/(resource|test)/
    RewriteRule /shared(.*) http://localhost:8001$1 [P,L,QSA]

    ProxyPassReverse /shared http://localhost:8001

    <Location />
        Options Indexes FollowSymLinks MultiViews
        Order Allow,Deny
        Allow from all
    </Location>
</VirtualHost>

参考

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13