LoginSignup
7
2

More than 5 years have passed since last update.

Windows10のVMware Workstation playerにCentOS7とMariaDBとGitとRubyとRailsを入れてみた

Last updated at Posted at 2017-12-26

Windows10のVMware Workstation playerにCentOS7とMariaDBとGitとRubyとRailsを入れてみた

産業技術大学院大学(AIIT)で1年生しております。2017年度第3Qでの「クラウドインフラ構築特論」という授業で、簡易的なIaaSシステムを作りましたので、備忘録的に記録を残しています。

  • アーキテクチャ的なプロっぽい考察は岩田さんのすばらしきブログに書いてあるのでそちらを参照してください。

  • 私は大学院に入学してから、初めてLinuxを触りました。っていうレベルですので、この記事も当然超初心者向けになります。プロの方は気分が悪くなると思うので閲覧注意です(笑)エラー対処ばっかりで、非常に見づらいです

  • 授業では実機に環境を構築するのですが、いきなり実機に構築するのも危険なので、まずは自分の仮想マシンで環境を整えたという次第です。

  • Windows10のSerface3で実装しています。

  • VMware Workstation PlayerとCentOS7は導入済み(Minimalモードでインストール)の前提です。

まずはCentosのバージョン確認

[yusuke@localhost]$ cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

参考:
https://qiita.com/tomisuker/items/f6dc62b4113febc3b62a

では始めましょう!

1.初期設定(Centos7インストール、ネットワーク、Vimの設定)

・Centos7をインストール(minimal)
・CentOS-7-x86_64-DVD-1708.iso
・rootユーザとyusukeユーザを作成

インストールしたら、ネットワークの設定

ip aコマンドでイーサ名を調べて、
/etc/sysconfig/network-script/ifcfg-en33
onboot=no になっていたのでyesにする
service network restart
Google先生にping飛ばして
ping 8.8.8.8
だめならnmcliで設定。
ネットワークがつながったらyum -y update

vimの設定

参考:
https://qiita.com/marrontan619/items/541a1374e1ac672977e6

yum -y install vim
vi /etc/vimrcに設定を追加

vim~とコマンドしたらVimエディタが立ち上がるけど、
viとコマンドしてもVimが立ち上がるようにエイリアス設定
vim /etc/profile
として、最終行に
alias vi=’vim’
と追加。

2.Gitの導入

参考:
https://qiita.com/mochimochi-inu/items/914debabca56acc20a6d
http://vdeep.net/centos7-git

必要なツールのインストール

[root@localhost ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel`

解凍
[root@localhost src]# cd /usr/local/src/
[root@localhost src]# wget https://www.kernel.org/pub/software/scm/git/git-2.13.3.tar.gz
[root@localhost src]# tar xzvf git-2.13.3.tar.gz

makeコマンドでインストール

[root@localhost src]# cd git-2.13.3
[root@localhost git-2.13.3]# make prefix=/usr/local all

以下のエラーが出たのでyum -y install gccを行う。
[root@localhost git-2.13.3]# make prefix=/usr/local all
GIT_VERSION = 2.13.3
    * new build flags
    CC credential-store.o
/bin/sh: cc: コマンドが見つかりません
make: *** [credential-store.o] エラー 127
[root@localhost git-2.13.3]# yum -y install gcc`

再度チェレンジ

[root@localhost git-2.13.3]# make prefix=/usr/local all

今度は以下のエラー

SUBDIR perl
/usr/bin/perl Makefile.PL PREFIX='/usr/local' INSTALL_BA      --`localedir='/usr/local/share/locale'
Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contain    `sr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/p     vendor_perl`/usr/share/perl5/vendor_perl /usr/lib64/perl
`r/share/perl5 .) atMakefile.PL line 3.
BEGIN failed--compilation aborted at Makefile.PL line 3.
make[1]: *** [perl.mak] エラー 2
make: *** [perl/perl.mak] エラー 2

パッケージをインストール

[root@localhost git-2.13.3]# yum -y install perl-ExtUtils-MakeMaker

再度

[root@localhost git-2.13.3]# make prefix=/usr/local all

うまくいったので、

[root@localhost git-2.13.3]# make prefix=/usr/local install
[root@localhost git-2.13.3]# git --version
git version 2.13.3

Gitの導入完了。
ついでにアップデート

参考https://qiita.com/ysti/items/e8b4271313e93bd9efa6

