akishin999の日記

調べた事などを書いて行きます。

RequestBin をインストールしてみる

発行された URL に対して送信された HTTP リクエストの内容を確認することが出来る Webサービス RequestBin ですが、ソースコードも MIT ライセンスで公開されている ようなので、今回は自前のサーバに立ててみました。
構築した環境は CentOS 6.5 x86_64 になります。

Python 2.7 のインストール

最初に CentOS 6 にデフォルトで入っていた Python 2.6.6 で試してみたのですが、インストールは出来たものの上手く動作してくれませんでした。
そのため、まずは Python 2.7 を入れるところから始めました。

まずはビルドに必要なライブラリをインストール。

# yum groupinstall -y "Development tools"
# yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel

必須ではないですが、ソースコードからインストールするので綺麗に消せるよう EPEL から paco も入れておきます。

# rpm --import http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
# yum install -y http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# yum install -y paco

2.7.6 をダウンロードして configure します。

# cd /usr/local/src
# wget http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz
# tar xzf Python-2.7.6.tgz
# cd Python-2.7.6
# ./configure --prefix=/usr/local

SSL サポートを有効にするためここで一手間かけます。
まずはインストールされている OpenSSL の場所を確認。

# find / -name openssl
/usr/lib64/openssl
/usr/bin/openssl
/usr/include/openssl
/etc/pki/ca-trust/extracted/openssl

Setup ファイルを編集します。

# vi ./Modules/Setup

218 行目辺りからのコメントアウトを解除して、「SSL=/usr/local/ssl」のところを先ほど調べた OpenSSL インストールパス(/usr)に修正します。

218 SSL=/usr
219 _ssl _ssl.c \
220         -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
221         -L$(SSL)/lib -lssl -lcrypto

修正したら make して make altinstall します。
paco 経由で実行するのを忘れずに。

# make
# paco -D make altinstall

バージョンを確認します。

# python2.7 -V
Python 2.7.6

以下を実行してエラーが出なければ SSL サポートが有効になっています。

# python2.7 -c "import ssl"

setuptools をインストールしておきます。

# cd /usr/local/src
# wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
# python2.7 ez_setup.py
# easy_install-2.7 --version
setuptools 3.3

pip もインストールしておきます。

# easy_install-2.7 pip
# pip2.7 --version
pip 1.5.4 from /usr/local/lib/python2.7/site-packages/pip-1.5.4-py2.7.egg (python 2.7)

これで Python 2.7 環境の準備ができました。

RequestBin のインストール

libevent-devel が必要なのでインストールしておきます。

# yum install -y libevent-devel

/opt 以下に requestbin を clone し、pip で必要なライブラリをインストールします。

# cd /opt/
# git clone git://github.com/Runscope/requestbin.git
# cd requestbin/
# pip install -r requirements.txt

RequestBin はデフォルトではメモリ上にデータを保持しますが、今回は Redis を使うようにしたいと思います。
ここでは Redis インストール用に Remi リポジトリを追加しました。

# rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
# yum install -y http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

リポジトリを追加したら Redis をインストールして起動しておきます。

# yum --enablerepo=remi install -y redis
# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
# sysctl -p
# service redis start
# chkconfig redis on
# chkconfig --list redis
redis           0:off   1:off   2:on    3:on    4:on    5:on    6:off

RequestBin の設定ファイルを編集。

# vi requestbin/config.py

ROOT_URL の値を環境に合わせて修正します。

ROOT_URL = "http://192.0.2.1:4000"

STORAGE_BACKEND の値を RedisStorage を変更します。

STORAGE_BACKEND = "requestbin.storage.redis.RedisStorage"

これでインストール自体は完了ですが、起動・終了を簡単にするため、Supervisor で管理するようにしてみます。

Supervisor のインストール

easy_install コマンドから supervisor をインストールします。

# easy_install supervisor

設定ファイルを作成して編集。

# echo_supervisord_conf > /etc/supervisord.conf
# vi /etc/supervisord.conf

ファイル末尾に以下の追記します。

[include]
files = /etc/supervisord.d/*.conf

指定したディレクトリを実際に作成。

# mkdir /etc/supervisord.d

Supervisor 用の起動スクリプトを配置します。

# cd /usr/local/src/
# git clone git://github.com/Supervisor/initscripts.git
# cp initscripts/redhat-init-jkoppe /etc/init.d/supervisord
# cp initscripts/redhat-sysconfig-jkoppe /etc/sysconfig/supervisord
# chkconfig --add supervisord

このままだと supervisord コマンドのパスが若干異なるため上手く動かないので、起動スクリプトを修正します。

# vi /etc/init.d/supervisord

スクリプト内のコマンドのパスを以下のように修正します。

  • /usr/bin/supervisord を /usr/local/bin/supervisord に変更。
  • /usr/bin/supervisorctl を /usr/local/bin/supervisorctl に変更。

次に以下のファイルを編集します。

# vi /etc/sysconfig/supervisord

PIDFILE のパスを /etc/supervisord.conf の内容に合わせて以下のように修正します。

PIDFILE=/tmp/supervisord.pid

これで Supervisor 自体の準備はできたので、いよいよ RequestBin 用の設定ファイルを作成します。

# vi /etc/supervisord.d/requestbin.conf

以下のような内容にしました。

[program:requestbin]
directory=/opt/requestbin
command=/usr/local/bin/python2.7 web.py
user=requestbin
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/requestbin.log
stopasgroup=true

RequestBin の場合、「stopasgroup=true」が無いと上手くプロセスを終了できないようなので注意してください。
指定した起動用ユーザを作成。

# useradd -s /sbin/nologin -M requestbin

supervisord サービスを起動します。
自動的に requestbin も起動したことがわかります。

# service supervisord start
Starting supervisord:
requestbin                       STARTING

これでブラウザで対象サーバのポート 4000 番にアクセスすると RequestBin を使用することができます。

Nginx の設定

ここまででも問題なく使用できるのですが、毎回ポート番号を指定してアクセスするのも面倒なので、Nginx 経由でアクセスするようにしてみます。
まずは Nginx をインストール。

# yum install -y http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# yum install -y nginx

設定ファイルを編集します。

# vi /etc/nginx/conf.d/default.conf

location / の設定を以下のように変更しました。

location / {
  proxy_pass         http://localhost:4000;
  proxy_redirect     off;

  proxy_set_header   Host              $host;
  proxy_set_header   X-Real-IP         $remote_addr;
  proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
  proxy_set_header   X-Forwarded-Proto $scheme;
}

Nginx を起動します。

# service nginx start

これでポート番号を指定しなくても RequestBin へアクセスできるようになります。

Webサービスとして公開されているし、Heroku を使うならもっと簡単に動かせるようですが、外部に公開されていない環境に自前で一つ立てておくと気軽に使えるのでそれはそれで便利なんじゃないかなー、と思います。

参考サイト

How to install Python 2.7 and Python 3.3 on CentOS 6 | Too Much Data
http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/

Codrspace - Run RequestBin locally to debug restful APIs by glenbot
http://codrspace.com/glenbot/run-requestbin-locally-to-debug-restful-apis/

GracefulExit - Running Flask App and Celery with Supervisor on Vagrant
http://papaeye.tumblr.com/post/76624602909/running-flask-app-and-celery-with-supervisor-on-vagrant