環 境
・さくらのVPS 1G
・Ubuntu 12.04.2 LTS
・RVM 1.18.18
・Ruby 2.0.0-p0
・RubyGems 2.0.0.rc.2
・Rails 3.2.12
・Apache2 2.2.22
・Passenger 4.0.0.rc4
・PostgreSQL 9.1.8
OSインストール後の設定から
ロケールの設定
Ranguage Packのインストール
$ sudo apt-get install language-pack-ja
インストールしたパッケージの適用
$ sudo dpkg-reconfigure locales
※ロケールの設定を省略した場合「perl: warning: …」というエラーが頻繁に出る。
RVM(Ruby Version Manager)のマルチユーザーインストール
cURLのインストール
$ sudo apt-get install curl
最新版をインストールするためlatestを指定
user$ curl -L https://get.rvm.io | sudo bash -s -- --version latest
rvmグループを作成してrootを追加
$ sudo usermod -a -G rvm root $ sudo gpasswd -a [user] rvm Adding user [user] to group rvm
※ [user]はrvmコマンドを使用するアカウントのユーザーネーム
※一度ログアウトしてからrvmが使用可能となる。
インストールの確認
user$ rvm --version rvm 1.18.18 (latest) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
必要なパッケージの確認とインストール
user$ rvm requirements
$ sudo apt-get --no-install-recommends install bash curl git patch bzip2 $ sudo apt-get --no-install-recommends install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev libgdbm-dev ncurses-dev automake libtool bison subversion pkg-config libffi-dev
Rubyのインストール
RVMでインストールできるRubyバージョンの確認
user$ rvm list known
[ruby-]2.0.0[-p0]をインストール
user$ rvm install ruby-2.0.0-p0
インストールの確認
user$ rvm list rvm rubies ruby-2.0.0-p0 [ x86_64 ] # Default ruby not set. Try 'rvm alias create default <ruby>'. # => - current # =* - current && default # * - default
デフォルトで使用するRubyのバージョン指定
user$ rvm alias create default ruby-2.0.0-p0
RVMをリロード
user$ rvm reload RVM reloaded!
インストールの確認
user$ ruby --version ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
user$ gem --version 2.0.0.rc.2
Railsのインストール
user$ gem install rails --no-ri --no-rdoc
Javascript Engineのインストール
$ sudo apt-get install nodejs
インストールの確認と動作確認
user$ rails --version Rails 3.2.12
user$ rails new sample_project
WEBrickで簡易的な動作確認
user$ cd sample_project user$ rails server
ブラウザで3000番ポートにアクセス(http://[さくらのVPS 仮ドメイン]:3000)
Apache2のインストール
$ sudo apt-get install apache2
Passengerのインストール
必要なGemパッケージのインストール
user$ gem install daemon_controller
最新版をインストールするため--preをつける。
user$ gem install passenger --pre
ライブラリのインストール
$ sudo apt-get install libcurl4-openssl-dev apache2-prefork-dev
Passengerモジュールのインストールコマンドを実行
user$ passenger-install-apache2-module Welcome to the Phusion Passenger Apache 2 module installer, v3.0.19. This installer will guide you through the entire installation process. It shouldn't take more than 3 minutes in total. Here's what you can expect from the installation process: 1. The Apache 2 module will be installed for you. 2. You'll learn how to configure Apache. 3. You'll learn how to deploy a Ruby on Rails application. Don't worry if anything goes wrong. This installer will advise you on how to solve any problems. Press Enter to continue, or Ctrl-C to abort.
-------------------------------------------- Checking for required software... * GNU C++ compiler... found at /usr/bin/g++ * Curl development headers with SSL support... found * OpenSSL development headers... found * Zlib development headers... found * Ruby development headers... found * OpenSSL support for Ruby... found * RubyGems... found Your RVM wrapper scripts are too old. Please update them first by running 'rvm get head && rvm reload && rvm repair all'.
-------------------------------------------- The Apache 2 module was successfully installed. Please edit your Apache configuration file, and add these lines: LoadModule passenger_module /usr/local/rvm/gems/ruby-2.0.0-p0/gems/passenger-4.0.0.rc4/libout/apache2/mod_passenger.so PassengerRoot /usr/local/rvm/gems/ruby-2.0.0-p0/gems/passenger-4.0.0.rc4 PassengerRuby /usr/local/rvm/wrappers/ruby-2.0.0-p0/ruby After you restart Apache, you are ready to deploy any number of Ruby on Rails applications on Apache, without any further Ruby on Rails-specific configuration! Press ENTER to continue.
-------------------------------------------- Deploying a Ruby on Rails application: an example Suppose you have a Rails application in /somewhere. Add a virtual host to your Apache configuration file and set its DocumentRoot to /somewhere/public: <VirtualHost *:80> ServerName www.yourhost.com # !!! Be sure to point DocumentRoot to 'public'! DocumentRoot /somewhere/public <Directory /somewhere/public> # This relaxes Apache security settings. AllowOverride all # MultiViews must be turned off. Options -MultiViews </Directory> </VirtualHost> And that's it! You may also want to check the Users Guide for security and optimization tips, troubleshooting and other useful information: /usr/local/rvm/gems/ruby-2.0.0-p0/gems/passenger-4.0.0.rc4/doc/Users guide Apache.html Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-) https://www.phusionpassenger.com Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.
Apache2の設定
vi等のエディタでapache2.confを編集
214行目のhttpd.confを無効に
$ sudo vi /etc/apache2/apache2.conf
# Include httpd.conf
プロジェクト保存先ディレクトリを作成
$ sudo mkdir /var/www/rails
ディレクトリの所有者をプロジェクトを作成(アップロード)するユーザーへ変更
$ sudo chown -R [user] /var/www/rails
※ [user]はプロジェクトを作成(アップロード)するアカウントのユーザーネーム
PassengerをApacheに組み込む
vi等のエディタでpassenger.loadを追加
$ sudo vi /etc/apache2/mods-available/passenger.load
LoadModule passenger_module /usr/local/rvm/gems/ruby-2.0.0-p0/gems/passenger-4.0.0.rc4/libout/apache2/mod_passenger.so PassengerRoot /usr/local/rvm/gems/ruby-2.0.0-p0/gems/passenger-4.0.0.rc4 PassengerRuby /usr/local/rvm/wrappers/ruby-2.0.0-p0/ruby
Passengerの設定を有効に
$ sudo a2enmod passenger Enabling module passenger. To activate the new configuration, you need to run: service apache2 restart
Apacheの設定
バーチャルホストの追加
$ sudo vi /etc/apache2/sites-available/vps_sakura
<VirtualHost *:80> # さくらのVPSホスト名 ServerName www0000XXXX.sakura.ne.jp # !!! Be sure to point DocumentRoot to 'public'! # Railsプロジェクト/publicまでのパスを記述 DocumentRoot /var/www/rails/sakura/public <Directory /var/www/rails/sakura/public> # This relaxes Apache security settings. AllowOverride all # MultiViews must be turned off. Options -MultiViews </Directory> </VirtualHost>
バーチャルホストの設定を有効に
$ sudo a2ensite vps_sakura Enabling site vps_sakura. To activate the new configuration, you need to run: service apache2 reload
デフォルトの設定を無効に
$ sudo a2dissite default Site default disabled. To activate the new configuration, you need to run: service apache2 reload
PostgreSQLのインストール
$ sudo apt-get install postgresql
PostgreSQLのユーザーを追加
$ sudo su postgres $ createuser [user] Shall the new role be a superuser? (y/n) y
ライブラリのインストール
$ sudo apt-get install libpq-dev
アダプターのインストール
user$ gem install pg --no-ri --no-rdoc
本番環境確認用プロジェクトを作成(PostgreSQLをデフォルト指定)
user$ cd /var/www/rails user$ rails new sakura -d postgresql
Production環境用のデータベースを作成
user$ createdb sakura_production
database.ymlの編集
user$ vi /var/www/rails/sakura/config/database.yml
usernameとpasswordをコメントアウト(必要に応じて設定)
production: adapter: postgresql encoding: unicode database: sakura_production pool: 5 # username: sakura # password:
taskアプリケーションの作成
user$ cd /var/www/rails/sakura user$ rails generate scaffold task title:string body:text invoke active_record create db/migrate/20130XXXXXXXXX_create_tasks.rb create app/models/task.rb invoke test_unit create test/unit/task_test.rb create test/fixtures/tasks.yml invoke resource_route route resources :tasks invoke scaffold_controller create app/controllers/tasks_controller.rb invoke erb create app/views/tasks create app/views/tasks/index.html.erb create app/views/tasks/edit.html.erb create app/views/tasks/show.html.erb create app/views/tasks/new.html.erb create app/views/tasks/_form.html.erb invoke test_unit create test/functional/tasks_controller_test.rb invoke helper create app/helpers/tasks_helper.rb invoke test_unit create test/unit/helpers/tasks_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/tasks.js.coffee invoke scss create app/assets/stylesheets/tasks.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scss
マイグレーションの実行(Production環境指定)
user$ rake db:migrate RAILS_ENV=production == CreateTasks: migrating ==================================================== -- create_table(:tasks) NOTICE: CREATE TABLE will create implicit sequence "tasks_id_seq" for serial column "tasks.id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tasks_pkey" for table "tasks" -> 0.0079s == CreateTasks: migrated (0.0080s) ===========================================
プリコンパイル済みのアセットを作成
user$ rake assets:precompile /usr/local/rvm/rubies/ruby-2.0.0-p0/bin/ruby /usr/local/rvm/gems/ruby-2.0.0-p0@global/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
Apache2の再起動
$ sudo /etc/init.d/apache2 restart * Restarting web server apache2 ... waiting [ OK ]