Elastic Beanstalk + Laravelで環境構築

Elastic Beanstalk + Laravelで環境構築した時のメモ

概要

Elastic Beanstalk + Laravelの組み合わせでデプロイの標準的な方法が中々見つからず苦戦し、最終的にAWSのドキュメントに沿って進めることにしました。 しかし、このドキュメントだけでは500エラーの表示となってしまい色々と調べていたところ、こちらの記事を呼んで環境変数の設定が抜けていることが分かりました。

記事に沿って環境変数を設定し、無事にデフォルトのページを表示できました。 文章で書くと短いですが、普段使っているRailsに比べて情報の数自体が少なくてなんだかんだで半日近く使いました。

作業内容

環境構築

Elastic Beanstalkから新しい環境を作成(カスタマイズでmysqlも作成)
キーペアを作成して紐付け

AWS Elastic Beanstalk 環境のセキュリティ - AWS Elastic Beanstalk

SSL対応
確認

ssl対応の独自ドメインでアクセスして画面表示を確認

f:id:tsyknsr:20181214115220p:plain

リダイレクト

httpからhttpsへのリダイレクト設定

[新機能]Webサーバでの実装不要!ALBだけでリダイレクト出来るようになりました! | DevelopersIO

ロードバランサへの対応

httpsリンクが生成されない問題があり、原因はロードバランサを使用していることにありました。リンクを参考に信用するプロキシの設定を行いました。

TLSSSL証明を行うロードバランサの裏でアプリケーションが実行されている場合、アプリケーションが時々HTTPSリンクを生成しないことに、気づくでしょう。典型的な理由は、トラフィックがロードバランサにより80番ポートへフォワーディングされるため、セキュアなリンクを生成すべきだと判断できないからです。

protected $proxies = '*';

デプロイ

今回はこちらで対応しましたが検討の余地ありと感じています。

  • Deployerを使用
  • ec2でデプロイ用のユーザーを作成
  • authorized_keysに開発メンバーの公開鍵を追加
  • リポジトリgit@github.com:xxxを指定

Linux インスタンスでのユーザーアカウントの管理 - Amazon Elastic Compute Cloud PHP - PHP Deployerのデプロイ時のGitアカウントの入力を省略したい|teratail

参考リンク

CentOS7で名前解決できないエラー対応

vagrant upで以下のエラーが出た時の対応メモ。

The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

yum install -y kernel-devel kernel-devel-`uname -r` gcc binutils make perl bzip2

Stdout from the command:

Loaded plugins: fastestmirror
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=vag error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"


Stderr from the command:



 One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64
$ vagrant ssh
# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
# ping google.com
ping: google.com: Name or service not known
# vi /etc/sysconfig/network-scripts/ifcfg-{xxx}
DNS1=8.8.8.8
DNS2=8.8.4.4
# systemctl restart network
# ping google.com
PING google.com (172.217.25.206) 56(84) bytes of data.

参考

CentOS6 or CentOS7(VirtualBox)で名前解決ができなくなった場合の対処方法 - web-technical-blog

VagrantでRails環境を構築

VagrantRails環境を構築した時のメモ。

$ vagrant ssh
$ sudo yum update
$ sudo yum -y install git
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
$ rbenv install --list
$ sudo yum install gcc make openssl-devel libffi-devel readline-devel
$ rbenv install 2.3.0
$ rbenv global 2.3.0
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
$ sudo yum install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
$ sudo yum install mysql mysql-devel mysql-server mysql-utilities
$ sudo chkconfig mysqld on
$ sudo service mysqld start
$ sudo yum -y install ImageMagick ImageMagick-devel
$ gem install bundler -N
$ sudo yum install libxml2-devel libxslt-devel
$ gem install nokogiri -v '{version}' -- --use-system-libraries=true --with-xml2-include=/usr/include/libxml2/
$ curl --silent --location https://rpm.nodesource.com/setup | bash -
$ yum -y install nodejs
$ sudo yum -y install epel-release
$ sudo yum -y install redis

参考

Oculus Goでスクリーンショット・録画を撮る方法

撮影方法

ホーム画面 > シェア > 録画を選択

確認方法

撮影した写真・映像は「ギャラリー」で確認可能

取り出し方法

調べるといくつか方法があるようですがAndroid File Transferの利用が簡単でした。 https://www.android.com/filetransfer/

参考

Oculus GoとUnityの開発環境構築

UnityでビルドしたアプリをOculus Goで実行するまでのメモ。

やったこと

  1. Oculus Goを購入 https://www.oculus.com/go/
  2. Unityをインストール https://store.unity.com/ja
  3. Oculus Developer Dashboardで団体を作成 https://dashboard.oculus.com/
  4. Oculus Go端末を開発者モードに変更
  5. Java Development Kitをインストール https://docs.unity3d.com/Manual/android-sdksetup.html
  6. UnityのAndroid開発環境を整備

    • File > Build Settings > Platform. AndroidPlayerをインストールしてUnityを再起動
    • File > Build Settings > Platform. Player Settingsを選択して表示
    • XR SettingsのVirtual Reality Supportedにチェック
    • Virtual Reality SDKsにOculusを指定
    • Other > Settings. IdentificationのMinimum API Levelを「Android 7.1」に変更
    • Other > Settings. Package Nameを任意の名前に変更
    • File > Build & Run. ビルドを実行
  7. Oculus Goで実機確認

参考

Active Storageを使って画像をアップロード

ローカル環境でActive Storageを使用して画像をアップロードする機能を実装した時のメモ。

事前準備

imagemagickが入っていなかったのでインストール

$ brew install imagemagick

Active Storageの環境構築

$ bin/rails active_storage:install
$ bin/rails db:migrate RAILS_ENV=development

画像をアップロード

class User < ApplicationRecord
  has_one_attached :avatar
end
class UsersController < ApplicationController
  def update
    @user.update(user_params)
  end
  
  private
    def user_params
      params.fetch(:user, {}).permit(:name, :avatar)
    end
end
= form_for @user do |f|
  = f.label :avatar
  = f.file_field :avatar
  = f.submit

画像を表示

gem 'mini_magick'
<%= image_tag user.avatar.variant(resize: "100x100") %>

参考

bundle updateでPermission deniedが出た時の対応

bundle updateでPermission deniedが出た時の対応メモ

エラー内容

$ bundle update
Fetching nokogiri 1.8.3 (was 1.8.2)
Installing nokogiri 1.8.3 (was 1.8.2) with native extensions
Errno::EACCES: Permission denied @ dir_s_mkdir - /Users/{username}/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/nokogiri-1.8.3
An error occurred while installing nokogiri (1.8.3), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.8.3'` succeeds before bundling.

原因

過去にsudoをつけてgemをinstallしていたため書き込み権限がなかった

本当ならば、次のディレクトリ「/Users/(ユーザ名)/.rbenv/versions」配下にあるものは全てユーザーアカウントの権限になるべきなのですが、そのディレクトリの中のある幾つかのファイルに書き込む権限がないというのが問題でした。

参考:Rails cが実行不可能、Permission denied @ rb_sysopen – /Users/…の対応策

対応

ディレクトリの所有者をユーザー権限に変更

$ sudo chown -R {username}:staff /Users/{username}/.rbenv

参考