Skip to content
This repository was archived by the owner on Jul 21, 2022. It is now read-only.

SetupProduction

Hal Seki edited this page Oct 15, 2017 · 7 revisions

Production server environment

Server: Ubuntu Server 16.04.3 LTS 64bit

create deploy user

create user 'deployer' and add ssh public key to ~/.ssh/authorized_keys

ubuntu rvm install

as a deployer user,

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
cd /tmp
curl -sSL https://get.rvm.io -o rvm.sh
cat /tmp/rvm.sh | bash -s stable --rails
source /home/deployer/.rvm/scripts/rvm
rvm install 2.4.2
gem install bundler

install mysqlclient

sudo apt-get install libmysqlclient-dev

install nodejs

cd /tmp
\curl -sSL https://deb.nodesource.com/setup_6.x -o nodejs.sh
less nodejs.sh
cat /tmp/nodejs.sh | sudo -E bash -
sudo apt-get update
sudo apt-get install -y nodejs

add db

sudo vi /etc/hosts
127.0.0.1 localhost db

install mysql

sudo apt-get install mysql-server
cd /etc/mysql/mysql.conf.d
sudo curl https://raw.githubusercontent.com/codeforjapan/codeforelection_front/master/db-server/my_cfje.cnf -o my_cfje.cnf
sudo service mysql restart

DB、ユーザ 作成

mysql -u root -p
% create database cfj_election_front_production default character set utf8;
% CREATE USER 'cfj_election_front'@'localhost' IDENTIFIED BY 'password';
% GRANT ALL PRIVILEGES ON cfj_election_front_production.* TO 'cfj_election_front'@'localhost';

Yarn インストール

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

install nginx

sudo apt-get install nginx 

setup nginx config

sudo vi /etc/nginx/sites-available/default

upstream unicorn {
  server unix:/var/www/codeforelection_front/shared/tmp/sockets/unicorn.sock;
}
server {
  listen 80 default_server;
  server_name kouhosha.info;

  access_log /var/log/nginx/candidatesnavi_access.log;
  error_log /var/log/nginx/candidatesnavi_error.log;

  root /var/www/codeforelection_front/current/public;

  client_max_body_size 100m;
  error_page 404 /404.html;
  error_page 500 502 503 504 /500.html;
  try_files $uri/index.html $uri @unicorn;

  location @unicorn {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_pass http://unicorn;
  }
}

create directory for the application

sudo mkdir /var/www/codeforelection_front/
sudo chown deployer /var/www/codeforelection_front/

Add public key to deployer user.`

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqQIsrYTwZKlkLzIF74uk+dNAy0MUFTDtjeRfHQ+TVqlRPvLZbIxdvo6+BMhd6a+mv/KjBUHQ5TF40u7acZHV58htvX2fVpTi63jasOOAtGsBGgnOf0fwr6VV0T6BiIc2gHy9p6c6fdlpBfwIGxgzCFCKejoGZnMHMxiweWG7f2kuuE31GFVTUQcX1Q3ZbC77ie4XKGa+GJf+jX23bmRDgHmUHm4eerrzu3KqqRtZjBoJWkQzuTgXvx4rqeUwqj/LovOheffpt+42TOpmrsBre355DwMBXsCerq+ZYEQ+O8wIpK5HUoWDYDYT0amxuoPgj3OTqnyge3daQficF0qsp hal@hal-sk.local

deploy code to the server

We use CircleCI to publish this repos. The first deployment will be failed once. Please follow next instruction.

copy database.yml and secrets.yml

cd /var/www/codeforelection_front/shared/config
curl https://raw.githubusercontent.com/codeforjapan/codeforelection_front/master/config/database.yml -o database.yml
curl https://raw.githubusercontent.com/codeforjapan/codeforelection_front/master/config/secrets.yml -o secrets.yml

# edit password
vi /var/www/codeforelection_front/shared/config/database.yml
# edit secret key
vi /var/www/codeforelection_front/shared/config/secrets.yml

deploy code to the server again

Setup ssh

Follow instructions in here

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx 
$ sudo certbot --nginx