Eric Guo's

in HTML5, JavaScript, Ruby & Rails, Python, and Cloud MES!

A Rubyist Web Developer Macbook Pro With Touch Bar Installation Notes


Just got the Macbook Pro 2016 with touch bar 15’ edition, setting it up takes my one full day. If the China network condition is good, I believe it will take much less time.

I decide the Macbook Pro from scratch instead of using migration assist because I want to keep the system clean. I will install as much application as possible from App Store, then the Homebrew, if both software repository can not find it, install it manually.

Apple store installation is the easiest one, once you log in and you will get all the application you have purchased.

brew are also relative easier, I even using brew to install Chrome and Firefox, via brew cask install google-chrome.

I would like to use percona instead of MySQL because, in China, Aliyun RDS is primarily based on percona, so I favor using the same software in dev env.

But you can not simply install percona directly.

Install mysql, then percona to avoid pitfall

brew install mysql
brew services start mysql
brew services stop mysql
brew remove mysql
brew install percona-server
brew services start percona-server

twitter cli need to rename tt instead of t

gem install t
mv /usr/local/bin/t /usr/local/bin/tt

Nokogiri using system library

brew install libxml2
# If installing directly
gem install nokogiri -- --use-system-libraries \
--with-xml2-include=$(brew --prefix libxml2)/include/libxml2
# If using Bundle
bundle config build.nokogiri --use-system-libraries \
--with-xml2-include=$(brew --prefix libxml2)/include/libxml2
bundle install

proxychains using Shadowsocks

To add Shadowsocks setting for proxychains.

vi /usr/local/Cellar/proxychains-ng/4.11/etc/proxychains.conf

It will finally take 76Gb after installing all my needs, about 33% used, so I believe in buying a 256Gb SSD edition of rMBP should be enough for my next four years usage.

Deploy Ruby-china as a New Forum Software to Aliyun


This wiki based on the Ubuntu 14.04.5 LTS Server.


apt-get update && apt-get upgrade
apt-get dist-upgrade
apt-get install software-properties-common htop curl git


apt-get install memcached


add-apt-repository ppa:chris-lea/redis-server
apt-get update && apt-get install redis-server


echo "deb trusty-pgdg main" > /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - | apt-key add -
apt-get update && apt-get install postgresql
apt-get install libpq-dev


apt-get install imagemagick


curl -sL | sudo -E bash -
apt-get install -y nodejs build-essential

Oracle Java

add-apt-repository ppa:webupd8team/java
apt-get update
apt-get install oracle-java8-installer


wget -qO - | apt-key add -
echo "deb stable main" | tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list
apt-get update && apt-get install elasticsearch
update-rc.d elasticsearch defaults
service elasticsearch start


gpg --keyserver hkp:// --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL | bash -s stable

Ruby 2.4.0

rvm requirements
echo "ruby_url=" > ~/.rvm/user/db # if need mirror.
rvm install 2.4.0 --disable-binary
rvm use 2.4.0 --default
echo "gem: --no-document" >> /etc/gemrc
echo "gem: --no-document" >> ~/.gemrc
gem sources --add --remove # if need mirror
gem install bundler


add-apt-repository ppa:nginx/stable
apt-get update && apt-get install nginx

Create deploy user

sudo adduser \
--system \
--shell /bin/bash \
--gecos 'deploy user' \
--group \
--disabled-password \
--home /data/www deploy
mkdir /data/www/deploy/.ssh
cp ~/.ssh/authorized_keys /data/www/deploy/.ssh/
chown deploy:deploy -R .ssh/

Prepare setting files while doing cap deploy

cap production deploy
cp config/secrets.yml.default config/secrets.yml
cp config/database.yml.default config/database.yml
cp config/config.yml.default config/config.yml
cp config/redis.yml.default config/redis.yml
cp config/puma.example.rb config/puma-web.rb
cp config/elasticsearch.yml.default config/elasticsearch.yml

Change as necessory

vi /data/www/ruby-china/shared/config/database.yml
vi /data/www/ruby-china/shared/config/config.yml
vi /data/www/ruby-china/shared/config/secrets.yml

