ラベル nginx の投稿を表示しています。 すべての投稿を表示
ラベル nginx の投稿を表示しています。 すべての投稿を表示
2012年7月6日金曜日

CentOS 6 でRails + thin + nginxを動かした

thinってオワコンじゃないですよね?herokuで採用されているし別にいいですよね。
本当はunicorn使いたかったんですが難しい…… ><;
ぐぐると記事沢山出てきますが uninitialized constant errorで起動しない問題が解決できなくてやめちゃいました。いつかリベンジします。

CentOS 6

マシンはvagrant使ってcentos6を用意しました。
参考:http://www.ryuzee.com/contents/blog/4292
vagrantって便利ですね。元々Virtual Boxでwindows動かしてたのですんなりです。ホントに。するっと。

IPアドレスと

config.vm.network :hostonly, "192.168.33.10"

メモリとnameを設定しておきました

config.vm.customize do |vm|
  vm.memory_size = 1024
  vm.name = 'vagrant_centos6'
end

Ruby

rbenv + ruby-buildで1.9.3-p194をインストール

Thin

Gemfileのgroup :productionにgem 'thin'を追加して普通にbundle install

config/thin.ymlは

pid: tmp/pids/thin.pid
log: log/thin.log
servers: 1
port: 3000
daemonize: true
environment: production
user: vagrant
group: staff

以下で起動、停止、再起動

$ be thin start|stop|restart -C config/thin.yml

Nginx

yumでnginxをインストール(参考:http://blog.livedoor.jp/sasata299/archives/51810645.html

/etc/nginx/nginx.confは

worker_processes  1; 

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;

    # include /etc/nginx/conf.d/*.conf;

    upstream backend {
        server localhost:3000;
    }
    upstream backend2 {
        server localhost:3001;
    }
    server {
        listen 80;
        server_name centos6.vag;
        location / {
            proxy_pass http://backend;
        }
    } 
    server {
        listen 80;
        server_name 2.centos6.vag;
        location / {
            proxy_pass http://backend2;
        }
    } 
}

試す

あとはmacのhostsに

192.168.33.10 centos6.vag
192.168.33.10 2.centos6.vag

を設定してブラウザでアクセス!

サクセス!

でもnginx.confの書き方冗長だなぁもっといいのあるんだろうか。

※追記

上記の状態で試しに request.url を覗いてみたら http://backend2/ とかでちゃったよ!困るよ!

というわけでnginx.confを一部修正してみたら問題なく動いた

    ...省略...
    upstream 2.centos6.vag {
        server localhost:3001;
    }
    server {
        listen 80;
        server_name 2.centos6.vag;
        location / {
            proxy_pass http://2.centos6.vag;
        }
    }
    ...省略...

request.url でhttp://2.centos6.vagが返ってきた!

※追記2

画像など静的ファイルが返らないことがわかったので以下のようにconfig修正

    server {
        listen 80;
        server_name 2.centos6.vag;

        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    Host            $http_host;
        proxy_redirect      off;
        proxy_max_temp_file_size    0;

        location ~ .*\.(jpg|JPG|gif|GIF|png|PNG|swf|SWF|css|CSS|js|JS|inc|INC|ico|ICO) {
            root /home/vagrant/sample2/public;
            ssi  on;
            break;
        }

        location / {
            proxy_pass http://2.centos6.vag;
        }
    }

※追記3

thinの再起動などダウンタイムにnginxのデフォルトエラーページが返されるのでconfig追加

     error_page   500 502 503 504  /500.html;↲
        location = /500.html {↲
        root   /home/vagrant/sample2/public;↲
     }

※追記4

マシンを再起動したときにthinも自動起動するようにした!

/etc/init.d/sampleを作成

#!/bin/sh
# chkconfig: 2345 86 25
# description: Sample
#

PATH=/home/vagrant/.rbenv/shims:/home/vagrant/.rbenv/bin:$PATH
eval "$(rbenv init -)"

APP_DIR=/home/vagrant/sample
THIN_CONFIG=$APP_DIR/config/thin.yml

case $1 in
  start)
  cd $APP_DIR
  bundle exec thin start -C $THIN_CONFIG
  ;;
  stop)
  cd $APP_DIR
  bundle exec thin stop -C $THIN_CONFIG
  ;;
  restart)
  cd $APP_DIR
  bundle exec thin restart -C $THIN_CONFIG
  ;;
