LoginSignup
0
0

More than 3 years have passed since last update.

vagrantでrailsの環境を構築

Last updated at Posted at 2020-10-09

バージョン確認

$ vagrant -v

vagrant boxの追加

$ vagrant box add <box名> <url>
$ vagrant box list

仮想マシーンの作成

# Vagrantfileの初期化
$ vagarnt init <box名>

# Vagrantfileの編集
$ vi Vagrantfile
#========================================================================
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"
  config.vm.box_version = "2004.01" # バージョンの指定(必要なら)

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 3000, host: 3000 # railsのデフォルトのポートを開ける

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks makqe the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"
  config.vm.synced_folder "./data", "/home/vagrant/data", create:"true" # マウントするファイルの設定

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "2048" # メモリの設定
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL

#========================================================================

# 仮想マシーン起動
$ vagrant up

# 仮想マシーンへssh
$ vagrant ssh

vagrantについてその他

Vagrantで立ち上げた仮想環境にIPアドレスを振ってsshでログインする

vagrantコマンド

# sshの設定
$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile xxxxxxx/vagrant/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

仮想マシーンのip確認

# vagrantが起動しているか確認
$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

# リッスンしているポートの確認
$ sudo lsof -i -P | grep "LISTEN"

# ホスト側
$ ifconfig
vboxnet1: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:01
    inet 192.168.44.1 netmask 0xffffff00 broadcast 192.168.44.255

# ゲスト側
$ ifconfig
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.44.10  netmask 255.255.255.0  broadcast 192.168.44.255
        inet6 fe80::a00:27ff:fecc:1bf5  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:cc:1b:f5  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 13  bytes 1102 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


centosの設定

ロケールの設定

$ sudo -s
# localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: n/a
# localectl set-locale LANG=ja_JP.utf8 # 設定ファイルの変更
# cat /etc/locale.conf # ロケールの設定ファイル
LANG=ja_JP.utf8
# localectl
   System Locale: LANG=ja_JP.utf8
       VC Keymap: us
      X11 Layout: n/a

日本語のlocateがないとき

キーボードの設定

$ sudo -s
# localectl
   System Locale: LANG=ja_JP.utf8
       VC Keymap: us
      X11 Layout: n/a
# localectl list-keymaps
# localectl set-keymap jp106
# cat /etc/vconsole.conf
KEYMAP=ja106
FONT=latarcyrheb-sun16
# localectl
System Locale: LANG=ja_JP.utf8
       VC Keymap: ja106
      X11 Layout: n/a

時間の設定

$ sudo -s
# timedatectl
Local time: Sat 2020-10-03 12:05:54 UTC
  Universal time: Sat 2020-10-03 12:05:54 UTC
        RTC time: Sat 2020-10-03 09:12:45
       Time zone: UTC (UTC, +0000)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
# timedatectl list-timezones
# timedatectl set-timezone Asia/Tokyo
# timedatectl
      Local time: Sat 2020-10-03 21:11:51 JST
  Universal time: Sat 2020-10-03 12:11:51 UTC
        RTC time: Sat 2020-10-03 09:18:42
       Time zone: Asia/Tokyo (JST, +0900)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a 

yum update

$ sudo yum -y update

mysql8.0

rmp、yumコマンドについて

rpm と yum を今更理解したい-Qiita
よく使うrpmコマンドまとめ-Qiita
【初心者にもわかる】rpmとyumの違いと使い分け一通り

mysqlダウンロードリンク

https://dev.mysql.com/downloads/file/?id=484922
のダウンロードリンクをコピー↓
https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

mysqlインストール

$ sudo -s
# パッケージの更新(mysql8.0)
# rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

# インストール出来るmysqlの確認
# yum info mysql-community-server

# yum -y install mysql-community-server
# mysqld --version
/usr/sbin/mysqld  Ver 8.0.21 for Linux on x86_64 (MySQL Community Server - GPL)

mysqlの起動

# systemctl start mysqld
# systemctl status mysqld
# systemctl enable mysqld

# 自動起動設定の確認
# systemctl -t service is-enabled mysqld
enabled

mysqlの設定

# 初期設定パスワードの取得
# cat /var/log/mysqld.log | grep password
2020-10-03T14:07:32.076975Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: lT3a/31Hm8qD

# セキュリティの初期設定
# mysql_secure_installation

# rootでログイン
# mysql -u root -p