notice SECRET_KEY_BASE can get from rails secret

Create DB

sudo su - postgres
vi /etc/postgresql/9.6/main/pg_hba.conf
local ftghub_prod deploy peer
service postgresql restart


cap production deploy

Mapping nginx to puma

upstream ruby_china_backend {
# This is the socket we configured in unicorn.rb
server unix:///data/www/ruby-china/shared/tmp/sockets/puma.sock fail_timeout=0;
keepalive 3;
server {
listen 80;
root /www/ruby-china/current/public;
gzip_static on; #to serve pre-gzipped version
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
location ~ (/assets|/system|/avatar.png|/favicon.ico|/*.txt) {
access_log off;
expires 14d;
gzip_static on;
add_header Cache-Control public;
location / {
if ($host != '') {
rewrite ^/(.*)$ http://ftghub/$1 permanent;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://ruby_china_backend;
gzip on;
error_page 500 502 503 504 /500.html;
location = /500.html {
root /www/ruby-china/current/public;

Running DB seed

bundle exec rake db:seed RAILS_ENV=production

Reindex ElasticSearch

Must ensure those model at least having one record each.

rake environment elasticsearch:import:model CLASS=Page FORCE=y RAILS_ENV=production
rake environment elasticsearch:import:model CLASS=Topic FORCE=y RAILS_ENV=production
rake environment elasticsearch:import:model CLASS=User FORCE=y RAILS_ENV=production

Monitor Sidekiq Service With Monit & Capistrano


Sidekiq as a background processing facility in Rails, usually more and more important during business grow, but it’s relative quite possible failed to startup due to there is some code error, so here is my solution to monitor it.

I choose the monit instead of god beause monit is written in C and very small memory footprint. The capistrano-sidekiq also having a out of box support, but when you combine with monit, capistrano, sidekiq it still trick, so I feel writing a blog is worth.

Install monit

yum install monit # assure using CentOS

Configure monit

vi /etc/monitrc:

set daemon 60
set mailserver localhost
set mail-format {
subject: monit alert -- $EVENT $SERVICE
message: $EVENT Service $SERVICE
Hi all people emails address;
Event happen at: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
Your faithful employee,
Monit (robot)
Hint: when resolved, please replay all.
set alert
set alert

Configure in capistrano-sidekiq

At config/deploy/production.rb

set :sidekiq_monit_conf_dir, '/etc/monit.d' # for CentOS folder
set :sidekiq_monit_use_sudo, false

At Capfile

require 'capistrano/sidekiq'
require 'capistrano/sidekiq/monit' # to require monit tasks

Enable alert by customize the monit template.

rails g capistrano:sidekiq:monit:template

and change as below:

# Monit configuration for Sidekiq : <%= fetch(:application) %>
<% processes_pids.each_with_index do |pid_file, idx| %>
check process <%= sidekiq_service_name(idx) %> with pidfile "<%= pid_file %>"
if changed pid then alert
start program = "/bin/su - <%= @role.user %> -c 'cd <%= current_path %> && <%= SSHKit.config.command_map[:sidekiq] %> <%= sidekiq_config %> --index <%= idx %> --pidfile <%= pid_file %> --environment <%= fetch(:sidekiq_env) %> <%= sidekiq_concurrency %> <%= sidekiq_logfile %> <%= sidekiq_queues %> <%= sidekiq_options_per_process[idx] %> -d'" with timeout 30 seconds
stop program = "/bin/su - <%= @role.user %> -c 'cd <%= current_path %> && <%= SSHKit.config.command_map[:sidekiqctl] %> stop <%= pid_file %>'" with timeout <%= fetch(:sidekiq_timeout).to_i + 10 %> seconds
group <%= fetch(:sidekiq_monit_group, fetch(:application)) %>-sidekiq
<% end %>

Deploya to generate minit config

cap production deploy

After that checking if /etc/monit.d having sidekiq conf file. or running cap sidekiq:monit:config to generate config.

Running monit at production


To test the mail, using monit reload, if still can not received the mail, checking my postfix blog.

Building a Postfix Satellite SMTP Service to Avoid Active Mailer Run in Aliyun


Some situation is better avoid Sidekiq like reporting the exception mail, so it’s always the best practices to building a Postfix Satellite SMTP service instead.

Install Postfix

yum install -y postfix cyrus-sasl-plain # cyrus-sasl-plain is no need for Ubuntu
yum erase -y sendmail* # In case Sendmail is installed


Similar to gmail or SendGrid, every mail service vendor offer different combination of setting, below is only apply to AliYun

/etc/postfix/ change

# comment out below
#inet_interfaces = localhost
relayhost = []:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CApath = /etc/ssl/certs
smtp_use_tls = yes
smtp_generic_maps = hash:/etc/postfix/generic
mynetworks =
mydestination =

Create /etc/postfix/sasl_passwd


Create /etc/postfix/generic

Create a database file

postmap /etc/postfix/sasl_passwd /etc/postfix/generic

Prevent non-root access:

chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db /etc/postfix/generic /etc/postfix/generic.db

Restart Postfix service

/etc/init.d/postfix restart # or service postfix restart


It’s very common that postfix will failed to sent mail at first time, so add below to

debug_peer_level = 3
debug_peer_list =

And open another console to testing via:

echo "body of your email" | mail -s 'mail subject from console' -r ''

You may also need postconf -n to review postfix configuration or postconf -d to review all setting.

YOu can also monitor the postfix activity via tail -f /var/log/syslog.

View Rails API and Guides in Localhost


Dash 3 switch to the subscription mode, if you just want to see the Rails document, in fact, it’s relative easier to build in your own box.

Building Rails API Document

cd ~/git/oss/
git clone
cd rails/
bundler install
rake rdoc

Building Rails Guides

cd guides/
rake guides:generate:html

Enable Apache

sudo apachectl start

Link Rails documents

cd /Library/WebServer/Documents/
sudo ln -s /Users/guochunzhong/git/oss/rails/doc/rdoc/ rails_api
sudo ln -s /Users/guochunzhong/git/oss/rails/guides/output/ rails_guides

View in localhost

visit Rails API at http://localhost/rails_api/

visit Rails Guides at http://localhost/rails_guides/

Installation Log on Ubuntu 14.04 LTS on vSphere 5.5


My log to build a Ubuntu server to fit my software stack usage, R & Ruby.

Install SSH

Upgrade Ubuntu and enable SSH server
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install open-vm-tools
sudo apt-get install openssh-server

Install JDK

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install oracle-java7-installer

Install rvm

sudo su - # switch to root if not yet
apt-get install curl
curl -L | bash -s master # install rvm
sudo su -
rvm requirements # to install all depends

Install Ruby

rvm install ruby-2.2.3
gem source --remove
gem source --add
gem update --system
apt-get install libgmp3-dev
echo "gem: --no-document" >> /etc/gemrc
gem update
gem install bundler

Install Oracle Client

apt-get install unrar
apt-get install libaio-dev # must need if you want to use sqlplus
mkdir -p /opt/oracle && cd /opt/oracle
unrar x /home/eric/instantclient-all-linux.x64- .
chmod +x instantclient_11_2/sqlplus
ln -s /opt/oracle/instantclient_11_2/ /opt/oracle/instantclient_11_2/
ln -s /opt/oracle/instantclient_11_2/sqlplus /usr/local/bin/sqlplus
echo "/opt/oracle/instantclient_11_2" >> /etc/
# append below line to /etc/enviroment file

Install Oracle gems ruby-oci8

update the library and install ruby-oci8
sudo su -
mkdir -p network/admin
mv /home/eric/tnsnames.ora .
gem install ruby-oci8 # should be no any error here :-)

Install R

add R source
sudo vi /etc/apt/sources.list
# append below line to end of sources.list
# you can view mirror at
deb trusty/
prepare to install R
apt-get update
sudo apt-key adv --keyserver --recv-keys 51716619E084DAB9
# replace your NO_PUBKEY error above, if pause/block port, using 80 as below line
gpg --keyserver hkp:// --recv-keys E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add -
apt-get update # make sure no error here
install R
apt-get install r-base

Install ROracle

manual install the ROracle
R CMD INSTALL DBI_0.3.1.tar.gz
R CMD INSTALL --configure-args='--with-oci-inc=/opt/oracle/instantclient_11_2/sdk/include --with-oci-lib=/opt/oracle/instantclient_11_2' ROracle_1.2-1.tar.gz

Install Passenger

Just following Passenger documentation

install phusionpassenger
sudo apt-key adv --keyserver hkp:// --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates
sudo sh -c 'echo deb trusty main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
sudo apt-get install -y nginx-extras passenger
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/rvm/gems/ruby-2.2.3/wrappers/ruby;
passenger_max_pool_size 30;

Install R studio server

Install R studio server

sudo apt-get install gdebi-core
sudo gdebi rstudio-server-0.99.473-amd64.deb
# Server Configuration File
server {
listen 80;
server_name cvprstudio;
client_max_body_size 10m;
location / {
proxy_redirect $scheme://$host/;
cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/cvprstudio cvprstudio
service nginx restart

Install RStudio Shiny Server

Install RStudio Shiny Server

run as root
R -e "install.packages('shiny', repos='')"
gdebi shiny-server-
server {
listen 80;
server_name shinyapp;
location / {
proxy_redirect $scheme://$host/;
cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/shinyapp shinyapp
service nginx restart

Following patch process in previous blog to re-install the shiny

R CMD INSTALL shiny_0.12.2-server.tar.gz

Notes to Convert Galaxy Note 2 N7100 to Note 4 by Just Refresh ROM


I’m stucked quite a while when refresh a Galaxy Note II for friends, so I decide write something down:

  1. Wait some time after USB drive installed N7100 driver, something due to china poor network and gfw, the driver install from internet may failed.
  2. Root before install any 3rd party recovery, it’s hardest part of change, I reference this for the Europe model, but you may need to found another way. This is must need before refresh any 3rd party recovery, because seems if not rooted, Knox will stop you do any further action.
  3. Install a 3rd recovery image, for example N7100 CWM Recovery v6.0.5.1
  4. Install customed notes 4 ROM for note 2, you can using this or mirror link

Resolve New RVM 1.26 GPG Key Import Failure Problem


When the hkp port blocked, so rvm suggested gpg --keyserver hkp:// --recv-keys D39DC0E3 always failed to running, here is how to resolve such problem.

  1. Find a hkp port not block server, run export after import the key:

    gpg --keyserver hkp:// --recv-keys D39DC0E3 gpg --export --armor D39DC0E3

  2. Go to hkp port blocked server, run:

    gpg --import -

and copy and paste the step 1 server public key content and press Ctrl+D

Another option you can try is using port 80, but it’s can not always work.

apt-key adv --keyserver hkp:// --recv 561F9B9CAC40B2F7

Compile Eventmachine 1.0.3 in Ruby 2.1.3 on Windows


eventmachine didn’t release a new gems for a long time, so we have to manually compile if you decide using Ruby 2.1.3, it’s probably the most complex gems which can still successfully install on windows as far as I known, here is the steps for your interesting:

git clone
cd eventmachine && bundle install

Now you will meet the bluecloth can not install problem first, which you can following stackoverflow steps including patch ext/bluecloth.h and 6 steps.

Now you need using updated version of rake-compiler (v0.9.3) to continue, so change eventmachine.gemspec to that version and do the bundle install again.

Now we can running rake package and it should generate the updated 1.0.3 gem file you need, but before that you need another depend lzma package mentioned in Ruby Installer Google Group, I only tested x86 but the x64 should be same.

  1. Put the download openssl-1.0.0m-x86-windows.tar.lzma to c:\temp
  2. run C:\DevKit\devkitvars.bat
  3. c:\Temp> bsdtar --lzma -xf openssl-1.0.0m-x86-windows.tar.lzma
  4. c:\git\eventmachine\pkg> gem install ./eventmachine-1.0.3.gem --platform=ruby -- --with-opt-dir=C:/Temp

Now enjoy eventmachine on ruby 2.1.3 windows!