Linux Unicorn Server Configuration

Source: Internet
Author: User

What is Unicorn?
1. HTTP server designed for Rack applications
2. developed based on advanced Unix features
3. Customer Service with low latency and high bandwidth connections

Features:
1. Designed for Rack, Unix, and fast clients and ease of debugging.
2. fully compatible with Ruby 1.8 and 1.9.
3. Process Management: Unicorn acquires and restarts tasks that are killed due to application errors. You do not need to manage multiple processes and ports on your own. Unicorn can generate and manage any number of task processes.
4. Server Load balancer is fully implemented by the operating system (Unix) core. Requests are not accumulated during busy task processes.
5. There is no need to worry about whether the application is thread-safe. workers runs in their own independent address space and serves only one client at a time.
6. All Rack applications are supported.
7. Use the USR1 signal to repeatedly open all log files of the application. Unicorn can also gradually determine that the multiline logs of a request are stored in the same file.
8. nginx binary upgrade without losing connections. You can upgrade Unicorn, your entire application, library, and even the Ruby editor without losing the client connection.
9. If the fork process has special requirements, you can use before_fork and after_fork. If "preload_app" is false, it cannot be used.
10. You can use copy-on-wirte-friendly memory management to save content (by setting "preload_app" to true ).
11. You can listen to multiple interfaces, including UNIX sockets. Each worker process can also be bound to a private port through the after_fork hook During simple debugging.
12. Configure and use a simple Ruby DSL.

 

Linux Unicorn Server Installation Configuration:

Gem install unicorn


Create a unicorn configuration file for the project

New_sxcoalts2.0/config/unicorn. rb
The content is as follows:
App_path = "/work/new_sxcoalts2.0" # program path
Listen 3000 # port number
Worker_processes 2 # Number of cpu Cores
Pid "# {app_path}/tmp/pids/unicorn. pid"
Stderr_path "# {app_path}/log/unicorn. log"
Stdout_path "# {app_path}/log/unicorn. log"
Rails_env = 'production'


Start:
Go to the project root directory cd/work/new_sxcoalts2.0/
Unicorn_rails-c/work/new_sxcoalts2.0/config/unicorn. rb
Parameter-c indicates the content in the configuration file after execution.

Stop Service:
Background Service: Kill Process
Command Line Service: ctrl + c


Set up startup and close the service:
Create a project configuration Folder:
/Etc/unicorn
Add all required project configurations under this directory (multiple project configurations can be placed)
Example: project1.conf
Content is
RAILS_ROOT =/work/project1
RAILS_ENV = production

Write the unicorn Startup Script
Create unicorn_init under/etc/init. d/
Content is
#! /Bin/sh
#
# Init. d script for single or multiple unicorn installations. Expects at least one. conf
# File in/etc/unicorn
#
# Modified by jay@gooby.org http://github.com/jaygooby
# Based http://gist.github.com/308216 by http://github.com/mguterl
#
# A sample/etc/unicorn/new_sxcoalts_2.0.conf
##
# RAILS_ENV = production
# RAILS_ROOT =/var/apps/www/my_app/current
#
# This configures a unicorn master for your app at/var/apps/www/my_app/current running in
# Production mode. It will read config/unicorn. rb for further set up.
#
# You shoshould ensure different ports or sockets are set in each config/unicorn. rb if
# You are running more than one master concurrently.
#
# If you call this script without any config parameters, it will attempt to run
# Init command for all your unicorn configurations listed in/etc/unicorn/*. conf
#
#/Etc/init. d/unicorn start # starts all unicorns
#
# If you specify a participant config, it will only operate on that one
#
#/Etc/init. d/unicorn start/etc/unicorn/new_sxcoalts_2.0.conf


Set-e

Sig (){
Test-s "$ PID" & kill-$1 'cat "$ PID "'
}

Oldsig (){
Test-s "$ OLD_PID" & kill-$1 'cat "$ OLD_PID "'
}

Cmd (){

Case $1 in
Start)
Sig 0 & echo> & 2 "Already running" & exit 0
Echo "Starting"
$ CMD
;;
Stop)
Sig QUIT & echo "Stopping" & exit 0
Echo> & 2 "Not running"
;;
Force-stop)
Sig TERM & echo "Forcing a stop" & exit 0
Echo> & 2 "Not running"
;;
Restart | reload)
Sig USR2 & sleep 5 & oldsig QUIT & echo "Killing old master" 'cat $ OLD_PID '& exit 0
Echo> & 2 "Couldn't reload, starting '$ cmd' instead"
$ CMD
;;
Upgrade)
Sig USR2 & echo Upgraded & exit 0
Echo> & 2 "Couldn't upgrade, starting '$ cmd' instead"
$ CMD
;;
Rotate)
Sig USR1 & echo rotated logs OK & exit 0
Echo> & 2 "Couldn't rotate logs" & exit 1
;;
*)
Echo> & 2 "Usage: $0"
Exit 1
;;
Esac
}

Setup (){

Echo-n "$ RAILS_ROOT :"
Cd $ RAILS_ROOT | exit 1
Export PID = $ RAILS_ROOT/tmp/pids/unicorn. pid
Export OLD_PID = "$ PID. oldbin"

CMD = "unicorn_rails-c config/unicorn. rb-E $ RAILS_ENV-D"
}

Start_stop (){

# Either run the start/stop/reload/etc command for every config under/etc/unicorn
# Or just do it for a specific one

#$1 contains the start/stop/etc command
#$2 if it exists, shocould be the specific config we want to act on
If [$2]; then
. $2
Setup
Cmd $1
Else
For CONFIG in/etc/unicorn/*. conf; do
# Import the variables
. $ CONFIG
Setup

# Run the start/stop/etc command
Cmd $1
Done
Fi
}


ARGS = "$1 $2"
Start_stop $ ARGS

Run the following command:
/Etc/init. d/unicorn_init start/stop/restart

Unicorn details: click here
Unicorn: click here

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.