Drive Rails on Nginx and Passenger, Install Instructions


I setup rails on Passenger and Nginx. But I didn’t install from package, but compiled the source to hide some http response header. I wrote the instruction to install them, and what I did.

Environment

  • Ruby 2.2.2
  • Nginx 1.8
  • Passenger 5.0.18
  • EC2 Amazon Linux 2015.03

Overview

I suppose you installed ruby.

  1. Install Passenger
  2. Install Nginx
    • Download Nginx and required module
    • Compile and Install
  3. nginx configuration
  4. (Re)Boot nginx

Install Passenger

Install Passenger, as gem install passenger.

Install Nginx

If you execute passenger-install-nginx-module as root, nginx will be installed. But in the case, Nginx version and Passenger version should be presented in http response header.

For removing such no use headers, you should compile the source of nginx with nginx module.

Download Nginx and required module

Execute sudo su -, and switch user to root. And execute cd /usr/local/src.

Download Nginx

Download Nginx (tar.gz format). I downloaded Nginx 1.8.0, which was the latest stable nginx. You can find it at Nginx Official Download Page.

And extract it.

Download headers-more-nginx-module

Plain Nginx and Passenger respond http header including versions of Nginx and Passenger, and execution time, etc. Not to expose server’s internal information, use headers-more-nginx-module with Nginx.

I got the module (.tar.gz format) of latest version from github, which is on github.

Release list page : Releases · openresty/headers-more-nginx-module

After downloading, Extract it.

After extraction, change the .tar.gz file name to understandable name. Tab helps you to change the name, using extracted directory name.

Compile and Install

The installer require the following components. If there’s any deficit, installation will be stopped, so don’t worry.

  • C compiler (/usr/bin/cc)
  • C++ compiler (/usr/bin/c++)
  • wget or curl
  • curl development header with ssl support
  • Openssl development header
  • Zlib development header
  • rake, rack, ruby development header, rubygems

If you have installed nginx into the same place in the same way, the name old nginx binary will be changed to nginx.old.

Swap

If the RAM is less than 1 GB, like EC2 micro instance, swap memory is required. Execute the following commands as root.

Don’t worry, even if you start installation with small amount of memory, instruction is shown in installation process.

Execute passenger-install-nginx-module, with no option.

Now, the installation will start.

まずは 処理の説明が表示され、 Press Enter to continue, or Ctrl-C to abort. と表示されます。 Enter を押して続行します。 途中で()めたくなったら いつでも Ctrl-C を押せば止められます。

続いて Which languages are you interested in? と表示されるので 必要なものを画面の指示にしたがって選択し、 Enter を押します。

インストールされているソフトウェアのチェックが終わると、Do you want this installer to download, compile and install Nginx for you? と、 インストール方法を聞かれます。 ソースからコンパイルを行うので 2. No: I want to customize my Nginx installation. (for advanced users) を選びます。

And in my case, not installed component PCRE was automatically downloaded and installed.

Next, you should input source file location and compile option. I inputted the following.

Where is your Nginx source code located?
/usr/local/src/nginx-1.8.0
Where do you want to install Nginx to?
Pressed Enter, without any character.
Extra Nginx configure options
--add-module=/usr/local/src/headers-more-nginx-module-0.261
To use headers-more-nginx-module, add --add-module. Options set as default is printed on console.

At last, confirmation message will be shown, then Enter if there is no mistake. And compiling and installing will be started.

nginx configuration

I copied /opt/nginx/conf/nginx.conf.default to /opt/nginx/conf/nginx.conf.

For running Rails application, the following configuration helps. I show a part of the configuration. I set client_max_body_size 10 MB for uploading images. (It’s 1M as default.) Other configuration setting is explained on Phusion Passenger users guide, Nginx version or so.

And add code to modify response header.

Check the configuration file with the following command. It’s ok if no error is shown.

(Re)Boot Nginx

I usually use /opt/nginx/sbin/nginx -s reload for rails reloading, but it wasn’t effective in this case.

Stop Nginx with the command /opt/nginx/sbin/nginx -s stop, and boot it by execution of /opt/nginx/sbin/nginx.

If you launch nginx first time, only execute /opt/nginx/sbin/nginx.


Google Apps Script : Process only on business day


Have you ever planed to execute Google Apps Script only on business day? Send mail only on business day, or process on the weekend except holiday, etc. I thought script to make it.

First, write core script and set trigger to work on weekdays.

Process except Holiday

It means to process only on business day. Get holiday information from Google Calendar.

Add the following code to the first of the process.

It stop the process on Sunday and Saturday, and also on holiday in Japanese holiday calendar, prepared by Google. You can configure the day on which the script works, by setting trigger, too.

It is good to create function that returns bool value.

If you have other special holiday, create special holiday calendar and add some code to check it. Calendar ID which should be passed to getCalendarById is saw in the page of its calendar.

Continue reading Google Apps Script : Process only on business day

Post Weather Forecast to Slack Regularly


I created a script to post weather forecast to Slack everyday, with Google Apps Script. It can be configured to post only specific weekdays.

Direction

  • Get forecast information from Open Weather Map.
  • Write script with CoffeeScript.
  • Post to Slack using Incoming WebHooks.
  • Post automatically using Google Apps Script.

Preparation

Get url of Incoming Webhooks in Slack.

Continue reading Post Weather Forecast to Slack Regularly

Rails 4 : Load Seed from TSV


Some days ago, I was requested to enable to load seed data from excel, by my client, in rails 4 project. I told that data to be edited should be managed in database, not excel. But he really requested excel, then I and he decided load data from tsv. Excel is functional, so TSV is appropriate.

Select data area and copy and paste with mouse, Excel data can be pasted as TSV data to text editor, and vise versa.

Environment

  • Ubuntu 14.04 LTS
  • Rails 4.1.8
  • Ruby 2.2.2

Direction

  1. Create feature to load TSV data as a task, and enable to execute with rake.
  2. On executing rake db:seed, execute the task to load TSV.
Continue reading Rails 4 : Load Seed from TSV

Rails 4 Send mail when Error occurs


I wrote code to send mail when error occurs in Rails 4.

Environment

  • Ubuntu 15.05 (64bit)
  • Ruby 2.2.2
  • Rails 4.1.8

Old Way

In Rails 3, I could use rescue_in_public method in ApplicationController and send error notification mail.

current_user returns user who logging in. It’s defined in devise gem.

But in rails 4, it doesn’t work.

Solution

I used rescue_from, which also exists in Rails 3.

rescue_from Exception catches all exception occurs in controller, but rescue_from StandardError doesn’t. It is because RuntimeError, for example, is inherits Exception but doesn’t inherit StandardError, which is raised by raise "exception!" simply. And, syntax error like 0 = 1 couldn’t be caught. Error caught by rescue_from Exception is errors occurs in running program. And errors occur before controller procedure can’t be caught.

The point is raise exception in handle_exception method. I don’t want to erase exception by handle_exception, but want only to send notification mail. It is like AOP. In the above, add method into application_controller.rb simply.

current_user in send_error_mail method returns logging in user, which is defined in the gem, devise. self is used for reporting in which controller the error occurs. request is for reporting request url, etc. The code !Rails.env.development? should be replace to config value. I heard that we can use custom configuration in Rails 4.2, so it is the issue after rails upgrading.

Attention

Exceptions before controller procedure should be reported by other way.

I know handling exception outside of controller is a way, but there are variables can be referred in controller and objects useful in controller, I thought, so I wrote code into applicatoin_controller.rb.

Continue reading Rails 4 Send mail when Error occurs

A Life Summary of an Gypsy