#yum -y remove git.x86_64
#yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker
#cd /usr/local/src/
#curl -o git-2.9.0.tar.gz  https://www.kernel.org/pub/software/scm/git/git-2.9.0.tar.gz
tar vfx git-2.9.0.tar.gz

#git clone https://github.com/git/git
#cd git/
#make prefix=/usr/local all
#make prefix=/usr/local install
#git --version
git version 2.9.5

ついでに遅ればせながらyusukeユーザのsudoersvisudoで設定

3.作業用フォルダのGit設定

mkdir ~/guitestでディレクトリを作って、cd guitestで移動しておく

[yusuke@localhost guitest]$git init
[yusuke@localhost guitest]$ git remote add origin https://github.com/yusukehaneda/cloud_infla_test.git
[yusuke@localhost guitest]$ git push -u origin master

4.Rubyの導入

参考:https://qiita.com/micheleno13/items/c0f8160b0deb2984677f

まずRubyのパッケージ管理するためにrbenv導入

[yusuke@localhost ~]$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
[yusuke@localhost ~]$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >>~/.bash_profile
[yusuke@localhost ~]$ ~/.rbenv/bin/rbenv init
# Load rbenv automatically by appending
# the following to ~/.bash_profile:
eval "$(rbenv init -)"
[yusuke@localhost ~]$ source ~/.bash_profile
[yusuke@localhost ~]$ rbenv -v
rbenv 1.1.1-6-g2d7cefe

rubybuildの導入

[yusuke@localhost ~]$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
[yusuke@localhost ~]$ sudo ~/.rbenv/plugins/ruby-build/install.sh
[sudo] yusuke のパスワード:
[yusuke@localhost ~]$ rbenv install -l
[yusuke@localhost ~]$ sudo yum install -y openssl-devel readline-devel zlib-devel

bzip2がないよと言われたので、

[yusuke@localhost ~]$ sudo yum install -y bzip2
[yusuke@localhost ~]$ rbenv install 2.4.2

とやったが、遅すぎるので、以下を参考に
https://qiita.com/yn-misaki/items/23ff4e0f1268511aed41

[yusuke@localhost ~]$ sudo yum install -y https://github.com/feedforce/ruby-rpm/releases/download/2.4.2/ruby-2.4.2-1.el7.centos.x86_64.rpm

と実行した。※ただし、バージョン管理はできない。

[yusuke@localhost ~]$ ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]

入ったっぽい。
試してみる。

[yusuke@localhost ~]$ echo "puts 'test'" > hello.rb
hello.rb
puts 'test'
[yusuke@localhost ~]$ ruby hello.rb
test

うまくいったようだ、

5.MariaDBの導入

参考:http://www.techbox.jp/entry/centos-mariadb-mysql
↑はおおざっぱなので、以下も参照
参考2:https://qiita.com/akito1986/items/98a8430d936b293cb354

まずはインストール

[yusuke@localhost ~]$ sudo yum install -y mariadb mariadb-server
[yusuke@localhost ~]$ rpm -qa | grep maria
mariadb-libs-5.5.56-2.el7.x86_64
mariadb-5.5.56-2.el7.x86_64
mariadb-server-5.5.56-2.el7.x86_64
[yusuke@localhost ~]$ sudo systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[yusuke@localhost ~]$ sudo systemctl start mariadb.service
[yusuke@localhost ~]$ mysql_secure_installation`

mariaDBにログイン

[yusuke@localhost ~]$ mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 5.5.56-MariaDB MariaDB Server`
`Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.`
`Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.`

DB(test_development)の作成

MariaDB [(none)]> create database test_development;
Query OK, 1 row affected (0.00 sec)`

DBを使うユーザの作成(ユーザ名test_development)

MariaDB [(none)]> CREATE USER test_development;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> CREATE USER test_development@localhost IDENTIFIED
BY PASSWORD 'test_development' ;
Query OK, 0 rows affected (0.00 sec)

先ほど作ったtest_developmentのDBにユーザtest_developmentの権限ALL追加

MariaDB [(none)]> GRANT ALL PRIVILEGES ON test_development.* TO test_development@'%'IDENTIFIED BY 'test_development';
Query OK, 0 rows affected (0.01 sec)

6.Railsの導入

参考:http://www.techbox.jp/entry/rubyonrails-mysql

まずはGemのアップデートとインストール

[yusuke@localhost ~]$ sudo gem update --system
[yusuke@localhost ~]$ sudo gem install --no-ri --no-rdoc rails
[yusuke@localhost ~]$ sudo gem install bundler
[yusuke@localhost ~]$ rbenv rehash
[yusuke@localhost ~]$ rails -v
Rails 5.1.4

install --no-ri --no-rdocはriとrdocというドキュメントはインストールしないという意味

rails newのテスト

[yusuke@localhost ~]$ cd ~/guitest/
[yusuke@localhost guitest]$ ls
README.md
[yusuke@localhost guitest]$ rails new gui_test -d mysql

エラーが出た。

mysql client is missing. You may need to 'apt-get install
libmysqlclient-dev' or 'yum install mysql-devel', and try again.

とりあえず警告で出てたものをインストールしてみる

[yusuke@localhost guitest]$ sudo yum install mysql-devel`

