Does Oneiros Dream?

そこら辺にいるクソガキのブログ

CentOS + Unicron + Ruby on Rails + Nginx

Passenger + Apache + Ruby on Rails is not very good.
In particular there is a difference in performance.

Therefore, I constructed CentOS + Unicron + Ruby on Rails + Nginx.
Incidentally, the performance was not much difference : )

Work are the following.





# confirmation of spec
$ cat /proc/version
$ cat /proc/cpuinfo
$ df -h

# confirmation of process
$ ps aux

# confirmation of iptables
$ sudo iptables --list


# confirmation of user
$ cat /etc/passwd
$ id

# confirmation of package
$ yum list installed

# getenforce

# update of package
$ sudo yum update
[y]

# update of system
$ sudo yum upgrade
[y]

# Installation of Nginx, MySql
$ sudo yum install -y nginx mysql mysql-server mysql-devel

# registered in the startup (run level35)
$ sudo chkconfig --level 35 nginx on
$ sudo chkconfig --level 35 mysqld on

# confirmation of the startup
$ chkconfig --list

# reboot and confirmation of the startup
$ sudo reboot
$ ps aux | grep nginx
$ ps aux | grep mysqld

# confirmation of ruby version
$ ruby -v

# confirmation of version
$ gem -v

# Installation of bundle
$ gem install

# Installation of rails
$ gem install rails

[Error]
Running 'patch' for libxml2 2.9.2... ERROR, review '/home/ec2-user/.gem/ruby/2.0/gems/nokogiri-1.6.6.2/ext/nokogiri/tmp/x86_64-redhat-linux-gnu/ports/libxml2/2.9.2/patch.log' to see what happened.
.
.
.
Gem files will remain installed in /home/ec2-user/.gem/ruby/2.0/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /home/ec2-user/.gem/ruby/2.0/gems/nokogiri-1.6.6.2/ext/nokogiri/gem_make.out

# When you install the nokogiri, an error has occurred in libxml2.
# Therefore, install them
sudo yum install -y gcc gcc-c++ ruby-devel libxml2 libxml2-devel libxslt libxslt-devel

# confirmation of version
xml2-config --version

# confirmation of version
xslt-config --version

# Installing nokogiri using the system libraries
gem install nokogiri -- --use-system-libraries

# Installation of rails
$ gem install rails

# confirmation
$ rails -v

# make work directory and move to the directory
$ sudo mkdir /var/ror/; cd /var/ror

# create app using the rails
$ rails new sample --database=mysql

[Error]
/usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:126:in `require': cannot load such file -- io/console (LoadError)
from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:126:in `require'
from /home/ec2-user/.gem/ruby/2.0/gems/thor-0.19.1/lib/thor/shell/basic.rb:2:in `'
from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:73:in `require'
from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:73:in `require'
.
.
.
# maybe io/console error?
# Installation of io/console
$ gem install io-console

# re: create app using the rails
$ rails new test --database=mysql

[Error]
create
/usr/share/ruby/2.0/fileutils.rb:245:in `mkdir': Permission denied - /var/ror/test (Errno::EACCES)
.
.
.

# Permission error
# chenge permission
$ sudo chown -R ec2-user:ec2-user /var/ror/

# re: re: create app using the rails
$ rails new test --database=mysql
$ cd /var/ror/test/

# Initial setting
$ vim /var/ror/test/Gemfile

[Gemfile]

        source 'https://rubygems.org'

        # Error avoidance
        ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] = 'YES'
        gem 'io-console'
        # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
        gem 'rails', '4.2.0'
        # Use mysql as the database for Active Record
        gem 'mysql2'
        # Use SCSS for stylesheets
        gem 'sass-rails', '~> 5.0'
        # Use Uglifier as compressor for JavaScript assets
        gem 'uglifier', '>= 1.3.0'
        # Use CoffeeScript for .coffee assets and views
        gem 'coffee-rails', '~> 4.1.0'
        # See https://github.com/sstephenson/execjs#readme for more supported runtimes
        gem 'therubyracer', platforms: :ruby

        # Use jquery as the JavaScript library
        gem 'jquery-rails'
        # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
        gem 'turbolinks'
        # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
        gem 'jbuilder', '~> 2.0'
        # bundle exec rake doc:rails generates the API under doc/api.
        gem 'sdoc', '~> 0.4.0', group: :doc

        # Use ActiveModel has_secure_password
        gem 'bcrypt', '~> 3.1.7'

        # Use Unicorn as the app server
        gem 'unicorn'

        # Use Capistrano for deployment
        gem 'capistrano-rails', group: :development

        group :development, :test do
          # Call 'byebug' anywhere in the code to stop execution and get a debugger console
          gem 'byebug'

          # Access an IRB console on exception pages or by using <%= console %> in views
          gem 'web-console', '~> 2.0'

          # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
          gem 'spring'
        end

# Setting of Unicron
$ vi /var/ror/test/config/unicorn.rb

