前回(#1 VPSでの開発サーバ作成(ユーザ、SSH設定))はSSHとユーザの設定を行いました。今回は、rubyやwebサーバ、postgresqlなど各種をインストールしていきます。
ruby、git、postgresql、nginxなどのインストール、各種config設定
インストールするバージョンがコマンドのなかに出てきます。もし異なるバージョンをインストールする場合はコマンドを見直してください。ディストリビューション違いやバージョン違いで、パッケージングツールが異なることもあるので、その場合も読み直しが必要です。
postgresql
まずはpostgresqlをインストールして、サービスから起動できるようにしておきます。
sudo yum -y install gcc make openssl openssl-devel gcc-c++ readline-devel libxml2-devel
sudo dnf -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo dnf module disable postgresql
sudo dnf clean all
sudo dnf -y install postgresql11-server postgresql11
sudo /usr/pgsql-11/bin/postgresql-11-setup initdb
sudo dnf -y install postgresql11-contrib postgresql11-devel
sudo systemctl enable --now postgresql-11
sudo systemctl restart postgresql-11
postgresqlの設定
postgresユーザへのパスワード設定、DBユーザの作成などをしておきます。
このあたりのユーザ名、パスワードは必ず控えておきます。絶対すぐわからなくなります。
su - postgres
psql -U postgres
##ここからpsqlのコンソール
ALTER USER postgres encrypted password 'パスワード';
createuser -P ユーザ名
ALTER USER ユーザ名
encrypted password 'パスワード';
create database DB名 owner ユーザ名;
git、ruby
gitとruby(rbenv)を入れます。バージョンは適宜変えたほうが良いですが、一例で、ここでは2.5.8です。
sudo yum -y install epel-release
sudo yum -y install nodejs npm
sudo npm install yarn -g
sudo yum install git
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 install --list
rbenv install 2.5.8
rbenv global 2.5.8
rbenv rehash
railsサーバの設定
gitからソースを落として、railsサーバの環境を整えます。一応本番用に書いておきます。developmentであればrails tutorialに記載があります。
/etc/systemd/system/puma.service
puma.serviceを作って保存しておきます。
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=simple
User=normal01
WorkingDirectory=/home/normal01/アプリ名
Environment="RAILS_ENV=production"
Environment="RAILS_SERVE_STATIC_FILES=true"
Environment="PORT=3000"
ExecStart=/home/normal01/.rbenv/shims/bundle exec puma -C /home/normal01/アプリ名
/config/puma.rb
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
gem install bundler
git clone git@xxxxxxxxxxxxxxxxxxxxxx.git
git pull origin master
bundle install --without development test
rails db:migrate RAILS_ENV=production
rails assets:precompile RAILS_ENV=production
SECRET_KEY_BASE=$(rake secret) RAILS_SERVE_STATIC_FILES=true RAILS_ENV=production puma -w 2
sudo systemctl start puma.service
sudo systemctl enable puma.service
nginxのインストール
railsのサーバ(=ここではpuma)とは別にWebサーバを入れます。メンテナンス作業をしている間はPumaサーバが落ちている状態ですが、その間でもメンテ中であることを表示できるようにできます。また、pumaが80番で起動できない?ようであったので、nginx経由で解決しています。
sudo yum install -y nginx
vi /etc/logrotate.d/nginx
#ユーザをnormal01に変更
/etc/nginx/nginx.conf
主な設定は下記のとおりです。
- ユーザの設定
- internalの定義変更
- vpsのIPアドレスを内部にする。
- httpをhttpsにリダイレクト
- Railsアプリのログインをhttpで行われると困ります。
- メンテ用のフラグファイルがあるときメンテhtmlを返す
- Googleアドセンスのadsファイルを設置する
- pumaサーバへのリバースプロキシ
- SSL証明書の設定
user normal01;
:
:
http {
:
:
geo $access_from{
default external;
127.0.0.1 internal;
999.999.999.999 internal;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
# root /usr/share/nginx/html;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
}
# Settings for a TLS enabled server.
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
ssl on;
server_name _;
root /usr/share/nginx/html;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers PROFILE=SYSTEM;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 503 @maintenance;
set $maintenance false;
if (-e /etc/nginx/maintenance) {
set $maintenance true;
}
if ($access_from !~ external) {
set $maintenance false;
}
if ($maintenance = true) {
return 503;
}
location @maintenance {
root /usr/share/nginx/html;
rewrite ^(.*)$ /mainte.html break;
}
location /ads.txt {
alias /usr/share/nginx/html/ads.txt;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-CSRF-Token $http_x_csrf_token;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:3000;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
ssl_certificate /etc/letsencrypt/live/ドメイン名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ドメイン名/privkey.pem;
}
}
firewallの設定を変更する
sudo firewall-cmd --zone=public --add-service=https --permanent
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --reload
ssl証明書関係の設定
cd /usr/local/nginx
sudo wget https://dl.eff.org/certbot-auto
sudo ./certbot-auto -d ドメイン名
#メールアドレスを打ったり、利用規約に同意します。ファイルが下記のように作られます。nginxの設定にこの点を書きます。
> ssl_certificate /etc/letsencrypt/live/ドメイン名/fullchain.pem;
> ssl_certificate_key /etc/letsencrypt/live/ドメイン名/privkey.pem;
次回
主要なサービスやツールはインストールできたので、最後にサービスを再起動しておきましょう。次回はバックアップ用のスクリプトやcronの設定、アプリリリース作業時の手順を整理しておきます。
もしかしたら、メール配信をlaterで動かすためにインストールしたsidekiqなども手順もプラスαとして書いておくかもしれません。