前回AWSからさくらのVPSのCentOS Stream9に移行しましたが、移行先でRailsを動かしてみることにします。
Ruby
まずはライブラリインストール。
sudo yum install ruby-devel rpm-build make gcc gcc-c++ libxml2 libxml2-devel zlib-devel libxslt-devel nodejs git openssl-devel readline-devel
yarnのインストール
# yarnのリポジトリ追加
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
# インストール
sudo yum -y install yarn
rbenvのインストール
# git cloneする
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
# .bash_profileの設定
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source .bash_profile
Rubyをインストールするためにruby-buildが必要
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
Rubyをインストール
# 自分の使用したいバージョンを選択して下さい
rbenv install 2.6.6
tarがないと言われる場合はインストール
yum install tar
以下のようなエラーが出る場合
Can’t locate FindBin.pm in @INC (you may need to install the FindBin module)
perlモジュールをインストール
sudo yum install perl perl-FindBin perl-open perl-YAML perl-File-HomeDir perl-Unicode-LineBreak
bzip2がないと言われた場合
sudo yum install bzip2
Githubと連携
gitにpush出来るように確認
# 質問は基本空エンターでOK
ssh-keygen -t rsa
# 別の名前でid_rsaを作りたい時は、ssh-keygen -t rsaの設定で以下のように設定する。
Enter file in which to save the key (/home/centos/.ssh/id_rsa): ファイル名
# 表示する
cat ~/.ssh/id_rsa.pub
鍵を登録する
https://github.com/settings/ssh
# githubに接続して確認
# 間違ってsudoつけるとrootの証明書を使うので接続失敗するので注意
ssh -T git@github.com
別名で鍵を作った場合
# ~/.ssh/configを作って読み込む鍵を設定
vim ~/.ssh/config
Host github github.com
HostName github.com
IdentityFile ~/.ssh/(鍵のファイル名)
User git
Gitクローン
# ディレクトリ作成
sudo mkdir /var/www/
# アプリをインストールするディレクトリに移動
cd /var/www/
# アプリをクローンする
git clone リポジトリのURL
Nginxの設定
# 設定ファイルを新規作成
sudo vim /etc/nginx/conf.d/rails.conf
# 以下をコピペす
upstream unicorn {
server unix:/var/www/リポジトリ名/tmp/sockets/unicorn.sock;
}
server {
listen 80;
server_name サーバーのIPアドレス;
root /var/www/リポジトリ名/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
try_files $uri/index.html $uri @unicorn;
error_page 500 502 503 504 /500.html;
}
server {
listen 443 ssl;
server_name サーバーのIPアドレス;
ssl_certificate /etc/letsencrypt/live/ホスト名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ホスト名/privkey.pem;
root /var/www/リポジトリ名/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host; proxy_redirect off;
proxy_pass http://unicorn;
}
try_files $uri/index.html $uri @unicorn;
error_page 500 502 503 504 /500.html;
}
HTTPS
# 証明書インストール
sudo yum install -y epel-release
sudo yum install -y certbot
sudo systemctl stop nginx
sudo certbot certonly --standalone -d ホスト名
sudo systemctl start nginx
# 設定ファイルを開く
sudo vim /etc/nginx/conf.d/ssl.conf
server {
listen 443 ssl;
server_name site1.com;
ssl_certificate /etc/letsencrypt/live/ホスト名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ホスト名/privkey.pem;
location / {
root /var/www/html/site1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Rails起動
# クローンしたリポジトリに入る
cd リポジトリ名
# Gemfile.lockに書いてあるbundlerをインストールする
gem install bundler -v 2.x.x
# bundle installする
bundle install
# mysql2でエラーになった場合 An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before
bundling.
# いかを実行する
sudo yum install mariadb-connector-c-devel
secret_key_baseの生成
# クローンしたリポジトリで以下を実行し、値をコピーしておく
rake secret
# 環境変数を登録
sudo vi /etc/environment
DATABASE_PASSWORD='データベースのrootユーザーのパスワード'
SECRET_KEY_BASE='secret_key_baseの値'
# 環境変数反映のため再起動
sudo reboot
DB作成
# リポジトリへ移動
cd /var/www/xxxxxx/
# DB作成
bundle exec rake db:create RAILS_ENV=production
# DBマイグレーション
bundle exec rake db:migrate RAILS_ENV=production
Rails起動
unicorn_rails -c config/unicorn.rb -E production -D
Rails停止
ps aux | grep unicorn
kill -9 xxxxx
Railsが起動出来たところで今回は終わります。
ちなみに以下は、構築時に遭遇した症状についての覚書です。環境によっては発生するかも。
覚書
selinuxでunicorn.sockにアクセス出来ない場合は以下を参照
https://qiita.com/NaokiIshimura/items/d4464f13c8610e7b6745
seaquel proに接続出来ない場合は以下を参考
https://qiita.com/ysk1o/items/7f0ca12ced72363f9448