Google App EngineからさくらのVPSにお引越しするまで
Google App EngineにRailsのサービスを乗っけていました。
Google Cloud Platformの無料トライアル期間があったし、gcloud app deployでデプロイできてめっちゃ簡単だな〜って思っていました。
しかし、1ヶ月でAppEngineのFlex環境で27000円とGoogleCloudSQL5000円で合計32000円もしたので引越しすることにしました。
流石に高すぎ。
ここからはさくらのVPSに引越しするまでの手順をかいつまんで備忘録として残します。
mysqldumpしておいてからさくらのvpsを借りる
mysqldump -h $host -u $username -p > output.sql
さくらのvpsを借りる
LetsEncryptのスタートアップスクリプトを走らせる
(ただ、nginxを入れてletsencryptの途中まで成功しているが証明書はこの手順だと取得できていないので注意。正しくやるならドメインの設定を終わらせておいてからだと思う)
rbenv, ruby-build, rubyを入れてソースをgit clone, bundle installまで
yum update
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 git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
cd ~/.rbenv/plugins/ruby-build
./install.sh
yum install -y readline-devel
rbenv install 2.4.2
cd /var/www/app
bitbucketからgit clone
gem install bundler
bundle install --path vendor/bundle
mysql
rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
(参考:https://dev.mysql.com/downloads/repo/yum/)
yum -y install mysql-devel
bundle exec rake db:create
mysql -u[ユーザー名] -p [インポートするデータベース名] < [インポートするファイル名]
bundle exec rake db:migrate
puma.rbに下記を追加
_proj_path = "#{File.expand_path("../..", __FILE__)}"
_proj_name = File.basename(_proj_path)
_home = ENV.fetch("HOME") { "your home directory" }
pidfile "#{_home}/tmp/#{_proj_name}.pid"
bind "unix://#{_home}/tmp/#{_proj_name}.sock"
directory _proj_path
nginx.confを設定
http内に下記を追加
upstream puma {
server unix:///your app/tmp/{your app name}.sock;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name {your domain or ip address};
root /var/www/app/{your app name}/public;
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri/index.html $uri.html @webapp;
}
location @webapp {
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://puma;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
systemctl start nginx
bundle exec pumactl -F config/puma.rb startで動くことを確認
nginx, pumaを常時起動設定
systemctl enable nginx
/etc/systemd/system/puma.serviceを作ってsystemctl enable puma.service
[Unit]
Description=Puma Application Server
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=var/www/app/{your app name}
Environment=HOME=var/www/app/{your app name}
Environment=RAILS_ENV=production
Environment=SECRET_KEY_BASE={bundle exec rake secretの値をコピペ}
ExecStart=/root/.rbenv/shims/bundle exec puma -C /var/www/app/{your app name}/config/puma.rb
TimeoutSec=300
Restart=always
[Install]
WantedBy=multi-user.target
お名前.comで取得したドメインをさくらのvpsと紐付ける
お名前.comで取得したドメインをさくらのvpsと紐付ける
参考:https://qiita.com/megane42/items/df84f87c0bdcdd015eb6
ttlは60など小さくしとくと反映されるのが早くて良い
https化
nginx.confのlistenを修正, ssl_certificate, ssl_certificate_keyを追加
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/{your domain}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/{your domain}/privkey.pem;
systemctl stop nginx
certbotはstandaloneで入れる
/usr/local/certbot/certbot-auto -n certonly -a standalone -d {your domain}
nginx.confのlistenを修正, ssl_certificate, ssl_certificate_keyを追加
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/{your domain}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/{your domain}/privkey.pem;
systemctl start nginx
certbot-auto -n certonly --webroot -w {your document directory} -d {your domain} -m {your mail adress} --agree-tos --force-renewal
standaloneで入れてからwebrootに変更すると楽そう
webrootはサーバを止めなくても証明書が入れられるオプション
config/environments/production.rbでconfig.force_ssl = true