スポンサーリンク

Windows7で、VirtualBox+CentOS7.2+Nginx+UnicornでRails本番環境

一番参考にさせていただいたページ

http://tech-blog.gc-story.com/centos7-%E3%81%A7rails%E3%81%AE%E7%92%B0%E5%A2%83%E6%A7%8B%E7%AF%89

CentOS7 でrailsの環境構築

(1)CentOS 7.2 のインストール
参考URL
http://qiita.com/100/items/80a899fbaeb1e82b3f67
Windows 7 Pro 64bit
VirtualBox 5.0.14
メモリ 4GB
HDD VDI 固定 16GB
設定 > ネットワーク > アダプター1 で、
NAT
高度 > ポートフォワーディング > +ボタンを押して、 3000, 3000
設定 > ネットワーク > アダプター2 で、
割り当て を、 ホストオンリーアダプター
起動
CentOS-7-x86_64-DVD-1511.iso
日本語
security policy : 一番上を選択
サーバー(GUI)
管理者パスワード
ユーザー 管理者にする パスワード 高度な設定で wheel グループがあることを確認。
インストールに15分くらい?
>再起動
Install CentOS7
security policy について聞かれるので、 1 や、c を入力して、Enter
root でログイン
日本語 などを選択。
画面右上の電源ボタンをクリックして、2つのネットワーク
Ethernet(enpOs8) と、Ethernet(enpOs3)が、接続済み になっていることを確認。
なっていなかったら、接続済み にする。

(2)『Guest Additions CD イメージの挿入』
参考URL
http://qiita.com/100/items/80a899fbaeb1e82b3f67
http://d.hatena.ne.jp/kazukichop/20110627/1309186845
デスクトップ上で右クリック > 端末の中に開く
rm -f /var/run/yum.pid
sudo yum -y upgrade
sudo yum -y install kernel-devel gcc
Guest Additionsのインストール
CentOSを再起動
デスクトップ上にある、CDROMのマークをダブルクリックして、
ソフトウェアの実行 をクリック。
ここで、いったん電源offして、centos7-gui_basic で保存。

(3)TeraTermで、rootでログイン
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
visudo
# %wheel  ALL=(ALL)       NOPASSWD: ALL
↓ ###コメントアウトを外す。
%wheel  ALL=(ALL)       NOPASSWD: ALL
usermod -G wheel vagrant
yum -y groupinstall "Development Tools"
yum -y install openssl-devel readline-devel zlib-devel curl-devel libyaml-devel
yum -y install mariadb-server mariadb-devel
yum -y install ImageMagick ImageMagick-devel ipa-pgothic-fonts
git clone git://github.com/sstephenson/rbenv.git /usr/local/rbenv
sudo git clone git://github.com/sstephenson/ruby-build.git
/usr/local/rbenv/plugins/ruby-build
rbenv の準備ができたら、/etc/profile に下記の設定を追記
export RBENV_ROOT=/usr/local/rbenv
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"
?設定を記述した後、/etc/profile を読み込む。
% source /etc/profile
sudo visudo
・・・
Defaults secure_path =
/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/rbenv/bin:/usr/local/rbenv/shims
Defaults env_keep += "RBENV_ROOT"
・・・
$ rbenv install 2.2.3
5分くらい?
$ rbenv rehash
$ rbenv global 2.2.3
$ yum -y install gem
$ gem install bundler
$ gem install rake
rake’s executable "rake" conflicts with /usr/local/rbenv/versions/2.2.3/bin/rake
Overwrite the executable? [yN]  y
$ gem install rails –v 4.2.2 –no-document
5分くらい

■mariadb設定
$ sudo vim /etc/my.cnf.d/my.cnf
[mysqld]
character-set-server = utf8?
$ sudo vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set = utf8
show-warnings
sudo systemctl enable mariadb.service

ここから、参考URL
http://www.unix-power.net/centos7/mariadb.html