もう一度アップデートする

[yusuke@localhost ~]$ sudo gem update --system

gui_test2にしてやりなおしてみる。

[yusuke@localhost guitest]$ rails new gui_test2 -d mysql

エラーは出なかったようだ。
rails sで確認してみる。

[yusuke@localhost gui_test2]$ rails s -b 0.0.0.0`

エラーが出た。

/usr/lib64/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/runtime.rb:85:in rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
Backtrace for gem load error is:

以下略

gem 'uglifier'とあったのでgemで入れてみる。

[yusuke@localhost gui_test2]$ sudo gem install uglifier

再度チャレンジ

[yusuke@localhost gui_test2]$ rails s -b 0.0.0.0

またエラーが出たので、以下を参考にした。
https://qiita.com/htk_jp/items/1100a04f45151c928378

[yusuke@localhost guitest]$ sudo gem install turbolinks
Could not find a JavaScript runtime. 

ジャバスクリプトのランタイムが無い、、、と。

[yusuke@localhost guitest]$ vi Gemfile`
#gem 'therubyracer', platforms: :ruby`

「#」を外します。:wqで終了。

[yusuke@localhost guitest]$ bundle install`

またエラーが。

An error occurred while installing therubyracer (0.12.3),
and Bundler cannot continue.
Make sure that `gem install therubyracer -v '0.12.3'` succeeds
before bundling.
In Gemfile:
  therubyracer``

言われたとおりにインストール。

[yusuke@localhost gui_test2]$ sudo gem install therubyracer -v '0.12.3'
make: g++: コマンドが見つかりませんでした`
make: *** [accessor.o] エラー 127`

今度はhttp://notsleeeping.com/archives/863
を参考に

[yusuke@localhost gui_test2]$ sudo yum -y install gcc-c++
[yusuke@localhost gui_test2]$ sudo gem install therubyracer -v '0.12.3'

成功。

[yusuke@localhost guitest]$ bundle install`

成功。

rails sは成功しているが、ゲストのポートが空いてないっぽいので、空ける。

参考 リンク

[root@localhost ~]# firewall-cmd --add-service=http --zone=public --permanent
success

/user以下と/etc以下のファイルを確認

[root@localhost ~]# cat /usr/lib/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
</zone>
[root@localhost ~]# cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="http"/>
</zone>

httpが追加されている。

3000番ポートを空けます。

[root@localhost ~]# cp /usr/lib/firewalld/services/http.xml /etc/firewalld/services/`
[root@localhost ~]# vi /etc/firewalld/services/http.xml
<中略>
5   <port protocol="tcp" port="80"/>$`
6   <port protocol="tcp" port="3000"/>$`
7 </service>$`
[root@localhost ~]# firewall-cmd --reload
success

もう一度rails sを試してみる

[yusuke@localhost gui_test2]$ rails s -b 192.168.112.128
=> Booting Puma
=> Rails 5.1.4 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.10.0 (ruby 2.4.2-p198), codename: Russell's Teapot
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://192.168.112.128:3000
Use Ctrl-C to stop
Started GET "/" for 192.168.112.1 at 2017-10-15 00:30:47 +0900
Cannot render console from 192.168.112.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Rails::WelcomeController#index as HTML
  Rendering /usr/lib64/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/templates/rails/welcome/index.html.erb
  Rendered /usr/lib64/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/templates/rails/welcome/index.html.erb (13.8ms)
Completed 200 OK in 780ms (Views: 28.4ms)``

つながった!

7.Gitショートカット、gitconfigの編集

git statusとか打つの面倒なので作成

[yusuke@localhost ~]$ git config --global user.name "Yusuke Haneda"
[yusuke@localhost ~]$ git config --global user.email "@gmail.com”
[yusuke@localhost ~]$ git config --global color.ui true
[yusuke@localhost ~]$ git config --global core.editor vim
[yusuke@localhost ~]$ vim ~/.gitconfig`
[alias]
        st = status
        br = branch
        co = checkout
        cm = commit
        gr = log --graph --date=short --decorate=short --pretty=format:'%Cgreen%h %Creset%cd %Cblue%cn %Cred%d %Creset%s'

