Cómo implementar una aplicación a un servidor en línea usando Heroku
Por: Lukasz Muzyka, en:
ES:This tutorial assumes you have already completed:
- 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
Nuestra aplicación ya está funcionando. Ahora es tiempo de que tambien funcione en el internet, así otras personas podrán acceder a ella. Antes, este un proceso era muy difícil. Hoy en día, gracias a empresas como Heroku, el proceso se ha vuelto muy simple. Los pasos para desplegar la aplicación en la plataforma de Heroku son muy sencillos.
Nuestra aplicación está preparada para funcionar en tres diferentes configuraciones: prueba, desarrollo y producción. El entorno de prueba es una configuración especial que se ejecuta en secuencias de comandos especiales. Estos scripts comprueban que la aplicación está trabajando en el orden correcto. El desarrollo es lo que podemos acceder cuando corremos $rails console
o $rails server
y abre un navegador con la dirección http://localhost:3000. La producción es una configuración especial para el servidor. Todos los ambientes están utilizando diferentes bases de datos y podemos hacerlos utilizar diferentes direcciones de correo electrónico y más.
Para nuestro entorno de producción, nosotros estaremos usando la plataforma heroku.com. No sólo podemos organizar nuestra aplicación gratuitamente allí (hasta que generamos tráfico razonable) sino también ya todo está listo para funcionar. Después de la configuración inicial, implementaras una aplicación. Esto será tan fácil como escribir un simple comando en la Terminal.
Paso 1: Crea una cuenta en Heroku
Ve a www.heroku.com y crea una cuenta.
Después de confirmar tu cuenta, vuelve a https://devcenter.heroku.com/articles/quickstart y descargar el Spade toolbelt.
Paso 2: Inicio de sesión de Heroku
Después de instalarlo, abre tu terminal y escribe:
bash
heroku login
Introduce tus credenciales:
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
Paso 3: Configura la Base de Datos
Primero tenemos que asegurarnos que estamos utilizamos el tipo de base de datos adecuado para nuestro entorno de producción. Rails, de forma predeterminada, nos dio una base de datos SQLite3, que está bien para nuestro entorno de desarrollo. Heroku sin embargo, quiere utilizar la base de datos PostgreSQL. Vamos a asegurarnos de que nuestro Gemfile
tiene las gemas necesarias.
Remplaza:
Gemfile
# Use sqlite3 as the database for Active Record gem 'sqlite3'
por
group :development, :test do gem 'sqlite3' end group :production do gem 'pg' end
Paso 4: Instala las dependencias
Ahora podemos instalar todas las nuevas dependencias. En la terminal, escribe:
bash
$ bundle install
Paso 5: Cambia la Configuración
Otro cambio que tenemos que hacer es cambiar el archivo de configuración para nuestra base de datos. Dirígete a 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
Ten en cuenta que hemos cambiado la configuración de la producción de base de datos.
Paso 6: Cambia el repositorio Git
Ahora podemos entregar los cambios al repositorio git:
$ git add . $ git commit -m "changed database type for production to postgres"
Paso 7: Desplega la aplicación en Heroku
Ya estamos listos para desplegar nuestra aplicación en Heroku. Para empezar, tenemos que crear una aplicación:
$ heroku create escoge-un-nombre-unico-para-tu-aplicacion
Creating peoplecancode-demo... done, stack is cedar http://peoplecancode-demo.herokuapp.com/ | git@heroku.com:peoplecancode-demo.git
Si la creación ha funcionado, serás capaz de desplegar la aplicación en 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
La antepenúltima línea muestra tu UTL. Abrelo en tu navegador. Tu aplicación está ya el en internet. Por favor comparte tu enlace en los comentarios :-)
Comentarios
Comentar
Tú puedes Inicio de sesión Comentar
en: Frank escribió:
en: Philip Lohrmann escribió:
en: Michael Lajlev escribió:
en: Brunitob escribió:
en: silvina escribió:
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.
en: silvina escribió:
Do we have to perform the configuration change every time we create a new project?
en: Brayan López escribió:
https://design1elearning.herokuapp.com/
en: David Cruz e Silva escribió:
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.