rpm -qa | grep maria
vim /etc/my.cnf
character-set-server=utf8
# systemctl enable mariadb.service
# systemctl start mariadb.service
# mysql_secure_installation
(rootのパスワードを設定)
mysql -u root -p
select Host, User,Password from mysql.user;
create user ‘rails’@’localhost’ identified by ‘パスワード’;
grant all privileges on rails_app.* to ‘rails’@’localhost’ identified
by ‘パスワード’;
quit
パスワード のところは英数字を入力する。

■nginxインストール
$ yum -y install epel-release
$ sudo yum –enablerepo=epel -y install nginx $ systemctl start nginx.service
(上の欄をコピペすると、なぜか、nginxと$の間が?になってしまうことがあるので、その場合は、その部分は手書きで打ち込む)

5分くらい

$ systemctl enable nginx.service
$ firewall-cmd –permanent –zone=public –add-service=http
$ firewall-cmd –permanent –zone=public –add-service=https
$ firewall-cmd –permanent –zone=public –add-service=ssh
$ firewall-cmd –reload
$ ip a
sudo systemctl enable nginx
sudo systemctl start nginx
これで、CentOS内から、localhostでも、外からIPアドレスでも、nginxのwelcomeページを見ることができた。

■railsアプリ
GUIで、ユーザー rails を作成。
http://qiita.com/shinyashikis@github/items/3501c5f7f71a8e345c3d
su rails
cd ~
su
でrootになってから、
yum -y install sqlite-devel
gem install sqlite3
1分くらい待つ。
su rails
cd ~
rails new sample ?-skip-bundle
cd ~/sample/
vim Gemfile
# gem ‘therubyracer’ ・・・
の#を消す
sample/ の、.Gemfileの最後に、
group :staging, :production do
  gem ‘unicorn’
end
を入れる。 (‘ は、半角で入力。直接入力した方がよいかも。)

(ここで、sampleフォルダは、 /home/rails/sample となっている。)

bundle install
3分くらい。。。
rake db:migrate RAILS_ENV=production

■unicorn動作確認
su
gem install unicorn
chmod 666 Gemfile.lock
bundle update
bundle install
sudo firewall-cmd –add-port=3000/tcp

参考URL
http://easyramble.com/rails-secret-key-base-env.html

bundle exec rake secret
で、出てくる
9c46a39c106570ba23d0f2cd402c730f81b3afb91879d3825ba8bfc116ddc2c702d465bdfb91d3488b39c4625cca179807ba9e0a7bd7750b1d78c9368b63af6a
をコピーして、
/sample/secrets.yml

の、<%= ENV["SECRET_KEY_BASE"] %> の部分にペーストして、保存。(長くなるが、そのまま貼り付け。途中に改行などは入らないようにする。)

/sample/app/controllers/application_controller.rb

  def hello
    render text: "hello, world!"
  end

(sampleフォルダは、 /home/rails/sample にある。)

/sample/config/routes.rb

root ‘application#hello’
bundle exec unicorn_rails -l 3000 -E production
これで、CentOS内から、localhost:3000でも、外からIPアドレス:3000でも、
hello, world! のページを見ることができた。

image

image

(とりあえずはここまで。)

———-

(memo1) development環境での起動

bundle exec rails s –b 0.0.0.0 –p 3000

(memo2) Gemfileの、

gem ‘therubyracer’, platforms: :ruby

は必要。

(memo3)Aptana Studio 3は、WindowsでもCentOS7でも、インストールしても、いろいろエラーが出て、AptanaからRailsプロジェクトを作成したりすることはできない。

しかし、ファイルが縦に並んでくれるところと、エディタがすごく使いやすい。

(memo4) CentOSで、sshキーの作成と登録(BitBucketのため)

https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html

(memo5) production環境で、css, imageなどを有効にする場合は、

bundle exec rake assets precompile rails_env production

が必要。

http://qiita.com/a_ishidaaa/items/74de8bdaecd637063c40

スポンサーリンク