LoginSignup
4
3

More than 5 years have passed since last update.

Herokuで使われているIPアドレスを調べたくてPHP

Last updated at Posted at 2017-05-02

はじめに

Herokuからサーバの監視を行っています。エラーができたときにはHerokuのHubotがSlackでサーバエラーを通知してくれます。

Screen Shot 2017-05-02 at 12.13.51.png

ですが特定のホストで監視ツールがエラーを吐いていて、でもサーバは問題がなさそうだったために調査しました。

疑われたのはファイアウォールやWAFによってHerokuからのアクセスが弾かれたことです。

HerokuからSlackに通知してくれるHubotな監視ツールはこちらです

その為Herokuから接続しているIPアドレスを調べました。
「IPアドレスを固定するには…」という記事はあるのですが、HerokuのIPアドレスを調べる記事はそんなにありませんでした。
今回はログを追うために調べましたが、もちろん実際には動的アドレスなことに注意が必要です。

PHPのサンプルアプリケーション作成

Herokuでアプリケーションを作ります。
Herokuをいじるのが久しぶりでしたので、ざっくりとした復習です。

まずはログインします。

CLIでログイン
$ heroku login

Screen Shot 2017-05-02 at 12.44.33.png

続いてappを作成して初期化します。

appの作成と初期化
$ mkdir -p ~/tmp/heroku_test ;  cd ~/tmp/heroku_test # 作業用のディレクトリを掘る
$ git init # Git化する
$ heroku apps:create yousan-test # Heroku側にアプリケーションを作る。このコマンドでGit remoteもセットされるみたい
$ heroku buildpacks:set heroku/php # PHPを使うことの宣言

続いてPHPファイルを置きます。

php系のファイルを置いていく
$ touch composer.json # なんとなくcomposer.jsonを置いておく(不要
$ echo '<?php echo "hello world"; ?>' > index.php # Helloworld 作成
$ php index.php # 手元で確認
hello world

$ git add -A ; git commit -am 'hello world'; git push heroku master # Herokuにデプロイ
$ heroku open # 確認。ブラウザが開きます

ブラウザが開いてHello worldすればOKです。

Screen Shot 2017-05-02 at 12.25.34.png

IPアドレスの確認

グローバルアドレスを取得するには http://ifconfig.io/ip にアクセスすれば良いです。

自分のサーバのGlobalIPが知りたい
http://qiita.com/kuwa_tw/items/2cfba99958275e2a54ae
僕が一方的に尊敬する銀河さんの記事です。ifconfig.ioのホスト名がかっこいいですよね。
ところでこれIPv6だった場合にv4に変換できなくて困っています…、が、他のホストを使えば解決できます。
http://qiita.com/kanpou_/items/734b947f5a95109e7bb9

$ php -r 'echo file_get_contents("http://ifconfig.io/ip"); ' # 手元で確認
203.0.113.1 # 例示用
$ echo '<?php echo file_get_contents("http://ifconfig.io/ip"); ?>' > ifconfig_curl.php # ファイルに書き出し
$ git add -A ; git commit -am 'yahao'; git push heroku master # Herokuにデプロイ
$ heroku open ifconfig_curl.php # 確認 

Screen Shot 2017-05-02 at 12.10.27.png

確認できました。
幾つか作ってみましたが、IPアドレスは54.0.0.0/8ぐらいの範囲で広く変動しているようでした。
WhoisしたところAmazonと出たのでAWSのアドレスかなと思います。

$ whois 54.87.54.215

Screen Shot 2017-05-02 at 12.33.07.png

ファイアウォールでHerokuをホワイトリストに入れたりするときに参考にするようにします。
以上です。

テスト用のappを削除

テストが終わったら消します。確認のためにアプリケーション名を入れて削除です。

$ heroku apps:destroy

Screen Shot 2017-05-02 at 12.40.11.png

builcpackでエラー

プッシュした時にエラーが出てしまいました。
buildpackが指定されていないよ、というエラーです。指定したはずなのですがエラーになってしまいました。

$ git push heroku master
emote: Compressing source files... done.
remote: Building source:
remote:
remote:  !     No default language could be detected for this app.
remote:             HINT: This occurs when Heroku cannot detect the buildpack to use for this application automatically.
remote:             See https://devcenter.heroku.com/articles/buildpacks
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !   Push rejected to yousan-test3.
remote:

よくわからないですが下記のようにクリアするとうまくいきました。

$ heroku buildpacks:set heroku/php
 ▸    The buildpack heroku/php is already set on your app.


$ heroku buildpacks:clear
Buildpacks cleared. Next release on yousan-test3 will detect buildpack normally.


$ heroku buildpacks:set heroku/php
Buildpack set. Next release on yousan-test3 will use heroku/php.
Run git push heroku master to create a new release using this buildpack.


$ git push heroku master
4
3
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
4
3