preloader
心得

Deploy Notice for Ruby on Rails app beginner | 部署Ruby on Rails app應學習的事情

This article is written for Rails beginners when he/she want to deploy an app. From my experience, many things must be noticed and fixed, whose range from Ubuntu, RVM, Apache, Passenger, Postgres, Git, Capistrano and GitHub.com. The following are indexes for problems, solutions and resources I had faced. It might evolve with times change.

 

  1. Ubuntu
  • List packages you have installed to a machine.
    • Command: dpkg -l | grep {packageFullnameorPartname}
  • Change machines' locale to right locations.
  • Beware what shell you are using.  (bash, csh, tcsh, or zsh)
  • Update your system regularly through commands and it will keep your system safer.
    • Command (a): sudo apt-get update
    • Command (b): sudo apt-get upgrade
    • Command (c): sudo apt-get dist-upgrade
  • Extend swap space to 1GB or same with memory https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-18-04 
  • Change login method of machine from password to RSA or other ways.
    • Generate your RSA public and private keys at local machine. Besides, remote machine is your production machine.  
      • ssh-keygen -b 4096 -t rsa
      • define name and directory for rsa keys
      • define passphrase if you want
    • Please modify your /etc/ssh/sshd_config file through root permission with
PermitRootLogin without-password
RSAAuthentication yes
PubKeyAuthentication yes
  • Append your public key to ~/.ssh/authorized_keys at remote machine from local one

    • cat ~/.ssh/your_id_rsa_name.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'
    • Or
    • cat ~/.ssh/your_id_rsa_name.pub | ssh [email protected] 'mkdir .ssh; cat >> .ssh/authorized_keys'
  • Configure your ssh agent for passphrase for easily login if you have generate ssh key with passphrase

  • service ssh restart at remote machine

  • Login from location machine to remote through this command

 

 

  1. RVM
  • Must use newest and stable version of rvm, ruby and rails.
  • Never forget to check websites of rvm and rubygems.
  • It’s an useful tool when creating environment for testing and migrating to newer ruby and rails.

 

  1. Upgrading ruby and rails on machines for development and production.
  • Instructions must be done both of machines for development and production before adopting new version of ruby and rails. Below numbers are for explanations.
    • Command (a): rvm install 2.3.0
    • Command (b): rvm gemset create rails-4.2.5.1
    • Command (c): rvm use 2.3.0
    • Command (d): rvm gemset use rails-4.2.5.1
    • Command (e): sudo apt-get install libgmp-dev
    • Command (f): gem install rails -v 4.2.5.1
    • Command (g): gem install bundler
  • Instructions must be done only on machines for production before the webapp be deployed.
    • Command (h): a2dissite youwebapp.conf # For disabling your configuration file for the webapp.
    • Command (i): sudo service apache2 reload # For reloading configurations that apache2 using instantly
    • If you use apache and passenger as http and app server respectively, and you have also installed ruby by rvm, you have to change value of PassengerRuby at /etc/apache2/site-available/yourwebapp.conf from /usr/local/rvm/gems/ruby-2.x.x/wrappers/ruby to /usr/local/rvm/gems/ruby-2.3.0/wrappers/ruby.
  • Instructions must be done only on machines for development. YOU MUST FINISH STEPS FIRSTLY STATED ABOVE PARAGRAPH OF INSTRUCTIONS MUST BE DONE BOTH OF MACHINES FOR DEVELOPMENT AND PRODUCTION BEFORE ADOPTING NEW VERSION OF RUBY AND RAILS. Because instructions vary from different versions of rails to newer one, you should focus on what function are being introduced into and deprecated from rails, which are described on that article about upgrading ruby on rails for your rails apps. Then you use the command: git checkout -b upgrading to change a branch named upgrading for starting rails app adjustments. After finishing adjustments for a rails app, you should follow below instuctions, which are described at sections of git workflow and updating configuration files in railsapp at github.io .
    • Command (a): cd homeDirectoryOfyourwebapp
    • Command (b): git add .
    • Command (c): git commit -m ''before rake rails:update"
    • Command (d): rake rails:update # You must read messages from terminal window carefully because this command would ask for an intention to overwriting a file if it encounters a conflict. Option[d] is for showing differences between the original file and overwritten one. If you always press option[y] without considering whether the actions are appropriate or not, your rails app might probably failed during launching it at machines of development and production.
    • Don’t forget to modify :rvm_ruby_version to newer version in Capfile.
    • Command (e): git add .
    • Command (f): git commit -m "work in progress"
    • Command (g): git checkout master
    • Command (h): git merge --squash upgrading
    • Command (i): git commit -m "upgrade to Rails 4.2.5.1"

 

  1. Apache
  • Listen IPv4 and IPv6 simultaneously, follows steps in this article.
  • Enable and disable a configuration of a site through apache build-in command
    • Command: a2ensite {WebsiteConfigurationFileAt/etc/apache2/sites-available}
    • Must use this command after using a2ensite. Command: service apache2 reload
    • Command: a2dissite {WebsiteConfigurationFileat/etc/apache2/sites-available}
    • Must use this command after using a2dissite. Command: service apache2 reload
  • Secure your websites when you are a intruder to someone’s one. And learning resource in chinese you can learn
  • Remember to fix uncertainty ServerName while enabling apache2 server. Follows instructions described in that article in chinese at blog.miniasp.com . I choose to add ServerName localhost at /etc/apache2/apache2.conf
    •  Or can choose to add {IP} {hostname} into the /etc/hosts file.

 

  1. Passenger

 

  1. Postgres
  • When I want to import a file regarding database dump to a new machine or new version Postgres like from 9.3 to 9.4 through remote login, I will use the command for importing quietly and stop when encountering an error
    • Command: psql -q --set ON_ERROR_STOP=on {yourDatabaseNameAlreadyInPostgres} < {inFile}
  • If I encounter an error about a collation supported by database’s encoding such as