[unicorn.rb]

        @app_path = '/var/ror/test'
        
        worker_processes 2
        working_directory "#{@app_path}/"
        
        # This loads the application in the master process before forking
        # worker processes
        # Read more about it here:
        # http://unicorn.bogomips.org/Unicorn/Configurator.html
        preload_app true
        
        timeout 30
        
        # This is where we specify the socket.
        # We will point the upstream Nginx module to this socket later on
        listen "/tmp/unicorn.sock", :backlog => 64
        pid "/tmp/unicorn.pid"
        
        # Set the path of the log files inside the log folder of the testapp
        stderr_path "#{@app_path}/log/unicorn.stderr.log"
        stdout_path "#{@app_path}/log/unicorn.stdout.log"
        
        before_fork do |server, worker|
        defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
        
        old_pid = "#{server.config[:pid]}.oldbin"
        if old_pid != server.pid
        begin
          sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
          Process.kill(sig, File.read(old_pid).to_i)
        rescue Errno::ENOENT, Errno::ESRCH
        end
        end
        
        sleep 1
        end
        
        after_fork do |server, worker|
        defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
        end

# Setting of Nginx
$ sudo vi /etc/nginx/conf.d/myapp.conf

[myapp.conf]

        upstream unicorn_server {
            server unix:/tmp/unicorn.sock
            fail_timeout=0;
        }
        
        server {
            listen 80;
            client_max_body_size 4G;
            server_name _;
        
            keepalive_timeout 5;
        
            root /var/www/ror/test/public;
        
            location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        
                # If you don't find the filename in the static files
                # Then request it from the unicorn server
                if (!-f $request_filename) {
                    proxy_pass http://unicorn_server;
                    break;
                }
            }
        
            error_page 500 502 503 504 /500.html;
            location = /500.html {
                root /var/www/ror/test/public;
            }
        }

# Autostart setting of Unicron
$ vi /etc/init.d/unicorn_rails

[unicorn_rails]

        #!/bin/sh
        ### BEGIN INIT INFO
        # Provides:          unicorn
        # chkconfig:         3456 99 35
        # Short-Description: starts the unicorn web server
        # Description:       starts unicorn
        ### END INIT INFO


        NAME="Unicorn"
        ENV=development

        ROOT_DIR="/var/ror/test"

        PID="${ROOT_DIR}/tmp/pids/unicorn.pid"
        CONF="${ROOT_DIR}/config/unicorn.rb"
        OPTIONS=""
        CMD="bundle exec unicorn_rails -c ${CONF} -E ${ENV} -D ${OPTIONS}"


        PATH="/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin"

        start()
        {
          if [ -e $PID ]; then
            echo "$NAME already started"
            exit 1
          fi
          echo "start $NAME"
          cd $ROOT_DIR
          gem install bundler
          $CMD
        }

        stop()
        {
          if [ ! -e $PID ]; then
            echo "$NAME not started"
            exit 1
          fi
          echo "stop $NAME"
          kill -QUIT `cat ${PID}`
          rm -f $PID
        }

        force_stop()
        {
          if [ ! -e $PID ]; then
            echo "$NAME not started"
            exit 1
          fi
          echo "stop $NAME"
          kill -INT `cat ${PID}`
          rm -f $PID
        }

        reload()
        {
          if [ ! -e $PID ]; then
            echo "$NAME not started"
            start
            exit 0
          fi
          echo "reload $NAME"
          kill -HUP `cat ${PID}`
        }

        restart()
        {
            stop
            start
        }

        case "$1" in
          start)
            start
            ;;
          stop)
            stop
            ;;
          force-stop)
            force_stop
            ;;
          reload)
            reload
            ;;
          restart)
            restart
            ;;
          *)
            echo "Syntax Error: release [start|stop|force-stop|reload|restart]"
            ;;
        esac

# Adding Unicron to the startup
$ sudo chkconfig --add unicorn_rails

# create page
$ rails g controller Roots index

# Adding routes
$ vi /var/ror/test/config/routes.rb

[routes.rb]

        root :to => 'roots#index'

# run unicorn
$ bundle exec unicorn_rails -c config/unicorn.rb -E development -D

[Error]
/var/ror/test/vendor/bundle/ruby/2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/mysql2_adapter.rb:23:in `rescue in mysql2_connection'
.
.
.

# SQL error...
# Setting of SQL
$ mysql_secure_installation

$ mysql -u root -p

$ vi /var/ror/test/conifg/database.yml

[database.yml]

             # MySQL.  Versions 5.0+ are recommended.
             #
             # Install the MYSQL driver
             #   gem install mysql2
             #
             # Ensure the MySQL gem is defined in your Gemfile
             #   gem 'mysql2'
             #
             # And be sure to use new-style password hashing:
             #   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
             #
             default: &default
               adapter: mysql2
               encoding: utf8
               pool: 5
               username: root
               password: amazon
               socket: /var/lib/mysql/mysql.sock

$ rake db:create

# re: run unicorn
$ bundle exec unicorn_rails -c config/unicorn.rb -E development -D

# create "Hello world"
$ rm -rf /var/ror/test/app/views/roots/index.html.erb
$ echo "HELLO WORLD" > /var/ror/test/app/views/roots/index.html.erb
$ rm -rf /var/ror/test/app/views/layouts/application.html.erb
$ echo "<%= yield -%>" > /var/ror/test/app/views/layouts/application.html.erb


$ curl YOUR IP
Return >> HELLO WORLD


Enjoy!