ngrokでサクっとローカル環境を外部に公開!

www.yoheim.net

このサイトで紹介されていた ngrok が面白そうだと思って使ってみました。

上の記事では ngrok の本サイト https://ngrok.com/

からDownloadして〜とあったんですが、「ちょっと面倒だなぁ」と思ったので、Homebrewで探してみました。

$ brew cask search ngrok
==> Exact match
ngrok

あった。すげぇなHomebrew。

というわけで、サクっとインストール

$ brew cask install ngrok
==> Downloading https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-darwin-amd64.zip
######################################################################## 100.0%
==> Verifying checksum for Cask ngrok
==> Symlinking Binary 'ngrok' to '/usr/local/bin/ngrok'
🍺  ngrok was successfully installed!

// インストールされたか確認
$ ngrok                                                                               ⏎
NAME:
   ngrok - tunnel local ports to public URLs and inspect traffic

DESCRIPTION:
    ngrok exposes local networked services behinds NATs and firewalls to the
    public internet over a secure tunnel. Share local websites, build/test
    webhook consumers and self-host personal services.
    Detailed help for each command is available with 'ngrok help <command>'.
    Open http://localhost:4040 for ngrok's web interface to inspect traffic.

EXAMPLES:
    ngrok http 80                    # secure public URL for port 80 web server
    ngrok http -subdomain=baz 8080   # port 8080 available at baz.ngrok.io
    ngrok http foo.dev:80            # tunnel to host:port instead of localhost
    ngrok tcp 22                     # tunnel arbitrary TCP traffic to port 22
    ngrok tls -hostname=foo.com 443  # TLS traffic for foo.com to port 443
    ngrok start foo bar baz          # start tunnels from the configuration file

VERSION:
   2.1.18

AUTHOR:
  inconshreveable - <alan@ngrok.com>

COMMANDS:
   authtoken    save authtoken to configuration file
   credits  prints author and licensing information
   http     start an HTTP tunnel
   start    start tunnels by name from the configuration file
   tcp      start a TCP tunnel
   tls      start a TLS tunnel
   update   update ngrok to the latest version
   version  print the version string
   help     Shows a list of commands or help for one command

入ってますね。

使ってみる

適当なディレクトリに移動して、サーバーを立てます

$ cd /〇〇/ngrok-test/

$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...

別のタブを開いて、ngrokの設定をします。

// 上で指定したポートを指定。
$ ngrok http 8080

ngrok by @inconshreveable                                                           (Ctrl+C to quit)

Session Status                online
Version                       2.1.18
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://〇〇.ngrok.io -> localhost:8080
Forwarding                    https://〇〇.ngrok.io -> localhost:8080

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

ここでブラウザから http://〇〇.ngrok.io にアクセスすると、ローカルファイルがひらきます。 すごくね?

ポートが振られていれば、MAMPなどで作成したWordpressのサイトも公開することが出来ます。

公開用のURLは毎回ランダムの文字列で構成される上、ターミナルを落とせば見れなくなります。
なので、リモートにいる仲間にローカルファイルをサクッと確認してもらう時に便利かと思います。