COLLATE pg_catalog."zh_TW.UTF-8" for encoding "UTF-8" doesn't exist."

when I import a file that generated by Postgres dump, I will add one line through line(a) before the statement of “create table” at the input file(such as inFile) and subsitute any occurrence of pg_catalog."zh_TW.UTF-8" through command(e) with the collation name I created like tw.  Before that, I also check locale listing of my ubuntu machine through command (a) whether it has zh_TW.UTF-8 and zh_TW. If it doen’t have them in listing, I will use the command (b), (c) and (d). Remind: THERE MUST SAME USERNAME WITHIN NEW VERSION DATABASE/MACHINE AS OLD VERSION/MACHINE DATABASE OWNED THE DATABASENAME.

  • Command (a): locale -a
  • Command (b): sudo locale-gen zh_TW
  • Command (c): sudo locale-gen zh_TW.UTF-8
  • Command (d): sudo update-locale
  • Line (a): CREATE COLLATION tw (locale='zh_TW.UTF-8');
  • Command (e) for vim view mode: :1,$s/pg_catalog."zh_TW.UTF-8"/tw/g
  • If I want to use full-text search function of postgres and add-on for Chinese full-text search, I should follow instructions inside that article at my blog and this article about accelerating full text search at my blog, too.
  • If I want to install postgres on Ubuntu 14.04 LTS successfully, I should follow instructions in this article at postgres.org .
  • Should practice commands often and they would accerlerate your routines.
  • Know more functions/features of postgres packages you have installed.
  • Adjust methods that postgres allows your database users login in pg_hba.conf at directory /etc/postgres/{number for version}/main/ like steps described in that article at stackoverflow.com when encountering a peer authentication failed within deploying your rails app.
    • Change authenticate methods of your user in postgres database from peer to md5.

 

  1. Git
  • Should not upload sensitive data to public repositories in GitHub.com like .DS_store, config/database.yml and config/secrets.yml files. You should add information about them into .gitignore file.
  • Git tutorial and scenarios at gogojimmy.net
    • It’s a helpful command when someone pull a request to your repo at GitHub.com. You can use command: git pull origin master. You may have to deal with conflicts between remote and local repo and remove lines generated by Git. After you finished dealing with conflicts, you should use the following commands at home directory of your rails app:
    • Command (a): git add .
    • Command (b): git commit -m {'WhatMessageYouWantToRemind'}
    • Command (c): git push origin master
  • A Git book tutorial in chinese at git-scm.com

 

  1. Capistrano
  • Recommend to follow videos of Chris Oliver(gorails.com) at YouTube.com and tutorial at Capistranorb.com
  • There is no methods directly to deploy rails app from local git repo, which are not pushed to github.com/bitbuckets, to remote production and development machine through Capistrano 3. Besides, doing this via Capistrano 2 are complicated and I think you don’t wanna try.
  • Use your git server or community version of gitlab when you don’t want to use github.com/bitbuckets as storage of your private repos.
  • Often used command to deploy is
    • Command: bundle exec cap production deploy --trace
    • If you have faced the problem like this: Permission denied (publickey) , and you sure that there already has one ssh public key of you for github.com, you can use this command : ssh-add ~/.ssh/id_rsa to solve this problem.
  • Should manully add two files, which are config/database.yml and config/secrets.yml, at local repo to the directory /var/www/yourrailsapp/shared/config of a remote machine.

 

  1. GitHub.com
  • Follow instructions to create a repo at github.com, setting your ssh keys for newbies at GitHub.com. Those descriptions are clear to follow.

 

  1. Google Drive
  • Before grabing files from google drive, you must put them in a public folder. And then get the {folder-id} of public directory from share-link field of google drive. Use the folder id with following command at a remote machine, you would grab a file at remote machine at last.
    • Command: wget https://googledrive.com/host/{folder-id}/{FileNameYouWant}

 

  1. sFTP 

 

  1. fail2ban

 

  1. ufw

 

 

Reference:

  1. Setup Ruby on Rails, Postgresql and Redis on Ubuntu [http://luugiathuy.com/2014/11/setup-ruby-on-rails-postgresql-redis-on-ubuntu/](Setup Ruby on Rails, PostgreSQL and Redis on Ubuntu)

  2. Postgres sorting and locale http://stackoverflow.com/questions/18932922/postgresql-sorting-language-specific-characters-collation

  3. Postgres-server-devel support pgxs  http://www.postgresql.org/message-id/[email protected]

  4. Postgres specify a collation before you creating a table.  http://www.postgresql.org/message-id/[email protected]

  5. Apache a2ensite, a2dissite http://manpages.ubuntu.com/manpages/jaunty/man8/a2ensite.8.html  , http://blog.csdn.net/hfahe/article/details/5490223 ,  http://stackoverflow.com/questions/20591889/site-does-not-exist-error-for-a2ensite  

  6. Apache upgrade to 2.4 from 2.2 http://httpd.apache.org/docs/trunk/upgrading.html 

  7. Apache multiviews and content negotiation http://httpd.apache.org/docs/current/content-negotiation.html 

  8. Apache AllowOverride http://dywang.csie.cyut.edu.tw/dywang/rhel7/node53.html 

  9. DNS basic for setting name http://wpnote.tw/dnsdomain-name-system基礎常識,如何設定dns/ 

  10. Grab files at GoogleDrive http://stackoverflow.com/questions/20665881/direct-download-from-google-drive-using-google-drive-api 

  11. SSH root or other users login without password https://www.cs.utah.edu/~bigler/code/sshkeys.html