EC2でサーバを構築 1 - Amazon Linux 2 準備編


記事一覧

概要

本記事ではAmazon Linux 2の初期設定を行います。

ユーザーec2-userでSSHログインして作業します (ダウンロードした鍵ファイルを使用)。

文字コードとキーボード

日本の環境に合わせます。

sudo timedatectl set-timezone Asia/Tokyo
sudo localectl set-locale LANG=ja_JP.UTF-8
sudo localectl set-keymap jp106

設定反映のため一旦切断して再接続します。

反映を確認

設定が正しく反映されているか確認します。

date

以下のように日本時間が日本語で表示されていればOKです。

2020年  4月 14日 火曜日 10:03:39 JST

初回アップデート

パッケージのアップデートと再起動を行っておきます。

sudo yum update
sudo reboot

ログイン関連

rootユーザーでSSHログインしたいので鍵ファイルの用意をします。

/root/.ssh/authorized_key

ssh rsaより前の部分が不要なので削除してから保存します。

sshdの設定

パスワード認証を排除します。

/etc/ssh/sshd_config

PermitRootLogin without-password

sshdを再起動します。

systemctl restart sshd

ec2-user切断後、rootでSSHログインします (ec2-userと同じ鍵ファイルを使用)。

ユーザー関連

新規ユーザー作成時のデフォルトを設定します。

useradd -D -s /sbin/nologin
mkdir -p /etc/skel/Maildir/{new,cur,tmp}
chmod -R 700 /etc/skel/Maildir/

シェルとVimの設定

  • デフォルトのエディタをVimにする
  • プロンプトのカラーを変更する
  • cpmvでワイルドカード*を使う際、ドットファイルがスルーされないようにする
  • ターミナルで [Ctrl + S] した際に応答しなくなる問題に対処する

~/.bashrc

alias vi='vim'

PS1='\[\033[32m\][\u@\h \W]\$\[\033[0m\] '

export EDITOR=vim

shopt -s dotglob
stty stop undef

設定の反映を行います。

source ~/.bashrc

EPELリポジトリ追加

標準で用意されていないパッケージをインストールするために、EPELリポジトリを使うよう設定します。

amazon-linux-extras install epel

ツール類インストール

今後の作業に必要となる各種ツール類をインストールします。

yum install git curl wget nmap finger htop jwhois

Vim環境設定(例)

個人の好み通りに設定すればOKです。わからなければ以下をコピペして使ってください。

ターミナル内でのコピペが不便なので、行番号は表示していません。:set nu:set nonuで切り替えられます。

~/.vimrc

set nocompatible

set encoding=utf-8
set fileencodings=iso-2022-jp,euc-jp,sjis,utf-8
set fileformats=unix,dos,mac

set runtimepath+=~/.vim/dein/repos/github.com/Shougo/dein.vim

call dein#begin(expand('~/.vim/dein'))

call dein#add('Shougo/dein.vim')
call dein#add('scrooloose/nerdcommenter')
call dein#add('chriskempson/vim-tomorrow-theme')
call dein#add('chr4/nginx.vim')

call dein#end()
call dein#save_state()

filetype plugin indent on
syntax enable

autocmd BufRead,BufNewFile /etc/php.ini set syntax=dosini
autocmd BufRead,BufNewFile /etc/php-fpm.conf set syntax=dosini
autocmd BufRead,BufNewFile /etc/php-fpm.d/*.conf set syntax=dosini

set t_Co=256
autocmd ColorScheme * highlight Normal ctermbg=none
autocmd ColorScheme * highlight LineNr ctermbg=none
colorscheme Tomorrow-Night-Bright

set autoindent
" set number
set nobackup
set paste
set tabstop=4
set shiftwidth=4

set pastetoggle=<C-e>

let NERDSpaceDelims = 1
nmap ,, <Plug>NERDCommenterToggle
vmap ,, <Plug>NERDCommenterToggle

if dein#check_install()
  call dein#install()
endif

プラグイン管理ツールのdeinをインストールします。

mkdir -p ~/.vim/dein/repos/github.com/Shougo/dein.vim
git clone https://github.com/Shougo/dein.vim.git ~/.vim/dein/repos/github.com/Shougo/dein.vim

次回Vim起動時にdeinによるプラグインのインストールが行われます。