Подготовка системы
1 | apt install curl git gnupg2 nodejs |
Устанавливаем RVM
Переходим на сайт rvm.io и смотрим команды установки:
1 2 | gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB curl -sSL https: //get .rvm.io | bash -s stable |
Добавляем пользователя в группу:
1 | vim /etc/group |
rvm:x:1001:ubuntu
Имя моего пользователя в системе — ubuntu
Затем от пользователя:
1 | source /etc/profile .d /rvm .sh |
Узнать версию RVM:
1 | rvm --version |
Посмотреть список доступных для установки версий Ruby:
1 | rvm list known |
Установим Ruby последней версии:
1 | rvm install "ruby-2.5.1" |
Список установленных Ruby версий и используемую по умолчанию можно узнать так:
1 | rvm list |
Посмотрим версию gem и обновим ее:
1 2 | gem - v gem update --system |
Установим Rails:
1 | gem install rails |
Создание приложения
Создадим тестовое приложение:
1 | rails new example_app -T |
Перейдем в директорию нашего приложения:
1 | cd example_app |
Создадим для теста index.html
1 | vim public /index .html |
Вставим туда следующее:
1 2 3 4 5 6 7 8 9 | <! DOCTYPE html> < html > < head > < title >Ruby</ title > </ head > < body > Example Ruby App </ body > </ html > |
Запускаем для теста на всех интерфейсах:
1 2 3 4 5 6 7 8 9 10 11 | bundle exec rails s -p 3000 -b '0.0.0.0' => Booting Puma => Rails 5.2.2 application starting in development => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.12.0 (ruby 2.5.1-p57), codename: Llamas in Pajamas * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp: //0 .0.0.0:3000 Use Ctrl-C to stop |
Проверяем в браузере:
1.2.3.4:3000
Где 1.2.3.4 — IP адрес компьютера, где запущено приложение
Если на компьютере есть графическая оболочка, можно не указывать IP адрес при запуске приложения, и зайти в браузере по локалхосту. Так же без графики локалхост можно проверить курлом или консольным браузером
Можно останавливать приложение — Ctrl-C
Git
Находясь в корне нашего приложения инициализируем репозиторий:
1 2 3 | git init Reinitialized existing Git repository in /home/ubuntu/heroku/example_app/ .git/ |
Проверим статус:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore .ruby-version Gemfile Gemfile.lock README.md Rakefile app/ bin/ config.ru config/ db/ lib/ log/ package.json public/ storage/ tmp/ vendor/ nothing added to commit but untracked files present (use "git add" to track) |
Добавим все файлы:
1 | git add . |
И делаем комит:
1 | git commit -am "Add Ruby files to repo" |
Проверим:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file : .gitignore new file : .ruby-version new file : Gemfile new file : Gemfile.lock new file : README.md new file : Rakefile new file : app /assets/config/manifest .js new file : app /assets/images/ .keep new file : app /assets/javascripts/application .js new file : app /assets/javascripts/cable .js new file : app /assets/javascripts/channels/ .keep new file : app /assets/stylesheets/application .css new file : app /channels/application_cable/channel .rb new file : app /channels/application_cable/connection .rb new file : app /controllers/application_controller .rb new file : app /controllers/concerns/ .keep new file : app /helpers/application_helper .rb new file : app /jobs/application_job .rb new file : app /mailers/application_mailer .rb new file : app /models/application_record .rb new file : app /models/concerns/ .keep new file : app /views/layouts/application .html.erb new file : app /views/layouts/mailer .html.erb new file : app /views/layouts/mailer .text.erb new file : bin /bundle new file : bin /rails new file : bin /rake new file : bin /setup new file : bin /spring new file : bin /update new file : bin /yarn new file : config.ru new file : config /application .rb new file : config /boot .rb new file : config /cable .yml new file : config /credentials .yml.enc new file : config /database .yml new file : config /environment .rb new file : config /environments/development .rb new file : config /environments/production .rb new file : config /environments/test .rb new file : config /initializers/application_controller_renderer .rb new file : config /initializers/assets .rb new file : config /initializers/backtrace_silencers .rb new file : config /initializers/content_security_policy .rb new file : config /initializers/cookies_serializer .rb new file : config /initializers/filter_parameter_logging .rb new file : config /initializers/inflections .rb new file : config /initializers/mime_types .rb new file : config /initializers/wrap_parameters .rb new file : config /locales/en .yml new file : config /puma .rb new file : config /routes .rb new file : config /spring .rb new file : config /storage .yml new file : db /seeds .rb new file : lib /assets/ .keep new file : lib /tasks/ .keep new file : log/.keep new file : package.json new file : public /404 .html new file : public /422 .html new file : public /500 .html new file : public /apple-touch-icon-precomposed .png new file : public /apple-touch-icon .png new file : public /favicon .ico new file : public /index .html new file : public /robots .txt new file : storage/.keep new file : tmp/.keep new file : vendor/.keep |
Heroku
Регистрируемся на heroku.com и заходим в аккаунт.
Для удобства работы с Heroku установим их CLI
1 | sudo snap install --classic heroku |
Установку для дистрибутивов отличных от Ubuntu можно посмотреть на сайте Heroku
Проверяем:
1 2 3 | heroku --version heroku /7 .19.4 linux-x64 node-v11.3.0 |
Авторизуемся в Heroku CLI:
1 | heroku login |
Жмем любою клавишу и получаем URL авторизации, копируем ее в браузер, логинимся.
Создадим приложение на Heroku:
1 2 3 4 | heroku create Creating app... done , ⬢ my-generated-app-name-83844 https: //my-generated-app-name-83844 .herokuapp.com/ | https: //git .heroku.com /my-generated-app-name-83844 .git |
Теперь нужно в Gemfile заменить "sqlite3" на "postgres", иначе Heroku выдаст следующую ошибку:
1 2 3 4 5 6 7 8 9 10 11 12 13 | remote: An error occurred while installing sqlite3 (1.3.13), and Bundler cannot remote: continue . remote: Make sure that `gem install sqlite3 - v '1.3.13' ` succeeds before bundling. remote: remote: In Gemfile: remote: sqlite3 remote: remote: ! remote: ! Failed to install gems via Bundler. remote: ! Detected sqlite3 gem which is not supported on Heroku: remote: ! https: //devcenter .heroku.com /articles/sqlite3 remote: ! remote: ! Push rejected, failed to compile Ruby app. |
Открываем Gemfile
1 | vim Gemfile |
Находим в начале файла запись о sqlite3
1 | gem 'sqlite3' |
И переносим ее в блок "group :development, :test do"
Чтобы получилось следующее:
1 2 3 4 5 | group :development, : test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' , platforms: [:mri, :mingw, :x64_mingw] gem 'sqlite3' end |
Перед этим блоком добавим новый блок:
1 2 3 | group :production do gem 'pg' end |
Сохраняем изменения в файле
Переустанавливаем у себя без postgres
1 | bundle install --without production |
Добавляем файлы, делаем коммит и пушаем в Heroku:
1 2 3 | git add . git commit -am "Heroku" git push heroku master |
Проверим:
1 | heroku open |
В ответ получим URL, по которому можем проверить приложение на Heroku.