esac

exit 0

登録する

$ sudo /etc/init.d/sample start
$ sudo chkconfig sample on

2012年4月16日月曜日

OS X lion にhomebrewでnginxとphpをインストールした

まずはnginxのインストール。

$ brew options nginx
nginx
--with-passenger
 Compile with support for Phusion Passenger module
--with-webdav
 Compile with support for WebDAV module

今回はどちらもいらないので普通に brew install nginx で設定をいじる。

$ cd /usr/local/etc/nginx
$ vim nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    include /usr/local/etc/nginx/sites-enabled/*;
}
こうしておいた
$ mkdir sites-enable
$ mkdir sites-available

このディレクトリ必要なの?って思ったけど、シンボリックリンクの作成/削除で複数ファイルの管理ができるよってことなんですね。なんかかっこいいですね。

で、availableの方に…

$ vim sites-available/localhost
server {
    listen 8080;
    server_name localhost;
    root /Users/kozo/Web/nginx/localhost/public;

    access_log  /Users/kozo/Web/nginx/localhost/logs/access.log;
    error_log  /Users/kozo/Web/nginx/localhost/logs/error.log warn;

    location / {
        index index.html index.htm index.php;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }
}

とした。

続いてphpのインストール。

$ brew options https://raw.github.com/ampt/homebrew/php/Library/Formula/php.rb
php
--with-mysql
 Include MySQL support
--with-pgsql
 Include PostgreSQL support
--with-mssql
 Include MSSQL-DB support
--with-fpm
 Enable building of the fpm SAPI executable
--with-apache
 Build shared Apache 2.0 Handler module
--with-intl
 Include intl extension
--with-readline
 Include readline extension

適当にオプション付けて

$ brew install https://raw.github.com/ampt/homebrew/php/Library/Formula/php.rb --with-mysql --with-pgsql  --with-fpm --with-intl --with-readline

そしたらエラー!
ココでも見てなんとかしろや的なこと言われたので見てみる
https://github.com/mxcl/homebrew/wiki/checklist-before-filing-a-new-issue

brew updateか。

$ brew update

またエラー。というかgitのリポジトリでconflict的なことが起きてるのかな?

$ cd /usr/local/
$ git fetch origin/master
$ git reset --hard origin/master
$ brew update
Already up-to-date.

OK! phpのインストール出来ました。 このままだとcliがMacにプリインストールされてる方を見ちゃうので

$ vim ~/.bash_profile
$ export PATH="$(brew --prefix)/bin:$PATH" 

上記を追加しました。

$ php -v
PHP 5.3.10 (cli) (built: Apr 16 2012 17:15:01) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.1.2, Copyright (c) 2002-2011, by Derick Rethans

よし。で、fpmの起動用plist作成

$ vim ~/Library/LaunchAgents/php-fpm.plist
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version='1'>
  <dict>
    <key>Label</key><string>org.php.php-fpm</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/sbin/php-fpm</string>
      <string>--fpm-config</string>
      <string>/usr/local/Cellar/php/5.3.10/etc/php-fpm.conf</string>
    </array>
    <key>Debug</key><false/>
    <key>RunAtLoad</key><true/>
    <key>KeepAlive</key><false/>
    <key>UserName</key><string>kozo</string>
  </dict>
</plist>

※.plistの拡張子付けないと後のlaunchctl loadで怒られました

php-fpm起動!

$ launchctl load -w ~/Library/LaunchAgents/php-fpm.plist

最終確認

$ cd ~/Web/nginx/localhost/public/
$ vim index.php
<?php echo 'Hello' ?>

と書いて http://localhost:8080/ を開いて Hello でました! 思ってたより簡単だったけどちょっと戸惑った。

参考にしたサイト

  • http://aoyagikouhei.blog8.fc2.com/blog-entry-195.html
  • http://d.hatena.ne.jp/YuhoYo/20110528/1306547056
  • http://labs.uechoco.com/blog/2011/12/php-homebrew-how-to-compile-intl-enabled-php.html
  • http://www26.atwiki.jp/nginx/pages/13.html
  • http://www.1x1.jp/blog/2011/05/yum_install_nginx_php-fpm.html

※追記 2012/04/17

再起動はこうすればいいらしい:

$ sudo nginx -s stop && sudo nginx