AWS S3 のArgumentError (Missing required arguments: aws_access_key_id, aws_secret_access_key):
環境
目的
エラー現象
原因
パスが通っていないエラーなので色々試したが,再起動したらパスが通った.
AWSを利用したRuby on Rails環境構築 #4
自動デプロイの設定
EC2サーバにローカルプロジェクトをクローンする前処理
EC2サーバのSSH鍵ペアの作成
- 鍵の生成
$ 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:
- 公開鍵の確認
$ cat ~/.ssh/id_rsa.pub
表示されたSSH公開鍵の値をコピー.
GitHubに公開鍵を登録
$ ssh -T git@github.com
アプリケーションサーバの設定
Unicornのインストール
- ローカルプロジェクトのGemfileを編集
Gemfile
group :production do gem 'unicorn', '5.4.1' end
config/unicorn.rb
ファイルを作成
各Unicorn設定項目について
設定項目 | 詳細 |
---|---|
worker_processes | 1 |
working_directory | app_path |
pid | "#{app_path}/tmp/pids/unicorn.pid" |
listen | 3000 |
Uglifierの編集
JavaScriptを軽量化するgemがテンプレートリテラル記法に対応していないのでコメントアウトする.
config/environments/production.rb
config.assets.js_compressor = :uglifier ↓ # config.assets.js_compressor = :uglifier
GitHubからコードをクローン
$ sudo mkdir /var/www/ $ sudo chown ec2-user /var/www/
3. GitHubからリポジトリのURLを取得,コードをクローン
$ cd /var/www/ $ git clone リポジトリURL
Railsの設定
/var/www/プロジェクト名ディレクトリ内で作業する.
Swap領域の作成
$ cd $ sudo dd if=/dev/zero of=/swapfile1 bs=1M count=512 $ sudo chmod 600 /swapfile1 $ sudo mkswap 600 /swapfile1 $ sudo swapon /swapfile1 $ sudo sh -c 'echo "/swapfile1 none swap sw 0 0" >> /etc/fstab'
gem のインストール
- Rubyのバージョン確認
$ cd /var/www/chat-space $ ruby -v
- bundler のインストール ローカル環境でのbunderlバージョン確認後
$ gem install bundler -v 2.1.4 $ bundle install
環境変数の設定
secret_key_base;Cookieの暗号化に用いられる文字列
1. secret_key_baseの作成
$ rake secret
文字列をコピー
2. 環境変数の設定
$ cd ~ $ sudo vim /etc/environment
i
で挿入モードにして
DATABASE_PASSWORD=「MySQLのrootユーザーのパスワード」 SECRET_KEY_BASE=「secret_key_baseの文字列」
esc
で編集モードにし:wq
で保存して閉じる.
3. 設定の確認
$ exit $ ssh -i インスタンス名.pem ec2-user@インスタンスのアドレス $ env | grep DATABASE_PASSWORD $ env | grep SECRET_KEY_BASE
ポートの開放
※リージョンの確認をすること.
- EC2インスタンスのセキュリティグループ(launch-wizard-1)をCLICK
- 「インバウンド」をCLICK → 「編集」をCLICK
モーダルウィンドウで「ルールの追加」をCLICK
タイプ プロトコル ポート範囲 カスタム カスタムTCPルール TCP 3000 0.0.0.0/0 - 保存
Railsの起動
エラーログの確認方法
ec2-user@リポジトリ名
$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D $ less log/unicorn.stderr.log
database.ymlの設定変更
production: <<: *default database: chat-space_production username: root password: <%= ENV['DATABASE_PASSWORD'] %> socket: /var/lib/mysql/mysql.sock
ローカルの変更をgit commitして
ec2-user@リポジトリ名
$ git pull origin master $ rails db:create RAILS_ENV=production $ rails db:migrate RAILS_ENV=production # MySQLが起動していない場合 $ sudo service mysqld start
Railsの再起動
$ bundle exec unicorn_rails -c config/unicorn.rb -E production -D
http://サーバに紐付けたElastic IP:3000/
にブラウザでアクセス.
アセットファイルのコンパイル
1. アセットコンパイルの実行
$ rails assets:precompile RAILS_ENV=production
2. Unicornプロセスの停止
$ ps aux | grep unicorn ec2-user 4993 0.0 8.7 399876 88484 pts/1 Sl+ 04:44 0:02 unicorn_rails master -c config/unicorn.rb -E production # PIDの停止 $ kill 4993 $ ps aux | grep unicorn # 強制終了させるには $ kill -9 4993
3. Unicornの再起動
```
RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D
```
http://サーバに紐付けたElastic IP:3000/
にブラウザでアクセス.
ログの確認
$ tail -f log/production.log # ⌃C でリアルタイム表示を停止
トラブルシューティング
$ less /var/www/リポジトリ>/log/unicorn.stderr.log $ cat /var/www/リポジトリ>/log/unicorn.stderr.log
AWSを利用したRuby on Rails環境構築 #3
データベースサーバの構築
MySQLの導入
インストール
‐ ターミナル
$ sudo yum -y install mysql56-server mysql56-devel mysql56
起動方法
$ sudo service mysqld start $ sudo service mysqld status
mysqldのdはdaemon(Linux用語でサーバの意)のd
パスワードの設定
$ mysqladmin -u root password 'new-password' $ root -h ip-172-31-44-177 password 'new-password'
最初のコマンドを使用
接続方法
$ mysql -u root -p
AutoHotkeyを導入しWindows10で日本語版Mac風US配列キーボードをEmacs風キーバインド
目的
- 購入したBK3001BA BluetoothワイヤレスキーボードをWindows PCで使用したい
- キー配列をMac風にしてメインの日本語版MacBook Proと互換性を持たせたい
必要なもの
- AutoHotkeyのインストール
- alt-ime-ahkのインストール
- gtk-emacs-theme-like.ahkのダウンロード
- KeySwapのインストール
【Karabiner】USキーボードを日本語版MacBookキーボード風にカスタマイズ
- ControlキーをCaps lockキーに割り当てるので
左Controlキーは右Commandキーに割り当てる. - 右OptionキーをFnキーに割り当てるので
Fnキーは左Commandキーに割り当てる.WindowsのCtrlキーと互換.
【Karabiner】USキーボード(Anker A7726)を日本語版MacBook Proで使用【CMDキーを英数/かな変換】
Karabiner-ElementsでCommandキーを英数/かな切替
- 公式サイトでKarabiner-Elementsをダウンロード
- Complex modificationsタブでAdd ruleをCLICK
- Import more rules form the Internet(open a web browser)をCLICK
- For JapaneseのImportボタンをCLICK
- 最終的にこの表示になればOK
AWSを利用したRuby on Rails環境構築 #2
Linuxサーバの構築
パッケージのアップデート
- ターミナル(ec2-user 以下同様)
$ sudo yum -y update $ sudo yum -y 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 libcurl libcurl-devel curl
※ -y; 全ての問いにYesと回答するコマンド.
Node.jsのインストール
サーバサイドのJavaScriptパッケージ.CSSや画像を圧縮する際に使用.
- ターミナル
$ sudo curl -sL https://rpm.nodesource.com/setup_6.x | sudo bash - DEPRECATION WARNING Node.js 6.x LTS Boron is no longer actively supported! You will not receive security or critical stability updates for this version. You should migrate to a supported version of Node.js as soon as possible. Use the installation script that corresponds to the version of Node.js you wish to install. e.g. https://deb.nodesource.com/setup_10.x — Node.js v10 LTS "Dubnium" (recommended) https://deb.nodesource.com/setup_12.x — Node.js v12 LTS "Erbium" Please see https://github.com/nodejs/Release for details about which version may be appropriate for you. The NodeSource Node.js distributions repository contains information both about supported versions of Node.js and supported Linux distributions. To learn more about usage, see the repository: https://github.com/nodesource/distributions
rbenvとruby-buildのインストール
rbenvはrubyのバージョンを管理するパッケージで,ruby-buildはそのプラグイン.
- ターミナル
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile $ source .bash_profile $ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build $ rbenv rehash
Rubyのインストール
今までのコマンドでミス等があるとRubyインストールでエラーが発生してしまう.
BUILD FAILED (Amazon Linux AMI 2018.03 using ruby-build 20200224-4-g0ef5e05) Inspect or clean up the working tree at /tmp/ruby-build.20200310235320.30555.GI0AMO Results logged to /tmp/ruby-build.20200310235320.30555.log Last 10 log lines: tool/config.sub already exists checking build system type... x86_64-pc-linux-gnu checking host system type... x86_64-pc-linux-gnu checking target system type... x86_64-pc-linux-gnu checking for gcc... no checking for cc... no checking for cl.exe... no configure: error: in `/tmp/ruby-build.20200310235320.30555.GI0AMO/ruby-2.5.1': configure: error: no acceptable C compiler found in $PATH See `config.log' for more details
- ターミナル
$ rbenv install 2.5.1 $ rbenv global 2.5.1 $ rbenv rehash $ ruby -v