さくらVPS:Nginx+unicorn+Rails+MySQL(その1)

MySQL

MySQLは導入済み。接続用のUNIXソケットは /ver/lib/mysql/mysql.sock。

Railsアプリ

専用のユーザーを作る。

[takatoh@www2465uo www]$ sudo useradd lathercraft
[sudo] password for takatoh: 
[takatoh@www2465uo www]$ sudo passwd lathercraft
ユーザー lathercraft のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: 全ての認証トークンが正しく更新できました。

GitHub からクローン。

[takatoh@www2465uo www]$ sudo git clone https://github.com/takatoh/Lathercraft.git
[sudo] password for takatoh: 
Initialized empty Git repository in /var/www/Lathercraft/.git/
remote: Counting objects: 1736, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 1736 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (1736/1736), 178.63 KiB | 157 KiB/s, done.
Resolving deltas: 100% (1132/1132), done.
[takatoh@www2465uo www]$ sudo chown -R lathercraft:lathercraft Lathercraft
[takatoh@www2465uo www]$ cd Lathercraft
[takatoh@www2465uo Lathercraft]$ bundle install

Gemfile.lock に書き込もうとした時に権限がなかった、ていうエラーがでた。気持ちが悪いので、パーミッションをつけてもう一度やり直し。

[takatoh@www2465uo Lathercraft]$ sudo chmod 766 Gemfile.lock
[sudo] password for takatoh: 
[takatoh@www2465uo Lathercraft]$ bundle install

OK。

config/database.yml はこうなってる。

(前略)
production:
  adapter: mysql2
  encoding: utf8
  pool: 5
  database: lathercraft_production
  username: lathercraft
  password: <%= ENV['LATHERCRAFT_DATABASE_PASSWORD'] %>
  host: localhost

データベースを作る。

