<DAY146>AWS EC2インスタンス作成後の 環境構築
●7/17(水)
●学習日数 146日
●学習時間(本日)4時間
●累計学習時間 710.5時間
●一日あたりの平均学習時間 4.88時間
SSHログインする時
sshまで移動する
$cd .ssh/
EC2インスタンス取得時のログイン名、IPアドレスが必要
ログイン名はデフォルトではec2-userとなる
$ssh -i <取得した鍵.pem> ec2-user@<IPアドレス>
設定用のツールをインストール
パッケージをアップデート
[ec2-user@ip-172-31-25-189 ~]$ sudo yum update
必要なパッケージのインストール
[ec2-user@ip-172-31-25-189 ~]$ sudo yum install \ git make gcc-c++ patch \ libyaml-devel libffi-devel libicu-devel \ zlib-devel readline-devel libxml2-devel libxslt-devel \ ImageMagick ImageMagick-devel \
openssl-develというRubyのインストールに必要なパッケージをインストール
sudo yum install -y openssl-devel
ECS上でJavaScriptを動かすためにNode.jsというものをインストール
[ec2-user@ip-172-31-25-189 ~]$ sudo curl -sL https://rpm.nodesource.com/setup_6.x | sudo bash - [ec2-user@ip-172-31-25-189 ~]$ sudo yum install nodejs
rbenvとruby-buildをインストール
#rbenvのインストール [ec2-user@ip-172-31-25-189 ~]$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv #パスを通す [ec2-user@ip-172-31-25-189 ~]$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile #rbenvを呼び出すための記述 [ec2-user@ip-172-31-25-189 ~]$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile #.bash_profileの読み込み [ec2-user@ip-172-31-25-189 ~]$ source .bash_profile #ruby-buildのインストール [ec2-user@ip-172-31-25-189 ~]$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build #rehashを行う [ec2-user@ip-172-31-25-189 ~]$ rbenv rehash
Rubyをインストール
バージョンは環境によって異なる
[ec2-user@ip-172-31-25-189 ~]$ rbenv install 2.5.1 [ec2-user@ip-172-31-25-189 ~]$ rbenv global 2.5.1 [ec2-user@ip-172-31-25-189 ~]$ rbenv rehash #rehashを行う [ec2-user@ip-172-31-25-189 ~]$ ruby -v # バージョンを確認
MySQLをインストール
[ec2-user@ip-172-31-25-189 ~]$ sudo yum install mysql56-server mysql56-devel mysql56
#起動 [ec2-user@ip-172-31-25-189 ~]$ sudo service mysqld start #状態確認 sudo service mysqld status
SQLパスワード設定
[ec2-user@ip-172-31-25-189 ~]$ sudo /usr/libexec/mysql56/mysqladmin -u root password '設定したいパスワード' #接続確認 mysql -u root -p
EC2サーバのSSH鍵ペアを作成
[ec2-user@ip-172-31-23-189 ~]$ ssh-keygen -t rsa -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/ec2-user/.ssh/id_rsa. Your public key has been saved in /home/ec2-user/.ssh/id_rsa.pub. The key fingerprint is: 3a:8c:1d:d1:a9:22:c7:6e:6b:43:22:31:0f:ca:63:fa ec2-user@ip-172-31-23-189 The key's randomart image is: +--[ RSA 4096]----+ | + | | . . = | | = . o . | | * o . o | |= * S | |.* + . | | * + | | .E+ . | | .o | +-----------------+ #値の表示 [ec2-user@ip-172-31-23-189 ~]$ cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2E...... catで表示させた公開鍵を、Githubにアクセスして登録。
接続できるか確認
[ec2-user@ip-172-31-23-189 ~]$ ssh -T git@github.com Hi <Githubユーザー名>/<レポジトリ名>! You've successfully authenticated, but GitHub does not provide shell access.
config/unicorn.rbにファイル作成
#サーバ上でのアプリケーションコードが設置されているディレクトリを変数に入れておく app_path = File.expand_path('../../', __FILE__) #アプリケーションサーバの性能を決定する worker_processes 1 #アプリケーションの設置されているディレクトリを指定 working_directory app_path #Unicornの起動に必要なファイルの設置場所を指定 pid "#{app_path}/tmp/pids/unicorn.pid" #ポート番号を指定 listen 3000 #エラーのログを記録するファイルを指定 stderr_path "#{app_path}/log/unicorn.stderr.log" #通常のログを記録するファイルを指定 stdout_path "#{app_path}/log/unicorn.stdout.log" #Railsアプリケーションの応答を待つ上限時間を設定 timeout 60 #以下は応用的な設定なので説明は割愛 preload_app true GC.respond_to?(:copy_on_write_friendly=) && GC.copy_on_write_friendly = true check_client_connection false run_once = true before_fork do |server, worker| defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect! if run_once run_once = false # prevent from firing again end old_pid = "#{server.config[:pid]}.oldbin" if File.exist?(old_pid) && server.pid != old_pid begin sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU Process.kill(sig, File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH => e logger.error e end end end after_fork do |_server, _worker| defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection end
databaseの構築
config/database.yml(ローカル)
production: <<: *default database: ~~~(それぞれのアプリケーション名によって異なっています。こちらは編集しないでください) username: root password: <%= ENV['DATABASE_PASSWORD'] %> socket: /var/lib/mysql/mysql.sock
構築
[ec2-user@ip-172-31-23-189 <レポジトリ名>]$ rails db:create RAILS_ENV=production Created database '<データベース名>' [ec2-user@ip-172-31-23-189 <レポジトリ名>]$ rails db:migrate RAILS_ENV=production
Githubからコードをクローン
#mkdirコマンドで新たにディレクトリを作成 [ec2-user@ip-172-31-23-189 ~]$ sudo mkdir /var/www/ #作成したwwwディレクトリの権限をec2-userに変更 [ec2-user@ip-172-31-23-189 ~]$ sudo chown ec2-user /var/www/ [ec2-user@ip-172-31-23-189 ~]$ cd /var/www/ [ec2-user@ip-172-31-23-189 www]$ git clone https://github.com/<レポジトリ名>.git
bundlerのインストール
[ec2-user@ip-172-31-23-189 <レポジトリ名>]$ gem install bundler -v 2.0.1 # ローカルで確認したbundlerのバージョンを導入する [ec2-user@ip-172-31-23-189 <レポジトリ名>]$ bundle install # 上記コマンドは、数分以上かかる場合もあります。
secret_key_baseの確認
[ec2-user@ip-172-31-23-189 <レポジトリ名>]$ rake secret 69619d9a75b78f2e1c87ec5e07541b42f23efeb6a54e97da3723de06fe74af29d5718adff77d2b04b2805d3a1e143fa61baacfbf4ca2c6fcc608cff8d5a28e8d
環境変数書き込む場所
[ec2-user@ip-172-31-23-189 ~]$ sudo vim /etc/environment
書き込む内容
#MySQLのrootユーザーのパスワードを入力 DATABASE_PASSWORD='MySQLのrootユーザーのパスワード' SECRET_KEY_BASE='先程コピーしたsecret_key_base'
環境変数読み込みの確認
一旦ログアウトする必要がある
[ec2-user@ip-172-31-23-189 ~]$ env | grep SECRET_KEY_BASE SECRET_KEY_BASE='secret_key_base' [ec2-user@ip-172-31-23-189 ~]$ env | grep DATABASE_PASSWORD DATABASE_PASSWORD='MySQLのrootユーザーのパスワード'
vimのコマンド
アセットファイルをコンパイル
[ec2-user@ip-172-31-23-189 <レポジトリ名>]$ rails assets:precompile
ポートを解放
HTTPで繋がるようにするため
unicornを起動
RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D