とりあえずコード書けよ

技術的なことの備忘録。

【メモ書き】テスト環境向けamazon linux2のEC2セットアップ1

linux2でテスト用の環境作りをしたので改めて手順まとめ。

ゴール

RDSとかELBとか使わずにhttps化してBasic認証アクセスするテスト環境作る。

概要

  1. VPC作成
  2. サブネット作成
  3. IGW作成
  4. ルートテーブル作成
  5. セキュリティグループ作成
  6. インスタンス作成
  7. セットアップ

ライブラリを一通りインストール

sudo yum install git make gcc-c++ patch openssl-devel libyaml-devel libffi-devel libicu-devel libxml2 libxslt libxml2-devel libxslt-devel zlib-devel readline-devel ImageMagick ImageMagick-devel

postgresqlセットアップ(RDS使わない版)

sudo rpm -ivh --nodeps https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo sed -i 's/\$releasever/7/g' /etc/yum.repos.d/pgdg-redhat-all.repo
sudo yum install -y postgresql12 postgresql12-server postgresql12-contrib postgresql12-devel
PGSETUP_INITDB_OPTIONS='--encoding=UTF-8 --locale=C' sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl start postgresql-12

linuxのユーザーでpostgresqlのロール作成

su - postgres
psql
CREATE ROLE yyyy WITH CREATEDB CREATEROLE LOGIN PASSWORD 'zzzz'

epelのインストール

sudo amazon-linux-extras install -y epel

nodejsとyarnのインストール

curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash -
sudo yum install -y nodejs
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install -y yarn

rbenvインストール

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インストール

rbenv install 2.6.5
rbenv global 2.6.5
rbenv rehash

git連携

vi .gitconfig
[user]
  name = USERNAME
  email = EMAIL

[color]
  ui = true

[url "github:"] 
    InsteadOf = https://github.com/
    InsteadOf = git@github.com:
cd .ssh
ssh-keygen -t rsa
vi config

configの内容

Host github
  Hostname github.com
  User git
  IdentityFile key名

githubssh keyを設定してgithub連携終わり
リポジトリをクローン

cd /
sudo chown xxx var
cd var
sudo mkdir www
sudo chown xxx www
cd www
sudo mkdir rails
sudo chown xxx rails
cd rails
git clone xxx

railsセットアップ

bundle install --without development test
yarn install --check-files

export RAILS_MASTER_KEY='xxx' #localのmaster.keyの値
または
EDITOR=vim bundle exec rails credentials:edit
vi config/master.key #localのmaster.keyをペースト

bundle exec rails db:create RAILS_ENV=production
bundle exec rails db:migrate RAILS_ENV=production

pgのインストールでエラーが出る場合はpg_configの場所を指定してやる

$ gem install pg -v '1.2.3' -- --with-pg-config=/usr/pgsql-12/bin/pg_config

次回はunicornとnginx, https化