[takatoh@www2465uo Lathercraft]$ rake db:create -E production
rake aborted!
NameError: undefined local variable or method `production' for #

(See full trace by running task with --trace)
[takatoh@www2465uo Lathercraft]$ rake db:create RAILS_ENV=production
Access denied for user 'lathercraft'@'localhost' (using password: NO)Please provide the root password for your mysql installation
>

あれ、ユーザーは先に作っとかなきゃいけないのか?

[takatoh@www2465uo Lathercraft]$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE USER lathercraft@localhost IDENTIFIED BY 'xxxxxxxx';
ERROR 1396 (HY000): Operation CREATE USER failed for 'lathercraft'@'localhost'
mysql> exit;
Bye
[takatoh@www2465uo Lathercraft]$ rake db:create RAILS_ENV=production

OK。
設定ファイルを編集。

[takatoh@www2465uo Lathercraft]$ sudo -s
[sudo] password for takatoh: 
[rooth@www2465uo Lathercraft]$ cp config/site_config.yml.example config/site_config.yml
[root@www2465uo Lathercraft]# vim config/site_config.yml
[root@www2465uo Lathercraft]# cp config/secrets.yml.example config/secretes.yml
[root@www2465uo Lathercraft]# vim config/secretes.yml

データベースのマイグレーション。

[root@www2465uo Lathercraft]# rake db:migrate RAILS_ENV=production
Could not find tzinfo-1.2.1 in any of the sources
Run `bundle install` to install missing gems.

tzinfo がないとな?

[root@www2465uo Lathercraft]# gem list

*** LOCAL GEMS ***

actionmailer (4.1.4)
actionpack (4.1.4)
actionview (4.1.4)
activemodel (4.1.4)
activerecord (4.1.4)
activesupport (4.1.4)
arel (5.0.1.20140414130214)
bigdecimal (1.2.0)
builder (3.2.2)
bundler (1.6.2)
bundler-unload (1.0.2)
coffee-rails (4.0.1)
coffee-script (2.3.0)
coffee-script-source (1.7.1)
erubis (2.7.0)
execjs (2.2.1)
executable-hooks (1.3.2)
gem-wrappers (1.2.4)
hike (1.2.3)
i18n (0.6.11)
io-console (0.4.2)
jbuilder (2.1.3)
jquery-rails (3.1.1)
json (1.8.1, 1.7.7)
mail (2.5.4)
mime-types (1.25.1)
minitest (5.4.0, 4.3.2)
multi_json (1.10.1)
mysql2 (0.3.16)
polyglot (0.3.5)
psych (2.0.0)
rack (1.5.2)
rack-test (0.6.2)
rails (4.1.4)
railties (4.1.4)
rake (10.3.2, 0.9.6)
rdoc (4.1.1, 4.0.0)
rubygems-bundler (1.4.4)
rvm (1.11.3.9)
sass (3.2.19)
sass-rails (4.0.3)
sdoc (0.4.1)
spring (1.1.3)
sprockets (2.12.1, 2.11.0)
sprockets-rails (2.1.3)
sqlite3 (1.3.9)
test-unit (2.0.0.0)
thor (0.19.1)
thread_safe (0.3.4)
tilt (1.4.1)
treetop (1.4.15)
turbolinks (2.2.2)
tzinfo (1.2.2)
uglifier (2.5.3)

入ってるじゃん。あ、1.2.1 じゃないとダメなのか?

[root@www2465uo Lathercraft]# gem install tzinfo -v '1.2.1'
Fetching: tzinfo-1.2.1.gem (100%)
Successfully installed tzinfo-1.2.1
Parsing documentation for tzinfo-1.2.1
Installing ri documentation for tzinfo-1.2.1
Done installing documentation for tzinfo after 4 seconds
1 gem installed

その後も mini_portile-0.6.0 とか nokogiri-1.6.3.1 とかが見つからないっていうエラーが出たけど、bundle install したら大丈夫のようだ。
じゃ、あらためて。

[root@www2465uo Lathercraft]# rake db:migrate RAILS_ENV=production
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:

  config.secret_key = 'd3fd9f7eeef0d53467781729d97e738b88f3f1e3c854d3b8151ef5b32f388dca8620943c83303e76d098d553f058e222ee09df5040b86400c48b48c28da54183'

Please ensure you restarted your application after installing Devise or setting the key.
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/devise-3.2.4/lib/devise/rails/routes.rb:481:in `raise_no_secret_key'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/devise-3.2.4/lib/devise/rails/routes.rb:206:in `devise_for'
/var/www/Lathercraft/config/routes.rb:20:in `block in '
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/actionpack-4.1.4/lib/action_dispatch/routing/route_set.rb:337:in `instance_exec'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/actionpack-4.1.4/lib/action_dispatch/routing/route_set.rb:337:in `eval_block'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/actionpack-4.1.4/lib/action_dispatch/routing/route_set.rb:315:in `draw'
/var/www/Lathercraft/config/routes.rb:1:in `'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `block in load'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application/routes_reloader.rb:40:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application/routes_reloader.rb:40:in `load_paths'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application/routes_reloader.rb:16:in `reload!'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application/routes_reloader.rb:26:in `block in updater'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/file_update_checker.rb:75:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/file_update_checker.rb:75:in `execute'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application/routes_reloader.rb:27:in `updater'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application/finisher.rb:71:in `block in '
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `instance_exec'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `run'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/initializable.rb:55:in `block in run_initializers'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/initializable.rb:54:in `run_initializers'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application.rb:300:in `initialize!'
/var/www/Lathercraft/config/environment.rb:5:in `'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application.rb:276:in `require_environment!'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.4/lib/rails/application.rb:379:in `block in run_tasks_blocks'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

うおう!今度はシークレットキーか。出力されてる文字列を、config/initializers/devise.rb の該当箇所に貼り付ける。

[root@www2465uo Lathercraft]# vim config/initializers/devise.rb

今度こそどうだ。

[root@www2465uo Lathercraft]# rake db:migrate RAILS_ENV=production
== 20140708134805 CreateBrands: migrating =====================================
-- create_table(:brands)
   -> 0.1367s
-- add_index(:brands, :name, {:unique=>true})
   -> 0.1430s
