Table of Contents
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.
- Install Passenger
- Install Nginx
- Download Nginx and required module
- Compile and Install
- nginx configuration
- (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.
1 |
wget http://nginx.org/download/nginx-1.8.0.tar.gz |
And extract it.
1 |
tar xvfz nginx-1.8.0.tar.gz |
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
1 |
wget https://github.com/openresty/headers-more-nginx-module/archive/v0.261.tar.gz |
After downloading, Extract it.
1 |
tar xvfz v0.261.tar.gz |
After extraction, change the .tar.gz file name to understandable name. Tab helps you to change the name, using extracted directory name.
1 |
mv v0.261.tar.gz headers-more-nginx-module-0.261.tar.gz |
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.
1 2 3 4 |
sudo dd if=/dev/zero of=/swap bs=1M count=1024 chmod 644 /swap sudo mkswap /swap sudo swapon /swap |
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.
1 |
passenger-install-nginx-module |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
http { passenger_root /path/to/passenger-x.x.x; passenger_ruby /path/to/bin/ruby; server { location / { index index.html index.htm; client_max_body_size 10M; passenger_enabled on; root /var/rails/root/public; passenger_base_uri /var/rails/root; rails_spawn_method smart; rails_env development; # or production } } } |
And add code to modify response header.
1 2 3 4 5 6 |
http { more_clear_headers Server; more_clear_headers ETag; more_clear_headers X-Powered-By; more_clear_headers X-Runtime; } |
Check the configuration file with the following command. It’s ok if no error is shown.
1 |
/opt/nginx/sbin/nginx -t |
(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
.