概要

ローカル開発環境の構築

Vagrant初期設定

$ vagrant box add Ubuntu16.04 https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-vagrant.box
$ mkdir heroku_lessons
$ cd heroku_lessons
$ vagrant init Ubuntu16.04
$ vi Vagrantfile
--------------------
config.vm.network "private_network", ip: "192.168.78.90"
--------------------
$ vagrant up
$ vagrant ssh
$ sudo apt-get update

Gitインストール

$ sudo apt-get install git
$ git --version
git version 2.7.4
$ git config --global user.name "Yuji Shimojo"
$ git config --global user.email "y.shimojo@example.com"
$ git config --global color.ui true

Rubyインストール

$ sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libpq-dev libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
$ exec $SHELL
$ echo "${PATH}"
/home/ubuntu/.rbenv/plugins/ruby-build/bin:/home/ubuntu/.rbenv/shims:/home/ubuntu/.rbenv/bin:/home/ubuntu/.rbenv/shims:/home/ubuntu/.rbenv/bin:/home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
$ rbenv install --list
$ sudo rbenv install 2.4.1
$ sudo rbenv global 2.4.1
$ rbenv global
2.4.1
$ rbenv version
2.4.1 (set by /home/ubuntu/.rbenv/version)
$ ruby --version
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
$ gem --version
2.6.11
$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 2.6.11
  - RUBY VERSION: 2.4.1 (2017-03-22 patchlevel 111) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/ubuntu/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0
  - USER INSTALLATION DIRECTORY: /home/ubuntu/.gem/ruby/2.4.0
  - RUBY EXECUTABLE: /home/ubuntu/.rbenv/versions/2.4.1/bin/ruby
  - EXECUTABLE DIRECTORY: /home/ubuntu/.rbenv/versions/2.4.1/bin
  - SPEC CACHE DIRECTORY: /home/ubuntu/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /home/ubuntu/.rbenv/versions/2.4.1/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/ubuntu/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0
     - /home/ubuntu/.gem/ruby/2.4.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - "install" => "--no-rdoc --no-ri"
     - "update" => "--no-rdoc --no-ri"
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/ubuntu/.rbenv/versions/2.4.1/bin
     - /home/ubuntu/.rbenv/libexec
     - /home/ubuntu/.rbenv/plugins/ruby-build/bin
     - /home/ubuntu/bin
     - /home/ubuntu/.local/bin
     - /home/ubuntu/.rbenv/plugins/ruby-build/bin
     - /home/ubuntu/.rbenv/shims
     - /home/ubuntu/.rbenv/bin
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
     - /usr/games
     - /usr/local/games
     - /snap/bin
$ rbenv rehash
$ sudo gem i rbenv-rehash

Ruby on Railsインストール

$ curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
$ sudo apt-get install -y nodejs
$ echo 'install: --no-rdoc --no-ri' >> ~/.gemrc
$ echo 'update: --no-rdoc --no-ri' >> ~/.gemrc
$ gem query -ra -n  "^rails$"
$ sudo gem install rails -v 5.1.3
$ rails -v
Rails 5.1.3

PostgreSQLインストール

$ sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
$ wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install -y postgresql-common
$ sudo apt-get install -y postgresql-9.6 libpq-dev
$ psql --version
psql (PostgreSQL) 9.6.3
$ psql -l
psql: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
$ sudo vi /etc/postgresql/9.6/main/postgresql.conf
listen_addresses = 'localhost'
$ sudo -u postgres createuser ubuntu -s
$ sudo systemctl restart postgresql
$ psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres

PaaS (Heroku) の仕組みを知ろう

Heroku CLIインストール

$ wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh
$ heroku --version
heroku-cli/6.13.7 (linux-x64) node-v8.2.1
$ brew upgrade
$ brew update
$ brew install heroku
$ heroku --version
heroku-cli/6.13.7 (darwin-x64) node-v8.2.1
$ ssh-keygen -t rsa -C y.shimojo@example.com
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): /home/ubuntu/.ssh/id_heroku_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_heroku_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_heroku_rsa.pub.
$ heroku login
Enter your Heroku credentials:
Email: y.shimojo@example.com
Password: ********
Logged in as y.shimojo@example.com
$ heroku keys:add /home/ubuntu/.ssh/id_heroku_rsa.pub
Uploading /home/ubuntu/.ssh/id_heroku_rsa.pub SSH key... done

Railsアプリケーションを作ろう

$ rails new mymemo -d postgresql
$ cd mymemo
$ vi Gemfile
gem 'therubyracer', platforms: :ruby
$ rails s
Could not find gem 'therubyracer' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
$ sudo gem install bundler --no-document
$ bundle install
$ rails s
=> Booting Puma
=> Rails 5.0.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.9.1 (ruby 2.3.1-p112), codename: Private Caller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
$ rails g scaffold Memo title:string body:text
$ sudo rbenv exec gem install bundler
$ rbenv rehash
$ bundle config build.nokogiri --with-xml2-config=/usr/local/opt/libxml2/bin/xml2-config --with-xslt-config=/usr/local/opt/libxslt/bin/xslt-config
$ bundle config build.mini_portile2 --use-system-libraries
$ bundle install
$ rbenv rehash
$ rails g scaffold Memo title:string body:text
Running via Spring preloader in process 13342
      invoke  active_record
      create    db/migrate/20170806093511_create_memos.rb
      create    app/models/memo.rb
      invoke    test_unit
      create      test/models/memo_test.rb
      create      test/fixtures/memos.yml
      invoke  resource_route
       route    resources :memos
      invoke  scaffold_controller
      create    app/controllers/memos_controller.rb
      invoke    erb
      create      app/views/memos
      create      app/views/memos/index.html.erb
      create      app/views/memos/edit.html.erb
      create      app/views/memos/show.html.erb
      create      app/views/memos/new.html.erb
      create      app/views/memos/_form.html.erb
      invoke    test_unit
      create      test/controllers/memos_controller_test.rb
      invoke    helper
      create      app/helpers/memos_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/memos/index.json.jbuilder
      create      app/views/memos/show.json.jbuilder
      create      app/views/memos/_memo.json.jbuilder
      invoke  test_unit
      create    test/system/memos_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/memos.coffee
      invoke    scss
      create      app/assets/stylesheets/memos.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss
$ bundle exec rake db:create
Created database 'mymemo_development'
Created database 'mymemo_test'
$ bundle exec rake db:migrate
== 20170806093511 CreateMemos: migrating ======================================
-- create_table(:memos)
   -> 0.0051s
== 20170806093511 CreateMemos: migrated (0.0063s) =============================
$ rails s
$ bundle install
$ pwd
/home/ubuntu/mymemo
$ git init
$ git add .
$ git commit -m "initial commit"

heroku createしてみよう

$ vi Gemfile
gem 'rails_12factor', group: :production
$ vi Procfile
web: bundle exec rails server -p $PORT
$ git add .
$ git commit -m "Gemfile updated, Procfile added"
$ heroku create
Creating app... done, ⬢ tranquil-headland-xxxxx
https://tranquil-headland-90386.herokuapp.com/ | https://git.heroku.com/tranquil-headland-xxxxx.git

Heroku上で確認してみよう

$ git push heroku master
$ heroku addons:add heroku-postgresql
Creating heroku-postgresql on ⬢ tranquil-headland-xxxxx... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-encircled-xxxxx as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation
$ heroku run rake db:migrate
$ heroku apps:info
=== tranquil-headland-xxxxx
Addons:         heroku-postgresql:hobby-dev
Auto Cert Mgmt: false
Dynos:
Git URL:        https://git.heroku.com/tranquil-headland-xxxxx.git
Owner:          y.shimojo@example.com
Region:         us
Repo Size:      0 B
Slug Size:      0 B
Stack:          heroku-16
Web URL:        https://tranquil-headland-xxxxx.herokuapp.com/
$ heroku apps:info
 ▸    No app specified.
 ▸    USAGE: heroku info my-app
