mysqlでRailsプロジェクトをスタート
プロジェクトディレクトリを作成し,bundle init ,さらにbundle install
$ mkdir dk_port $ cd dk_port $ bundle init
bundle init
で作成されたGemfileでrailsを有効にする
$ emacs Gemfile
source "https://rubygems.org" gem 'rails'
bundleで入れるものは vender/bundle に配置する.railsコマンドについても,bundle exec ~~
として利用する.
gem をインストールするパスを指定して,bundle install
する.
$ bundle install --path vendor/bundle
rails new で プロジェクトを新規作成
ここで,プロジェクトを新規作成.データベースにはMySQLを指定する.
$ bundle exec rails new . -d mysql -T --skip-bundle --edge
bundle install すると必要なGemが入る
$ emacs Gemfile source 'https://rubygems.org' gem 'rails', github: 'rails/rails' gem 'arel', github: 'rails/arel' # Use mysql as the database for Active Record gem 'mysql2' # Use edge version of sprockets-rails gem 'sprockets-rails', github: 'rails/sprockets-rails' # Use SCSS for stylesheets gem 'sass-rails', github: 'rails/sass-rails' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', github: 'rails/coffee-rails' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 1.2' group :doc do # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', require: false end # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.1.2' # Use unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano', group: :development # Use debugger # gem 'debugger', group: [:development, :test]
Gitリポジトリを初期化
この辺りでgitを設定する.
$ git init $ git add . $ git commit -m "first commit"
プロジェクト用のMySQLユーザーを作成する
MySQLにログインしてユーザーを作成する.
$ mysql -u root -p # パスワード入力
ユーザー作成.
mysql> GRANT ALL PRIVILEGES ON *.* TO ユーザー名@localhost IDENTIFIED BY 'パスワード';
ユーザーができているか確認
mysql> select host,user from mysql.user;
config/database.ymlで設定を編集
$ emacs config/database.yml # MySQL. Versions 4.1 and 5.0 are recommended. # # Install the MYSQL driver # gem install mysql2 # # Ensure the MySQL gem is defined in your Gemfile # gem 'mysql2' # # And be sure to use new-style password hashing: # http://dev.mysql.com/doc/refman/5.0/en/old-client.html development: adapter: mysql2 encoding: utf8 database: railsapp_development pool: 5 username: DBユーザー名 #ここと password: DBパスワード #ここだけ socket: /tmp/mysql.sock host: localhost charset: utf8 collation: utf8_general_ci # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: adapter: mysql2 encoding: utf8 database: railsapp_test pool: 5 username: DBユーザー名 password: DBパスワード socket: /tmp/mysql.sock host: localhost charset: utf8 collation: utf8_general_ci production: adapter: mysql2 encoding: utf8 database: railsapp_production pool: 5 username: DBユーザー名 password: DBパスワード socket: /tmp/mysql.sock host: localhost charset: utf8 collation: utf8_general_ci
rails g を用いてControllerを作成し,確認
$ bundle exec rails g controller Hello index $ bundle exec rails s
http://localhost:3000/hello/index
削除するには
$ bundle exec rails destroy controller Hello index
Nginx + Unicornで複数のRailsアプリケーションをサブディレクト形式で起動
事前にサーバ準備は完了している前提
サーバはubuntuを使用
構成
- Webサーバ:Nginx(他にもApacheなどが有名)
- サーバ単位で動作させる
- アプリケーションサーバ:unicorn(他にも
Passenger,Thin,Mongrelなど)
- Railsアプリケーション単位で動作させる
インターネット == Nginx == [unicorn - Rails App]
アクセスをNginxが管理し、各アプリケーション等(unicorn)へ振り分ける
Nginxの設定
/etc/nginx
にあるnginx.conf
がNginxの全体設定ファイル。さらに、そこから詳細設定ファイルをconf.d/
(もしくはsite-enable
)をインクルードしている。
conf.d
にmyapp.conf
を作成し、設定を記述。
unicornとの接続にはunixソケットを利用するため、ソケット部分はnginxとunicornの設定ファイルで揃えること。
# unixソケット、Railsの/tmpあたりに設定する upstream unicorn { server unix:/[path_to_rails_root]/tmp/unicorn.sock fail_timeout=0; } server { listen 80; server_name xxx.xxx.xxx.xxx; # ここでサブディレクト形式で設定 # unicorn起動コマンドにも使用 location /app1 { alias /[path_to_rails_root]; try_files $uri/index.html $uri.html $uri @unicorn; } location ~ ^/assets/(.*) { alias /[path_to_rails_root]/public/assets/$1; } location @unicorn { 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_pass http://unicorn; } } # 複数のアプリケーションを立てるなら、同様にしてupstream, serverを記述する upstream { } server { }
Nginxの起動
sudo nginx -t #nginxの設定ファイル構文テスト sudo service nginx start
unicornの設定
/[path_to_rails_root]/tmp/unicorn.rb
に記述。
working_directory '/[path_to_rails_root]' # Unicorn PID file location # pid '/path/to/pids/unicorn.pid' pid '/[path_to_rails_root]/tmp/pids/unicorn.pid' # Path to logs stderr_path '/[path_to_rails_root]/log/unicorn.log' stdout_path '/[path_to_rails_root]/log/unicorn.log' # Unix socket listen '/[path_to_rails_root]/tmp/unicorn.sock' # Number of processes worker_processes 2 # Time-out timeout 30
config.ruを削除
Rails_rootにあるconfig.ru
を削除しないと動作できないことある。削除しても恐らくRailsが作成してくれる。
unicornの起動
別途、起動、停止、再起動のスクリプトを書くべし。
bundle exec unicorn_rails -c config/unicorn.rb -D --path /app1
アップデート中、本当にいつも苦戦する。。
参考|http://qiita.com/iriya-ufo@github/items/0c2122550e69d9b0aff6
ubuntuでRailsプロジェクトを動かす #1
1. さくらのVPSを申請
以下のプランでひとまず申請。ここからスタート。
2. カスタムOSインストールでUbuntu 14.04をインストール
https://help.sakura.ad.jp/app/answers/detail/a_id/2403
作成したユーザでsshログイン。ここからはsshで設定を進めていく。
3. ひとまずapt-getを最新化
apt-get update apt-get upgrade
4. 編集用にvimを入れよう
sudo apt-get install vim
5. zshにしよう
sudo apt-get install zsh
6. 設定ファイルを準備
〜〜後ほど〜〜
7. sshdの設定
--- snip --- #Port 22 Port XXXXXX #(49152〜65535) ... #PermitRootLogin yes PermitRootLogin no ... #X11Forwarding yes #X11DisplayOffset 10 X11Forwarding no
sudo service ssh reload
8. ufwを使ってiptableを設定
Ubuntuのufwというiptableラッパーを使ってファイヤーウォールを設定する
http://babie.hatenablog.com/entry/20110220/
9. 他にも入れる
sudo apt-get install git sudo apt-get install tmux
9-1. git用に鍵を設定
mkdir .ssh cd .ssh ssh-keygen
10. rbenv, ruby-build,ruby のインストール
sudo apt-get install build-essential sudo apt-get install libssl-dev sudo apt-get install -y libreadline-dev
rbenv, ruby-buildをgitでインストールし rbenvのPATHを.zshenvに記述
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv echo '' >> ~/.zshenv echo '# rbenv' >> ~/.zshenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshenv echo 'eval "$(rbenv init -)"' >> ~/.zshenv source ~/.zshenv rbenv -v git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenvで好きなバージョンのrubyをインストール
rbenv install -l rbenv install 2.3.0 rbenv global 2.3.0
11. mysqlをインストール
sudo apt-get install mysql-server sudo apt-get install libmysqlclient-dev