# パスワード変更
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'パスワード';

# rootでpasswordなしでログイン出来るように設定
# vi /etc/my.cnf
# ===================================
# 追記する
[client]
user=root
password=rootパスワード
# ===================================

# mysql -u root
# パスワードの設定確認
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| validate_password.dictionary_file    |        |
| validate_password.length             | 8      |
| validate_password.mixed_case_count   | 1      |
| validate_password.number_count       | 1      |
| validate_password.policy             | MEDIUM |
| validate_password.special_char_count | 1      |
+--------------------------------------+--------+
# キャラセットの確認
mysql> show variables like "chara%";
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8mb4                        |
| character_set_connection | utf8mb4                        |
| character_set_database   | utf8mb4                        |
| character_set_filesystem | binary                         |
| character_set_results    | utf8mb4                        |
| character_set_server     | utf8mb4                        |
| character_set_system     | utf8                           |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+

# パスワードの設定変更
$ sudo -s
# vi /etc/my.conf
# ================================================
[mysqld]
validate_password_length=4
validate_password_policy=LOW
# ================================================
# mysql再起動してパスワードの設定変更

gitのインストール(download a tarball)

yumだと最新版がインストール出来ないのでhttps://mirrors.edge.kernel.org/pub/software/scm/git/
からダウンロード

wget
wgetでこういう時はこうする!!-Qiita

tar
Linux tar.gz tar 圧縮 解凍-Qiita

$ sudo -s

# gitが入ってないことを確認
# yum list installed | grep git
Failed to set locale, defaulting to C
crontabs.noarch                    1.11-6.20121102git.el7     @anaconda
hyperv-daemons.x86_64              0-0.34.20180415git.el7     @anaconda
hyperv-daemons-license.noarch      0-0.34.20180415git.el7     @anaconda
hypervfcopyd.x86_64                0-0.34.20180415git.el7     @anaconda
hypervkvpd.x86_64                  0-0.34.20180415git.el7     @anaconda
hypervvssd.x86_64                  0-0.34.20180415git.el7     @anaconda
linux-firmware.noarch              20191203-76.gite8a0f4c.el7 @anaconda
net-tools.x86_64                   2.0-0.25.20131004git.el7   @BASE 

# インストールに適切な場所に移動
# cd /usr/local/src/

# サイトから Git の圧縮ファイルをダウンロード
# yum -y install wget
# wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.27.0.tar.gz
# もしくは
# yum -y install curl-devel
# sudo curl -LkvOf https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.27.0.tar.gz

# ファイルの解凍
# tar -zxvf git-2.27.0.tar.gz

# tar.gzの削除
# rm -rf git-2.27.0.tar.gz

# make コマンドでインストール
# cd git-2.27.0/
# make prefix=/usr/local all

# エラーでつまずくのでその都度必要なパッケージを入れる
# make: curl-config: Command not found
# yum -y install curl-devel

# git-compat-util.h:297:25: fatal error: openssl/ssl.h: No such file or directory
# yum -y install openssl-devel

# http-push.c:22:19: fatal error: expat.h: No such file or directory
# yum -y install expat-devel

# make prefix=/usr/local install

# gitがインストールされたか確認
# exit
$ git --version
git version 2.27.0
$ which git
/usr/local/bin/git

rbenvのインストール

/etc/profile.dについて

exportコマンド

PATHについて

「PATH を通す」の意味をできるだけわかりやすく説明する試み-Qiita

eavlコマンド

sourceコマンド

$ su -
# cd /usr/local/
# git clone git://github.com/sstephenson/rbenv.git rbenv
# vi /etc/profile.d/rbenv.sh
###################################
export RBENV_ROOT=/usr/local/rbenv
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"
###################################

# git clone git://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build
# source /etc/profile.d/rbenv.sh
# rbenv -v
rbenv 1.1.2-34-g0843745
# which rbenv
/usr/local/rbenv/bin/rbenv
# rbenv install --list-all

# rubyのインストール(足りないパッケージはその都度インストールする)
# rubyのインストールはrootで行う。/usr/local/rbenv/versions/2.6.3に書き込むため、パーミッションエラーになるため
# rbenv install 2.6.3
# rbenv global 2.6.3
# ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
# which ruby
/usr/local/rbenv/shims/ruby

# bundlerのインストール
# rbenv exec gem install bundler
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0