-- add_index(:brands, :slug, {:unique=>true})
   -> 0.2081s
== 20140708134805 CreateBrands: migrated (0.4885s) ============================

== 20140708135345 CreateProducts: migrating ===================================
-- create_table(:products)
   -> 0.0521s
-- add_index(:products, [:brand_id, :slug], {:unique=>true})
   -> 0.0269s
== 20140708135345 CreateProducts: migrated (0.0794s) ==========================

== 20140708140110 CreateItems: migrating ======================================
-- create_table(:items)
   -> 0.0132s
-- add_index(:items, :md5, {:unique=>true})
   -> 0.0350s
== 20140708140110 CreateItems: migrated (0.0485s) =============================

== 20140708215016 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0087s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0153s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0266s
-- add_index(:users, :confirmation_token, {:unique=>true})
   -> 0.0146s
== 20140708215016 DeviseCreateUsers: migrated (0.0658s) =======================

== 20140719083223 CreateCategories: migrating =================================
-- create_table(:categories)
   -> 0.0059s
== 20140719083223 CreateCategories: migrated (0.0060s) ========================

== 20140719085230 AddCategoryIdToItem: migrating ==============================
-- add_column(:items, :category_id, :integer)
   -> 0.0115s
== 20140719085230 AddCategoryIdToItem: migrated (0.0116s) =====================

== 20140720065147 AddAdminToUsers: migrating ==================================
-- add_column(:users, :admin, :boolean, {:null=>false, :default=>false})
   -> 0.0157s
== 20140720065147 AddAdminToUsers: migrated (0.0158s) =========================

== 20140720134222 CreateStorages: migrating ===================================
-- create_table(:storages)
   -> 0.0070s
== 20140720134222 CreateStorages: migrated (0.0071s) ==========================

== 20140721073254 CreateDownloads: migrating ==================================
-- create_table(:downloads)
   -> 0.0060s
== 20140721073254 CreateDownloads: migrated (0.0060s) =========================

== 20140721080127 CreateStoredItems: migrating ================================
-- create_table(:stored_items)
   -> 0.0075s
== 20140721080127 CreateStoredItems: migrated (0.0076s) =======================

== 20140801123109 CreateRemovedItems: migrating ===============================
-- create_table(:removed_items)
   -> 0.0074s
== 20140801123109 CreateRemovedItems: migrated (0.0075s) ======================

== 20140801132346 AddBrandToRemovedItems: migrating ===========================
-- add_column(:removed_items, :brand, :string)
   -> 0.0109s
== 20140801132346 AddBrandToRemovedItems: migrated (0.0110s) ==================

== 20140802001131 AddNameToUsers: migrating ===================================
-- add_column(:users, :name, :string)
   -> 0.0770s
-- add_index(:users, :name, {:unique=>true})
   -> 0.0185s
== 20140802001131 AddNameToUsers: migrated (0.0958s) ==========================

== 20140802070045 CreateContacts: migrating ===================================
-- create_table(:contacts)
   -> 0.0095s
== 20140802070045 CreateContacts: migrated (0.0096s) ==========================

== 20140808134556 AddOnHoldToItems: migrating =================================
-- add_column(:items, :on_hold, :boolean)
   -> 0.0145s
== 20140808134556 AddOnHoldToItems: migrated (0.0147s) ========================

やった!
ついでに、production 環境用の secret_key_base を環境変数に設定して、アプリを起動してみる。

[root@www2465uo Lathercraft]# export SECRET_KEY_BASE=`rake secret`
[root@www2465uo Lathercraft]# rails server -e production
=> Booting WEBrick
=> Rails 4.1.4 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
[2014-08-14 15:18:04] INFO  WEBrick 1.3.1
[2014-08-14 15:18:04] INFO  ruby 2.0.0 (2014-05-08) [x86_64-linux]
[2014-08-14 15:18:04] INFO  WEBrick::HTTPServer#start: pid=27822 port=3000

ブラウザからアクセスすると、ちゃんとページが表示された。

というところで、今回はここまで。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください