Skip to content

Commit

Permalink
Merge pull request #17 from cre-ne-jp/add-unicorn
Browse files Browse the repository at this point in the history
Rack サーバ (unicorn) を追加する
  • Loading branch information
koi-chan authored Jan 8, 2017
2 parents bab0c58 + 67aefa5 commit 65ae5fe
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ gem 'meta-tags'
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'
gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
Expand Down
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
kgio (2.11.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -163,6 +164,7 @@ GEM
activesupport (= 4.2.6)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
raindrops (0.17.0)
rake (11.3.0)
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
Expand Down Expand Up @@ -210,6 +212,9 @@ GEM
thread_safe (~> 0.1)
uglifier (3.0.2)
execjs (>= 0.3.0, < 3)
unicorn (5.2.0)
kgio (~> 2.6)
raindrops (~> 0.7)
web-console (2.3.0)
activemodel (>= 4.0)
binding_of_caller (>= 0.7.2)
Expand Down Expand Up @@ -243,7 +248,8 @@ DEPENDENCIES
spring
sysexits
uglifier (>= 1.3.0)
unicorn
web-console (~> 2.0)

BUNDLED WITH
1.13.2
1.13.6
61 changes: 61 additions & 0 deletions config/unicorn.rb.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Rails のルートパス
rails_root = File.expand_path('../../', __FILE__)


# 環境変数 RAILS_ENV を取得する
rails_env = ENV['RAILS_ENV'] || 'development'


# Unicorn のワーカー数
worker_processes 2


# ワーキングディレクトリ
working_directory rails_root


# 接続タイムアウト
timeout 30


# ログファイルのパス
stderr_path File.expand_path('../../log/unicorn_stderr.log', __FILE__)
stdout_path File.expand_path('../../log/unicorn_stdout.log', __FILE__)


# フロントエンドサーバとの接続設定
#listen File.expand_path('../../tmp/sockets/unicorn.sock', __FILE__)
listen 8081


# pid ファイルのパス
pid File.expand_path('../../tmp/pids/unicorn.pid', __FILE__)


# 再起動時にダウンタイムを作るか
preload_app true


# nginx との連携に必要
# USR2 シグナルを受けたときに古いプロセスを停止させる
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
end

after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end


# ライブラリのパス
ENV['BUNDLE_GEMFILE'] = rails_root + '/Gemfile'
17 changes: 10 additions & 7 deletions doc/log-archiver_ircbot.service
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
[Unit]
Description=LogArchiver IRC Bot - IRC Log Save and Viewer
Documentation=https://github.com/cre-ne-jp/log-archiver
After=network.target
After=network.target, mariadb.service

[Service]
User=logarchiver
Group=logarchiver
User=log-archiver
Group=log-archiver
Environment=RAILS_ENV=production
SyslogIdentifier=log-archiver-ircbot
Type=simple
PIDFile=/home/logarchiver/log-archiver/tmp/ircbot.pid
WorkingDirectory=/home/logarchiver/log-archiver
ExecStart=/home/logarchiver/log-archiver/bin/ircbot -v
PIDFile=/home/log-archiver/log-archiver/tmp/pids/ircbot.pid
WorkingDirectory=/home/log-archiver/log-archiver
Restart=always

ExecStart=/home/log-archiver/log-archiver/bin/ircbot -v -m production
#ExecReload=/usr/bin/kill -HUP $MAINPID
ExecStop=/usr/bin/kill $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target
21 changes: 21 additions & 0 deletions doc/log-archiver_unicorn.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[Unit]
Description=LogArchiver Rack Server(Unicorn) - IRC Log Save and Viewer
Documentation=https://github.com/cre-ne-jp/log-archiver
After=network.target, mariadb.service

[Service]
User=log-archiver
Group=log-archiver
Environment=RAILS_ENV=production
SyslogIdentifier=log-archiver-unicorn
Type=simple
PIDFile=/home/log-archiver/log-archiver/tmp/pids/unicorn.pid
WorkingDirectory=/home/log-archiver/log-archiver
Restart=always

ExecStart=/usr/bin/bundle exec "unicorn_rails -c config/unicorn.rb -E production"
ExecReload=/usr/bin/kill -USR2 $MAINPID
ExecStop=/usr/bin/kill -QUIT $MAINPID

[Install]
WantedBy=multi-user.target
14 changes: 14 additions & 0 deletions doc/logrotates/log-archiver
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/home/log-archiver/log-archiver/log/*.log {
weekly
rotate 4
missingok
notifempty
copytruncate
create 0664 log-archiver log-archiver

# unicorn master プロセスに、ログファイルの開き直しをさせる
lastaction
pid=/home/log-archiver/log-archiver/tmp/pids/unicorn.pid
test -s $pid && kill -USR1 "$(cat $pid)"
endscript
}
72 changes: 72 additions & 0 deletions doc/nginx/log-archiver
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## Log Archiver
##
## Lines starting with two hashes (##) are comments with information.
## Lines starting with one hash (#) are configuration parameters that can be uncommented.
##
###################################
## configuration ##
###################################
##

upstream log-archiver {
#server unix:/home/log-archiver/log-archiver/tmp/sockets/unicorn.sock fail_timeout=0;
server 127.0.0.1:8081;
}

map $http_upgrade $connection_upgrade_gitlab {
default upgrade;
'' close;
}

## Normal HTTP host
server {
## Either remove "default_server" from the listen line below,
## or delete the /etc/nginx/sites-enabled/default file. This will cause gitlab
## to be served if you visit any address that your server responds to, eg.
## the ip address of the server (http://x.x.x.x/)n 0.0.0.0:80 default_server;
listen 0.0.0.0:80 default_server;
listen [::]:80 default_server;
server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com
server_tokens off; ## Don't show the nginx version number, a security best practice

## See app/controllers/application_controller.rb for headers set

## Individual nginx logs for this GitLab vhost
access_log /var/log/nginx/log-archiver_access.log;
error_log /var/log/nginx/log-archiver_error.log;

root /home/log-archiver/log-archiver/public;
try_files $uri/index.html $uri.html $uri @app;

location @app {
client_max_body_size 0;
gzip off;

## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;

proxy_http_version 1.1;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade_gitlab;

proxy_pass http://log-archiver;
}

error_page 404 /404.html;
error_page 422 /422.html;
error_page 500 /500.html;
error_page 502 /502.html;
error_page 503 /503.html;
location ~ ^/(404|422|500|502|503)\.html$ {
root /home/log-archiver/log-archiver/public;
internal;
}
}
114 changes: 114 additions & 0 deletions doc/nginx/log-archiver-ssl
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
## GitLab
##
## Modified from nginx http version
## Lines starting with two hashes (##) are comments with information.
## Lines starting with one hash (#) are configuration parameters that can be uncommented.
##
###################################
## configuration ##
###################################
##

upstream log-archiver {
#server unix:/home/log-archiver/log-archiver/tmp/sockets/unicorn.sock fail_timeout=0;
server 127.0.0.1:8081;
}

map $http_upgrade $connection_upgrade_log-archiver_ssl {
default upgrade;
'' close;
}

## Redirects all HTTP traffic to the HTTPS host
server {
## Either remove "default_server" from the listen line below,
## or delete the /etc/nginx/sites-enabled/default file. This will cause log-archiver
## to be served if you visit any address that your server responds to, eg.
## the ip address of the server (http://x.x.x.x/)
listen 0.0.0.0:80;
listen [::]:80 ipv6only=on default_server;
server_name YOUR_SERVER_FQDN; ## Replace this with something like log-archiver.example.com
server_tokens off; ## Don't show the nginx version number, a security best practice
return 301 https://$http_host$request_uri;
access_log /var/log/nginx/log-archiver_access.log;
error_log /var/log/nginx/log-archiver_error.log;
}

## HTTPS host
server {
listen 0.0.0.0:443 ssl;
listen [::]:443 ipv6only=on ssl default_server;
server_name YOUR_SERVER_FQDN; ## Replace this with something like log-archiver.example.com
server_tokens off; ## Don't show the nginx version number, a security best practice

## Strong SSL Security
## https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html & https://cipherli.st/
ssl on;
ssl_certificate /etc/nginx/ssl/log-archiver.crt;
ssl_certificate_key /etc/nginx/ssl/log-archiver.key;

# GitLab needs backwards compatible ciphers to retain compatibility with Java IDEs
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;

## See app/controllers/application_controller.rb for headers set

## [Optional] If your certficate has OCSP, enable OCSP stapling to reduce the overhead and latency of running SSL.
## Replace with your ssl_trusted_certificate. For more info see:
## - https://medium.com/devops-programming/4445f4862461
## - https://www.ruby-forum.com/topic/4419319
## - https://www.digitalocean.com/community/tutorials/how-to-configure-ocsp-stapling-on-apache-and-nginx
# ssl_stapling on;
# ssl_stapling_verify on;
# ssl_trusted_certificate /etc/nginx/ssl/stapling.trusted.crt;
# resolver 208.67.222.222 208.67.222.220 valid=300s; # Can change to your DNS resolver if desired
# resolver_timeout 5s;

## [Optional] Generate a stronger DHE parameter:
## sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 4096
##
# ssl_dhparam /etc/ssl/certs/dhparam.pem;

## Individual nginx logs for this GitLab vhost
access_log /var/log/nginx/log-archiver_access.log;
error_log /var/log/nginx/log-archiver_error.log;

root /home/log-archiver/log-archiver/public;
try_files $uri/index.html $uri.html $uri @app;

location @app {
client_max_body_size 0;
gzip off;

## https://github.com/log-archiverhq/log-archiverhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;

proxy_http_version 1.1;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade_log-archiver_ssl;

proxy_pass http://log-archiver;
}

error_page 404 /404.html;
error_page 422 /422.html;
error_page 500 /500.html;
error_page 502 /502.html;
error_page 503 /503.html;
location ~ ^/(404|422|500|502|503)\.html$ {
root /home/log-archiver/log-archiver/public;
internal;
}
}
Empty file added public/502.html
Empty file.
Empty file added public/503.html
Empty file.

0 comments on commit 65ae5fe

Please sign in to comment.