Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vagrantで作るRails開発環境構築

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 Vagrantで作るRails開発環境構築

Vagrantで作るRails開発環境構築

Avatar for Tetsushi Hasesaku

Tetsushi Hasesaku

September 03, 2018
Tweet

More Decks by Tetsushi Hasesaku

Other Decks in Programming

Transcript

  1. MVC

  2. VagrantFile 17 VMの構成を記述した、スクリプト VAGRANTFILE_API_VERSION="2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "centos/7"

    config.vm.synced_folder ".", "/vagrant", type: "virtualbox" config.vm.network :forwarded_port, guest:8080, host:8080, id:"http" config.vm.provider "virtualbox" do |v| v.name = "vm_name" v.cpus = 2 v.memory = 1024 end end 構築のベースになる環境 (box) 構築するマシンの構成
  3. VMの構成編集 `vagrant init`で作成された、VagrantFileを編集 Vagrant.configure("2") do |config| config.vm.box = "centos/7" config.vm.network

    :forwarded_port, guest: 3000, host: 8080 config.vm.provider "virtualbox" do |v| v.name = "spz-colab" v.cpus = 1 v.memory = 1024 end end
  4. Vagrant ファイル修正 Vagrant.configure("2") do |config| config.vm.box = "centos/7" config.vm.synced_folder ".",

    "/vagrant", nfs: true config.vm.network :private_network, ip: "192.168.33.57" config.vm.network :forwarded_port, guest: 3000, host: 8080 config.vm.provider "virtualbox" do |v| v.name = "spz-colab" v.cpus = 1 v.memory = 1024 end end 2行追加 ※ Macの場合はnfs共有で設定 しているが、Windowsの場合は nfsが使えないため、,nfs: trueの 部分は入れず、VirtualBoxの共 有機能を使うこと
  5. 日本語ロケールの設定 VagrantFile内のconfigブロックの中に下記を記載 # ロケール設定 config.vm.provision :shell, inline: <<-SHELL localedef -i

    ja_JP -f UTF-8 -A /usr/share/locale/locale.alias ja_JP.UTF-8 yum install -y ibus-kkc vlgothic-* localectl set-locale LANG=ja_JP.UTF-8 source /etc/locale.conf SHELL
  6. MySQLのインストール VagrantFile内のconfigブロックの中に下記を記載 config.vm.provision :shell, inline:<<-SHELL yum update yum remove -y

    mariadb-libs rm -rf /var/lib/mysql/ yum localinstall -y #{MYSQL_RPM_PATH} yum install -y mysql mysql-devel mysql-server mysql-utilities systemctl start mysqld.service systemctl enable mysqld.service tmp_password=`cat /var/log/mysqld.log | grep password | head -1 | awk -F' ' '{print $NF}'` mysql --user=#{MYSQL_USER} --password=\${tmp_password} --connect-expired-password -e "SET PASSWORD = PASSWORD('#{MYSQL_PASSWORD}');" SHELL
  7. 各種Rubyに必要なものをインストール VagrantFile内のconfigブロックの中に下記を記載 config.vm.provision :shell, privileged: false, inline: <<-SHELL git clone

    https://github.com/sstephenson/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(rbenv init -)"' >> ~/.bash_profile source ~/.bash_profile git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build rbenv install #{RUBY_VERSION} rbenv global #{RUBY_VERSION} rbenv rehash gem install bundler curl --silent --location https://rpm.nodesource.com/setup_8.x | bash - yum install -y nodejs SHELL
  8. 各種Rubyに必要なものをインストール VagrantFile内のconfigブロックの中に下記を記載 config.vm.provision :shell, privileged: false, inline: <<-SHELL git clone

    https://github.com/sstephenson/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(rbenv init -)"' >> ~/.bash_profile source ~/.bash_profile git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build rbenv install #{RUBY_VERSION} rbenv global #{RUBY_VERSION} rbenv rehash gem install bundler curl --silent --location https://rpm.nodesource.com/setup_8.x | bash - yum install -y nodejs SHELL vagrantユーザでシェルを実 行するためのオプション
  9. Gemfileの作成 vagrant ssh cd /vagrant mkdir spz-colab cd spz-colab bundle

    init ディレクトリ共有をしているディレクトリ配下にRailsのプロジェク トを作成する
  10. database.ymlの修正 • config/database.ymlの接続先を、VagrantFileに書いた情報を元に設定する default: &default adapter: mysql2 encoding: utf8 pool:

    <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: 1q2w3e4r5t%=Z socket: /var/lib/mysql/mysql.sock パスワードを追加
  11. Rails サーバの起動 • ここまでで、Railsの環境が整ったため、試しにRailsサーバ を起動してみる。下記コマンドで、Railsサーバを稼働させる RAILS_ENV=development bundle exec rails server

    -b 0.0.0.0 ここの「-b 0.0.0.0」はどのIPアドレスからでもサーバにアクセスできるようにするオプション。 これをつけないとVMの内部からしかアクセスできないため、このオプションをつける
  12. Rails.application.routes.draw do resources :users # For details on the DSL

    available within this file, see http://guides.rubyonrails.org/routing.html root to: "users#index" end rootをUserコント ローラのindexに 変更