nginxとgunicorn の準備してみた。
【環境】さくらVPSでCent OS
nginxとgunicorn それぞれ動かせるようにインストールなどおこないました。
▶nginxインストール
nginxインストールの前に必要なパッケージをインストール
$ sudo yum install zlib-devel$ cd /usr/local/src/
$ wget http://nginx.org/download/nginx-1.0.4.tar.gz
$ tar zxf nginx-1.0.4.tar.gz
$ cd nginx-1.0.4
$./configure
$ make
$ make install
nginxのbinにpathを通して、起動と停止が出来るようになりました。
起動:$ sudo nginx
停止:$ sudo nginx -s stop
諸々の設定は下記で設定しました。
/usr/local/nginx/conf/nginx.conf
(一部抜粋)
server {
listen 80;#サーバーの番号設定
server_name 49.212.84.220;#charset koi8-r;
#access_log logs/host.access.log main;
#49.212.84.220にアクセスする時にここを通る
location / {#ドキュメントルート
root /home/megane;
index index.html index.htm;
}
http://49.212.84.220でドキュメントルートの中身が見れるはず・・
▶gunicornインストール
gunicornはpython2.5以上でないと動いてくれないようです。
さくらのVPSのデフォルトはpython2.4なので
python2.6をインストールしました。
python2.4のpathを変更してしまうと、
python2.4使用している諸々が動かなくなってしまうようなので注意が必要。
(yum動かなくなってかなりあせりました。)
ソースコードをダウンロードしてコンパイル。
(参考サイト) http://d.hatena.ne.jp/tohae/20081009/1223568809
$ yum install gunicorn
gunicorn にもpathを通す。
起動方法
conf.pyとtest.pyがある階層で以下のコマンド実行。
$ gunicorn -c conf.py test:app
このコマンドでは -c で設定ファイルの読み込み、test.pyを実行しています。
□conf.py
import osdef numCpus():
if not hasattr(os, 'sysconf'):
raise RuntimeError('No sysconf detected.')
return os.sysconf('SC_NPROCESSORS_ONLN')#任意の場所
bind = '49.212.84.220'
workers = numCpus() * 2 + 1
#workerにmeinheld使用してます。easy_install-2.6 でインストールするだけ
worker_class = 'egg:meinheld#gunicorn_worker'#任意の場所
pidfile = '/var/run/gunicorn/gunicorn.pid'
■test.py
def app(environ, start_response):
data = "Hello, World!\n"
start_response("200 OK", [
("Content-Type", "text/plain"),
("Content-Length", str(len(data)))
])
return iter([data])
これでへんなエラーが出たけど、その場合は
gunicornで指定されているpyhtonのpathを2.6の方に
変更してあげるとうまくいきました。
http://49.212.84.220:8000
でハローワールドが見えました。
gunicornの停止方法ってどうすれば良いのかわからない・・
今はこれで切ってます。
$sudo pkill gunicorn
設定の方法とか色々あやふやな部分多すぎるので、
色々いじりながら勉強します。
意味わからずに使ってる部分が多すぎて・・!泣ける・・!