CentOS6+nginx+MongoDB+RockMongo+Sleepy.Mongoose(REST API)

for nginx + php-fpm

yum install nginx
yum install php-fpm

service nginx start
service php-fpm start

chkconfig php-fpm on
chkconfig nginx on
#
# The default server
#
server {
    listen       80 default_server;
    server_name  _;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        #root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

for MongoDB

yum install mongo-10gen mongo-10gen-server
service mongod start
chkconfig mongod on

defaultの設定であれば
http://localhost:28017/
でサーバーStatusが見れる。

mongodのデフォルトのrestインターフェースを利用する為に、/etc/mondod.confに以下の1文を追加しておく。

rest = true

for RockMongo

yum install php-devel php-pear
yum remove httpd
yum remove httpd-tools
pecl install mongo

PHPにmongoのモジュールを組み込む。

peclの出力では

configuration option "php_ini" is not set to php.ini location
You should add "extension=mongo.so" to php.ini

と出るが、最近のPHPでは、/etc/php.d/に*.iniファイルを作るのが流儀らしいので
以下の様にした。

cat <<'EOF' >> /etx/php.d/mongo.ini
;Enable extension for monogo.so
extension=mongo.so
EOF

RockMongo本体のInstall

nginxが公開しているlocationにRockMongoを配置

wget http://rockmongo.com/release/rockmongo-1.1.5.zip
unzip rockmongo-1.1.5.zip

必要に応じて、config.phpを書き換える。
http://rockmongo.com/wiki/configuration?lang=en_us
localhostにinstallしたMongoDBであれば、何も設定しなくてもいけるっぽい。

初期UserとPassは[admin]

REST API for Mongod

install pymong

http://api.mongodb.org/python/current/

yum install python-devel
wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py
easy_install pymongo

install Sleepy.Mongoose

MongoDB用のHTTPインターフェースとして、Sleepy.Mongooseと呼ばれる物が存在する。
MongoDBのオフィシャルでも紹介されているが、このSleepy.Mongooseって全然メンテナンスされて無い気がする。

https://github.com/10gen-labs/sleepy.mongoose/wiki
http://www.kchodorow.com/blog/2010/02/22/sleepy-mongoose-a-mongodb-rest-interface/

git cloneなりwgetなり、好きな方法で拾ってくる。

wget https://github.com/10gen-labs/sleepy.mongoose/archive/master.tar.gz
mv master master.tar.gz
tar xzvf master.tar.gz

尚、オフィシャルの方では、HTTPのOPTIONSメソッドに対応していないので、必要があれば対応済みの物を利用すると良い。

https://github.com/mb21/sleepy.mongoose


Sleepy.Mongooseを、DB書き換え有り(-xオプション)で起動する。

python httpd.py -x

ちゃんと動いているか動作確認する

curl 'http://localhost:27080/_hello'

レスポンスは…なんだか良く判らないけど…

{"ok" : 1, "msg" : "Uh, we had a slight weapons malfunction, but uh... everything's perfectly all right now. We're fine. We're all fine here now, thank you. How are you?"}

Sleepy.Mongooseは動作テストを提供しているので走らせてみる。
Sleepy.Mongooseを展開したフォルダにて以下を実行。

easy_install restclient
python t/get.py
python t/post.py