Jak zrobić deploy aplikacji na serwer Heroku w Internecie
Przez: Lukasz Muzyka, Z dnia:
Ten tutorial zakłada, że już ukończyłeś:
- Install Ruby on Rails
- Create Ruby on Rails application
- Create Static Pages - without this deploy will not work
- Install Git
- Create Remote Git Repository - optional but recommended
Nasza prosta aplikacja działa. Nadszedł czas by działała też w Internecie po to, aby innu ludzie mieli do niej dostęp. Kiedyś był to trudny proces. Obecnie, dzięki firmom takim jak Heroku stało się bardzo, bardzo łatwe. Cały proces to tylko kilka kroków i już będziesz miał aplikację rozlokowaną na platformie Heroku.
Nasza aplikacja jest przygotowana do pracy w 3 róznych konfiguracjach: Test, Rozwój (Development) i Produkcja (Production). Środowisko testowe to specjalna konfiguracja która uruchamia odpowiednie skrypty. Sparwdzają one aplikację by upewnić się, że działa ona we właściwym porządku. Środowisko rozwoju jest tym, do którego mamy dostęp przez komendy $rails console
lub $rails server
, i które możemy otworzyć w przeglądarce pod adresem http://localhost:3000
. Produkcja to specjalna konfiguracja dla serwera. Wszystkie te środowiska używają różnych baz danych i możemy w nich używać różnych adresóœ email, itd.
Będziemy używać platformy heroku.com jako naszego środowiska produkcji. Nie tylko możemy tam umieścić naszą aplikację za darmo (dopuki mamy rozsądny ruch), ale także wszystko jest gotowe do działania. Robienie deploy aplikacji, po początkowej konfiguracji, będzie tak proste jak pisanie prostych komand w Terminalu. Zacznijmy działa:
Krok 1: Stwórz konto na Heroku
Przejdź do www.heroku.com i stwórz tam konto.
Po tym, jak potwierdzisz swoje konto, wróć na https://devcenter.heroku.com/articles/quickstart by pobrać pasek narzędzi Heroku.
Krok 2: Logowanie do Heroku
Po zainstalowaniu, otwórz terminal i wpisz:
bash
heroku login
Wpisz swoje referencje:
Enter your Heroku credentials. Email: adam@example.com Password: Could not find an existing public key. Would you like to generate one? [Yn] Generating new SSH public key. Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Krok 3: Skonfiguruj bazę danych
Najpierw musimy się upewnić, że używamy właściwego typu bazy danych dla środowiska produkcji. Rails domyślnie daje nam bazę danych SQLite3, jest ona okej dla środowiska rozwoju. Jednakże Heroku wymaga używania bazy danych PostgreSQL. Upewnijmy się, że nasz Gemfile
posiada niezbędne gemy.
Zastąp:
Gemfile
# Use sqlite3 as the database for Active Record gem 'sqlite3'
z
group :development, :test do gem 'sqlite3' end group :production do gem 'pg' end
Ponieważ heroku używa pliku Gemfile.lock by zbudować naszą aplikację, będziemy musieli je teraz usunąć i ponownie zbudować z komendą "bundle install". W przeciwnym razie, heroku odrzuci nasz "push". Usuń więc plik Gemfile.lock
. Jest obok Gemfile.
Krok 4: Zainstaluj zależności
Wreszcie możemy zainstalować wszystkie nowe zależności. Wpisz w terminalu:
bash
$ bundle install
Krok 5: Zmień plik konfiguracyjny
Musimy dokonać jeszcze jednej zmiany, mianowice, modyfikacji pliku konfiguracyjnego naszej bazy danych. Przejdź do config/database.yml
:
config/database.yml
# SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: db/test.sqlite3 production: adapter: postgresql host: localhost pool: 5 timeout: 5000 database: demo_production
Zauważ, że zmieniliśmy ustawienia dla bazy danych środowiska produkcji.
Krok 6: Zmiany w repozytorium Git
Możemy teraz dodać zmiany do repozytorium git:
$ git add . $ git commit -m "changed database type for production to postgres"
Krok 7: Deploy aplikacji do Heroku
W tym momencie, jesteśmy gotowi by zrobić deploy naszej aplikacji do Heroku. Po pierwsze, musimy stworzyć aplikację:
$ heroku create pick-unique-name-of-your-application
Creating peoplecancode-demo... done, stack is cedar http://peoplecancode-demo.herokuapp.com/ | git@heroku.com:peoplecancode-demo.git
Jeżeli tworzenie się powiodło, możesz zrobić deploy aplikacji do Heroku:
$ git push heroku master
Fetching repository, done. Counting objects: 9, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (5/5), 970 bytes | 0 bytes/s, done. Total 5 (delta 4), reused 0 (delta 0) -----> Ruby app detected -----> Compiling Ruby/Rails -----> Using Ruby version: ruby-2.0.0 -----> Installing dependencies using 1.5.2 Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment Using rake (10.3.1) Using json (1.8.1) Using i18n (0.6.9) Using thread_safe (0.3.3) Using builder (3.2.2) Using erubis (2.7.0) Using minitest (5.3.3) Using rack (1.5.2) Using polyglot (0.3.4) Using mime-types (1.25.1) Using arel (5.0.1.20140414130214) Using bcrypt (3.1.7) Using coffee-script-source (1.7.0) Using execjs (2.0.2) Using thor (0.19.1) Using orm_adapter (0.5.0) Using hike (1.2.3) Using multi_json (1.9.3) Using pg (0.17.1) Using bundler (1.5.2) Using tilt (1.4.1) Using sass (3.2.19) Using rdoc (4.1.1) Using tzinfo (1.1.0) Using treetop (1.4.15) Using rack-test (0.6.2) Using warden (1.2.3) Using uglifier (2.5.0) Using sprockets (2.11.0) Using coffee-script (2.2.0) Using sdoc (0.4.0) Using activesupport (4.1.0) Using mail (2.5.4) Using actionview (4.1.0) Using activemodel (4.1.0) Using jbuilder (2.0.6) Using activerecord (4.1.0) Using actionpack (4.1.0) Using actionmailer (4.1.0) Using railties (4.1.0) Using sprockets-rails (2.1.3) Using coffee-rails (4.0.1) Using devise (3.2.4) Using rails (4.1.0) Using jquery-rails (3.1.0) Using sass-rails (4.0.3) Using turbolinks (2.2.2) Your bundle is complete! Gems in the groups development and test were not installed. It was installed into ./vendor/bundle Bundle completed (0.58s) Cleaning up the bundler cache. -----> Preparing app for Rails asset pipeline Running: rake assets:precompile Asset precompilation completed (2.05s) Cleaning assets Running: rake assets:clean -----> WARNINGS: Include 'rails_12factor' gem to enable all platform features See https://devcenter.heroku.com/articles/rails-integration-gems for more information. You have not declared a Ruby version in your Gemfile. To set your Ruby version add this line to your Gemfile: ruby '2.0.0' # See https://devcenter.heroku.com/articles/ruby-versions for more information. No Profile detected, using the default web server (webrick) https://devcenter.heroku.com/articles/ruby-default-web-server -----> Discovering process types Procfile declares types -> (none) Default types for Ruby -> console, rake, web, worker -----> Compressing... done, 21.5MB -----> Launching... done, v9 http://sc-demo-peoplecancode.herokuapp.com/ deployed to Heroku To git@heroku.com:sc-demo-peoplecancode.git e8bbe97..6f47150 master -> master
Druga linijka od dołu pokazuje ardes Twojej aplikacji. Otwórz go w przeglądarce. Twoja aplikacjia jest teraz w Internecie. Śmiało, podzielcie się linkami w komentarzach :).
Komentarze
Dodaj komentarz
Możesz się zalogować by skomentować
Z dnia: Frank napisał:
Z dnia: Philip Lohrmann napisał:
Z dnia: Michael Lajlev napisał:
Z dnia: Brunitob napisał:
Z dnia: silvina napisał:
Hello: thank you for the wonderful tutorial and my issue is with heroku. I had an account from long ago and this is the error i get silvinas-MacBook-Pro:static formsmama$ git push heroku master fatal: 'git.heroku.com/floating-dragon-42.git' does not appear to be a git repository fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
I did add the new ssh key to heroku but the error still there.
Z dnia: silvina napisał:
Do we have to perform the configuration change every time we create a new project?
Z dnia: Brayan López napisał:
https://design1elearning.herokuapp.com/
Z dnia: David Cruz e Silva napisał:
I did bundle install and got the following error
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run
bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java
. Fetching gem metadata from https://rubygems.org/.......... Fetching version metadata from https://rubygems.org/.. Fetching dependency metadata from https://rubygems.org/. Resolving dependencies... Using rake 12.0.0 Using concurrent-ruby 1.0.5 Using i18n 0.8.1 Using minitest 5.10.1 Using threadsafe 0.3.6 Using builder 3.2.3 Using erubis 2.7.0 Using miniportile2 2.1.0 Using rack 2.0.1 Using nio4r 2.0.0 Using websocket-extensions 0.1.2 Using mime-types-data 3.2016.0521 Using arel 7.1.4 Using bundler 1.14.6 Using byebug 9.0.6 Using coffee-script-source 1.12.2 Using execjs 2.7.0 Using methodsource 0.8.2 Using thor 0.19.4 Using debuginspector 0.0.2 Using ffi 1.9.18 Using multi_json 1.12.1 Using rb-fsevent 0.9.8 Installing pg 0.19.0 with native extensions Using puma 3.7.1 Using sass 3.4.23 Using tilt 2.0.6 Using sqlite3 1.3.13 Using turbolinks-source 5.0.0 Using tzinfo 1.2.2 Using nokogiri 1.7.0.1 Using rack-test 0.6.3 Using sprockets 3.7.1 Using websocket-driver 0.6.5 Using mime-types 3.1 Using coffee-script 2.4.1 Using uglifier 3.1.4 Using rb-inotify 0.9.8 Gem::Ext::BuildError: ERROR: Failed to build gem native extension./Users/David/.rvm/rubies/ruby-2.4.0/bin/ruby -r ./siteconf20170306-20385-1sgh9li.rb extconf.rb checking for pgconfig... no No pgconfig... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header extconf.rb failed Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.
Provided configuration options:
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Users/David/.rvm/gems/ruby-2.4.0/extensions/x86_64-darwin-16/2.4.0/pg-0.19.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Users/David/.rvm/gems/ruby-2.4.0/gems/pg-0.19.0 for inspection. Results logged to /Users/David/.rvm/gems/ruby-2.4.0/extensions/x8664-darwin-16/2.4.0/pg-0.19.0/gemmake.out
An error occurred while installing pg (0.19.0), and Bundler cannot continue. Make sure that
gem install pg -v '0.19.0'
succeeds before bundling.