試してみる

[yusuke@localhost gui_test2]$ git br
* master`

うまくいっているようだ。

8.アプリケーションの作成

ブランチを切っておく

[yusuke@localhost gui_test2]$ git co -b testscaffold
Switched to a new branch 'testscaffold'
[yusuke@localhost gui_test2]$ git br
  master
* testscaffold``

とりあえずScaffoldする。

[yusuke@localhost gui_test2]$ rails g scaffold Machine instance:integer hostname:integer ipaddr:string cpus:integer memory:integer disk:integer status:integer machineid:integer``

MariaDB用にパスワード設定

[yusuke@localhost gui_test2]$ vi config/database.yml`
<中略>
16   username: root$
17   password: ********$
18   socket: /var/lib/mysql/mysql.sock$

MariaDBを立ち上げ

[yusuke@localhost gui_test2]$ sudo systemctl start mariadb
[sudo] yusuke のパスワード:
[yusuke@localhost gui_test2]$ sudo systemctl status mariadb

まずDBを作る。

[yusuke@localhost gui_test2]$ bundle exec rake db:create
Created database 'gui_test2_development'
Created database 'gui_test2_test'

そしてmigrateする。

[yusuke@localhost gui_test2]$ bundle exec rake db:migrate
== 20171015142844 CreateMachines: migrating ===================================
-- create_table(:machines)
   -> 0.0177s
== 20171015142844 CreateMachines: migrated (0.0180s) ==========================``

ルーティングの確認

[yusuke@localhost gui_test2]$ rails routes
      Prefix Verb   URI Pattern                  Controller#Action
    machines GET    /machines(.:format)          machines#index
             POST   /machines(.:format)          machines#create
 new_machine GET    /machines/new(.:format)      machines#new
edit_machine GET    /machines/:id/edit(.:format) machines#edit
     machine GET    /machines/:id(.:format)      machines#show
             PATCH  /machines/:id(.:format)      machines#update
             PUT    /machines/:id(.:format)      machines#update
             DELETE /machines/:id(.:format)      machines#destroy

DBとテーブルができているか確認

DBにログインする

[yusuke@localhost gui_test2]$ rails db
Enter password:

ログインしたらDBやテーブルを確認

MariaDB [gui_test2_development]> show databases;
+-----------------------+
| Database              |
+-----------------------+
| information_schema    |
| gui_test2_development |
| gui_test2_test        |
| mysql                 |
| performance_schema    |
| test_development      |
+-----------------------+
6 rows in set (0.01 sec)

MariaDB [gui_test2_development]> SHOW TABLES FROM gui_test2_development ;
+---------------------------------+
| Tables_in_gui_test2_development |
+---------------------------------+
| ar_internal_metadata            |
| machines                        |
| schema_migrations               |
+---------------------------------+
3 rows in set (0.00 sec)

MariaDB [gui_test2_development]> SELECT * FROM machines;
+----+----------+----------+---------------+------+--------+--------+--------+-----------+---------------------+---------------------+
| id | instance | hostname | ipaddr        | cpus | memory | disk   | status | machineid | created_at          | updated_at          |
+----+----------+----------+---------------+------+--------+--------+--------+-----------+---------------------+---------------------+
|  1 |        1 |        1 | 192.168.1.101 |    1 |   1024 | 800000 |      1 |         1 | 2017-10-15 14:48:28 | 2017-10-15 14:48:28 |
|  2 |        1 |        1 | 192.168.1.101 |    1 |   1024 | 800000 |      1 |         1 | 2017-10-15 14:53:55 | 2017-10-15 14:53:55 |
+----+----------+----------+---------------+------+--------+--------+--------+-----------+---------------------+---------------------+
2 rows in set (0.00 sec)

MariaDB [gui_test2_development]>

うまくできているようだ。

終わり

ものすごーく長くなりましたが、備忘録的に。

7
2
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
7
2