$ heroku list
=== y.shimojo@example.com Apps
tranquil-headland-xxxxx
$ heroku info tranquil-headland-xxxxx
=== tranquil-headland-xxxxx
Addons:         heroku-postgresql:hobby-dev
Auto Cert Mgmt: false
Dynos:          web: 1
Git URL:        https://git.heroku.com/tranquil-headland-xxxxx.git
Owner:          y.shimojo@example.com
Region:         us
Repo Size:      37 KB
Slug Size:      38 MB
Stack:          heroku-16
Web URL:        https://tranquil-headland-xxxxx.herokuapp.com/

heroku ps/logs/maintenanceを使おう

$ heroku ps
Free dyno hours quota remaining this month: 550h 0m (100%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping

=== web (Free): bundle exec rails server -p $PORT (1)
web.1: up 2017/08/06 10:52:12 +0000 (~ 4m ago)
$ heroku ps:scale web=2
Scaling dynos... !
 ▸    Cannot update to more than 1 Free size dynos per process type.
$ heroku logs
$ heroku logs --tail
$ heroku maintenance:on
$ heroku maintenance
on
$ heroku maintenance:off

One-off Dynosを使ってみよう

$ heroku run bash
~ $ ls
app  bin  config  config.ru  db  Gemfile  Gemfile.lock	lib  log  package.json	Procfile  public  Rakefile  README.md  test  tmp  vendor
~ $ exit
exit
$ heroku run rails console
irb(main):001:0> m = Memo.new({title:"hoge",body:"hogehoge"})
=> #<Memo id: nil, title: "hoge", body: "hogehoge", created_at: nil, updated_at: nil>
irb(main):002:0> m.save
   (1.3ms)  BEGIN
  SQL (1.7ms)  INSERT INTO "memos" ("title", "body", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["title", "hoge"], ["body", "hogehoge"], ["created_at", "2017-08-06 11:07:29.045107"], ["updated_at", "2017-08-06 11:07:29.045107"]]
   (2.1ms)  COMMIT
=> true
irb(main):003:0> auit

heroku releasesを使ってみよう

$ vi app/views/memos/index.html.erb
<p>hello world</p>
$ git add .
$ git commit -m "added hello world"
$ git push heroku master
$ heroku releases
=== tranquil-headland-xxxxx Releases - Current: v6
v6  Deploy 24731a70                                                                                             y.shimojo@example.com  2017/08/06 11:15:12 +0000 (~ 1m ago)
v5  Deploy 80b93304                                                                                             y.shimojo@example.com  2017/08/06 10:52:04 +0000 (~ 24m ago)
v4  Set LANG, RACK_ENV, RAILS_ENV, RAILS_LOG_TO_STDOUT, RAILS_SERVE_STATIC_FILES, SECRET_KEY_BASE config vars   yjshimojo@gmail.com  2017/08/06 10:52:03 +0000 (~ 24m ago)
v3  Attach DATABASE (@ref:postgresql-encircled-86963)                                                           y.shimojo@example.com  2017/08/06 10:42:49 +0000 (~ 33m ago)
v2  Enable Logplex                                                                                              y.shimojo@example.com  2017/08/06 10:35:32 +0000 (~ 40m ago)
v1  Initial release                                                                                             y.shimojo@example.com  2017/08/06 10:35:32 +0000 (~ 40m ago)
$ heroku releases:rollback

環境変数を使ってみよう

$ heroku config:set mykey=value
$ heroku config:get mykey
$ heroku config
$ heroku config:unset mykey
$ vi app/views/memos/index.html.erb
<p><%= ENV['mykey'] %></p>
$ git add .
$ git commit -m "mykey added"
$ git push heroku master

foremanを使ってみよう

$ sudo apt-get install ruby-foreman
$ vi .env
mykey=mylocalvalue
$ foreman start
$ vi .gitignore
.env

Python 環境構築

ローカル開発環境の構築

$ sudo apt-get install libbz2-dev llvm libncurses5-dev libncursesw5-dev
$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv
$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
$ vi ~/.bashrc
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
fi
$ source ~/.bashrc
$ pyenv install -l | grep 3.6
  3.3.6
  3.6.0
  3.6-dev
  3.6.1
  3.6.2
$ pyenv install 3.6.1
$ pyenv global 3.6.1
$ python --version
Python 3.6.1

Heroku アプリ作成 / デプロイ

$ git clone https://github.com/heroku/python-getting-started.git
$ cd python-getting-started
$ heroku create
Creating app... done, &#11042; lit-shelf-xxxxx
https://lit-shelf-xxxxx.herokuapp.com/ | https://git.heroku.com/lit-shelf-xxxxx.git
$ git push heroku master
Counting objects: 328, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (146/146), done.
Writing objects: 100% (328/328), 50.90 KiB | 0 bytes/s, done.
Total 328 (delta 160), reused 328 (delta 160)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-3.6.2
remote: -----> Installing pip
remote: -----> Installing requirements with pip
remote:        Collecting dj-database-url==0.4.1 (from -r /tmp/build_bb2966442a02c5c434c952dd4f1f9c93/requirements.txt (line 1))
remote:          Downloading dj-database-url-0.4.1.tar.gz
remote:        Collecting Django==1.11 (from -r /tmp/build_bb2966442a02c5c434c952dd4f1f9c93/requirements.txt (line 2))
remote:          Downloading Django-1.11-py2.py3-none-any.whl (6.9MB)
remote:        Collecting gunicorn==19.6.0 (from -r /tmp/build_bb2966442a02c5c434c952dd4f1f9c93/requirements.txt (line 3))
remote:          Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
remote:        Collecting psycopg2==2.6.2 (from -r /tmp/build_bb2966442a02c5c434c952dd4f1f9c93/requirements.txt (line 4))
remote:          Downloading psycopg2-2.6.2.tar.gz (376kB)
remote:        Collecting whitenoise==3.3.0 (from -r /tmp/build_bb2966442a02c5c434c952dd4f1f9c93/requirements.txt (line 5))
remote:          Downloading whitenoise-3.3.0-py2.py3-none-any.whl
remote:        Collecting pytz (from Django==1.11->-r /tmp/build_bb2966442a02c5c434c952dd4f1f9c93/requirements.txt (line 2))
remote:          Downloading pytz-2017.2-py2.py3-none-any.whl (484kB)
remote:        Installing collected packages: dj-database-url, pytz, Django, gunicorn, psycopg2, whitenoise
remote:          Running setup.py install for dj-database-url: started
remote:            Running setup.py install for dj-database-url: finished with status 'done'
remote:          Running setup.py install for psycopg2: started
remote:            Running setup.py install for psycopg2: finished with status 'done'
remote:        Successfully installed Django-1.11 dj-database-url-0.4.1 gunicorn-19.6.0 psycopg2-2.6.2 pytz-2017.2 whitenoise-3.3.0
remote:
remote: -----> $ python manage.py collectstatic --noinput
remote:        63 static files copied to '/tmp/build_bb2966442a02c5c434c952dd4f1f9c93/gettingstarted/staticfiles', 79 post-processed.
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 49.1M
remote: -----> Launching...
remote:        Released v4
remote:        https://lit-shelf-xxxxx.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/lit-shelf-xxxxx.git
 * [new branch]      master -> master
$ heroku ps:scale web=1
$ heroku ps
$ heroku logs --tail
$ view Procfile

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS