さくらVPSでRubyの環境構築

さくらVPSRailsの環境構築を行った際のメモ。

セットアップ

  • OSインストール
  • ssh接続を確認
$ ssh root@xxx.xxx.xxx.xxx
# yum update
  • 一般ユーザー作成
# adduser vpsuser
# passwd vpsuser
# exit
$ ssh vpsuser@xxx.xxx.xxx.xxx
$ su -
  • sudo 設定の有効化
# usermod -G wheel vpsuser

次回ログイン以降に設定が有効化

$ sudo id
[sudo] password for vpsuser:
uid=0(root) gid=0(root) groups=0(root)
  • rootで直接アクセスを不可に変更
# cd /etc/ssh
# cp sshd_config sshd_config.old
# vim sshd_config

変更前

#PermitRootLogin yes 

変更後

PermitRootLogin no
  • 変更を保存後、sshdを再起動
# systemctl restart sshd.service
  • rootユーザーでssh接続不可を確認
$ ssh root@xxx.xxx.xxx.xxx

以上で一般ユーザーで接続できるところまで準備できました。

鍵認証設定

  • 前提:秘密鍵と公開鍵を作成済
  • 公開鍵をコピー
$ cat ~/.ssh/id_rsa.pub
$ mkdir .ssh && chmod 700 .ssh && cd .ssh
  • .sshディレクトリの下でauthorized_keysを作成し、コピーした公開鍵を貼り付け
$ vim authorized_keys
  • ファイルの読み書きを自分のみに変更
$ chmod 600 authorized_keys
  • 公開鍵で接続確認
$ ssh -i ~/.ssh/id_rsa vpsuser@xxx.xxx.xxx.xxx
  • パスワードによるログインを不可に変更
$ su -
# cd /etc/ssh
# vim sshd_config

変更前

#PasswordAuthentication yes 

変更後

PasswordAuthentication no
  • 変更を保存後、sshdを再起動
# systemctl restart sshd.service

パスワードによるログインができないこと、公開鍵認証によるログインができることを確認して完了します。

ssh接続用ポート番号の変更

$ su -
# vim /etc/ssh/sshd_config
  • ポート番号を任意の値に設定
#Port 22
Port 10022
  • 変更を保存後、sshdを再起動
# systemctl restart sshd.service
  • firewalld の設定ファイルを編集。ポート番号の22の部分を変更。
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="22"/>
</service>
  • 変更を保存後、設定を有効化
# firewall-cmd --reload
success
  • ポート番号を指定して接続を確認
$ ssh vpsuser@xxx.xxx.xxx.xxx -p 10022

Rubyの環境構築

  • Ruby のインストールに必要なライブラリをインストール
$ sudo yum install -y openssl-devel zlib-devel readline-devel libyaml-devel libffi-devel
  • rbenv をインストール
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ rbenv install 2.5.3
$ rbenv global 2.5.3
$ rbenv rehash

参考