LoginSignup
2
0

More than 5 years have passed since last update.

GCEでstaging環境を作ったのでめも

Last updated at Posted at 2017-12-22

※ 自分用

GCEでサーバーを立てる

前提環境

RoR アプリケーション
Jsでwebpackを利用している
ディスクのOSイメージをCentOS6にしている

前提知識

以下のワードの理解があると望ましい

sudo chown chmod ssh
gcloud vmインスタンス
npm yarn webpack webpack-dev-server
webpacker nginx unicorn

結論

新規ユーザーを作り、yum,node,rbenv,rubyで積む。
sshでgithubと繋げるようにし、nginx,unicornの設定をする。
mysqlを入れ直し、db作りマイグレートする。
.env内容やmy.cnfを/etc配下にそれぞれ設定する
(getenforceをdisabledにしたりする)
npmでyarnを入れ、webpackerでcompileする
unicorn, nginxを再起動しデプロイdone

コマンド一覧

※ user->hoge, directory->fuga, nginxの設定->piyo.confはそれぞれコンテキストに依存する
※ mkdir, cd, sudoなど細かいことはあまり気にしてない

1   sudo adduser hoge
2   sudo passwd hoge
3   sudo visudo #ALL以下に追記
4   sudo su - hoge
5   sudo yum install \
    git make gcc-c++ patch \
    libyaml-devel libffi-devel libicu-devel \
    zlib-devel readline-devel mysql-server mysql-devel  -y
6   sudo curl -sL https://rpm.nodesource.com/setup_6.x | sudo bash -
7   sudo yum install -y nodejs
8   git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
9   echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
10  echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
11  git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
12  rbenv rehash
13  source ~/.bash_profile
14  sudo yum -y install bzip2
15  rbenv install -v 2.4.1
16  rbenv global 2.4.1
17  rbenv rehash
18  source ~/.bash_profile
19  vim .gitconfig #github情報を入れる
20  cd .ssh/ | ssh-keygen -t rsa #githubにpub鍵を登録する
21  vim config #gihtub情報を入れる
22  chmod 600 config
23  ssh -T github
24  sudo chown -R hoge /var/www/
25  git clone git@github.com:hogehoge/fuga.git
26  sudo vi /etc/selinux/config
27  sudo reboot
28  sudo yum install nginx
29  sudo vi /etc/nginx/conf.d/piyo.conf
30  sudo chmod -R 775 /var/lib/nginx
31  gem install bundler
32  bundle install
33  sudo yum remove mysql*
34  sudo yum install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
35  sudo yum install mysql-client mysql-server
36  sudo /etc/init.d/mysqld start
37  sudo mysql -uroot -p
    mysql> GRANT ALL PRIVILEGES ON *.* TO ''@'localhost';
38  sudo /sbin/service mysqld restart
39  bundle exec gem uninstall mysql2
40  sudo yum install mysql-devel
41  bundle install
42  bundle exec rake db:create RAILS_ENV=staging
43  sudo vim /etc/my.cnf
44  sudo /sbin/service mysqld restart
45  source /etc/environment
46  RAILS_ENV=staging bundle exec ridgepole -c config/database.yml -E staging --apply -f db/schemas/Schemafile
47  sudo npm install --global yarn
48  yarn install
49  bundle exec rails webpacker:compile NODE_ENV=staging
50  bundle exec unicorn_rails -c /var/www/fuga/config/unicorn.conf.rb -D -E staging
51  sudo /sbin/service nginx restart
あとは状況に応じて以下のコマンドを打つ(yarnやridgepoleを打たなくていい時も当然ある)
上記面倒なので、capistranoあたりを入れ、最終的にslackのprivate chから誰でもコマンドでデプロイできるようにしておく
1   gcloud compute --project "hoge-fuga-piyo" ssh --zone "asia-northeast1-a" "hoge-staging"
2   sudo su - hoge
3   cd /var/www/fuga/
4   sudo chown -R hoge /var/www #複数人でgceをさわっている場合
5   git pull origin master
6   sudo kill -QUIT `cat tmp/pids/unicorn.pid`
7   yarn install
8   NODE_ENV=staging bundle exec rails webpacker:compile
9   RAILS_ENV=staging bundle exec ridgepole -c config/database.yml -E staging --apply -f db/schemas/Schemafile
10  bundle exec unicorn_rails -c /var/www/fuga/config/unicorn.conf.rb -D -E staging
11  sudo /sbin/service nginx restart

※ 地味にrack-dev-markが良い

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0