環境
CentOS 7
これから導入するもの
Git 2
Ruby 2.4.5
(Rails 5.2.1)
Nginx 1.14.2
アプリ
GitHub – take5553/translator
Reactを使うのでyarnが必要
Railsのインストール
CentOS7にRubyとRuby on Rails環境構築 – Qiita
まずはyumリポジトリの確認・追加
ここを参照。
【サーバー】yumのリポジトリ(EPEL、IUS)を追加 | Takeshi’s Blog
GitやRubyなど必要な物のインストール
- Git(最新版)
古いバージョンのものがすでに入っている場合は一旦削除。
# yum -y remove git
IUSのリポジトリ経由でGitをインストール。
# yum --enablerepo=ius install git2u yum-utils
インストール確認。
# git --version git version 2.16.5
- bzip2 gcc openssl-devel readline-devel zlib-develなど
Rubyに必要なものを一気にインストール。
# yum install -y bzip2 gcc openssl-devel readline-devel zlib-devel
- rbenv
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv $ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
~/.bashrc
に以下を追記。
# rbenv export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)"
設定を反映させてインストール確認。
$ source ~/.bashrc $ rbenv -v # バージョンが表示される。
- Ruby
$ rbenv install 2.4.5 # 時間がかかる $ rbenv global 2.4.5 # インストールしたバージョンを指定 $ ruby -v # Rubyが実行できることを確認
- bundler
$ gem install bundler
- Node.jsのバージョン管理ツールnvm
cURLを使ってインストール
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash $ source ~/.bashrc
インストール確認
$ nvm --version 0.34.0
- Node.jsのインストール
$ nvm install stable $ node -v v11.12.0
- yarnのインストール
$ npm install yarn -g
- ImageMagickのインストール
まず準備物から。
# yum install -y fftw3 libXt libtool-ltdl
その後、ibsと本体とdevelを持ってくる。
# yum install -y https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-libs-7.0.8-34.x86_64.rpm # yum install -y https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-7.0.8-34.x86_64.rpm # yum install -y https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-devel-7.0.8-34.x86_64.rpm
インストール確認。
# convert -version Version: ImageMagick 7.0.8-34 Q16 x86_64 2019-03-13 https://imagemagick.org Copyright: © 1999-2019 ImageMagick Studio LLC License: https://imagemagick.org/script/license.php Features: Cipher DPC HDRI Modules OpenMP Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib jng jp2 jpeg lcms ltdl lzma openexr pangocairo png ps rsvg tiff webp wmf x xml zlib
RailsのアプリをGithubから引っ張ってきて、Railsサーバー起動まで
- 既存のアプリを持ってくる
$ git clone https://github.com/take5553/translator.git
このとき、「translator」ディレクトリが直下にできるので、これ以降の作業はその中に入ってやる。
cloneだけだとmasterだけ持ってくるみたい。
takeブランチが最新なので、ブランチを切ってそっちにtakeブランチをpullする。
$ git checkout take $ git pull origin take
credentials.yml.enc
用のmaster.key
をアップロードする。
ローカルのmaster.key
が置いてある場所に移動、またはそこでPowerShell起動して
> scp -P (SSH用ポート) master.key (ユーザー名)@(サーバーのIP):~/translator/config
- 必要なGemをインストール
$ bundle install
もしここでSQLite3関連のエラーが出た場合は
# yum install sqlite-devel
をしてから、再度bundle install
をするとGemのインストールが成功するはず。
- Reactのためのもろもろインストール。
$ yarn install
- いよいよRailsサーバー立ち上げ
$ bundle exec rails server
そしてブラウザを立ち上げて(サーバーのIPアドレス):3000
にアクセスして動作確認。
Nginxのインストール
CentOS 7 (5, 6) で “安定版 (最新版)” のNginxをインストールする方法 – Qiita
まずyumのリポジトリを自分で作成。
# vim /etc/yum.repos.d/nginx.repo
そして以下を記述。
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
そしてyum
でインストール。
# yum -y --enablerepo=nginx install nginx # nginx -v nginx version: nginx/1.14.2
最新の安定版かどうかはここで確認。
NginxとRailsをUnicornで連携させる(Developmentモード)
Unicornのインストール
今回のアプリにはすでにUnicornが入っているので割愛。
設定ファイルを確認
これも今回のアプリにはすでに設定ファイルが作成されているので、注意点だけ。
config/unicorn.rb
listen '/home/(ユーザー名)/translator/tmp/unicorn.sock' pid '/home/(ユーザー名)/translator/tmp/unicorn.pid'
になっているか確認。
lib/tasks/unicorn.rake
task(:start) { config = Rails.root.join('config', 'unicorn.rb') sh "unicorn -c #{config} -E development -D" } File.read("/home/(ユーザー名)/translator/tmp/unicorn.pid").to_i
になっているか確認。
Unicorn起動テスト
Railsのプロジェクトフォルダで
$ rake unicorn:start unicorn -c /home/(ユーザー名)/translator/config/unicorn.rb -E development -D
この1行が表示されたら起動成功。
確認は
$ ps -ef | grep unicorn | grep -v grep
このコマンドで3行か4行ぐらい出てくればOK。起動していなければ何も出てこない。
Nginxの設定
まずデフォルトの設定ファイルを適当に移動。
# mv /etc/nginx/conf.d/default.conf (適当な場所)
独自の設定ファイルを作成。
# vim /etc/nginx/conf.d/rails.conf
以下を記述。
upstream unicorn { server unix:/home/(ユーザー名)/translator/tmp/unicorn.sock; } server { listen 80; server_name (サーバーのIPアドレス); access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; root /home/(ユーザー名)/translator/public; client_max_body_size 100m; error_page 404 /404.html; error_page 500 502 503 504 /500.html; try_files $uri/index.html $uri @unicorn; location @unicorn { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://unicorn; } }
Nginxが起動しているかどうか確認。
$ systemctl status nginx
起動していなければ起動。
# systemctl start nginx
起動していれば再起動。
# systemctl restart nginx
権限の設定
chmod
での設定。
$ chmod 701 /home/(ユーザー名)
これでブラウザでサーバーのIPにアクセスすればアプリが動くはずだけど、
もしSELinuxが動いてる場合はアクセスが遮断される。
SELinuxが原因かどうかは
# setenforce 0
にしてみて、再度ブラウザからサーバーのIPにアクセスする。
アプリが表示されたらSELinuxの設定に取り掛かる。
# cat /var/log/audit/audit.log | grep nginx | audit2allow # これで設定の変更内容を事前に確認 # grep nginx /var/log/audit/audit.log | audit2allow -M nginx # semodule -i nginx.pp
これでアプリが無事表示される。
NginxとRailsをUnicornで連携させる(Productionモード)
いよいよ最後の仕上げ。
まずUnicornを停止
Railsのプロジェクトフォルダに移動して
$ rake unicorn:stop
Unicornの設定ファイルを書き換え
lib/tasks/unicorn.rake
を開き、7行目のdevelopment
と書かれている部分をproduction
に変更
アセットパイプラインのプリコンパイル
$ rake assets:precompile RAILS_ENV=production
起動の儀
$ rake unicorn:start # systemctl start nginx # もしnginxが起動していなければ
お疲れ様でした。
参考
全ての出発点
【CentOS 7】Nginx + Unicorn で Rails アプリケーションを本番環境で立ち上げる方法 – Qiita
ピンバック: Nginx + Unicorn on CentOS 8 with SELinux | blog.PanicBlanket.com